<commit_before>#!/usr/bin/env bash
#
# Usage: ./provision-vagrant-controller.sh
#

function echo_color {
  echo -e "\033[1m$1\033[0m"
}

THIS_DIR=$(cd $(dirname $0); pwd) # absolute path
CONTRIB_DIR=$(dirname $THIS_DIR)

# check for Deis' general dependencies
if ! $CONTRIB_DIR/check-deis-deps.sh; then
  echo 'Deis is missing some dependencies.'
  exit 1
fi

# Make sure SSHD is installed
# TODO: Better SSH server detection
if [ ! -f /etc/ssh/sshd_config ] && [ ! -f /etc/sshd_config ]; then
  echo 'Please install an SSH server'
  exit 1
fi

# Make sure avahi-daemon is installed and running
if [[ `uname -s` =~ Linux ]]; then
  if ! pgrep avahi-daemon >/dev/null; then
    echo 'Please install avahi-daemon to broadcast your hostname to the local network.'
    exit 1
  fi
fi

#################
# chef settings #
#################
node_name=deis-controller
run_list="recipe[deis::controller]"
chef_version=11.6.2

################
# SSH settings #
################
ssh_key_path=~/.vagrant.d/insecure_private_key
ssh_user="vagrant"
ssh_port="22"

# create data bags
knife data bag create deis-users 2>/dev/null
knife data bag create deis-formations 2>/dev/null
knife data bag create deis-apps 2>/dev/null

# Boot the deis-controller VM
echo_color "Booting $node_name with 'vagrant up'"
pushd $THIS_DIR/../../
vagrant up --provision
if [ $? -gt 0 ]; then
  echo_color "Canceling provision because 'vagrant up' failed"
  exit 1
fi

# Add the Controller's public SSH key to user's machine. This allows the Controller to
# issue vagrant commands on the host machine.
read -p "Add the Deis Controller's SSH key to your authorized_keys file? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then #TODO: Might be nice to have flag to make manual confirmation optional?

  # Create an SSH key pair for the deis user
  vagrant ssh -c "
  if [ ! -f ~/.ssh/id_rsa ]; then
    ssh-keygen -t rsa -N \"\" -f ~/.ssh/id_rsa
    chmod a+r ~/.ssh/id_rsa # Not strictly best practice, but the deis user needs to be able to read it
  fi"

  # Copy the created key over to your local machine
  scp \
    -P22 \
    -o IdentityFile=$ssh_key_path \
    '<EMAIL>:/home/vagrant/.ssh/id_rsa.pub' \
    '/tmp/vagrant_key'
  KEY=$(cat /tmp/vagrant_key)

  if [ ! -n "$KEY" ]; then
    echo_color "Aborting. No SSH key copied from the Deis Controller"
    exit 1
  fi

  if [ -z "$(grep "$KEY" ~/.ssh/authorized_keys )" ]; then
    echo $KEY >> ~/.ssh/authorized_keys;
    echo_color "Key added."
  else
    echo_color "Key already added."
  fi

fi

echo_color "Provisioning $node_name with knife vagrant..."
set -x
knife bootstrap "$node_name.local" \
  --bootstrap-version $chef_version \
  --ssh-user $ssh_user \
  --ssh-port $ssh_port \
  --identity-file $ssh_key_path \
  --node-name $node_name \
  --run-list $run_list \
  --sudo
set +x

echo_color "Updating Django site object from 'example.com' to 'deis-controller'..."
vagrant ssh -c "sudo su deis -c \"psql deis -c \\\" \
  UPDATE django_site \
  SET domain = 'deis-controller.local', \
      name = 'deis-controller.local' \
  WHERE id = 1 \\\"\"" >/dev/null

if [ $? -eq 0 ]; then
  echo_color "Site object updated."
fi
popd

echo_color "Setting devmode flag on 'deis-controller'..."
knife exec -E 'nodes.transform("name:deis-controller") {|n| n.normal_attrs["deis"]["devmode"] = true; n.save }

# Need Chef admin permission in order to add and remove nodes and clients
echo -e "\033[35mPlease ensure that \"deis-controller\" is added to the Chef \"admins\" group.\033[0m"<commit_msg>Fixed typo in vagrant provision-controller.sh<commit_after>#!/usr/bin/env bash
#
# Usage: ./provision-vagrant-controller.sh
#

function echo_color {
  echo -e "\033[1m$1\033[0m"
}

THIS_DIR=$(cd $(dirname $0); pwd) # absolute path
CONTRIB_DIR=$(dirname $THIS_DIR)

# check for Deis' general dependencies
if ! $CONTRIB_DIR/check-deis-deps.sh; then
  echo 'Deis is missing some dependencies.'
  exit 1
fi

# Make sure SSHD is installed
# TODO: Better SSH server detection
if [ ! -f /etc/ssh/sshd_config ] && [ ! -f /etc/sshd_config ]; then
  echo 'Please install an SSH server'
  exit 1
fi

# Make sure avahi-daemon is installed and running
if [[ `uname -s` =~ Linux ]]; then
  if ! pgrep avahi-daemon >/dev/null; then
    echo 'Please install avahi-daemon to broadcast your hostname to the local network.'
    exit 1
  fi
fi

#################
# chef settings #
#################
node_name=deis-controller
run_list="recipe[deis::controller]"
chef_version=11.6.2

################
# SSH settings #
################
ssh_key_path=~/.vagrant.d/insecure_private_key
ssh_user="vagrant"
ssh_port="22"

# create data bags
knife data bag create deis-users 2>/dev/null
knife data bag create deis-formations 2>/dev/null
knife data bag create deis-apps 2>/dev/null

# Boot the deis-controller VM
echo_color "Booting $node_name with 'vagrant up'"
pushd $THIS_DIR/../../
vagrant up --provision
if [ $? -gt 0 ]; then
  echo_color "Canceling provision because 'vagrant up' failed"
  exit 1
fi

# Add the Controller's public SSH key to user's machine. This allows the Controller to
# issue vagrant commands on the host machine.
read -p "Add the Deis Controller's SSH key to your authorized_keys file? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then #TODO: Might be nice to have flag to make manual confirmation optional?

  # Create an SSH key pair for the deis user
  vagrant ssh -c "
  if [ ! -f ~/.ssh/id_rsa ]; then
    ssh-keygen -t rsa -N \"\" -f ~/.ssh/id_rsa
    chmod a+r ~/.ssh/id_rsa # Not strictly best practice, but the deis user needs to be able to read it
  fi"

  # Copy the created key over to your local machine
  scp \
    -P22 \
    -o IdentityFile=$ssh_key_path \
    '<EMAIL>:/home/vagrant/.ssh/id_rsa.pub' \
    '/tmp/vagrant_key'
  KEY=$(cat /tmp/vagrant_key)

  if [ ! -n "$KEY" ]; then
    echo_color "Aborting. No SSH key copied from the Deis Controller"
    exit 1
  fi

  if [ -z "$(grep "$KEY" ~/.ssh/authorized_keys )" ]; then
    echo $KEY >> ~/.ssh/authorized_keys;
    echo_color "Key added."
  else
    echo_color "Key already added."
  fi

fi

echo_color "Provisioning $node_name with knife vagrant..."
set -x
knife bootstrap "$node_name.local" \
  --bootstrap-version $chef_version \
  --ssh-user $ssh_user \
  --ssh-port $ssh_port \
  --identity-file $ssh_key_path \
  --node-name $node_name \
  --run-list $run_list \
  --sudo
set +x

echo_color "Updating Django site object from 'example.com' to 'deis-controller'..."
vagrant ssh -c "sudo su deis -c \"psql deis -c \\\" \
  UPDATE django_site \
  SET domain = 'deis-controller.local', \
      name = 'deis-controller.local' \
  WHERE id = 1 \\\"\"" >/dev/null

if [ $? -eq 0 ]; then
  echo_color "Site object updated."
fi
popd

echo_color "Setting devmode flag on 'deis-controller'..."
knife exec -E 'nodes.transform("name:deis-controller") {|n| n.normal_attrs["deis"]["devmode"] = true; n.save }'

# Need Chef admin permission in order to add and remove nodes and clients
echo -e "\033[35mPlease ensure that \"deis-controller\" is added to the Chef \"admins\" group.\033[0m"
{'fraction_non_alphanumeric': 0.14605468196628418, 'fraction_numerical': 0.011049723756906077, 'mean_word_length': 3.328632740649908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2964071', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2465, 'n_words': 813}