"""utility methods for AWS"""

import boto.ec2
import os

aws_region = 'us-west-2'
aws_image_id = 'ami-e7527ed7'
aws_instance_type = 't2.micro'
aws_user_data = """#!/bin/bash
yum install -y docker
service docker start
pip install docker-compose
yum install -y git
git clone https://github.com/csdl/makahiki
cd makahiki
/usr/local/bin/docker-compose build
/usr/local/bin/docker-compose pull db
/usr/local/bin/docker-compose start db
sleep 2
/usr/local/bin/docker-compose run web python makahiki/scripts/initialize_instance.py -t default -d
/usr/local/bin/docker-compose up -d
"""

aws_access_key_id = os.environ['MAKAHIKI_AWS_ACCESS_KEY_ID']
aws_secret_access_key = os.environ['MAKAHIKI_AWS_SECRET_ACCESS_KEY']
aws_subnet_id = os.environ['MAKAHIKI_AWS_SUBNET_ID']
aws_security_group_id = os.environ['MAKAHIKI_AWS_SECURITY_GROUP_ID']
aws_key_name = os.environ['MAKAHIKI_AWS_KEY_NAME']

conn = boto.ec2.connect_to_region(aws_region,
        aws_access_key_id=aws_access_key_id,
        aws_secret_access_key=aws_secret_access_key)


def launch_instance():
    interface = boto.ec2.networkinterface.NetworkInterfaceSpecification(
            subnet_id=aws_subnet_id,
            groups=[aws_security_group_id,],
            associate_public_ip_address=True)

    interfaces = boto.ec2.networkinterface.NetworkInterfaceCollection(interface)


    reservation = conn.run_instances(aws_image_id,
                       key_name=aws_key_name,
                       instance_type=aws_instance_type,
                       network_interfaces=interfaces,
                       user_data=aws_user_data)
    print reservation.instances;

def stop_instances(instance_id):
    conn.stop_instances(instance_ids=[instance_id,])

def status():
    statuses = conn.get_all_instance_status(include_all_instances=True)

    print statuses
    for status in statuses:
        print "%s - %s - %s - %s" % (status.id, status.state_name, status.events, status.system_status)

@@ -47,18 +47,58 @@
 %0Aimport 
-os
+boto%0Aimport os%0Afrom boto.s3.key import Key
 %0A%0Aaws_re
@@ -177,19 +177,25 @@
 .micro'%0A
-aws
+%0Amakahiki
 _user_da
@@ -923,19 +923,29 @@
 NAME'%5D%0A%0A
-con
+EC2_connectio
 n = boto
@@ -1078,16 +1078,1193 @@
 s_key)%0A%0A
+S3_connection = boto.connect_s3(aws_access_key_id, aws_secret_access_key)%0A%0A%0Adef create_bucket(bucket):%0A    S3_connection.create_bucket(bucket)%0A%0Adef delete_bucket(bucket):%0A    b = S3_connection.get_bucket(bucket)%0A    for key in b.list():%0A        print 'deleting %25s' %25 key%0A        key.delete()%0A%0A    # The bucket is empty now. Delete it.%0A    S3_connection.delete_bucket(bucket)%0A%0Adef get_key(bucket, key):%0A    b = S3_connection.get_bucket(bucket)%0A    k = Key(b)%0A    k.key = key%0A    return k%0A%0Adef list_all():%0A    rs = S3_connection.get_all_buckets()%0A    for b in rs:%0A        print b.name%0A%0Adef download(bucket):%0A    b = S3_connection.get_bucket(bucket)%0A    rs = b.list()%0A    for key in rs:%0A        k.get_contents_to_filename(key.name)%0A%0Adef put_string(bucket, key, string_value):%0A    k = get_key(bucket, key)%0A    k.set_contents_from_string(string_value)%0A%0Adef get_string(bucket, key):%0A    k = get_key(bucket, key)%0A    return k.get_contents_as_string()%0A%0Adef put_file(bucket, key, in_filename):%0A    k = get_key(bucket, key)%0A    k.set_contents_from_filename(in_filename)%0A%0Adef get_file(bucket, key, out_filename):%0A    k = get_key(bucket, key)%0A    k.get_contents_to_filename(out_filename)%0A%0A
 %0Adef lau
@@ -2276,16 +2276,29 @@
 nstance(
+makahiki=None
 ):%0A    i
@@ -2577,16 +2577,108 @@
 face)%0A%0A%0A
+    if makahiki:%0A        user_data = makahiki_user_data%0A    else:%0A        user_data = None%0A%0A
     rese
@@ -2687,19 +2687,29 @@
 ation = 
-con
+EC2_connectio
 n.run_in
@@ -2919,20 +2919,16 @@
 er_data=
-aws_
 user_dat
@@ -3001,19 +3001,29 @@
 d):%0A    
-con
+EC2_connectio
 n.stop_i
@@ -3090,19 +3090,29 @@
 tuses = 
-con
+EC2_connectio
 n.get_al

add s3 support in aws_utils