diff --git a/scripts/test/fixture.sh b/scripts/test/fixture.sh index c5e6070eec8317b367bd951a3317dadb06912889..821cb71971cdaea64f0d4522160af20e87068552 100755 --- a/scripts/test/fixture.sh +++ b/scripts/test/fixture.sh @@ -28,6 +28,12 @@ repo_root="/vagrant" target_root="/opt/clmc" config_file="rspec.json" +# make sure UID is root +if [ "$EUID" -ne 0 ]; then + echo "please run as root (requires to change iptables)" + exit +fi + usage() { echo "Usage: $0 create|start|stop|destroy [-f config_file] [-r repo_root] [-c container_name|all]" 1>&2 echo " -f defaults to '${config_file}'" 1>&2 @@ -39,8 +45,8 @@ usage() { failed() { msg=$1 sn=$2 - echo "failed on: $msg" - delete_c $sn + echo "failed to: $msg for service $service_name" + delete $sn exit 1 } @@ -55,26 +61,28 @@ create() { echo $SERVICE ip=$(echo $SERVICE | jq -r '.ip_address') - lxc init ubuntu:16.04 ${service_name} || return + lxc init ubuntu:16.04 ${service_name} || failed "crate lxc image" ${service_name} echo "Creating network: ${service_name}" - lxc network attach lxcbr0 ${service_name} eth0 - lxc config device set ${service_name} eth0 ipv4.address ${ip} - lxc config set ${service_name} security.privileged true + lxc network attach lxcbr0 ${service_name} eth0 || failed "attach lxcbr0" ${service_name} + lxc config device set ${service_name} eth0 ipv4.address ${ip} || failed "config eth0" ${service_name} + lxc config set ${service_name} security.privileged true || failed "config security" ${service_name} # copy flame clmc files into the root container echo "Copying files to rootfs" container_dir="/var/lib/lxd/containers/"${service_name}"/rootfs" container_repo_root=${container_dir}${target_root} - mkdir -p ${container_repo_root} || failed "to create target directory" ${service_name} - cp -f ${repo_root}/reporc ${container_repo_root} || failed "to copy reporc" ${service_name} - cp -rf ${repo_root}/scripts ${container_repo_root} || failed "to copy scripts" ${service_name} - cp -rf ${repo_root}/src ${container_repo_root} || failed "to copy src" ${service_name} - chown -R 100000:100000 ${container_repo_root} || failed "to change permissions" ${service_name} + mkdir -p ${container_repo_root} || failed "create target directory" ${service_name} + cp -f ${repo_root}/reporc ${container_repo_root} || failed "copy reporc" ${service_name} + cp -rf ${repo_root}/scripts ${container_repo_root} || failed "copy scripts" ${service_name} + cp -rf ${repo_root}/src ${container_repo_root} || failed "copy src" ${service_name} + chown -R 100000:100000 ${container_repo_root} || failed "change permissions" ${service_name} # start the container echo "Starting: ${service_name}" lxc start ${service_name} + # add delay to avoid while-loop lxc command failing initially + sleep 1 while :; do echo "Waiting for container to start: ${service_name}" lxc file pull ${service_name}/etc/resolv.conf - | grep -q nameserver && break @@ -123,7 +131,7 @@ create() { lxc exec ${service_name} --env REPO_ROOT="${target_root}" -- ${cmd} # check that telegraf installed (it may not have if the reporc file was not present or Nexus server was down) - #if lxc-attach -n ${service_name} -- ls /etc/telegraf |& grep 'ls: cannot access'; then + if lxc exec ${service_name} -- ls /etc/telegraf |& grep 'ls: cannot access'; then echo "Telegraf agent failed to install for ${service_name} (check reporc?)" exit 1 @@ -133,13 +141,14 @@ create() { lxc exec ${service_name} -- service telegraf stop # copy telegraf configuration templates - cp -f ${repo_root}/scripts/clmc-agent/telegraf.conf ${container_dir}/etc/telegraf/ - cp -f ${repo_root}/scripts/clmc-agent/telegraf_output.conf ${container_dir}/etc/telegraf/telegraf.d/ + echo "copy telegraf configuration templates" + lxc file push -r scripts/clmc-agent/telegraf.conf ${service_name}/etc/telegraf/ + lxc file push -r scripts/clmc-agent/telegraf_output.conf ${service_name}/etc/telegraf/telegraf.d/ # copy the 'host' config into all service containers - cp -f ${repo_root}/src/test/clmctest/services/host/telegraf*.conf ${container_dir}/etc/telegraf/telegraf.d/ + lxc file push -r src/test/clmctest/services/host/telegraf*.conf ${service_name}/etc/telegraf/telegraf.d/ # copy the service-specific config - cp -f ${repo_root}/src/test/clmctest/services/${sf_package_id}/telegraf*.conf ${container_dir}/etc/telegraf/telegraf.d/ - chown -R 100000:100000 ${container_dir}/etc/telegraf/ + lxc file push -r src/test/clmctest/services/${sf_package_id}/telegraf*.conf ${service_name}/etc/telegraf/telegraf.d/ + lxc exec ${service_name} -- chown -R 100000:100000 /etc/telegraf/ # replace telegraf template with container parameters cmd="${target_root}/scripts/clmc-agent/configure.sh ${location} ${sfc_id} ${sfc_instance_id} ${sf_package_id} ${sf_id} ${sf_endpoint_id} ${influxdb_url}" @@ -180,7 +189,7 @@ stop() { fi } -delete_c() { +delete() { service_name=$1 if lxc list | grep -q ${service_name}; then echo "Deleting container: ${service_name}"