From a32d4eaf75685204f2358d58075dd464f216a585 Mon Sep 17 00:00:00 2001 From: MJB <mjb@it-innovation.soton.ac.uk> Date: Sat, 2 Jun 2018 20:18:00 +0100 Subject: [PATCH] #44 refactored container scripts into one script fixtures.sh, supports all of single container management --- README.md | 22 +++- scripts/clmc-service/install.sh | 39 ++++--- scripts/test/conts-destroy.sh | 17 --- scripts/test/conts-env.sh | 4 - scripts/test/conts-start.sh | 13 --- scripts/test/conts-stop.sh | 13 --- scripts/test/{conts-create.sh => fixture.sh} | 104 +++++++++++++++++-- 7 files changed, 139 insertions(+), 73 deletions(-) delete mode 100755 scripts/test/conts-destroy.sh delete mode 100755 scripts/test/conts-env.sh delete mode 100755 scripts/test/conts-start.sh delete mode 100755 scripts/test/conts-stop.sh rename scripts/test/{conts-create.sh => fixture.sh} (61%) mode change 100755 => 100644 diff --git a/README.md b/README.md index 405b567..e68c2d8 100644 --- a/README.md +++ b/README.md @@ -59,16 +59,30 @@ SSH into the VM `vagrant ssh` -Create the services needed for integration tests +The containers are controlled using a script called /vagrant/scripts/test/fixtures.sh + +``` +Usage: fixturs.sh create|start|stop|destroy [-f config_file] [-r repo_root] [-c service_name]" +``` + +To create all the services needed for integration tests ``` sudo su -/vagrant/scripts/test/conts-create.sh +/vagrant/scripts/test/fixtures.sh create -f /vagrant/src/test/clmctest/rspec.json ``` -All of the containers created are defined in the file `/vagrant/src/test/clmctest/rspec.json` +The containers created are defined an rspec.json file, there's an example here `/vagrant/src/test/clmctest/rspec.json` + +The `fixtures.sh` script defaults to look for a rspec.json in the current directory, you can specify a specific rspec.json file using the -f option + +To create|start|stop|destroy specific services use the -c option e.g. + +``` +/vagrant/scripts/test/fixtures.sh create -f /vagrant/src/test/clmctest/rspec.json -c clmc-service +``` -Attach to the test-runner +Attach to the test-runner to run the tests `lxc-attach -n test-runner` diff --git a/scripts/clmc-service/install.sh b/scripts/clmc-service/install.sh index 94ff9b5..2c25e72 100755 --- a/scripts/clmc-service/install.sh +++ b/scripts/clmc-service/install.sh @@ -95,9 +95,6 @@ systemctl start chronograf ## CLMC-SERVICE ## ---------------------------------------------------------------------------------- - - - echo "----> Configuring virtualenvwrapper" export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh @@ -150,15 +147,33 @@ if [ $? -ne 0 ] ; then exit 1 fi -# start the service -echo "----> Starting CLMC web service" -nohup pserve production.ini > /dev/null 2>&1 & -if [ $? -ne 0 ] ; then - echo "Failed: starting clmc-webservice" - exit 1 -else - echo "CLMC service started." -fi +# Install minioclmc as systemctl service +# ----------------------------------------------------------------------- +mkdir -p /opt/flame/clmc +start_script_file="/opt/flame/clmc/start.sh" +echo "#!/bin/bash" > $start_script_file +echo "export WORKON_HOME=${HOME}/.virtualenvs" >> $start_script_file +echo "source /usr/local/bin/virtualenvwrapper.sh" >> $start_script_file +echo "workon CLMC" >> $start_script_file +echo "pserve ${REPO_ROOT}/src/service/production.ini > /dev/null 2>&1 &" >> $start_script_file + +chmod 755 $start_script_file + +file="/lib/systemd/system/flameclmc.service" +echo "[Unit]" > $file +echo "Description=flameclmc" >> $file +echo "After=network.target" >> $file +echo "" >> $file +echo "[Service]" >> $file +echo "Type=forking" >> $file +echo "ExecStart=${start_script_file}" >> $file +echo "" >> $file +echo "[Install]" >> $file +echo "WantedBy=multi-user.target" >> $file + +systemctl daemon-reload +systemctl enable flameclmc.service +systemctl start flameclmc.service # wait for the clmc service to start while ! nc -z localhost 9080 diff --git a/scripts/test/conts-destroy.sh b/scripts/test/conts-destroy.sh deleted file mode 100755 index 456816d..0000000 --- a/scripts/test/conts-destroy.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -set -eu -o pipefail -cd `dirname $0` - -source conts-env.sh - -service_names=$(jq -r '.[].name' ${rspec_file}) -for service_name in $service_names; do - if lxc-info -n ${service_name}; then - echo "Stopping container: ${service_name}" - lxc-stop -n ${service_name} - echo "Destroying container: ${service_name}" - lxc-destroy -n ${service_name} - ip=$(jq -r --arg NAME ${service_name} '.[] | select(.name==$NAME) | .ip_address' ${rspec_file}) - sed -i "/dhcp-host=${service_name},/d" /etc/lxc/dnsmasq.conf - fi -done diff --git a/scripts/test/conts-env.sh b/scripts/test/conts-env.sh deleted file mode 100755 index 742d547..0000000 --- a/scripts/test/conts-env.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -repo_root="/vagrant" -rspec_file=$repo_root"/src/test/clmctest/rspec.json" \ No newline at end of file diff --git a/scripts/test/conts-start.sh b/scripts/test/conts-start.sh deleted file mode 100755 index 2af56c1..0000000 --- a/scripts/test/conts-start.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -set -eu -o pipefail -cd `dirname $0` - -source conts-env.sh - -service_names=$(jq -r '.[].name' ${rspec_file}) -for service_name in $service_names; do - if lxc-info -n ${service_name}; then - echo "Starting container: ${service_name}" - lxc-start -n ${service_name} - fi -done diff --git a/scripts/test/conts-stop.sh b/scripts/test/conts-stop.sh deleted file mode 100755 index a647869..0000000 --- a/scripts/test/conts-stop.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -set -eu -o pipefail -cd `dirname $0` - -source conts-env.sh - -service_names=$(jq -r '.[].name' ${rspec_file}) -for service_name in $service_names; do - if lxc-info -n ${service_name}; then - echo "Stopping container: ${service_name}" - lxc-stop -n ${service_name} - fi -done \ No newline at end of file diff --git a/scripts/test/conts-create.sh b/scripts/test/fixture.sh old mode 100755 new mode 100644 similarity index 61% rename from scripts/test/conts-create.sh rename to scripts/test/fixture.sh index 637d8ba..acc239e --- a/scripts/test/conts-create.sh +++ b/scripts/test/fixture.sh @@ -1,16 +1,18 @@ #!/bin/bash -set -eu -o pipefail -cd `dirname $0` -source conts-env.sh +usage() { + echo "Usage: $0 create|start|stop|destroy [-f config_file] [-r repo_root] [-c service_name]" 1>&2 + exit 1 +} -# iterate through each service required for integration testing -service_names=$(jq -r '.[].name' ${rspec_file}) -for service_name in $service_names; do +create() { + service_name=$1 + config_file=$2 + repo_root=$3 if ! lxc-info -n ${service_name}; then # create a container with a static ip address echo "Creating container: ${service_name}" - SERVICE=$(jq --arg NAME ${service_name} '.[] | select(.name==$NAME)' ${rspec_file}) + SERVICE=$(jq --arg NAME ${service_name} '.[] | select(.name==$NAME)' ${config_file}) echo $SERVICE ip=$(echo $SERVICE | jq -r '.ip_address') echo "dhcp-host=${service_name},${ip}" >> /etc/lxc/dnsmasq.conf @@ -83,10 +85,92 @@ for service_name in $service_names; do lxc-attach -n ${service_name} -- service telegraf restart fi fi +} + +start() { + service_name=$1 + if lxc-info -n ${service_name}; then + echo "Starting container: ${service_name}" + lxc-start -n ${service_name} + fi +} + +stop() { + service_name=$1 + if lxc-info -n ${service_name}; then + echo "Stopping container: ${service_name}" + lxc-stop -n ${service_name} + fi +} + +destroy() { + service_name=$1 + config_file=$2 + if lxc-info -n ${service_name}; then + echo "Stopping container: ${service_name}" + lxc-stop -n ${service_name} + echo "Destroying container: ${service_name}" + lxc-destroy -n ${service_name} + ip=$(jq -r --arg NAME ${service_name} '.[] | select(.name==$NAME) | .ip_address' ${config_file}) + sed -i "/dhcp-host=${service_name},/d" /etc/lxc/dnsmasq.conf + fi +} + +# inc option index by 1 as first argument is the command and not parsed by getopts +if [ $# -gt 1 ]; then + OPTIND=$((OPTIND+1)) +fi + +repo_root="/vagrant" +config_file="rspec.json" + +# collect the optional arguments +while getopts "hf:r:c:" opt; do + case $opt in + h) usage; exit ;; + f) config_file=${OPTARG} ;; + r) repo_root=${OPTARG} ;; + c) container=${OPTARG} ;; + \?) usage ;; + esac done -#./conts-stop.sh +# check configuration file exists +if [ ! -f ${config_file} ]; then + echo "Configuration file does not exist: ${config_file}" >&2 + usage + exit 1 +fi -#service lxc-net restart +# check repo root directory exists +if [ ! -d ${repo_root} ]; then + echo "Repo root directory does not exist: ${repo_root}" >&2 + usage + exit 1 +fi -#./conts-start.sh \ No newline at end of file +# iterate of list of services in configuration file +command=$1 +service_names=$(jq -r '.[].name' ${config_file}) +for service_name in $service_names; do + # if all or specific container + if [ -z ${container} ] || [ ${container} == ${service_name} ]; then + case "${command}" in + create) + create ${service_name} ${config_file} ${repo_root} + ;; + start) + start ${service_name} ${config_file} ${repo_root} + ;; + stop) + stop ${service_name} ${config_file} ${repo_root} + ;; + destroy) + destroy ${service_name} ${config_file} ${repo_root} + ;; + *) + usage + ;; + esac + fi +done -- GitLab