From ee324391bcb436b1d3a1c44951aa1aa673005cf6 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Thu, 1 Jun 2017 16:39:50 -0700 Subject: tools: add centos scripts to build and test The added 'run-centos' does: - Creates centos 6 or 7 lxd container * Sets http_proxy variable for yum if set locally * Creates centos user - Push local tree * Tar's up working directory * Pushes to container and untars - Installs pip and yum dependencies - As user centos it can then based on flags: * runs unittests * run ./packages/brpm * run ./packages/brpm --srpm * artifact the built *.rpm --- tools/run-centos | 218 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100755 tools/run-centos (limited to 'tools/run-centos') diff --git a/tools/run-centos b/tools/run-centos new file mode 100755 index 00000000..de21d756 --- /dev/null +++ b/tools/run-centos @@ -0,0 +1,218 @@ +#!/bin/bash +# This file is part of cloud-init. See LICENSE file for license information. + +set -u + +VERBOSITY=0 +TEMP_D="" +KEEP=false +CONTAINER="" + +error() { echo "$@" 1>&2; } +fail() { [ $# -eq 0 ] || error "$@"; exit 1; } +errorrc() { local r=$?; error "$@" "ret=$r"; return $r; } + +Usage() { + cat <&2; [ $# -eq 0 ] || error "$@"; return 1; } +cleanup() { + if [ -n "$CONTAINER" -a "$KEEP" = "false" ]; then + delete_container "$CONTAINER" + fi + [ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}" +} + +debug() { + local level=${1}; shift; + [ "${level}" -gt "${VERBOSITY}" ] && return + error "${@}" +} + + +inside_as() { + # inside_as(container_name, user, cmd[, args]) + # executes cmd with args inside container as user in users home dir. + local name="$1" user="$2" + shift 2 + local stuffed="" b64="" + stuffed=$(getopt --shell sh --options "" -- -- "$@") + stuffed=${stuffed# -- } + b64=$(printf "%s\n" "$stuffed" | base64 --wrap=0) + inside "$name" su "$user" -c \ + 'cd; eval set -- "$(echo '$b64' | base64 --decode)" && exec "$@"' +} + +inside() { + local name="$1" + shift + lxc exec "$name" -- "$@" +} + +inject_cloud_init(){ + local name="$1" + tarball_name='cloud-init.tar.gz' + top_d=$(git rev-parse --show-toplevel) || + fail "failed to get top level" + cd "$top_d" || + fail "failed to cd to git top dir" + tar_folder=${PWD##*/} + cd .. + tar -czf "$TEMP_D/$tarball_name" "$tar_folder" || + fail "failed: creating tarball_name" + cd "$tar_folder" || + fail "failed: changing directory" + + user='centos' + tarball="/home/$user/$tarball_name" + inside "$name" useradd "$user" + lxc file push "$TEMP_D/$tarball_name" "$name/home/$user"/ + inside "$name" chown "$user:$user" "$tarball" + inside_as "$name" "$user" tar -C "/home/$user" -xzf "$tarball" || + fail "failed: extracting tarball" +} + +start_container() { + local src="$1" name="$2" + debug 1 "starting container $name from '$src'" + lxc launch "$src" "$name" || { + errorrc "Failed to start container '$name' from '$src'"; + return + } + CONTAINER=$name + + local out="" ret="" + debug 1 "waiting for networking" + out=$(inside "$name" sh -c ' + i=0 + while [ $i -lt 60 ]; do + getent hosts mirrorlist.centos.org && exit 0 + sleep 2 + done' 2>&1) + ret=$? + if [ $ret -ne 0 ]; then + error "Waiting for network in container '$name' failed. [$ret]" + error "$out" + return $ret + fi + + if [ ! -z "${http_proxy-}" ]; then + debug 1 "configuring proxy ${http_proxy}" + inside "$name" sh -c "echo proxy=$http_proxy >> /etc/yum.conf" + fi +} + +delete_container() { + debug 1 "removing container $1 [--keep to keep]" + lxc delete --force "$1" +} + +main() { + local short_opts="ahkrsuv:" + local long_opts="artifact,help,keep,rpm,srpm,unittest,verbose:" + local getopt_out="" + getopt_out=$(getopt --name "${0##*/}" \ + --options "${short_opts}" --long "${long_opts}" -- "$@") && + eval set -- "${getopt_out}" || + { bad_Usage; return; } + + local cur="" next="" + local artifact="" keep="" rpm="" srpm="" unittest="" version="" + + while [ $# -ne 0 ]; do + cur="${1:-}"; next="${2:-}"; + case "$cur" in + -a|--artifact) artifact=1;; + -h|--help) Usage ; exit 0;; + -k|--keep) KEEP=true;; + -r|--rpm) rpm=1;; + -s|--srpm) srpm=1;; + -u|--unittest) unittest=1;; + -v|--verbose) VERBOSITY=$((${VERBOSITY}+1));; + --) shift; break;; + esac + shift; + done + + [ $# -eq 1 ] || { bad_Usage "ERROR: Must provide version!"; return; } + version="$1" + + TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") || + fail "failed to make tempdir" + trap cleanup EXIT + + # program starts here + local uuid="" name="" + uuid=$(uuidgen -t) || { error "no uuidgen"; return 1; } + name="cloud-init-centos-${uuid%%-*}" + + start_container "images:centos/$version" "$name" + # CentOS 6 does not come with tar + if [ "$version" = "6" ]; then + inside "$name" yum install --assumeyes tar || { + errorrc "FAIL: yum install tar failed"; + } + fi + + debug 1 "inserting cloud-init" + inject_cloud_init "$name" || { + errorrc "FAIL: injecting cloud-init into $name failed." + return + } + + # install dependencies + debug 1 "installing dependencies" + inside "$name" /bin/sh Date: Wed, 14 Jun 2017 09:33:54 -0400 Subject: tools/run-centos: cleanups and move to using read-dependencies These changes are all in an effort to get tools/run-centos using read-dependencies rather than the 'setup-centos' script with a separate set of dependencies listed. - tools/read-dependencies: support taking multiple --requirements options. This allows run-centos to get both test and build dependencies. Ultimately, I think it might be nicer for read-dependencies to take a list of "goals" (build, test, run or test-tox) rather than having the caller need to know to provide multiple --requirements. - packages/pkg-deps.json: drop the version on the sudo package. centos 6 has newer (1.8.6p3) version than listed, so its not a problem. - test_handler_disk_setup.py: a test case here was using assertLogs which is not present in the version of unittest2 that is available in centos 6 epel. We just adjust it to use with_logs = True. - tools/run-cents: - improve usage with example - add 'inside_as_cd' to provide the dir you want to cd first to. - avoid the intermediate tarball on disk in the container. - add 'prep' subcommand and use it to install pre-dependencies. - use read-dependencies. --- packages/pkg-deps.json | 2 +- .../test_handler/test_handler_disk_setup.py | 32 ++--- tools/read-dependencies | 32 +++-- tools/run-centos | 142 ++++++++++++++------- tools/setup-centos | 49 ------- 5 files changed, 139 insertions(+), 118 deletions(-) delete mode 100755 tools/setup-centos (limited to 'tools/run-centos') diff --git a/packages/pkg-deps.json b/packages/pkg-deps.json index 8b8f3c37..822d29d9 100644 --- a/packages/pkg-deps.json +++ b/packages/pkg-deps.json @@ -62,7 +62,7 @@ "procps", "rsyslog", "shadow-utils", - "sudo >= 1.7.2p2-3" + "sudo" ] }, "suse" : { diff --git a/tests/unittests/test_handler/test_handler_disk_setup.py b/tests/unittests/test_handler/test_handler_disk_setup.py index 916a0d7a..8a6d49ed 100644 --- a/tests/unittests/test_handler/test_handler_disk_setup.py +++ b/tests/unittests/test_handler/test_handler_disk_setup.py @@ -3,7 +3,7 @@ import random from cloudinit.config import cc_disk_setup -from ..helpers import ExitStack, mock, TestCase +from ..helpers import CiTestCase, ExitStack, mock, TestCase class TestIsDiskUsed(TestCase): @@ -174,32 +174,32 @@ class TestUpdateFsSetupDevices(TestCase): return_value=('/dev/xdb1', False)) @mock.patch('cloudinit.config.cc_disk_setup.device_type', return_value=None) @mock.patch('cloudinit.config.cc_disk_setup.util.subp', return_value=('', '')) -class TestMkfsCommandHandling(TestCase): +class TestMkfsCommandHandling(CiTestCase): + + with_logs = True def test_with_cmd(self, subp, *args): """mkfs honors cmd and logs warnings when extra_opts or overwrite are provided.""" - with self.assertLogs( - 'cloudinit.config.cc_disk_setup') as logs: - cc_disk_setup.mkfs({ - 'cmd': 'mkfs -t %(filesystem)s -L %(label)s %(device)s', - 'filesystem': 'ext4', - 'device': '/dev/xdb1', - 'label': 'with_cmd', - 'extra_opts': ['should', 'generate', 'warning'], - 'overwrite': 'should generate warning too' - }) + cc_disk_setup.mkfs({ + 'cmd': 'mkfs -t %(filesystem)s -L %(label)s %(device)s', + 'filesystem': 'ext4', + 'device': '/dev/xdb1', + 'label': 'with_cmd', + 'extra_opts': ['should', 'generate', 'warning'], + 'overwrite': 'should generate warning too' + }) self.assertIn( - 'WARNING:cloudinit.config.cc_disk_setup:fs_setup:extra_opts ' + + 'extra_opts ' + 'ignored because cmd was specified: mkfs -t ext4 -L with_cmd ' + '/dev/xdb1', - logs.output) + self.logs.getvalue()) self.assertIn( - 'WARNING:cloudinit.config.cc_disk_setup:fs_setup:overwrite ' + + 'overwrite ' + 'ignored because cmd was specified: mkfs -t ext4 -L with_cmd ' + '/dev/xdb1', - logs.output) + self.logs.getvalue()) subp.assert_called_once_with( 'mkfs -t ext4 -L with_cmd /dev/xdb1', shell=True) diff --git a/tools/read-dependencies b/tools/read-dependencies index 4ba2c1bc..8a585343 100755 --- a/tools/read-dependencies +++ b/tools/read-dependencies @@ -18,6 +18,7 @@ import re import subprocess import sys +DEFAULT_REQUIREMENTS = 'requirements.txt' # Map the appropriate package dir needed for each distro choice DISTRO_PKG_TYPE_MAP = { @@ -51,8 +52,9 @@ def get_parser(): """Return an argument parser for this command.""" parser = ArgumentParser(description=__doc__) parser.add_argument( - '-r', '--requirements-file', type=str, dest='req_file', - default='requirements.txt', help='The pip-style requirements file') + '-r', '--requirements-file', type=str, dest='req_files', + action='append', default=None, + help='pip-style requirements file [default=%s]' % DEFAULT_REQUIREMENTS) parser.add_argument( '-d', '--distro', type=str, choices=DISTRO_PKG_TYPE_MAP.keys(), help='The name of the distro to generate package deps for.') @@ -144,12 +146,24 @@ def main(distro): topd = os.path.realpath(os.environ.get('CLOUD_INIT_TOP_D')) else: topd = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - req_path = os.path.join(topd, args.req_file) - if not os.path.isfile(req_path): - sys.stderr.write("Unable to locate '%s' file that should " - "exist in cloud-init root directory." % req_path) - return 1 - pip_pkg_names = parse_pip_requirements(req_path) + + if args.req_files is None: + args.req_files = [os.path.join(topd, DEFAULT_REQUIREMENTS)] + if not os.path.isfile(args.req_files[0]): + sys.stderr.write("Unable to locate '%s' file that should " + "exist in cloud-init root directory." % + args.req_files[0]) + sys.exit(1) + + bad_files = [r for r in args.req_files if not os.path.isfile(r)] + if bad_files: + sys.stderr.write( + "Unable to find requirements files: %s\n" % ','.join(bad_files)) + sys.exit(1) + + pip_pkg_names = set() + for req_path in args.req_files: + pip_pkg_names.update(set(parse_pip_requirements(req_path))) deps_from_json = get_package_deps_from_json(topd, args.distro) renames = deps_from_json.get('renames', {}) translated_pip_names = translate_pip_to_system_pkg( @@ -174,7 +188,7 @@ def pkg_install(pkg_list, distro, dry_run=False): """Install a list of packages using the DISTRO_INSTALL_PKG_CMD.""" print('Installing deps: {0}{1}'.format( '(dryrun)' if dry_run else '', ' '.join(pkg_list))) - pkg_list.extend(EXTRA_SYSTEM_BASE_PKGS) + pkg_list = list(pkg_list) + EXTRA_SYSTEM_BASE_PKGS install_cmd = [] if dry_run: install_cmd.append('echo') diff --git a/tools/run-centos b/tools/run-centos index de21d756..99ba6be0 100755 --- a/tools/run-centos +++ b/tools/run-centos @@ -14,17 +14,22 @@ errorrc() { local r=$?; error "$@" "ret=$r"; return $r; } Usage() { cat </dev/null 2>&1 || needed="${needed} $pkg" + done + if ! command -v python3; then + python -c "import argparse" >/dev/null 2>&1 || + needed="${needed} python-argparse" + fi + needed=${needed# } + if [ -z "$needed" ]; then + error "No prep packages needed" + return 0 + fi + error "Installing prep packages: ${needed}" + yum install --assumeyes ${needed} } start_container() { @@ -121,8 +162,8 @@ delete_container() { } main() { - local short_opts="ahkrsuv:" - local long_opts="artifact,help,keep,rpm,srpm,unittest,verbose:" + local short_opts="ahkrsuv" + local long_opts="artifact,help,keep,rpm,srpm,unittest,verbose" local getopt_out="" getopt_out=$(getopt --name "${0##*/}" \ --options "${short_opts}" --long "${long_opts}" -- "$@") && @@ -149,60 +190,70 @@ main() { [ $# -eq 1 ] || { bad_Usage "ERROR: Must provide version!"; return; } version="$1" + case "$version" in + 6|7) :;; + *) error "Expected version of 6 or 7, not '$version'"; return;; + esac TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") || fail "failed to make tempdir" trap cleanup EXIT # program starts here - local uuid="" name="" + local uuid="" name="" user="ci-test" cdir="" + cdir="/home/$user/cloud-init" uuid=$(uuidgen -t) || { error "no uuidgen"; return 1; } name="cloud-init-centos-${uuid%%-*}" start_container "images:centos/$version" "$name" - # CentOS 6 does not come with tar - if [ "$version" = "6" ]; then - inside "$name" yum install --assumeyes tar || { - errorrc "FAIL: yum install tar failed"; - } - fi + + # prep the container (install very basic dependencies) + inside "$name" bash -s prep <"$0" || + { errorrc "Failed to prep container $name"; return; } + + # add the user + inside "$name" useradd "$user" debug 1 "inserting cloud-init" - inject_cloud_init "$name" || { + inject_cloud_init "$name" "$user" || { errorrc "FAIL: injecting cloud-init into $name failed." return } - # install dependencies - debug 1 "installing dependencies" - inside "$name" /bin/sh &2; } -fail() { [ $# -eq 0 ] || error "$@"; exit 1; } -info() { echo "$@"; } - -pips=$(for p in $pips; do echo "$p"; done | sort -u) -packages=$(for p in $packages; do echo "$p"; done | sort -u) - -if ! rpm -q epel-release >/dev/null; then - yum install --assumeyes epel-release || - fail "failed: yum install epel-release" -fi -yum install --assumeyes $packages || - fail "failed: yum install" "$packages" - -pip install --upgrade $pips || - fail "failed: pip install $pips" -- cgit v1.2.3 From b23d9d7c5c112612dbaaf8c8371c9e735500b2eb Mon Sep 17 00:00:00 2001 From: Chad Smith Date: Wed, 14 Jun 2017 17:11:43 -0600 Subject: ci deps: Add --test-distro to read-dependencies to install all deps read-dependencies now takes --test-distro param to indicate we want to install all system package depenencies to allow for testing and building for our continous integration environment. It allows us to install all needed deps on a fresh system with: python3 ./tools/read-dependencies --distro ubuntu --test-distro [--dry-run]. Additionally read-dependencies now looks at what version of python is running the script (py2 vs p3) and opts to install python 2 or 3 system deps respectively. This behavior can still be overridden with python3 ./tools/read-dependencies ... --python-version 2. There are also some distro-specific packaging and test dependencies, like devscripts, tox and libssl-dev on debian or ubuntu. Those pkg dependencies have now been broken out from common pkg deps to avoid trying to install them on centos/redhat/suse. --- Makefile | 6 ++---- packages/bddeb | 4 +++- tools/read-dependencies | 46 +++++++++++++++++++++++++++++++++++++--------- tools/run-centos | 5 +---- 4 files changed, 43 insertions(+), 18 deletions(-) (limited to 'tools/run-centos') diff --git a/Makefile b/Makefile index c752530c..e9f54982 100644 --- a/Makefile +++ b/Makefile @@ -54,12 +54,10 @@ unittest3: clean_pyc nosetests3 $(noseopts) tests/unittests ci-deps-ubuntu: - @$(PYVER) $(CWD)/tools/read-dependencies --distro ubuntu --install --python-version 3 - @$(PYVER) $(CWD)/tools/read-dependencies --distro ubuntu --requirements-file test-requirements.txt --install --python-version 3 + @$(PYVER) $(CWD)/tools/read-dependencies --distro-ubuntu --test-distro ci-deps-centos: - @$(PYVER) $(CWD)/tools/read-dependencies --distro centos --install - @$(PYVER) $(CWD)/tools/read-dependencies --distro centos --requirements-file test-requirements.txt --install + @$(PYVER) $(CWD)/tools/read-dependencies --distro centos --test-distro pip-requirements: @echo "Installing cloud-init dependencies..." diff --git a/packages/bddeb b/packages/bddeb index e45af6ee..609a94fb 100755 --- a/packages/bddeb +++ b/packages/bddeb @@ -72,7 +72,9 @@ def write_debian_folder(root, templ_data, is_python2, cloud_util_deps): requires = ['cloud-utils | cloud-guest-utils'] if cloud_util_deps else [] # We consolidate all deps as Build-Depends as our package build runs all # tests so we need all runtime dependencies anyway. - requires.extend(reqs + test_reqs + [python]) + # NOTE: python package was moved to the front after debuild -S would fail with + # 'Please add apropriate interpreter' errors (as in debian bug 861132) + requires.extend([python] + reqs + test_reqs) templater.render_to_file(util.abs_join(find_root(), 'packages', 'debian', 'control.in'), util.abs_join(deb_dir, 'control'), diff --git a/tools/read-dependencies b/tools/read-dependencies index 8a585343..2a648680 100755 --- a/tools/read-dependencies +++ b/tools/read-dependencies @@ -40,8 +40,13 @@ DISTRO_INSTALL_PKG_CMD = { } -# List of base system packages required to start using make -EXTRA_SYSTEM_BASE_PKGS = ['make', 'sudo', 'tar'] +# List of base system packages required to enable ci automation +CI_SYSTEM_BASE_PKGS = { + 'common': ['make', 'sudo', 'tar'], + 'redhat': ['python-tox'], + 'centos': ['python-tox'], + 'ubuntu': ['devscripts', 'python3-dev', 'libssl-dev', 'tox', 'sbuild'], + 'debian': ['devscripts', 'python3-dev', 'libssl-dev', 'tox', 'sbuild']} # JSON definition of distro-specific package dependencies @@ -70,10 +75,16 @@ def get_parser(): dest='install', help='When specified, install the required system packages.') parser.add_argument( - '-v', '--python-version', type=str, dest='python_version', default="2", + '-t', '--test-distro', action='store_true', default=False, + dest='test_distro', + help='Additionally install continuous integration system packages ' + 'required for build and test automation.') + parser.add_argument( + '-v', '--python-version', type=str, dest='python_version', default=None, choices=["2", "3"], - help='The version of python we want to generate system package ' - 'dependencies for.') + help='Override the version of python we want to generate system ' + 'package dependencies for. Defaults to the version of python ' + 'this script is called with') return parser @@ -114,13 +125,17 @@ def parse_pip_requirements(requirements_path): return dep_names -def translate_pip_to_system_pkg(pip_requires, renames, python_ver="2"): +def translate_pip_to_system_pkg(pip_requires, renames, python_ver): """Translate pip package names to distro-specific package names. @param pip_requires: List of versionless pip package names to translate. @param renames: Dict containg special case renames from pip name to system package name for the distro. + @param python_ver: Optional python version string "2" or "3". When None, + use the python version that is calling this script via sys.version_info. """ + if python_ver is None: + python_ver = str(sys.version_info[0]) if python_ver == "2": prefix = "python-" else: @@ -147,6 +162,16 @@ def main(distro): else: topd = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + if args.test_distro: + # Give us all the system deps we need for continuous integration + if args.req_files: + sys.stderr.write( + "Parameter --test-distro overrides --requirements-file. Use " + "one or the other.\n") + sys.exit(1) + args.req_files = [os.path.join(topd, DEFAULT_REQUIREMENTS), + os.path.join(topd, 'test-' + DEFAULT_REQUIREMENTS)] + args.install = True if args.req_files is None: args.req_files = [os.path.join(topd, DEFAULT_REQUIREMENTS)] if not os.path.isfile(args.req_files[0]): @@ -179,16 +204,19 @@ def main(distro): else: all_deps = pip_pkg_names if args.install: - pkg_install(all_deps, args.distro, args.dry_run) + pkg_install(all_deps, args.distro, args.test_distro, args.dry_run) else: print('\n'.join(all_deps)) -def pkg_install(pkg_list, distro, dry_run=False): +def pkg_install(pkg_list, distro, test_distro=False, dry_run=False): """Install a list of packages using the DISTRO_INSTALL_PKG_CMD.""" + if test_distro: + pkg_list = list(pkg_list) + CI_SYSTEM_BASE_PKGS['common'] + distro_base_pkgs = CI_SYSTEM_BASE_PKGS.get(distro, []) + pkg_list += distro_base_pkgs print('Installing deps: {0}{1}'.format( '(dryrun)' if dry_run else '', ' '.join(pkg_list))) - pkg_list = list(pkg_list) + EXTRA_SYSTEM_BASE_PKGS install_cmd = [] if dry_run: install_cmd.append('echo') diff --git a/tools/run-centos b/tools/run-centos index 99ba6be0..b10e3bc4 100755 --- a/tools/run-centos +++ b/tools/run-centos @@ -221,10 +221,7 @@ main() { } inside_as_cd "$name" root "$cdir" \ - ./tools/read-dependencies \ - --requirements-file=requirements.txt \ - --requirements-file=test-requirements.txt \ - --distro=centos --install || { + ./tools/read-dependencies --distro=centos --test-distro || { errorrc "FAIL: failed to install dependencies with read-dependencies" return } -- cgit v1.2.3 From 33d573907d3ffc790e28102ecac15c3be6a85462 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 21 Jul 2017 13:34:05 -0400 Subject: tools/run-centos: make running with no argument show help. If you ran tools/run-centos without an argument it would fail due to 'set -u' like: ./tools/run-centos: line 266: 1: unbound variable --- tools/run-centos | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/run-centos') diff --git a/tools/run-centos b/tools/run-centos index b10e3bc4..d44d5145 100755 --- a/tools/run-centos +++ b/tools/run-centos @@ -262,7 +262,7 @@ main() { return 0 } -if [ "$1" = "prep" ]; then +if [ "${1:-}" = "prep" ]; then shift prep "$@" else -- cgit v1.2.3