Skip to content
Snippets Groups Projects
Commit de9f537e authored by panos's avatar panos
Browse files

improve script to exit early on failure

parent 20b571c1
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,12 @@ repo_root="/vagrant" ...@@ -28,6 +28,12 @@ repo_root="/vagrant"
target_root="/opt/clmc" target_root="/opt/clmc"
config_file="rspec.json" 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() { usage() {
echo "Usage: $0 create|start|stop|destroy [-f config_file] [-r repo_root] [-c container_name|all]" 1>&2 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 echo " -f defaults to '${config_file}'" 1>&2
...@@ -39,8 +45,8 @@ usage() { ...@@ -39,8 +45,8 @@ usage() {
failed() { failed() {
msg=$1 msg=$1
sn=$2 sn=$2
echo "failed on: $msg" echo "failed to: $msg for service $service_name"
delete_c $sn delete $sn
exit 1 exit 1
} }
...@@ -55,26 +61,28 @@ create() { ...@@ -55,26 +61,28 @@ create() {
echo $SERVICE echo $SERVICE
ip=$(echo $SERVICE | jq -r '.ip_address') 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}" echo "Creating network: ${service_name}"
lxc network attach lxcbr0 ${service_name} eth0 lxc network attach lxcbr0 ${service_name} eth0 || failed "attach lxcbr0" ${service_name}
lxc config device set ${service_name} eth0 ipv4.address ${ip} lxc config device set ${service_name} eth0 ipv4.address ${ip} || failed "config eth0" ${service_name}
lxc config set ${service_name} security.privileged true lxc config set ${service_name} security.privileged true || failed "config security" ${service_name}
# copy flame clmc files into the root container # copy flame clmc files into the root container
echo "Copying files to rootfs" echo "Copying files to rootfs"
container_dir="/var/lib/lxd/containers/"${service_name}"/rootfs" container_dir="/var/lib/lxd/containers/"${service_name}"/rootfs"
container_repo_root=${container_dir}${target_root} container_repo_root=${container_dir}${target_root}
mkdir -p ${container_repo_root} || failed "to create target directory" ${service_name} mkdir -p ${container_repo_root} || failed "create target directory" ${service_name}
cp -f ${repo_root}/reporc ${container_repo_root} || failed "to copy reporc" ${service_name} cp -f ${repo_root}/reporc ${container_repo_root} || failed "copy reporc" ${service_name}
cp -rf ${repo_root}/scripts ${container_repo_root} || failed "to copy scripts" ${service_name} cp -rf ${repo_root}/scripts ${container_repo_root} || failed "copy scripts" ${service_name}
cp -rf ${repo_root}/src ${container_repo_root} || failed "to copy src" ${service_name} cp -rf ${repo_root}/src ${container_repo_root} || failed "copy src" ${service_name}
chown -R 100000:100000 ${container_repo_root} || failed "to change permissions" ${service_name} chown -R 100000:100000 ${container_repo_root} || failed "change permissions" ${service_name}
# start the container # start the container
echo "Starting: ${service_name}" echo "Starting: ${service_name}"
lxc start ${service_name} lxc start ${service_name}
# add delay to avoid while-loop lxc command failing initially
sleep 1
while :; do while :; do
echo "Waiting for container to start: ${service_name}" echo "Waiting for container to start: ${service_name}"
lxc file pull ${service_name}/etc/resolv.conf - | grep -q nameserver && break lxc file pull ${service_name}/etc/resolv.conf - | grep -q nameserver && break
...@@ -123,7 +131,7 @@ create() { ...@@ -123,7 +131,7 @@ create() {
lxc exec ${service_name} --env REPO_ROOT="${target_root}" -- ${cmd} 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) # 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 if lxc exec ${service_name} -- ls /etc/telegraf |& grep 'ls: cannot access'; then
echo "Telegraf agent failed to install for ${service_name} (check reporc?)" echo "Telegraf agent failed to install for ${service_name} (check reporc?)"
exit 1 exit 1
...@@ -133,13 +141,14 @@ create() { ...@@ -133,13 +141,14 @@ create() {
lxc exec ${service_name} -- service telegraf stop lxc exec ${service_name} -- service telegraf stop
# copy telegraf configuration templates # copy telegraf configuration templates
cp -f ${repo_root}/scripts/clmc-agent/telegraf.conf ${container_dir}/etc/telegraf/ echo "copy telegraf configuration templates"
cp -f ${repo_root}/scripts/clmc-agent/telegraf_output.conf ${container_dir}/etc/telegraf/telegraf.d/ 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 # 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 # copy the service-specific config
cp -f ${repo_root}/src/test/clmctest/services/${sf_package_id}/telegraf*.conf ${container_dir}/etc/telegraf/telegraf.d/ lxc file push -r src/test/clmctest/services/${sf_package_id}/telegraf*.conf ${service_name}/etc/telegraf/telegraf.d/
chown -R 100000:100000 ${container_dir}/etc/telegraf/ lxc exec ${service_name} -- chown -R 100000:100000 /etc/telegraf/
# replace telegraf template with container parameters # 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}" 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() { ...@@ -180,7 +189,7 @@ stop() {
fi fi
} }
delete_c() { delete() {
service_name=$1 service_name=$1
if lxc list | grep -q ${service_name}; then if lxc list | grep -q ${service_name}; then
echo "Deleting container: ${service_name}" echo "Deleting container: ${service_name}"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment