Turn Off The Lights

After upgrading to macOS Sierra, and having Apex fail to work locally anymore, I decided it was time to build a dev instance, on EBS within EC2. I found a good idea from CloudSSH, but I really wanted to allow my instance to run docker commands while I was away. I decided to add the "--no-stop" flag to CloudSSH and just handle terminating the instances myself using a cronscript.


Here's the script:

#!/bin/sh
# Don't even bother checking if we haven't been up for more than
# 3500 seconds (just under 1 hour)
UPTIME=`cat /proc/uptime|awk '{printf "%i", $1}'`
if [ "$UPTIME" -gt "3500" ]
then
  if [ -z "$(who)" ]
  then
    if [ -z "$(docker ps -q)" ]
    then
      shutdown -P now
    fi
  fi

fi

And here's the /etc/cron.d/turn-off-the-lights file:

*/5 * * * * root /usr/local/bin/turn-off-the-lights


I also set up a nice little bash function to handle automatically configuring most of the common things I did for CloudSSH. I did this instead of an alias because I have multiple AWS profiles that I connect to:

# Connect to a "Cloud Environment"
typeset -A CLOUD_INSTANCE_ID
CLOUD_INSTANCE_ID+=(aci i-MY-DEV-INSTANCE)
CLOUD_INSTANCE_ID+=(newstex i-OTHER-DEV-INSTANCE)
cloud(){
  export AWS_REGION=us-east-1
  export AWS_PROFILE=$1
  $HOME/bin/cloudssh --no-stop ec2-user@${CLOUD_INSTANCE_ID[$1]}.us-east-1 -- -i ~/.ssh/$1-cmoyer.pem -o HostKeyAlias=${CLOUD_INSTANCE_ID[$1]}
}
cloud_profiles=(aci newstex)
compctl -k cloud_profiles cloud


Now I can just type "cloud aci" and automatically get logged into my ACI dev instance. Note that using "HostKeyAlias" allows me to not have to re-verify the host key every time the IP address changes.
0