#!/usr/bin/env python

import boto.ec2
import time

ec2 = boto.ec2.connect_to_region('us-west-2')

user_data_script ="""
#!/bin/sh

yum install -y python-pip git zsh
pip install --upgrade boto

# setup an script to install the awesomeness
cat <<'EOT' > ~ec2-user/.zinstall
zsh -c "$(curl -fsSL https://raw.githubusercontent.com/cplee/prezto/master/install.zsh)"
rm -f ~ec2-user/.zinstall
zsh
EOT

# run install from .bash_profile
cat <<'EOT' >> ~ec2-user/.bash_profile
[[ -f "$HOME/.zinstall" ]] && source "$HOME/.zinstall"
EOT
"""

res = ec2.run_instances('ami-d93622b8', 
                  key_name='casey-labs-uswest2',
                  security_groups=['casey-ssh'],
                  instance_profile_name='casey-labs-ec2',
                  user_data=user_data_script,
                  instance_type='t1.micro')

instance = res.instances[0]
while instance.ip_address is None:
    time.sleep(1)
    instance.update()


instance_id = instance.id
ip_address = instance.ip_address
ec2.create_tags([instance_id], {"Name": "casey-dev"})

print "%s %s" % (instance_id, ip_address)

