dcabeadfaaed1004b32426c4c50d081d468fb52c
[apex-tripleo-heat-templates.git] / deployed-server / scripts / enable-ssh-admin.sh
1 #!/bin/bash
2
3 set -eu
4
5 # whitespace (space or newline) separated list
6 OVERCLOUD_HOSTS=${OVERCLOUD_HOSTS:-""}
7 OVERCLOUD_SSH_USER=${OVERCLOUD_SSH_USER:-"$USER"}
8 # this is just for compatibility with CI
9 SUBNODES_SSH_KEY=${SUBNODES_SSH_KEY:-"$HOME/.ssh/id_rsa"}
10 # this is the intended variable for overriding
11 OVERCLOUD_SSH_KEY=${OVERCLOUD_SSH_KEY:-"$SUBNODES_SSH_KEY"}
12
13 SLEEP_TIME=5
14
15 function overcloud_ssh_hosts_json {
16     echo "$OVERCLOUD_HOSTS" | python -c '
17 from __future__ import print_function
18 import json, re, sys
19 print(json.dumps(re.split("\s+", sys.stdin.read().strip())))'
20 }
21
22 function overcloud_ssh_key_json {
23     # we pass the contents to Mistral instead of just path, otherwise
24     # the key file would have to be readable for the mistral user
25     cat "$OVERCLOUD_SSH_KEY" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
26 }
27
28 function workflow_finished {
29     local execution_id="$1"
30     openstack workflow execution show -f shell $execution_id | grep 'state="SUCCESS"' > /dev/null
31 }
32
33 if [ -z "$OVERCLOUD_HOSTS" ]; then
34     echo 'Please set $OVERCLOUD_HOSTS'
35     exit 1
36 fi
37
38 echo "Starting workflow to create ssh admin on deployed servers."
39 echo "SSH user: $OVERCLOUD_SSH_USER"
40 echo "SSH key file: $OVERCLOUD_SSH_KEY"
41 echo "Hosts: $OVERCLOUD_HOSTS"
42 echo
43
44 EXECUTION_PARAMS="{\"ssh_user\": \"$OVERCLOUD_SSH_USER\", \"ssh_servers\": $(overcloud_ssh_hosts_json), \"ssh_private_key\": $(overcloud_ssh_key_json)}"
45 EXECUTION_CREATE_OUTPUT=$(openstack workflow execution create -f shell -d 'deployed server ssh admin creation' tripleo.access.v1.enable_ssh_admin "$EXECUTION_PARAMS")
46 echo "$EXECUTION_CREATE_OUTPUT"
47 EXECUTION_ID=$(echo "$EXECUTION_CREATE_OUTPUT" | grep '^id=' | awk '-F"' '{ print $2 }')
48
49 if [ -z "$EXECUTION_ID" ]; then
50     echo "Failed to get workflow execution ID for ssh admin creation workflow"
51     exit 1
52 fi
53
54 echo -n "Waiting for the workflow execution to finish (id $EXECUTION_ID)."
55 while ! workflow_finished $EXECUTION_ID; do
56     sleep $SLEEP_TIME
57     echo -n .
58 done
59
60 echo "Success."