From 83c4ac47c2fc31eb06f81d191266ed476f5161aa Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 6 Dec 2018 03:11:20 -0500 Subject: factor out some common bits into a helper library --- bin/helpers/functions.sh | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ bin/helpers/launch-algo | 2 +- bin/helpers/launch-base | 56 ++++------------------------------ bin/helpers/launch-mail | 61 ++++--------------------------------- bin/helpers/launch-tozt | 58 ++++------------------------------- 5 files changed, 94 insertions(+), 161 deletions(-) create mode 100644 bin/helpers/functions.sh (limited to 'bin') diff --git a/bin/helpers/functions.sh b/bin/helpers/functions.sh new file mode 100644 index 0000000..a606a9b --- /dev/null +++ b/bin/helpers/functions.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +conf_location="/usr/local/share/puppet-tozt" +conf_repo="git://github.com/doy/puppet-tozt" + +create_droplet() { + _name="$1" + + if [ -n "${2:-}" ]; then + _volume_opt="--volumes $2" + else + _volume_opt="" + fi + + echo "Creating droplet for ${_name}..." + + # shellcheck disable=SC2086 + _data=$(doctl \ + -t "$(cat /mnt/digitalocean)" \ + compute droplet create \ + "$_name" \ + --image debian-9-x64 \ + --region nyc3 \ + --size s-1vcpu-1gb \ + --ssh-keys 23160354 \ + $_volume_opt \ + --format ID,PublicIPv4 \ + --no-header \ + --wait) + id=$(echo "$_data" | awk '{print $1}') + ip=$(echo "$_data" | awk '{print $2}') + + echo "Created droplet with id $id and ip $ip" +} + +remote() { + # shellcheck disable=SC2029 + ssh -oStrictHostKeyChecking=accept-new root@"$ip" "$@" +} + +ensure_conf_exists() { + if remote test -d "$conf_location"; then + remote "cd '$conf_location' && git pull" + else + remote "mkdir -p '$conf_location'" + remote "cd '$conf_location' && git clone '$conf_repo' ." + fi + remote "cd '$conf_location' && git submodule update --init --recursive" +} + +provision_droplet() { + _name="$1" + + echo "Provisioning droplet ${_name}..." + + while ! remote true; do + sleep 5 + done + + if remote test ! -e /usr/bin/pacman; then + remote apt-get -y update + remote apt-get -y install git + ensure_conf_exists + remote "cd '$conf_location/digitalocean-debian-to-arch' && bash install.sh --i_understand_that_this_droplet_will_be_completely_wiped --extra_packages 'puppet git ruby-shadow'" + sleep 30 + while ! remote true; do + sleep 30 + done + fi + + ensure_conf_exists + if [ -d "/mnt/puppet/${_name}" ]; then + scp -r "/mnt/puppet/${_name}/" root@"$ip":/usr/local/share/puppet-tozt/modules/secret/files + remote "cd '$conf_location' && puppet apply --modulepath=./modules manifests" + fi + + echo "Done provisioning" +} diff --git a/bin/helpers/launch-algo b/bin/helpers/launch-algo index 024c325..f926529 100755 --- a/bin/helpers/launch-algo +++ b/bin/helpers/launch-algo @@ -42,4 +42,4 @@ ansible-playbook main.yml -e " # XXX there should be a way to tell it to exit, but i don't know how to # calculate the correct controlpath sleep 60 -echo "done." +echo "Done" diff --git a/bin/helpers/launch-base b/bin/helpers/launch-base index c70bcac..7ca91cd 100755 --- a/bin/helpers/launch-base +++ b/bin/helpers/launch-base @@ -2,57 +2,11 @@ set -eu set -o pipefail -echo "Creating droplet for base..." -data=$(doctl \ - -t "$(cat /mnt/digitalocean)" \ - compute droplet create \ - base \ - --image debian-9-x64 \ - --region nyc3 \ - --size s-1vcpu-1gb \ - --ssh-keys 23160354 \ - --format ID,PublicIPv4 \ - --no-header \ - --wait) -id=$(echo "$data" | awk '{print $1}') -ip=$(echo "$data" | awk '{print $2}') -echo "Created droplet with id $id and ip $ip" +# shellcheck source=functions.sh +. "$(dirname "$0")/functions.sh" -echo "Provisioning droplet..." +create_droplet base -conf_location="/usr/local/share/puppet-tozt" -conf_repo="git://github.com/doy/puppet-tozt" +provision_droplet base -remote() { - # shellcheck disable=SC2029 - ssh -oStrictHostKeyChecking=accept-new root@"$ip" "$@" -} - -ensure_conf_exists() { - if remote test -d "$conf_location"; then - remote "cd '$conf_location' && git pull" - else - remote "mkdir -p '$conf_location'" - remote "cd '$conf_location' && git clone '$conf_repo' ." - fi - remote "cd '$conf_location' && git submodule update --init --recursive" -} - -while ! remote true; do - sleep 5 -done - -if remote test ! -e /usr/bin/pacman; then - remote apt-get -y update - remote apt-get -y install git - ensure_conf_exists - remote "cd '$conf_location/digitalocean-debian-to-arch' && bash install.sh --i_understand_that_this_droplet_will_be_completely_wiped --extra_packages 'puppet git ruby-shadow'" - sleep 30 - while ! remote true; do - sleep 30 - done -fi - -ensure_conf_exists - -echo "Done provisioning" +echo "Done" diff --git a/bin/helpers/launch-mail b/bin/helpers/launch-mail index 1da13b8..634c86a 100755 --- a/bin/helpers/launch-mail +++ b/bin/helpers/launch-mail @@ -2,22 +2,10 @@ set -eu set -o pipefail -echo "Creating droplet for mail..." -data=$(doctl \ - -t "$(cat /mnt/digitalocean)" \ - compute droplet create \ - mail \ - --image debian-9-x64 \ - --region nyc3 \ - --size s-1vcpu-1gb \ - --ssh-keys 23160354 \ - --volumes 5e4d8c7b-f840-11e8-b59e-0a58ac1467fb \ - --format ID,PublicIPv4 \ - --no-header \ - --wait) -id=$(echo "$data" | awk '{print $1}') -ip=$(echo "$data" | awk '{print $2}') -echo "Created droplet with id $id and ip $ip" +# shellcheck source=functions.sh +. "$(dirname "$0")/functions.sh" + +create_droplet mail 5e4d8c7b-f840-11e8-b59e-0a58ac1467fb echo "Creating DNS entry for $ip..." record_id=$(doctl \ @@ -40,46 +28,7 @@ doctl \ --record-ttl 600 echo "Done creating DNS entry" -echo "Provisioning droplet..." - -conf_location="/usr/local/share/puppet-tozt" -conf_repo="git://github.com/doy/puppet-tozt" - -remote() { - # shellcheck disable=SC2029 - ssh root@"$ip" "$@" -} - -ensure_conf_exists() { - if remote test -d "$conf_location"; then - remote "cd '$conf_location' && git pull" - else - remote "mkdir -p '$conf_location'" - remote "cd '$conf_location' && git clone '$conf_repo' ." - fi - remote "cd '$conf_location' && git submodule update --init --recursive" -} - -while ! remote true; do - sleep 5 -done - -if remote test ! -e /usr/bin/pacman; then - remote apt-get -y update - remote apt-get -y install git - ensure_conf_exists - remote "cd '$conf_location/digitalocean-debian-to-arch' && bash install.sh --i_understand_that_this_droplet_will_be_completely_wiped --extra_packages 'puppet git ruby-shadow'" - sleep 30 - while ! remote true; do - sleep 30 - done -fi - -ensure_conf_exists -scp -r /mnt/puppet/tozt/ root@"$ip":/usr/local/share/puppet-tozt/modules/secret/files -remote "cd '$conf_location' && puppet apply --modulepath=./modules manifests" - -echo "Done provisioning" +provision_droplet mail echo "Creating DKIM entry" dkim=$(remote "perl -pe'chomp; s/.*\"(.*)\".*/\$1/' /media/persistent/dkim/new.tozt.net.dkim.pub") diff --git a/bin/helpers/launch-tozt b/bin/helpers/launch-tozt index 0d93aac..006cc1d 100755 --- a/bin/helpers/launch-tozt +++ b/bin/helpers/launch-tozt @@ -2,20 +2,10 @@ set -eu set -o pipefail -echo "Creating droplet for tozt..." -id=$(doctl \ - -t "$(cat /mnt/digitalocean)" \ - compute droplet create \ - tozt \ - --image debian-9-x64 \ - --region nyc3 \ - --size s-1vcpu-1gb \ - --ssh-keys 23160354 \ - --volumes 4206344e-cf4f-11e8-a5a7-0a58ac1465db \ - --format ID \ - --no-header \ - --wait) -echo "Created droplet with id $id" +# shellcheck source=functions.sh +. "$(dirname "$0")/functions.sh" + +create_droplet tozt 4206344e-cf4f-11e8-a5a7-0a58ac1465db echo "Assigning floating ip to tozt..." # XXX this returns an error for some reason, but actually succeeds: @@ -28,44 +18,6 @@ doctl \ "$id" || true echo "Done assigning floating ip" -echo "Provisioning droplet..." - -host="${1:-tozt.net}" -conf_location="/usr/local/share/puppet-tozt" -conf_repo="git://github.com/doy/puppet-tozt" - -remote() { - # shellcheck disable=SC2029 - ssh root@"$host" "$@" -} - -ensure_conf_exists() { - if remote test -d "$conf_location"; then - remote "cd '$conf_location' && git pull" - else - remote "mkdir -p '$conf_location'" - remote "cd '$conf_location' && git clone '$conf_repo' ." - fi - remote "cd '$conf_location' && git submodule update --init --recursive" -} - -while ! remote true; do - sleep 5 -done - -if remote test ! -e /usr/bin/pacman; then - remote apt-get -y update - remote apt-get -y install git - ensure_conf_exists - remote "cd '$conf_location/digitalocean-debian-to-arch' && bash install.sh --i_understand_that_this_droplet_will_be_completely_wiped --extra_packages 'puppet git ruby-shadow'" - sleep 30 - while ! remote true; do - sleep 30 - done -fi - -ensure_conf_exists -scp -r /mnt/puppet/tozt/ root@"$host":/usr/local/share/puppet-tozt/modules/secret/files -remote "cd '$conf_location' && puppet apply --modulepath=./modules manifests" +provision_droplet tozt echo "Done" -- cgit v1.2.3-54-g00ecf