Fix py3 issue of heat-container-agent

Now the default python version of rawhide has been upgraded to
python 3.7.4, this patch fixes those py2->py3 issues which followed
from the origin heat-agent commits[1][2].

[1] 25cd394bbe
[2] 73e2125532

Task: 35989
Story: 2006283

Change-Id: I23056513dcc6f0cb0c7d41aa529f6a2e77679db9
This commit is contained in:
Feilong Wang 2019-07-26 16:53:55 +12:00
parent 451358a57c
commit bd23e58825
5 changed files with 18 additions and 15 deletions

View File

@ -18,6 +18,7 @@ import os
import shutil import shutil
import stat import stat
import subprocess import subprocess
import six
import sys import sys
import requests import requests
@ -93,7 +94,7 @@ def invoke_hook(c, log):
hot_inputs = c.get('inputs', []) hot_inputs = c.get('inputs', [])
for hot_input in hot_inputs: for hot_input in hot_inputs:
if hot_input.get('type', None) == 'String' and \ if hot_input.get('type', None) == 'String' and \
not isinstance(hot_input['value'], basestring): not isinstance(hot_input['value'], six.string_types):
hot_input['value'] = str(hot_input['value']) hot_input['value'] = str(hot_input['value'])
iv = dict((i['name'], i['value']) for i in c['inputs']) iv = dict((i['name'], i['value']) for i in c['inputs'])
# The group property indicates whether it is softwarecomponent or # The group property indicates whether it is softwarecomponent or
@ -145,7 +146,8 @@ def invoke_hook(c, log):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
stdout, stderr = subproc.communicate(input=json.dumps(c)) stdout, stderr = subproc.communicate(
input=json.dumps(c).encode('utf-8', 'replace'))
log.info(stdout) log.info(stdout)
log.debug(stderr) log.debug(stderr)
@ -158,7 +160,7 @@ def invoke_hook(c, log):
try: try:
if stdout: if stdout:
signal_data = json.loads(stdout) signal_data = json.loads(stdout.decode('utf-8', 'replace'))
except ValueError: except ValueError:
signal_data = { signal_data = {
'deploy_stdout': stdout, 'deploy_stdout': stdout,
@ -178,7 +180,8 @@ def invoke_hook(c, log):
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
stdout, stderr = subproc.communicate(input=json.dumps(signal_data)) stdout, stderr = subproc.communicate(
input=json.dumps(signal_data).encode('utf-8', 'replace'))
log.info(stdout) log.info(stdout)

View File

@ -58,7 +58,7 @@ def trim_response(response, trimmed_values=None):
""" """
trimmed_values = trimmed_values or ('deploy_stdout', 'deploy_stderr') trimmed_values = trimmed_values or ('deploy_stdout', 'deploy_stderr')
str_response = json.dumps(response, ensure_ascii=True, encoding='utf-8') str_response = json.dumps(response, ensure_ascii=True)
len_total = len(str_response) len_total = len(str_response)
offset = MAX_RESPONSE_SIZE - len_total offset = MAX_RESPONSE_SIZE - len_total
if offset >= 0: if offset >= 0:

View File

@ -30,8 +30,8 @@ def prepare_dir(path):
def build_response(deploy_stdout, deploy_stderr, deploy_status_code): def build_response(deploy_stdout, deploy_stderr, deploy_status_code):
return { return {
'deploy_stdout': deploy_stdout, 'deploy_stdout': deploy_stdout.decode('utf-8', 'replace'),
'deploy_stderr': deploy_stderr, 'deploy_stderr': deploy_stderr.decode('utf-8', 'replace'),
'deploy_status_code': deploy_status_code, 'deploy_status_code': deploy_status_code,
} }

View File

@ -38,13 +38,13 @@ def write_input_file(file_path, content):
prepare_dir(os.path.dirname(file_path)) prepare_dir(os.path.dirname(file_path))
with os.fdopen(os.open( with os.fdopen(os.open(
file_path, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f: file_path, os.O_CREAT | os.O_WRONLY, 0o600), 'w') as f:
f.write(content.encode('utf-8')) f.write(content)
def build_response(deploy_stdout, deploy_stderr, deploy_status_code): def build_response(deploy_stdout, deploy_stderr, deploy_status_code):
return { return {
'deploy_stdout': deploy_stdout, 'deploy_stdout': deploy_stdout.decode('utf-8', 'replace'),
'deploy_stderr': deploy_stderr, 'deploy_stderr': deploy_stderr.decode('utf-8', 'replace'),
'deploy_status_code': deploy_status_code, 'deploy_status_code': deploy_status_code,
} }

View File

@ -59,15 +59,15 @@ def main(argv=sys.argv):
env['heat_outputs_path'] = heat_outputs_path env['heat_outputs_path'] = heat_outputs_path
with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f: with os.fdopen(os.open(fn, os.O_CREAT | os.O_WRONLY, 0o700), 'w') as f:
f.write(c.get('config', '').encode('utf-8')) f.write(c.get('config', ''))
log.debug('Running %s' % fn) log.debug('Running %s' % fn)
subproc = subprocess.Popen([fn], stdout=subprocess.PIPE, subproc = subprocess.Popen([fn], stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=env) stderr=subprocess.PIPE, env=env)
stdout, stderr = subproc.communicate() stdout, stderr = subproc.communicate()
log.info(stdout) log.info(stdout.decode('utf-8', 'replace'))
log.debug(stderr) log.debug(stderr.decode('utf-8', 'replace'))
if subproc.returncode: if subproc.returncode:
log.error("Error running %s. [%s]\n" % (fn, subproc.returncode)) log.error("Error running %s. [%s]\n" % (fn, subproc.returncode))
@ -85,8 +85,8 @@ def main(argv=sys.argv):
pass pass
response.update({ response.update({
'deploy_stdout': stdout, 'deploy_stdout': stdout.decode('utf-8', 'replace'),
'deploy_stderr': stderr, 'deploy_stderr': stderr.decode('utf-8', 'replace'),
'deploy_status_code': subproc.returncode, 'deploy_status_code': subproc.returncode,
}) })