|
| 1 | +#!/bin/bash -l |
| 2 | +# note: the -l is needed here for bash to always make a login shell and load rvm if it hasn't been loaded |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 5 | +# or more contributor license agreements. See the NOTICE file |
| 6 | +# distributed with this work for additional information |
| 7 | +# regarding copyright ownership. The ASF licenses this file |
| 8 | +# to you under the Apache License, Version 2.0 (the |
| 9 | +# "License"); you may not use this file except in compliance |
| 10 | +# with the License. You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, |
| 15 | +# software distributed under the License is distributed on an |
| 16 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | +# KIND, either express or implied. See the License for the |
| 18 | +# specific language governing permissions and limitations |
| 19 | +# under the License. |
| 20 | + |
| 21 | +# build script which wraps around test-kitchen to test the systemvm |
| 22 | + |
| 23 | +function usage() { |
| 24 | + cat <<END |
| 25 | +Usage: |
| 26 | + ./build.sh |
| 27 | +END |
| 28 | + exit 0 |
| 29 | +} |
| 30 | +echo $@ | grep help >/dev/null && usage |
| 31 | +echo $@ | grep '\-h' >/dev/null && usage |
| 32 | + |
| 33 | +set -e |
| 34 | + |
| 35 | +### |
| 36 | +### Configuration |
| 37 | +### |
| 38 | +# whether to show DEBUG logs |
| 39 | +DEBUG="${DEBUG:-}" |
| 40 | +# whether to have other commands trace their actions |
| 41 | +TRACE="${TRACE:-0}" |
| 42 | +JENKINS_HOME=${JENKINS_HOME:-} |
| 43 | +if [[ ! -z "${JENKINS_HOME}" ]]; then |
| 44 | + DEBUG=1 |
| 45 | +fi |
| 46 | + |
| 47 | +kitchen_args= |
| 48 | +if [[ "${DEBUG}" == "1" ]]; then |
| 49 | + kitchen_args="-l debug" |
| 50 | +fi |
| 51 | + |
| 52 | +# optional (jenkins) build number tag to put into the image filename |
| 53 | +VPC_IP="${VPC_IP:-192.168.56.30}" |
| 54 | +export VPC_IP |
| 55 | + |
| 56 | +### |
| 57 | +### Generic helper functions |
| 58 | +### |
| 59 | + |
| 60 | +# how to tell sed to use extended regular expressions |
| 61 | +os=`uname` |
| 62 | +sed_regex_option="-E" |
| 63 | +if [ "${os}" == "Linux" ]; then |
| 64 | + sed_regex_option="-r" |
| 65 | +fi |
| 66 | + |
| 67 | +# logging support |
| 68 | +if [[ "${DEBUG}" == "1" ]]; then |
| 69 | + set -x |
| 70 | +fi |
| 71 | + |
| 72 | +function log() { |
| 73 | + local level=${1?} |
| 74 | + shift |
| 75 | + |
| 76 | + if [[ "${DEBUG}" != "1" && "${level}" == "DEBUG" ]]; then |
| 77 | + return |
| 78 | + fi |
| 79 | + |
| 80 | + local code= |
| 81 | + local line="[$(date '+%F %T')] $level: $*" |
| 82 | + if [ -t 2 ] |
| 83 | + then |
| 84 | + case "$level" in |
| 85 | + INFO) code=36 ;; |
| 86 | + DEBUG) code=30 ;; |
| 87 | + WARN) code=33 ;; |
| 88 | + ERROR) code=31 ;; |
| 89 | + *) code=37 ;; |
| 90 | + esac |
| 91 | + echo -e "\033[${code}m${line}\033[0m" |
| 92 | + else |
| 93 | + echo "$line" |
| 94 | + fi >&2 |
| 95 | +} |
| 96 | + |
| 97 | +function error() { |
| 98 | + log ERROR $@ |
| 99 | + exit 1 |
| 100 | +} |
| 101 | + |
| 102 | +# cleanup code support |
| 103 | +declare -a on_exit_items |
| 104 | + |
| 105 | +function on_exit() { |
| 106 | + for (( i=${#on_exit_items[@]}-1 ; i>=0 ; i-- )) ; do |
| 107 | + sleep 2 |
| 108 | + log DEBUG "on_exit: ${on_exit_items[i]}" |
| 109 | + eval ${on_exit_items[i]} |
| 110 | + done |
| 111 | +} |
| 112 | + |
| 113 | +function add_on_exit() { |
| 114 | + local n=${#on_exit_items[*]} |
| 115 | + on_exit_items[${n}]="$*" |
| 116 | + if [ ${n} -eq 0 ]; then |
| 117 | + log DEBUG "Setting trap" |
| 118 | + trap on_exit EXIT |
| 119 | + fi |
| 120 | +} |
| 121 | + |
| 122 | +# retry code support |
| 123 | +function retry() { |
| 124 | + local times=$1 |
| 125 | + shift |
| 126 | + local count=0 |
| 127 | + while [ ${count} -lt ${times} ]; do |
| 128 | + "$@" && break |
| 129 | + count=$(( $count + 1 )) |
| 130 | + sleep ${count} |
| 131 | + done |
| 132 | + |
| 133 | + if [ ${count} -eq ${times} ]; then |
| 134 | + error "Failed ${times} times: $@" |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | +### |
| 139 | +### Script logic |
| 140 | +### |
| 141 | + |
| 142 | +function setup_ruby() { |
| 143 | + local bundle_args= |
| 144 | + if [[ ! -z "${JENKINS_HOME}" ]]; then |
| 145 | + # inspired by https://github.com/CloudBees-community/rubyci-clickstart/blob/master/bin/run-ci |
| 146 | + # also see https://rvm.io/integration/jenkins |
| 147 | + # .rvmrc won't get trusted/auto-loaded by jenkins by default |
| 148 | + export VAGRANT_HOME=$HOME/.vagrant.d-release-cloudstack |
| 149 | + rvm use ruby-1.9.3@vagrant-release-cloudstack --create |
| 150 | + # do not use --deployment since that requires Gemfile.lock...and we prefer an up-to-date veewee |
| 151 | + bundle_args="--path vendor/bundle" |
| 152 | + fi |
| 153 | + bundle check || bundle install ${bundle_args} |
| 154 | +} |
| 155 | + |
| 156 | +function prepare() { |
| 157 | + log INFO "preparing for build" |
| 158 | + setup_ruby |
| 159 | + rm -f systemvm.iso |
| 160 | +} |
| 161 | + |
| 162 | +function box_update() { |
| 163 | + log INFO "invoking vagrant box update" |
| 164 | + vagrant box update |
| 165 | + log INFO "vagrant box update complete" |
| 166 | +} |
| 167 | + |
| 168 | +function converge_kitchen() { |
| 169 | + log INFO "invoking test-kitchen converge" |
| 170 | + kitchen create ${kitchen_args} |
| 171 | + kitchen converge ${kitchen_args} |
| 172 | + log INFO "test-kitchen complete" |
| 173 | +} |
| 174 | + |
| 175 | +function verify_kitchen() { |
| 176 | + log INFO "invoking test-kitchen verify" |
| 177 | + |
| 178 | + kitchen verify ${kitchen_args} |
| 179 | + |
| 180 | + # re-run busser test with patched serverspec gem to get a rspec.xml |
| 181 | + kitchen exec ${kitchen_args} -c ' |
| 182 | +BUSSER_ROOT="/tmp/busser" GEM_HOME="/tmp/busser/gems" GEM_PATH="/tmp/busser/gems" GEM_CACHE="/tmp/busser/gems/cache" |
| 183 | +export BUSSER_ROOT GEM_HOME GEM_PATH GEM_CACHE |
| 184 | +/tmp/kitchen/bootstrap.sh |
| 185 | +sudo -E /tmp/busser/bin/busser test |
| 186 | +' |
| 187 | + |
| 188 | + # ssh to machine ourselves to avoid kitchen output |
| 189 | + (cd .kitchen/kitchen-vagrant/default-systemvm; vagrant ssh-config) > vagrant_ssh_config |
| 190 | + add_on_exit rm -f vagrant_ssh_config |
| 191 | + scp -F vagrant_ssh_config default:/tmp/rspec.xml rspec.xml |
| 192 | + log INFO "test results in rspec.xml" |
| 193 | + |
| 194 | + log INFO "test-kitchen complete" |
| 195 | +} |
| 196 | + |
| 197 | +function destroy_kitchen() { |
| 198 | + log INFO "invoking test-kitchen destroy" |
| 199 | + kitchen destroy ${kitchen_args} |
| 200 | + log INFO "test-kitchen destroy complete" |
| 201 | +} |
| 202 | +### |
| 203 | +### Main invocation |
| 204 | +### |
| 205 | + |
| 206 | +function main() { |
| 207 | + prepare |
| 208 | + box_update |
| 209 | + add_on_exit destroy_kitchen |
| 210 | + converge_kitchen |
| 211 | + verify_kitchen |
| 212 | + add_on_exit log INFO "BUILD SUCCESSFUL" |
| 213 | +} |
| 214 | + |
| 215 | +# we only run main() if not source-d |
| 216 | +return 2>/dev/null || main |
0 commit comments