#!/bin/bash

# AWS Server Bootstrap File # This script is used to configure the AWS boxes for OpenStudio-server

ENTRY=“127.0.0.1 SERVER_HOSTNAME” FILE=/etc/hosts if grep -q “$ENTRY” $FILE; then

echo "Hosts '$SERVER_HOSTNAME' entry already exists"

else

sudo sh -c "echo $ENTRY >> /etc/hosts"

fi

WORKER_PRIVATE_KEY=“WORKER_PRIVATE_KEY_TEMPLATE” WORKER_PUBLIC_KEY=“WORKER_PUBLIC_KEY_TEMPLATE”

USER=ubuntu

mkdir -p /home/$USER/.ssh && chmod 700 /home/$USER/.ssh echo -e $WORKER_PRIVATE_KEY > /home/$USER/.ssh/id_rsa echo -e $WORKER_PUBLIC_KEY > /home/$USER/.ssh/id_rsa.pub chmod 600 /home/$USER/.ssh/id_rsa chmod 644 /home/$USER/.ssh/id_rsa.pub chown ubuntu.ubuntu /home/$USER/.ssh/id_rsa chown ubuntu.ubuntu /home/$USER/.ssh/id_rsa.pub

# Only allow localhost (and localhosts IP) to connect ot itself with this key. The IP is used for R cluster ENTRY=“from="localhost,127.0.0.1" $WORKER_PUBLIC_KEY” FILE=/home/$USER/.ssh/authorized_keys if grep -q “$ENTRY” $FILE; then

echo "Key already exists in file"

else

echo $ENTRY >> $FILE

fi chmod 644 /home/$USER/.ssh/authorized_keys

# SSH config echo -e “Host *ntStrictHostKeyChecking no” > /home/$USER/.ssh/config chmod 644 /home/$USER/.ssh/config && chown ubuntu.ubuntu /home/$USER/.ssh/config

# ec2 instance information curl -o /usr/local/bin/ec2-metadata s3.amazonaws.com/ec2metadata/ec2-metadata chmod 775 /usr/local/bin/ec2-metadata mkdir -p /etc/openstudio-server ec2-metadata -a -i -t -h -o -z -p -v > /etc/openstudio-server/instance.yml

# make sure supervisor is running sudo service supervisor start

# stop the various services that use mongo service delayed_job stop supervisorctl stop delayed_job service apache2 stop service mongodb stop service mongod stop

# make sure the the /mnt directory exists if i2 instances. # For now this assumes that the volume is xvdb. In the future this # should be dynamic if ec2-metadata –instance-type | grep -q 'i2.'; then

mkfs.ext4 /dev/xvdb
mkdir -p /mnt
mount -t ext4 /dev/xvdb /mnt

echo "/dev/xvdb /mnt auto noatime 0 0" | sudo tee -a /etc/fstab
mount -a

fi

# remove mongo db & add it back mkdir -p /mnt/mongodb/data chown mongodb:nogroup /mnt/mongodb/data rm -rf /var/lib/mongodb

# restart mongo - old images has mongodb as the service. New ones use mongod service mongodb start service mongod start

# delay the continuation because mongo is a forked process and when it initializes # it has to create the preallocated journal files (takes ~ 90 seconds on a slower system) # Wait until mongo logs that it's ready (or timeout after 120s) COUNTER=0 MONGOLOG=/var/log/mongo/mongod.log

# Clear out the log first cat /dev/null > $MONGOLOG

grep -q 'waiting for connections on port' $MONGOLOG while [[ $? -ne 0 && $COUNTER -lt 120 ]] ; do

sleep 2
let COUNTER+=2
echo "Waiting for mongo to initialize... ($COUNTER seconds so far)"
grep -q 'waiting for connections on port' $MONGOLOG

done

# Now we know mongo is ready and can continue with other commands echo “Mongo is ready. Moving on…”

# restart the rails application service apache2 stop service apache2 start

# Add in the database indexes after making the db directory chmod 777 /var/www/rails/openstudio/public su - ubuntu -c 'cd /var/www/rails/openstudio && bundle exec rake db:purge' su - ubuntu -c 'cd /var/www/rails/openstudio && bundle exec rake db:mongoid:create_indexes'

## Worker Data Configuration – On Vagrant this is a separate file

rm -f /tmp/snow.log

# Force the generation of various directories that are in the EBS mount rm -rf /mnt/openstudio mkdir -p /mnt/openstudio chown -R ubuntu:www-data /mnt/openstudio chmod -R 775 /mnt/openstudio

# save application files into the right directory rsync -a –chown ubuntu:www-data –exclude Gemfile.lock /data/worker-nodes/ /mnt/openstudio/

# install workflow dependencies # note: vagrant/ubuntu are now members of rbenv but it still doesn't work to not call sudo on bundle # Give full path to bundle because sudoers path is not available with cloud-init root cd /mnt/openstudio && /opt/rbenv/shims/bundle

# copy over the models needed for mongo cd /mnt/openstudio/rails-models && unzip -o rails-models.zip -d models

# rerun the permissions after unzipping the files chown -R ubuntu:www-data /mnt/openstudio find /mnt/openstudio -type d -print0 | xargs -0 chmod 775 find /mnt/openstudio -type f -print0 | xargs -0 chmod 664

## End Worker Data Configuration

# restart rserve service Rserve restart supervisorctl restart Rserve

# start delayed jobs service delayed_job start supervisorctl start delayed_job

file flag the user_data has completed cat /dev/null > /home/ubuntu/user_data_done