From 10874d8672c9d34b971ae174c10c58e6fe33de14 Mon Sep 17 00:00:00 2001 From: John Hua Date: Fri, 28 Oct 2016 17:23:15 +0800 Subject: [PATCH] Fix execute and file patching The old execute() will silently ignore error regardless if it is in allowed_return_codes. And the old file patching will get return code 1 when running for the second time. The new implementation will firstly check if the base file can be reversed(by dry-run). If it is so, then the patch will not be applied. Change-Id: I055428ec4b142ac9caa9b21a1609e2718e06aaf2 --- .../compute_post_deployment.py | 28 +++++-------- plugin_source/deployment_scripts/utils.py | 42 ++++++++++++++----- 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/plugin_source/deployment_scripts/compute_post_deployment.py b/plugin_source/deployment_scripts/compute_post_deployment.py index d54f080..d7b9195 100755 --- a/plugin_source/deployment_scripts/compute_post_deployment.py +++ b/plugin_source/deployment_scripts/compute_post_deployment.py @@ -8,7 +8,6 @@ import os import re from socket import inet_ntoa from struct import pack -import sys import utils from utils import HIMN_IP @@ -282,15 +281,13 @@ def patch_ceilometer(): ceilometer-rates-always-zero.patch ceilometer-support-network-bytes.patch """ - patchset_dir = sys.path[0] patchfile_list = [ - '%s/patchset/ceilometer-poll-cpu-util.patch' % patchset_dir, - '%s/patchset/ceilometer-rates-always-zero.patch' % patchset_dir, - '%s/patchset/ceilometer-support-network-bytes.patch' % patchset_dir, + 'ceilometer-poll-cpu-util.patch', + 'ceilometer-rates-always-zero.patch', + 'ceilometer-support-network-bytes.patch', ] for patch_file in patchfile_list: - utils.execute('patch', '-d', DIST_PACKAGES_DIR, '-p1', '-i', - patch_file) + utils.patch(DIST_PACKAGES_DIR, patch_file, 1) def patch_compute_xenapi(): @@ -302,15 +299,14 @@ def patch_compute_xenapi(): ovs-interim-bridge.patch neutron-security-group.patch """ - patchset_dir = sys.path[0] patchfile_list = [ - '%s/patchset/support-disable-image-cache.patch' % patchset_dir, - '%s/patchset/speed-up-config-drive.patch' % patchset_dir, - '%s/patchset/ovs-interim-bridge.patch' % patchset_dir, - '%s/patchset/neutron-security-group.patch' % patchset_dir] + 'support-disable-image-cache.patch', + 'speed-up-config-drive.patch', + 'ovs-interim-bridge.patch', + 'neutron-security-group.patch', + ] for patch_file in patchfile_list: - utils.execute('patch', '-d', DIST_PACKAGES_DIR, '-p1', '-i', - patch_file) + utils.patch(DIST_PACKAGES_DIR, patch_file, 1) def patch_neutron_ovs_agent(): @@ -318,9 +314,7 @@ def patch_neutron_ovs_agent(): Add conntrack-tools patch to support conntrack in Dom0 """ - patchset_dir = sys.path[0] - patch_file = '%s/patchset/fix-xenapi-returncode.patch' % patchset_dir - utils.execute('patch', '-d', '/usr/bin', '-p2', '-i', patch_file) + utils.patch('/usr/bin', 'fix-xenapi-returncode.patch', 2) def reconfig_multipath(): diff --git a/plugin_source/deployment_scripts/utils.py b/plugin_source/deployment_scripts/utils.py index 48cdcc8..a5f6edb 100644 --- a/plugin_source/deployment_scripts/utils.py +++ b/plugin_source/deployment_scripts/utils.py @@ -26,7 +26,7 @@ def reportError(err): raise FatalException(err) -def execute(*cmd, **kwargs): +def detailed_execute(*cmd, **kwargs): cmd = map(str, cmd) _env = kwargs.get('env') env_prefix = '' @@ -42,19 +42,15 @@ def execute(*cmd, **kwargs): stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) - if 'prompt' in kwargs: - prompt = kwargs.get('prompt') - proc.stdout.flush() + prompt = kwargs.get('prompt') + if prompt: (out, err) = proc.communicate(prompt) else: - out = proc.stdout.readlines() - err = proc.stderr.readlines() - (out, err) = map(' '.join, [out, err]) - - # Both if/else need to deal with "\n" scenario - (out, err) = (out.replace('\n', ''), err.replace('\n', '')) + (out, err) = proc.communicate() if out: + # Truncate "\n" if it is the last char + out = out.strip() logging.debug(out) if err: logging.info(err) @@ -66,9 +62,35 @@ def execute(*cmd, **kwargs): else: raise ExecutionError(err) + return proc.returncode, out, err + + +def execute(*cmd, **kwargs): + _, out, _ = detailed_execute(*cmd, **kwargs) return out +def patch(directory, patch_file, level): + cwd = os.getcwd() + patchset_dir = os.path.join(cwd, "patchset") + + patches_applied = os.path.join(patchset_dir, "patches_applied") + + patched = False + if os.path.exists(patches_applied): + with open(patches_applied) as f: + patches = f.read().split('\n') + patched = (patch_file) in patches + + if not patched: + execute('patch', '-d', directory, '-p%s' % level, '-i', + os.path.join(patchset_dir, patch_file)) + with open(patches_applied, "a") as f: + f.write(patch_file + "\n") + else: + logging.info(patch_file + " is already applied - skipping") + + def ssh(host, username, *cmd, **kwargs): cmd = map(str, cmd)