Skip to content

Instantly share code, notes, and snippets.

@askedrelic
Created May 21, 2010 20:48
Show Gist options
  • Save askedrelic/409398 to your computer and use it in GitHub Desktop.
Save askedrelic/409398 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Automated rollout script for Amazon EC2 + MM
# 2010/05/20 15:20:35
# Matt Behrens <askedrelic@gmail.com>
from boto.ec2.connection import EC2Connection
import paramiko
import time
def main():
pass
class Automatron(object):
def __init__(self):
self.xmlrpc_port = '20500'
self.aws_key = 'aws_key'
self.aws_secret = 'aws_secret'
self.conn = EC2Connection(self.aws_key, self.aws_secret)
self.security_name = "autotest"
self.reservation = None
self.ips = None
self.clients = []
def setup_amazon(self):
#only needs to happen once
#creae the group, authorize ssh access and xmlrpc access
security_group = self.conn.get_all_security_groups(groupnames=[self.security_name])[0]
#use your ip for more protection
security_group.authorize(ip_protocol='tcp', from_port='22', to_port='22', cidr_ip='0.0.0.0/0')
security_group.authorize(ip_protocol='tcp', from_port=xmlrpc_port, to_port=xmlrpc_port, cidr_ip='0.0.0.0/0')
def spin_up(self, num_of_instances=1):
#specific chosen ubuntu distro
ami_id = "ami-2d4aa444"
#get specific security group for the instances (assuming group already created)
security_group = self.conn.get_all_security_groups(groupnames=[self.security_name])[0]
ssh_key_name = 'mattkey'
security_groups = [security_group]
instance_size = 'm1.small'
ami = self.conn.get_all_images([ami_id])[0]
reservation = ami.run(min_count=num_of_instances,
max_count=num_of_instances,
key_name=ssh_key_name,
security_groups=security_groups,
instance_type=instance_size)
while True:
#check if instances are up
if len([i.state for i in reservation.instances if i.update() == 'running']) >= num_of_instances:
self.reservation = reservation
self.ips = [i.public_dns_name for i in reservation.instances]
return True
time.sleep(10)
return False
def make_ssh_connections(self):
if self.ips:
for ip in self.ips:
client = paramiko.SSHClient()
#ignore that these are unknown hosts
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username='ubuntu', key_filename='publickey.pem')
self.clients.append(client)
print "created ssh client for %s" % ip
else:
return "No ips to connect to"
def run_command(self, command):
if self.clients:
for client in self.clients:
client.exec_command(command)
return True
else:
return "No ssh clients"
def run_command_with_output(self, command):
if self.clients:
for client in self.clients:
output = []
output.append(client.exec_command(command)[1].read())
return output
else:
return "No ssh clients"
def build_out(self):
#install git
self.run_command('sudo apt-get -y install git-core python-mechanize python-matplotlib')
#clone repo
#run project build script
pass
def launch_xmlrpc_server(self):
self.run_command('python multi-mechanize.py default_project -p %s &' % self.xmlrpc_port)
def kill_all_servers(self):
if self.reservation:
self.reservation.stop_all()
else:
return "No reservation"
if __name__ == main:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment