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
(cherry-picked from commit 10874d8672)
This commit is contained in:
John Hua 2016-10-28 17:23:15 +08:00 committed by Bob Ball
parent 0c700b43f8
commit 9b0ce9d548
2 changed files with 43 additions and 27 deletions

View File

@ -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():

View File

@ -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)