Salt Cloud: write/read cached data in UTF-8 explicitly

This commit is contained in:
Denys Havrysh 2018-01-18 15:15:35 +02:00
parent cd999201be
commit b370796e9d
No known key found for this signature in database
GPG key ID: 6407DC470520CDF7

View file

@ -88,6 +88,10 @@ try:
except ImportError:
HAS_GETPASS = False
# This is required to support international characters in AWS EC2 tags or any
# other kind of metadata provided by particular Cloud vendor.
MSGPACK_ENCODING = 'utf-8'
NSTATES = {
0: 'running',
1: 'rebooting',
@ -2508,7 +2512,7 @@ def cachedir_index_add(minion_id, profile, driver, provider, base=None):
if os.path.exists(index_file):
with salt.utils.fopen(index_file, 'r') as fh_:
index = msgpack.load(fh_, encoding='utf-8')
index = msgpack.load(fh_, encoding=MSGPACK_ENCODING)
else:
index = {}
@ -2524,7 +2528,7 @@ def cachedir_index_add(minion_id, profile, driver, provider, base=None):
})
with salt.utils.fopen(index_file, 'w') as fh_:
msgpack.dump(index, fh_)
msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)
unlock_file(index_file)
@ -2540,7 +2544,7 @@ def cachedir_index_del(minion_id, base=None):
if os.path.exists(index_file):
with salt.utils.fopen(index_file, 'r') as fh_:
index = msgpack.load(fh_, encoding='utf-8')
index = msgpack.load(fh_, encoding=MSGPACK_ENCODING)
else:
return
@ -2548,7 +2552,7 @@ def cachedir_index_del(minion_id, base=None):
del index[minion_id]
with salt.utils.fopen(index_file, 'w') as fh_:
msgpack.dump(index, fh_)
msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)
unlock_file(index_file)
@ -2605,7 +2609,7 @@ def request_minion_cachedir(
fname = '{0}.p'.format(minion_id)
path = os.path.join(base, 'requested', fname)
with salt.utils.fopen(path, 'w') as fh_:
msgpack.dump(data, fh_)
msgpack.dump(data, fh_, encoding=MSGPACK_ENCODING)
def change_minion_cachedir(
@ -2637,12 +2641,12 @@ def change_minion_cachedir(
path = os.path.join(base, cachedir, fname)
with salt.utils.fopen(path, 'r') as fh_:
cache_data = msgpack.load(fh_, encoding='utf-8')
cache_data = msgpack.load(fh_, encoding=MSGPACK_ENCODING)
cache_data.update(data)
with salt.utils.fopen(path, 'w') as fh_:
msgpack.dump(cache_data, fh_)
msgpack.dump(cache_data, fh_, encoding=MSGPACK_ENCODING)
def activate_minion_cachedir(minion_id, base=None):
@ -2713,7 +2717,7 @@ def list_cache_nodes_full(opts, provider=None, base=None):
fname = '{0}.p'.format(minion_id)
fpath = os.path.join(min_dir, fname)
with salt.utils.fopen(fpath, 'r') as fh_:
minions[driver][prov][minion_id] = msgpack.load(fh_, encoding='utf-8')
minions[driver][prov][minion_id] = msgpack.load(fh_, encoding=MSGPACK_ENCODING)
return minions
@ -2892,7 +2896,7 @@ def cache_node_list(nodes, provider, opts):
diff_node_cache(prov_dir, node, nodes[node], opts)
path = os.path.join(prov_dir, '{0}.p'.format(node))
with salt.utils.fopen(path, 'w') as fh_:
msgpack.dump(nodes[node], fh_)
msgpack.dump(nodes[node], fh_, encoding=MSGPACK_ENCODING)
def cache_node(node, provider, opts):
@ -2917,7 +2921,7 @@ def cache_node(node, provider, opts):
os.makedirs(prov_dir)
path = os.path.join(prov_dir, '{0}.p'.format(node['name']))
with salt.utils.fopen(path, 'w') as fh_:
msgpack.dump(node, fh_)
msgpack.dump(node, fh_, encoding=MSGPACK_ENCODING)
def missing_node_cache(prov_dir, node_list, provider, opts):
@ -2992,7 +2996,7 @@ def diff_node_cache(prov_dir, node, new_data, opts):
with salt.utils.fopen(path, 'r') as fh_:
try:
cache_data = msgpack.load(fh_, encoding='utf-8')
cache_data = msgpack.load(fh_, encoding=MSGPACK_ENCODING)
except ValueError:
log.warning('Cache for {0} was corrupt: Deleting'.format(node))
cache_data = {}