Fix the EC2 salt-cloud driver to work with Python 3

Fixes #41410
This commit is contained in:
rallytime 2017-05-30 18:31:17 -06:00
parent 55d5e6c6fe
commit 29702a3397
3 changed files with 19 additions and 12 deletions

View File

@ -90,6 +90,7 @@ import decimal
# Import Salt Libs # Import Salt Libs
import salt.utils import salt.utils
import salt.utils.hashutils
from salt._compat import ElementTree as ET from salt._compat import ElementTree as ET
import salt.utils.http as http import salt.utils.http as http
import salt.utils.aws as aws import salt.utils.aws as aws
@ -342,7 +343,7 @@ def query(params=None, setname=None, requesturl=None, location=None,
canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n' canonical_headers = 'host:' + host + '\n' + 'x-amz-date:' + amz_date + '\n'
signed_headers = 'host;x-amz-date' signed_headers = 'host;x-amz-date'
payload_hash = hashlib.sha256('').hexdigest() payload_hash = salt.utils.hashutils.sha256_digest('')
ec2_api_version = provider.get( ec2_api_version = provider.get(
'ec2_api_version', 'ec2_api_version',
@ -351,7 +352,7 @@ def query(params=None, setname=None, requesturl=None, location=None,
params_with_headers['Version'] = ec2_api_version params_with_headers['Version'] = ec2_api_version
keys = sorted(params_with_headers.keys()) keys = sorted(list(params_with_headers))
values = map(params_with_headers.get, keys) values = map(params_with_headers.get, keys)
querystring = _urlencode(list(zip(keys, values))) querystring = _urlencode(list(zip(keys, values)))
querystring = querystring.replace('+', '%20') querystring = querystring.replace('+', '%20')
@ -365,7 +366,7 @@ def query(params=None, setname=None, requesturl=None, location=None,
string_to_sign = algorithm + '\n' + amz_date + '\n' + \ string_to_sign = algorithm + '\n' + amz_date + '\n' + \
credential_scope + '\n' + \ credential_scope + '\n' + \
hashlib.sha256(canonical_request).hexdigest() salt.utils.hashutils.sha256_digest(canonical_request)
kDate = sign(('AWS4' + provider['key']).encode('utf-8'), datestamp) kDate = sign(('AWS4' + provider['key']).encode('utf-8'), datestamp)
kRegion = sign(kDate, region) kRegion = sign(kDate, region)
@ -2000,7 +2001,7 @@ def request_instance(vm_=None, call=None):
'salt/cloud/{0}/requesting'.format(vm_['name']), 'salt/cloud/{0}/requesting'.format(vm_['name']),
args={ args={
'kwargs': __utils__['cloud.filter_event']( 'kwargs': __utils__['cloud.filter_event'](
'requesting', params, params.keys() 'requesting', params, list(params)
), ),
'location': location, 'location': location,
}, },
@ -2786,7 +2787,7 @@ def create(vm_=None, call=None):
'event', 'event',
'created instance', 'created instance',
'salt/cloud/{0}/created'.format(vm_['name']), 'salt/cloud/{0}/created'.format(vm_['name']),
args=__utils__['cloud.filter_event']('created', event_data, event_data.keys()), args=__utils__['cloud.filter_event']('created', event_data, list(event_data)),
sock_dir=__opts__['sock_dir'], sock_dir=__opts__['sock_dir'],
transport=__opts__['transport'] transport=__opts__['transport']
) )
@ -3394,7 +3395,8 @@ def _get_node(name=None, instance_id=None, location=None):
provider=provider, provider=provider,
opts=__opts__, opts=__opts__,
sigver='4') sigver='4')
return _extract_instance_info(instances).values()[0] instance_info = _extract_instance_info(instances).values()
return next(iter(instance_info))
except IndexError: except IndexError:
attempts -= 1 attempts -= 1
log.debug( log.debug(

View File

@ -22,6 +22,7 @@ import salt.config
import re import re
# Import Salt libs # Import Salt libs
import salt.utils.hashutils
import salt.utils.xmlutil as xml import salt.utils.xmlutil as xml
from salt._compat import ElementTree as ET from salt._compat import ElementTree as ET
@ -244,7 +245,7 @@ def sig4(method, endpoint, params, prov_dict,
# Create payload hash (hash of the request body content). For GET # Create payload hash (hash of the request body content). For GET
# requests, the payload is an empty string (''). # requests, the payload is an empty string ('').
if not payload_hash: if not payload_hash:
payload_hash = hashlib.sha256(data).hexdigest() payload_hash = salt.utils.hashutils.sha256_digest(data)
new_headers['X-Amz-date'] = amzdate new_headers['X-Amz-date'] = amzdate
new_headers['host'] = endpoint new_headers['host'] = endpoint
@ -280,7 +281,7 @@ def sig4(method, endpoint, params, prov_dict,
algorithm, algorithm,
amzdate, amzdate,
credential_scope, credential_scope,
hashlib.sha256(canonical_request).hexdigest() salt.utils.hashutils.sha256_digest(canonical_request)
)) ))
# Create the signing key using the function defined above. # Create the signing key using the function defined above.

View File

@ -2536,7 +2536,8 @@ def cachedir_index_add(minion_id, profile, driver, provider, base=None):
lock_file(index_file) lock_file(index_file)
if os.path.exists(index_file): if os.path.exists(index_file):
with salt.utils.fopen(index_file, 'r') as fh_: mode = 'rb' if six.PY3 else 'r'
with salt.utils.fopen(index_file, mode) as fh_:
index = msgpack.load(fh_) index = msgpack.load(fh_)
else: else:
index = {} index = {}
@ -2552,7 +2553,8 @@ def cachedir_index_add(minion_id, profile, driver, provider, base=None):
} }
}) })
with salt.utils.fopen(index_file, 'w') as fh_: mode = 'wb' if six.PY3 else 'w'
with salt.utils.fopen(index_file, mode) as fh_:
msgpack.dump(index, fh_) msgpack.dump(index, fh_)
unlock_file(index_file) unlock_file(index_file)
@ -2568,7 +2570,8 @@ def cachedir_index_del(minion_id, base=None):
lock_file(index_file) lock_file(index_file)
if os.path.exists(index_file): if os.path.exists(index_file):
with salt.utils.fopen(index_file, 'r') as fh_: mode = 'rb' if six.PY3 else 'r'
with salt.utils.fopen(index_file, mode) as fh_:
index = msgpack.load(fh_) index = msgpack.load(fh_)
else: else:
return return
@ -2576,7 +2579,8 @@ def cachedir_index_del(minion_id, base=None):
if minion_id in index: if minion_id in index:
del index[minion_id] del index[minion_id]
with salt.utils.fopen(index_file, 'w') as fh_: mode = 'wb' if six.PY3 else 'w'
with salt.utils.fopen(index_file, mode) as fh_:
msgpack.dump(index, fh_) msgpack.dump(index, fh_)
unlock_file(index_file) unlock_file(index_file)