|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Usage: ./provision-vagrant-controller.sh |
| 4 | +# |
| 5 | + |
| 6 | +function echo_color { |
| 7 | + echo -e "\033[1m$1\033[0m" |
| 8 | +} |
| 9 | + |
| 10 | +THIS_DIR="$(cd $(dirname $0); pwd)" # absolute path |
| 11 | +CONTRIB_DIR=$(dirname "$THIS_DIR") |
| 12 | + |
| 13 | +# check for Deis' general dependencies |
| 14 | +if ! "$CONTRIB_DIR/check-deis-deps.sh"; then |
| 15 | + echo 'Deis is missing some dependencies.' |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +# Make sure SSHD is installed |
| 20 | +# TODO: Better SSH server detection |
| 21 | +if [ ! -f /etc/ssh/sshd_config ] && [ ! -f /etc/sshd_config ]; then |
| 22 | + echo 'Please install an SSH server' |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +# Make sure avahi-daemon is installed and running |
| 27 | +if [[ `uname -s` =~ Linux ]]; then |
| 28 | + if ! pgrep avahi-daemon >/dev/null; then |
| 29 | + echo 'Please install avahi-daemon to broadcast your hostname to the local network.' |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | +fi |
| 33 | + |
| 34 | +################# |
| 35 | +# chef settings # |
| 36 | +################# |
| 37 | +node_name=deis-controller |
| 38 | +run_list="recipe[deis::controller]" |
| 39 | +chef_version=11.6.2 |
| 40 | + |
| 41 | +################ |
| 42 | +# SSH settings # |
| 43 | +################ |
| 44 | +ssh_key_path=~/.vagrant.d/insecure_private_key |
| 45 | +ssh_user="vagrant" |
| 46 | +ssh_port="22" |
| 47 | + |
| 48 | +# create data bags |
| 49 | +knife data bag create deis-formations 2>/dev/null |
| 50 | +knife data bag create deis-apps 2>/dev/null |
| 51 | + |
| 52 | +# Boot the deis-controller VM |
| 53 | +echo_color "Booting $node_name with 'vagrant up'" |
| 54 | +pushd "$THIS_DIR/../../" |
| 55 | +vagrant up --provision |
| 56 | +if [ $? -gt 0 ]; then |
| 57 | + echo_color "Canceling provision because 'vagrant up' failed" |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +# Add the Controller's public SSH key to user's machine. This allows the Controller to |
| 62 | +# issue vagrant commands on the host machine. |
| 63 | +read -p "Add the Deis Controller's SSH key to your authorized_keys file? " -n 1 -r |
| 64 | +echo |
| 65 | +if [[ $REPLY =~ ^[Yy]$ ]]; then #TODO: Might be nice to have flag to make manual confirmation optional? |
| 66 | + |
| 67 | + # Create an SSH key pair for the deis user |
| 68 | + vagrant ssh -c " |
| 69 | + if [ ! -f ~/.ssh/id_rsa ]; then |
| 70 | + ssh-keygen -t rsa -N \"\" -f ~/.ssh/id_rsa |
| 71 | + chmod a+r ~/.ssh/id_rsa # Not strictly best practice, but the deis user needs to be able to read it |
| 72 | + fi" |
| 73 | + |
| 74 | + # Copy the created key over to your local machine |
| 75 | + scp \ |
| 76 | + -P22 \ |
| 77 | + -o IdentityFile=$ssh_key_path \ |
| 78 | + 'vagrant@deis-controller.local:/home/vagrant/.ssh/id_rsa.pub' \ |
| 79 | + '/tmp/vagrant_key' |
| 80 | + KEY=$(cat /tmp/vagrant_key) |
| 81 | + |
| 82 | + if [ ! -n "$KEY" ]; then |
| 83 | + echo_color "Aborting. No SSH key copied from the Deis Controller" |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + |
| 87 | + if [ -z "$(grep "$KEY" ~/.ssh/authorized_keys )" ]; then |
| 88 | + echo $KEY >> ~/.ssh/authorized_keys; |
| 89 | + echo_color "Key added." |
| 90 | + else |
| 91 | + echo_color "Key already added." |
| 92 | + fi |
| 93 | + |
| 94 | +fi |
| 95 | + |
| 96 | +echo_color "Provisioning $node_name with knife vagrant..." |
| 97 | +set -x |
| 98 | +knife bootstrap "$node_name.local" \ |
| 99 | + --bootstrap-version $chef_version \ |
| 100 | + --ssh-user $ssh_user \ |
| 101 | + --ssh-port $ssh_port \ |
| 102 | + --identity-file $ssh_key_path \ |
| 103 | + --node-name $node_name \ |
| 104 | + --run-list $run_list \ |
| 105 | + --sudo |
| 106 | +set +x |
| 107 | + |
| 108 | +echo_color "Updating Django site object from 'example.com' to 'deis-controller'..." |
| 109 | +vagrant ssh -c "sudo su deis -c \"psql deis -c \\\" \ |
| 110 | + UPDATE django_site \ |
| 111 | + SET domain = 'deis-controller.local', \ |
| 112 | + name = 'deis-controller.local' \ |
| 113 | + WHERE id = 1 \\\"\"" >/dev/null |
| 114 | + |
| 115 | +if [ $? -eq 0 ]; then |
| 116 | + echo_color "Site object updated." |
| 117 | +fi |
| 118 | +popd |
| 119 | + |
| 120 | +echo_color "Setting devmode flag on 'deis-controller'..." |
| 121 | +knife exec -E 'nodes.transform("name:deis-controller") {|n| n.normal_attrs["deis"]["devmode"] = true; n.save }' |
| 122 | + |
| 123 | +# Need Chef admin permission in order to add and remove nodes and clients |
| 124 | +echo -e "\033[35mPlease ensure that \"deis-controller\" is added to the Chef \"admins\" group.\033[0m" |
0 commit comments