Skip to content

Commit

Permalink
Consistently use salt.utils.json and salt.utils.msgpack.
Browse files Browse the repository at this point in the history
This commit introduces salt.utils.msgpack modifies all places in the
code that either use json or msgpack to use salt.utils.json or
salt.utils.msgpack respectively.

While this change itself does not have any effect, it is important to
allow for centrally dealing with objects that cannot be directly
serialied via json or msgpack.
  • Loading branch information
smarsching committed Nov 27, 2018
1 parent 4d9e2a0 commit 0133353
Show file tree
Hide file tree
Showing 31 changed files with 198 additions and 100 deletions.
7 changes: 4 additions & 3 deletions salt/cloud/clouds/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@
import binascii
import datetime
import base64
import msgpack
import re
import decimal

Expand All @@ -91,6 +90,7 @@
import salt.utils.files
import salt.utils.hashutils
import salt.utils.json
import salt.utils.msgpack
import salt.utils.stringutils
import salt.utils.yaml
from salt._compat import ElementTree as ET
Expand Down Expand Up @@ -4859,7 +4859,7 @@ def _parse_pricing(url, name):
__opts__['cachedir'], 'ec2-pricing-{0}.p'.format(name)
)
with salt.utils.files.fopen(outfile, 'w') as fho:
msgpack.dump(regions, fho)
salt.utils.msgpack.dump(regions, fho)

return True

Expand Down Expand Up @@ -4927,7 +4927,8 @@ def show_pricing(kwargs=None, call=None):
update_pricing({'type': name}, 'function')

with salt.utils.files.fopen(pricefile, 'r') as fhi:
ec2_price = salt.utils.stringutils.to_unicode(msgpack.load(fhi))
ec2_price = salt.utils.stringutils.to_unicode(
salt.utils.msgpack.load(fhi))

region = get_location(profile)
size = profile.get('size', None)
Expand Down
6 changes: 3 additions & 3 deletions salt/cloud/clouds/gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import re
import pprint
import logging
import msgpack
from ast import literal_eval
from salt.utils.versions import LooseVersion as _LooseVersion

Expand Down Expand Up @@ -91,6 +90,7 @@
import salt.utils.cloud
import salt.utils.files
import salt.utils.http
import salt.utils.msgpack
import salt.config as config
from salt.cloud.libcloudfuncs import * # pylint: disable=redefined-builtin,wildcard-import,unused-wildcard-import
from salt.exceptions import (
Expand Down Expand Up @@ -2628,7 +2628,7 @@ def update_pricing(kwargs=None, call=None):
__opts__['cachedir'], 'gce-pricing.p'
)
with salt.utils.files.fopen(outfile, 'w') as fho:
msgpack.dump(price_json['dict'], fho)
salt.utils.msgpack.dump(price_json['dict'], fho)

return True

Expand Down Expand Up @@ -2667,7 +2667,7 @@ def show_pricing(kwargs=None, call=None):
update_pricing()

with salt.utils.files.fopen(pricefile, 'r') as fho:
sizes = msgpack.load(fho)
sizes = salt.utils.msgpack.load(fho)

per_hour = float(sizes['gcp_price_list'][size][region])

Expand Down
6 changes: 3 additions & 3 deletions salt/engines/stalekey.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
import salt.key
import salt.utils.files
import salt.utils.minions
import salt.utils.msgpack
import salt.wheel

# Import 3rd-party libs
from salt.ext import six
import msgpack

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -60,7 +60,7 @@ def start(interval=3600, expire=604800):
if os.path.exists(presence_file):
try:
with salt.utils.files.fopen(presence_file, 'r') as f:
minions = msgpack.load(f)
minions = salt.utils.msgpack.load(f)
except IOError as e:
log.error('Could not open presence file %s: %s', presence_file, e)
time.sleep(interval)
Expand Down Expand Up @@ -95,7 +95,7 @@ def start(interval=3600, expire=604800):

try:
with salt.utils.files.fopen(presence_file, 'w') as f:
msgpack.dump(minions, f)
salt.utils.msgpack.dump(minions, f)
except IOError as e:
log.error('Could not write to presence file %s: %s', presence_file, e)
time.sleep(interval)
7 changes: 4 additions & 3 deletions salt/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
from salt.ext.six.moves import input, zip_longest
# pylint: enable=import-error,no-name-in-module,redefined-builtin

# Import third party libs
# We do not always need msgpack, so we do not want to fail here if msgpack is
# not available.
try:
import msgpack
import salt.utils.msgpack
except ImportError:
pass

Expand Down Expand Up @@ -1044,7 +1045,7 @@ def check_minion_cache(self, preserve_minions=False):
if ext == '.json':
data = salt.utils.json.load(fp_)
elif ext == '.msgpack':
data = msgpack.load(fp_)
data = salt.utils.msgpack.load(fp_)
role = salt.utils.stringutils.to_unicode(data['role'])
if role not in minions:
os.remove(path)
Expand Down
5 changes: 4 additions & 1 deletion salt/log/handlers/fluent_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,17 @@
try:
# Attempt to import msgpack
import msgpack
import salt.utils.msgpack
# There is a serialization issue on ARM and potentially other platforms
# for some msgpack bindings, check for it
if msgpack.loads(msgpack.dumps([1, 2, 3]), use_list=True) is None:
raise ImportError
import salt.utils.msgpack
except ImportError:
# Fall back to msgpack_pure
try:
import msgpack_pure as msgpack
import salt.utils.msgpack
except ImportError:
# TODO: Come up with a sane way to get a configured logfile
# and write to the logfile when this error is hit also
Expand Down Expand Up @@ -455,7 +458,7 @@ def _make_packet(self, label, timestamp, data):
packet = (tag, timestamp, data)
if self.verbose:
print(packet)
return msgpack.packb(packet)
return salt.utils.msgpack.packb(packet, _msgpack_module=msgpack)

def _send(self, bytes_):
self.lock.acquire()
Expand Down
2 changes: 1 addition & 1 deletion salt/modules/saltcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import logging
import os
import time
from json import loads, dumps
from salt.utils.json import loads, dumps

# Import Salt libs
import salt.utils.files
Expand Down
10 changes: 5 additions & 5 deletions salt/modules/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import salt.utils.hashutils
import salt.utils.jid
import salt.utils.json
import salt.utils.msgpack
import salt.utils.platform
import salt.utils.state
import salt.utils.stringutils
Expand All @@ -44,7 +45,6 @@

# Import 3rd-party libs
from salt.ext import six
import msgpack

__proxyenabled__ = ['*']

Expand Down Expand Up @@ -184,7 +184,7 @@ def _get_pause(jid, state_id=None):
data[state_id] = {}
if os.path.exists(pause_path):
with salt.utils.files.fopen(pause_path, 'rb') as fp_:
data = msgpack.loads(fp_.read())
data = salt.utils.msgpack.loads(fp_.read())
return data, pause_path


Expand Down Expand Up @@ -255,7 +255,7 @@ def soft_kill(jid, state_id=None):
data, pause_path = _get_pause(jid, state_id)
data[state_id]['kill'] = True
with salt.utils.files.fopen(pause_path, 'wb') as fp_:
fp_.write(msgpack.dumps(data))
fp_.write(salt.utils.msgpack.dumps(data))


def pause(jid, state_id=None, duration=None):
Expand Down Expand Up @@ -290,7 +290,7 @@ def pause(jid, state_id=None, duration=None):
if duration:
data[state_id]['duration'] = int(duration)
with salt.utils.files.fopen(pause_path, 'wb') as fp_:
fp_.write(msgpack.dumps(data))
fp_.write(salt.utils.msgpack.dumps(data))


def resume(jid, state_id=None):
Expand Down Expand Up @@ -324,7 +324,7 @@ def resume(jid, state_id=None):
if state_id == '__all__':
data = {}
with salt.utils.files.fopen(pause_path, 'wb') as fp_:
fp_.write(msgpack.dumps(data))
fp_.write(salt.utils.msgpack.dumps(data))


def orchestrate(mods,
Expand Down
5 changes: 1 addition & 4 deletions salt/modules/win_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@
PER_REMOTE_ONLY
)
from salt.ext import six
try:
import msgpack
except ImportError:
import msgpack_pure as msgpack # pylint: disable=import-error
import salt.utils.gitfs
import salt.utils.msgpack
# pylint: enable=unused-import

log = logging.getLogger(__name__)
Expand Down
32 changes: 24 additions & 8 deletions salt/payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#sys.exit(salt.defaults.exitcodes.EX_GENERIC)


if HAS_MSGPACK:
import salt.utils.msgpack


if HAS_MSGPACK and not hasattr(msgpack, 'exceptions'):
class PackValueError(Exception):
'''
Expand All @@ -75,14 +79,15 @@ def package(payload):
This method for now just wraps msgpack.dumps, but it is here so that
we can make the serialization a custom option in the future with ease.
'''
return msgpack.dumps(payload)
return salt.utils.msgpack.dumps(payload, _msgpack_module=msgpack)


def unpackage(package_):
'''
Unpackages a payload
'''
return msgpack.loads(package_, use_list=True)
return salt.utils.msgpack.loads(package_, use_list=True,
_msgpack_module=msgpack)


def format_payload(enc, **kwargs):
Expand Down Expand Up @@ -142,12 +147,17 @@ def ext_type_decoder(code, data):
# that under Python 2 we can still work with older versions
# of msgpack.
try:
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder, encoding=encoding)
ret = salt.utils.msgpack.loads(msg, use_list=True,
ext_hook=ext_type_decoder,
encoding=encoding,
_msgpack_module=msgpack)
except UnicodeDecodeError:
# msg contains binary data
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
else:
ret = msgpack.loads(msg, use_list=True, ext_hook=ext_type_decoder)
ret = salt.utils.msgpack.loads(msg, use_list=True,
ext_hook=ext_type_decoder,
_msgpack_module=msgpack)
if six.PY3 and encoding is None and not raw:
ret = salt.transport.frame.decode_embedded_strs(ret)
except Exception as exc:
Expand Down Expand Up @@ -214,9 +224,12 @@ def ext_type_encoder(obj):
# Due to this, if we don't need it, don't pass it at all so
# that under Python 2 we can still work with older versions
# of msgpack.
return msgpack.dumps(msg, default=ext_type_encoder, use_bin_type=use_bin_type)
return salt.utils.msgpack.dumps(msg, default=ext_type_encoder,
use_bin_type=use_bin_type,
_msgpack_module=msgpack)
else:
return msgpack.dumps(msg, default=ext_type_encoder)
return salt.utils.msgpack.dumps(msg, default=ext_type_encoder,
_msgpack_module=msgpack)
except (OverflowError, msgpack.exceptions.PackValueError):
# msgpack<=0.4.6 don't call ext encoder on very long integers raising the error instead.
# Convert any very long longs to strings and call dumps again.
Expand All @@ -239,9 +252,12 @@ def verylong_encoder(obj):

msg = verylong_encoder(msg)
if msgpack.version >= (0, 4, 0):
return msgpack.dumps(msg, default=ext_type_encoder, use_bin_type=use_bin_type)
return salt.utils.msgpack.dumps(msg, default=ext_type_encoder,
use_bin_type=use_bin_type,
_msgpack_module=msgpack)
else:
return msgpack.dumps(msg, default=ext_type_encoder)
return salt.utils.msgpack.dumps(msg, default=ext_type_encoder,
_msgpack_module=msgpack)

def dump(self, msg, fn_):
'''
Expand Down
6 changes: 2 additions & 4 deletions salt/renderers/msgpack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

# Import third party libs
import msgpack

# Import salt libs
import salt.utils.msgpack
from salt.ext import six


Expand All @@ -28,4 +26,4 @@ def render(msgpack_data, saltenv='base', sls='', **kws):
msgpack_data = msgpack_data[(msgpack_data.find('\n') + 1):]
if not msgpack_data.strip():
return {}
return msgpack.loads(msgpack_data)
return salt.utils.msgpack.loads(msgpack_data)
6 changes: 3 additions & 3 deletions salt/returners/local_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import salt.utils.files
import salt.utils.jid
import salt.utils.minions
import salt.utils.msgpack
import salt.utils.stringutils
import salt.exceptions

# Import 3rd-party libs
import msgpack
from salt.ext import six
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin

Expand Down Expand Up @@ -514,7 +514,7 @@ def save_reg(data):
raise
try:
with salt.utils.files.fopen(regfile, 'a') as fh_:
msgpack.dump(data, fh_)
salt.utils.msgpack.dump(data, fh_)
except:
log.error('Could not write to msgpack file %s', __opts__['outdir'])
raise
Expand All @@ -528,7 +528,7 @@ def load_reg():
regfile = os.path.join(reg_dir, 'register')
try:
with salt.utils.files.fopen(regfile, 'r') as fh_:
return msgpack.load(fh_)
return salt.utils.msgpack.load(fh_)
except:
log.error('Could not write to msgpack file %s', __opts__['outdir'])
raise
7 changes: 2 additions & 5 deletions salt/runners/winrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@

# Import third party libs
from salt.ext import six
try:
import msgpack
except ImportError:
import msgpack_pure as msgpack # pylint: disable=import-error

# Import salt libs
from salt.exceptions import CommandExecutionError, SaltRenderError
import salt.utils.files
import salt.utils.gitfs
import salt.utils.msgpack
import salt.utils.path
import logging
import salt.minion
Expand Down Expand Up @@ -124,7 +121,7 @@ def genrepo(opts=None, fire_event=True):
ret.setdefault('name_map', {}).update(revmap)
with salt.utils.files.fopen(
os.path.join(winrepo_dir, winrepo_cachefile), 'w+b') as repo:
repo.write(msgpack.dumps(ret))
repo.write(salt.utils.msgpack.dumps(ret))
return ret


Expand Down
Loading

0 comments on commit 0133353

Please sign in to comment.