From 4671a981a76f67a62534d8954bca34bc66beb154 Mon Sep 17 00:00:00 2001 From: Jerome Petazzoni Date: Mon, 1 Jun 2020 20:58:23 +0200 Subject: [PATCH] Add deployment automation steps The settings file can now specify an optional list of steps. After creating a bunch of instances, the steps are then automatically executed. This helps since virtually all deployments will be a sequence of 'start + deploy + otheractions'. It also helps to automatically excecute steps like webssh and tailhist (since I tend to forget them often). --- prepare-vms/lib/commands.sh | 49 ++++++++++++++++++++++---- prepare-vms/settings/fundamentals.yaml | 6 ++++ prepare-vms/settings/jerome.yaml | 7 ++++ 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/prepare-vms/lib/commands.sh b/prepare-vms/lib/commands.sh index c48a3166..b49e56dc 100644 --- a/prepare-vms/lib/commands.sh +++ b/prepare-vms/lib/commands.sh @@ -339,7 +339,7 @@ _cmd_maketag() { if [ -z $USER ]; then export USER=anonymous fi - MS=$(($(date +%N)/1000000)) + MS=$(($(date +%N | tr -d 0)/1000000)) date +%Y-%m-%d-%H-%M-$MS-$USER } @@ -493,6 +493,7 @@ _cmd_start() { --settings) SETTINGS=$2; shift 2;; --count) COUNT=$2; shift 2;; --tag) TAG=$2; shift 2;; + --students) STUDENTS=$2; shift 2;; *) die "Unrecognized parameter: $1." esac done @@ -504,8 +505,14 @@ _cmd_start() { die "Please add --settings flag to specify which settings file to use." fi if [ -z "$COUNT" ]; then - COUNT=$(awk '/^clustersize:/ {print $2}' $SETTINGS) - warning "No --count option was specified. Using value from settings file ($COUNT)." + CLUSTERSIZE=$(awk '/^clustersize:/ {print $2}' $SETTINGS) + if [ -z "$STUDENTS" ]; then + warning "Neither --count nor --students was specified." + warning "According to the settings file, the cluster size is $CLUSTERSIZE." + warning "Deploying one cluster of $CLUSTERSIZE nodes." + STUDENTS=1 + fi + COUNT=$(($STUDENTS*$CLUSTERSIZE)) fi # Check that the specified settings and infrastructure are valid. @@ -523,11 +530,41 @@ _cmd_start() { infra_start $COUNT sep info "Successfully created $COUNT instances with tag $TAG" - sep echo created > tags/$TAG/status - info "To deploy Docker on these instances, you can run:" - info "$0 deploy $TAG" + # If the settings.yaml file has a "steps" field, + # automatically execute all the actions listed in that field. + # If an action fails, retry it up to 10 times. + python -c 'if True: # hack to deal with indentation + import sys, yaml + settings = yaml.safe_load(sys.stdin) + print ("\n".join(settings.get("steps", []))) + ' < tags/$TAG/settings.yaml \ + | while read step; do + if [ -z "$step" ]; then + break + fi + sep + info "Automatically executing step '$step'." + TRY=1 + MAXTRY=10 + while ! $0 $step $TAG ; do + TRY=$(($TRY+1)) + if [ $TRY -gt $MAXTRY ]; then + error "This step ($step) failed after $MAXTRY attempts." + info "You can troubleshoot the situation manually, or terminate these instances with:" + info "$0 stop $TAG" + die "Giving up." + else + sep + info "Step '$step' failed. Let's wait 10 seconds and try again." + info "(Attempt $TRY out of $MAXTRY.)" + sleep 10 + fi + done + done + sep + info "Deployment successful." info "To terminate these instances, you can run:" info "$0 stop $TAG" } diff --git a/prepare-vms/settings/fundamentals.yaml b/prepare-vms/settings/fundamentals.yaml index 1c4000ce..3dc70a27 100644 --- a/prepare-vms/settings/fundamentals.yaml +++ b/prepare-vms/settings/fundamentals.yaml @@ -21,3 +21,9 @@ machine_version: 0.15.0 # Password used to connect with the "docker user" docker_user_password: training + +steps: + - deploy + - webssh + - tailhist + - cards diff --git a/prepare-vms/settings/jerome.yaml b/prepare-vms/settings/jerome.yaml index 626751c5..57470504 100644 --- a/prepare-vms/settings/jerome.yaml +++ b/prepare-vms/settings/jerome.yaml @@ -20,3 +20,10 @@ machine_version: 0.14.0 # Password used to connect with the "docker user" docker_user_password: training +steps: + - deploy + - webssh + - tailhist + - kube + - cards + - kubetest