|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Usage: ./provision-digitalocean-controller.sh <region-id> |
| 4 | +# |
| 5 | +# Retrieve the region-id by using `knife digital_ocean region list` |
| 6 | +# |
| 7 | + |
| 8 | +function echo_color { |
| 9 | + echo -e "\033[1m$1\033[0m" |
| 10 | +} |
| 11 | + |
| 12 | +THIS_DIR=$(cd $(dirname $0); pwd) # absolute path |
| 13 | +CONTRIB_DIR=$(dirname $THIS_DIR) |
| 14 | + |
| 15 | +echo_color "Provisioning a deis controller on Digital Ocean!" |
| 16 | + |
| 17 | +# check for Deis' general dependencies |
| 18 | +if ! $CONTRIB_DIR/check-deis-deps.sh; then |
| 19 | + echo 'Deis is missing some dependencies.' |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# connection details for using digital ocean's API |
| 24 | +client_id=$DIGITALOCEAN_CLIENT_ID |
| 25 | +api_key=$DIGITALOCEAN_API_KEY |
| 26 | + |
| 27 | +# Check that client ID and API key was set |
| 28 | +if test -z $client_id; then |
| 29 | + echo "Please add your client id to ${knife_file}." |
| 30 | +fi |
| 31 | + |
| 32 | +if test -z $api_key; then |
| 33 | + echo "Please add your api key to ${knife_file}." |
| 34 | +fi |
| 35 | + |
| 36 | +################# |
| 37 | +# chef settings # |
| 38 | +################# |
| 39 | + |
| 40 | +node_name=deis-controller |
| 41 | +run_list="recipe[deis::controller]" |
| 42 | +chef_version=11.4.4 |
| 43 | + |
| 44 | +########################## |
| 45 | +# digital ocean settings # |
| 46 | +########################## |
| 47 | + |
| 48 | +# the name of the location we want to work with |
| 49 | +location_id=$1 |
| 50 | +# The snapshot that we want to use (deis-base) |
| 51 | +image_id=$(knife digital_ocean image list | grep "deis-base" | awk '{print $1}') |
| 52 | +# the ID of the size (1GB) |
| 53 | +size_id=$(knife digital_ocean size list | grep "2GB" | awk '{print $1}') |
| 54 | + |
| 55 | +################ |
| 56 | +# SSH settings # |
| 57 | +################ |
| 58 | + |
| 59 | +key_name="deis-controller" |
| 60 | +ssh_key_path=~/.ssh/$key_name |
| 61 | + |
| 62 | +# create ssh keypair and store it |
| 63 | +if ! test -e $ssh_key_path; then |
| 64 | + echo_color "Creating new SSH key: $key_name" |
| 65 | + set -x |
| 66 | + ssh-keygen -f $ssh_key_path -t rsa -N '' -C "deis-controller" >/dev/null |
| 67 | + curl -X GET \ |
| 68 | + --data-urlencode "name=$node_name" \ |
| 69 | + --data-urlencode "ssh_pub_key=$(cat $ssh_key_path.pub)" \ |
| 70 | + --data-urlencode "client_id=$client_id" \ |
| 71 | + --data-urlencode "api_key=$api_key" \ |
| 72 | + https://api.digitalocean.com/ssh_keys/new |
| 73 | + ssh-add $ssh_key_path |
| 74 | + set +x |
| 75 | + echo_color "Saved to $ssh_key_path" |
| 76 | +else |
| 77 | + echo_color "WARNING: SSH key $ssh_key_path exists, skipping upload" |
| 78 | +fi |
| 79 | + |
| 80 | +# get the id of the SSH key that we just uploaded |
| 81 | +ssh_key_id=$(knife digital_ocean sshkey list | grep "$key_name" | awk '{print $1}') |
| 82 | + |
| 83 | +# create data bags |
| 84 | +knife data bag create deis-users 2>/dev/null |
| 85 | +knife data bag create deis-formations 2>/dev/null |
| 86 | +knife data bag create deis-apps 2>/dev/null |
| 87 | + |
| 88 | +# trigger digital ocean instance bootstrap |
| 89 | +echo_color "Provisioning $node_name with knife digital_ocean..." |
| 90 | + |
| 91 | +set -x |
| 92 | +knife digital_ocean droplet create \ |
| 93 | + --server-name $node_name \ |
| 94 | + --image $image_id \ |
| 95 | + --location $location_id \ |
| 96 | + --size $size_id \ |
| 97 | + --ssh-keys $ssh_key_id \ |
| 98 | + --identity-file $ssh_key_path \ |
| 99 | + --bootstrap \ |
| 100 | + --run-list $run_list |
| 101 | +set +x |
0 commit comments