11#! /usr/bin/env bash
22set -euo pipefail
33
4+ # ###############################################################################
5+ # Root script to provision AWS EC2 instances for the workshop.
6+ #
7+ # The DB server is provisioned first, and then each team server is provisioned
8+ # in parallel by the neighbor script.
9+ # ###############################################################################
10+
11+ cd " $( dirname " $0 " ) "
12+
13+ # Make sure GNU Parallel is installed for later, but check early so we don't
14+ # waste anyone's time
15+ if ! command -v parallel > /dev/null ; then
16+ printf ' ERROR: GNU Parallel does not seem to be installed. You can install it via "[brew|apt|dnf|pacman|etc] install parallel"\n'
17+ exit 1
18+ fi
19+
20+ # Set all the variables
421outputs_file=' /tmp/outputs.json'
522
6- cd " $( dirname $0 ) "
7-
823printf ' >>> Getting Terraform outputs...\n'
24+ # TODO: use -chdir here but needs testing if you change the Makefile target as well
925(cd ../terraform && terraform output -json) > " ${outputs_file} "
1026
1127printf ' >>> Determining IP addresses of DB server...\n'
@@ -18,25 +34,22 @@ num_teams="$(jq '[.instance_ips.value[]] | length' ${outputs_file})"
1834team_server_ips=" $( jq -c ' [.instance_ips.value[]]' ${outputs_file} ) "
1935printf ' >>> %s teams, with IPs of: %s\n' " ${num_teams} " " ${team_server_ips} "
2036
37+ # Provision the DB server first, so that if it fails we know we're about to have
38+ # a bad time overall
2139printf ' >>> Adding DB server init script...\n'
2240scp -P 2332 -o StrictHostKeyChecking=accept-new -r ../scripts ../score-server admin@" ${db_pub_ip} " :/tmp
2341ssh -p 2332 admin@" ${db_pub_ip} " -- ' sudo cp -r /tmp/score-server /root/'
2442printf ' >>> Running DB server init script...\n'
2543ssh -p 2332 admin@" ${db_pub_ip} " ' sudo bash /tmp/scripts/init-db.sh'
2644
27- for server_num in $( seq 1 " ${num_teams} " ) ; do
28- server_index=$(( server_num - 1 ))
29- server_ip=$( echo " ${team_server_ips} " | jq -rc " .[${server_index} ]" )
30- printf ' >>> Team %s IP is %s\n' " ${server_num} " " ${server_ip} "
31-
32- printf ' >>> Adding files to Team server %s at %s...\n' " ${server_num} " " ${server_ip} "
33- scp -P 2332 -r -o StrictHostKeyChecking=accept-new ../scripts ../services ../instructions ../dummy-app-src admin@" ${server_ip} " :/tmp
34-
35- printf ' >>> Running init on Team server %s at %s...\n' " ${server_num} " " ${server_ip} "
36- ssh -p 2332 admin@" ${server_ip} " " export team_name=Team-${server_num} && export db_addr=${db_priv_ip} && sudo -E bash /tmp/scripts/init.sh"
37-
38- printf ' >>> Running tests on Team server %s at %s...\n' " ${server_num} " " ${server_ip} "
39- ssh -p 2332 admin@" ${server_ip} " " sudo -E bats /.ws/scripts/test.bats"
40-
41- printf ' >>> Done with Team server %s at %s\n' " ${server_num} " " ${server_ip} "
42- done
45+ # Export needed vars so the subscript can see them
46+ export db_priv_ip
47+ export team_server_ips
48+
49+ # Parallelize provisioning of the team servers
50+ parallel \
51+ -j0 \
52+ --lb \
53+ -- \
54+ bash ./provision-ec2-team-parallelizer.sh {} \
55+ :::: <( seq 1 " ${num_teams} " )
0 commit comments