← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~powersj/cloud-init:create-centos-tests into cloud-init:master

 

Joshua Powers has proposed merging ~powersj/cloud-init:create-centos-tests into cloud-init:master.

Requested reviews:
  Server Team CI bot (server-team-bot): continuous-integration
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~powersj/cloud-init/+git/cloud-init/+merge/324982

Looking for a review on this. It isn't fully working yet, complains about uncommitted changes, which I have to figure out why but:

* Creates centos 6 or 7 lxd container
* Installs pip and yum dependencies
* Creates centos user
* Tar's up working directory
* Pushes to container and untars
* As user centos, runs unittests
* As user centos, runs ./packages/brpm

I did not use make-tarball because it removes all the git stuff, and to run brpm we have to have the git info since it in turn calls make-tarball. 
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~powersj/cloud-init:create-centos-tests into cloud-init:master.
diff --git a/tools/run-centos b/tools/run-centos
new file mode 100755
index 0000000..50008b3
--- /dev/null
+++ b/tools/run-centos
@@ -0,0 +1,172 @@
+#!/bin/bash
+# This file is part of cloud-init. See LICENSE file for license information.
+
+set -ux
+
+VERBOSITY=0
+TEMP_D=""
+
+error() { echo "$@" 1>&2; }
+errorp() { printf "$@" 1>&2; }
+fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
+failp() { [ $# -eq 0 ] || errorp "$@"; exit 1; }
+
+Usage() {
+    cat <<EOF
+Usage: ${0##*/} [ options ] CentOS version
+
+    This utility can makes it easier to run tests, build rpm and source rpm
+        generation inside a LXC of the specified version of CentOS.
+
+    options:
+      -a | --artifact keep .rpm artifacts
+      -k | --keep     keep container after tests
+      -r | --rpm      build .rpm
+      -s | --srpm     build .src.rpm
+      -u | --unittest run unit tests
+EOF
+}
+
+bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
+cleanup() {
+    if [ ! -n "$keep" ]; then
+        delete_container
+    fi
+
+    [ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
+}
+
+debug() {
+    local level=${1}; shift;
+    [ "${level}" -gt "${VERBOSITY}" ] && return
+    error "${@}"
+}
+
+inject_cloud_init(){
+    # CentOS 6 does not come with tar and network is not up by here,
+    # so sleep a bit to let network finish coming up and install tar
+    if [[ "$version" -eq "6" ]]; then
+        sleep 10
+        lxc exec "$name" -- yum install --assumeyes tar
+    fi
+
+    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"
+    lxc exec "$name" -- useradd "$user"
+    lxc file push "$TEMP_D/$tarball_name" "$name/home/$user"/
+    lxc exec "$name" -- chown "$user:$user" "$tarball"
+    lxc exec "$name" -- su "$user" -c "tar -C /home/$user -xzf $tarball" ||
+        fail "failed: extracting tarball"
+}
+
+start_container() {
+    name=cloud-init-centos-$(uuidgen -t | cut -c1-8)
+    lxc launch images:centos/"$version" "$name"
+
+    for i in {1..120} 
+    do
+        sleep 1
+        runlevel=$(lxc exec "$name" -- /sbin/runlevel)
+        if [ "$runlevel" != "unknown" ]; then
+            break
+        fi 
+    done
+
+    if [ ! -z "${http_proxy-}" ]; then
+        lxc exec "$name" -- sh -c "echo proxy=$http_proxy >> /etc/yum.conf"
+    fi
+}
+
+delete_container() {
+    lxc stop "$name"
+    lxc delete "$name"
+}
+
+main() {
+    local short_opts="ahkrsuv:"
+    local long_opts="artifact,help,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=0;;
+            -h|--help) Usage ; exit 0;;
+            -k|--keep) keep=0;;
+            -r|--rpm) rpm=0;;
+            -s|--srpm) srpm=0;;
+            -u|--unittest) unittest=0;;
+            -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
+    start_container
+    inject_cloud_init
+
+    # install dependencies
+    lxc exec "$name" -- /bin/sh <tools/setup-centos ||
+        fail "failed: setting up container $name"
+
+    rc=0
+	if [ -n "$unittest" ]; then
+        lxc exec "$name" -- su "$user" -c \
+            "cd /home/$user/$tar_folder; nosetests tests/unittests"
+        rc=$((rc+$?))
+	fi
+
+    lxc exec "$name" -- su "$user" -c \
+        "cd /home/$user/$tar_folder; git checkout .; git status"
+
+	if [ -n "$srpm" ]; then
+        lxc exec "$name" -- su "$user" -c \
+            "cd /home/$user/$tar_folder; ./packages/brpm --srpm"
+        rc=$((rc+$?))
+	fi
+
+	if [ -n "$rpm" ]; then
+        lxc exec "$name" -- su "$user" -c \
+            "cd /home/$user/$tar_folder; ./packages/brpm"
+        rc=$((rc+$?))
+	fi
+
+    if [ -n "$artifact" ]; then
+        cmd="ls /home/$user/$tar_folder/*.rpm"
+        for built_rpm in $(lxc exec "$name" -- sh -c "$cmd"); do
+            lxc file pull "$name/$built_rpm" .
+        done
+    fi
+
+    exit "$rc"
+}
+
+main "$@"
+# vi: ts=4 noexpandtab
diff --git a/tools/setup-centos b/tools/setup-centos
new file mode 100755
index 0000000..bc5da8a
--- /dev/null
+++ b/tools/setup-centos
@@ -0,0 +1,49 @@
+#!/bin/sh
+# This file is part of cloud-init. See LICENSE file for license information.
+set -fux
+export LANG=C
+
+packages="
+    file
+    git
+    pyserial
+    python-argparse
+    python-cheetah
+    python-configobj
+    python-devel
+    python-jinja2
+    python-jsonpatch
+    python-oauthlib
+    python-pip
+    python-prettytable
+    python-requests
+    python-six
+    PyYAML
+    rpm-build
+"
+
+pips="
+    contextlib2
+    httpretty
+    mock
+    nose
+    pep8
+    unittest2
+"
+
+error() { echo "$@" 1>&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"

Follow ups