def launch(self): SIGINTWatcher(self.cleanup_after_kill) t_start = time.time() ami = self.config.get_ami() keypair = self.config.get_keypair() insttypes = self.config.get_instance_type() zone = self.config.get_ec2_zone() # Parse the instance type option role_insttype = {} for ri in insttypes.split(): role, insttype = ri.split(":") role_insttype[role] = insttype default_insttype = role_insttype["*"] log.init_logging(self.loglevel) try: log.debug("Connecting to EC2...") self.conn = create_ec2_connection() if self.conn == None: print "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are not set." exit(1) log.debug("Connected to EC2.") except BotoClientError, exc: print "\033[1;31mERROR\033[0m - Could not connect to EC2." print " Reason: %s" % exc.reason exit(1)
def run(self): conn = create_ec2_connection() print "Creating instance" reservation = conn.run_instances(self.ami, min_count=1, max_count=1, instance_type='t1.micro', key_name=self.keypair) instance = reservation.instances[0] print "Instance %s created. Waiting for it to start..." % instance.id while instance.update() != "running": time.sleep(2) print "Instance running. Creating volume." vol = conn.create_volume(1, instance.placement) vol.attach(instance.id, '/dev/sdh') while vol.update() != "in-use": time.sleep(2) print "Volume created." ssh = SSH("ubuntu", instance.public_dns_name, self.keyfile) ssh.open() print "Preparing volume." ssh.scp("%s/lib/scripts/prepare_chef_volume.sh" % self.demogrid_dir, "/tmp/prepare_chef_volume.sh") ssh.run("chmod u+x /tmp/prepare_chef_volume.sh") ssh.run("sudo /tmp/prepare_chef_volume.sh") print "Copying Chef files." ssh.scp_dir("%s/chef" % self.demogrid_dir, "/chef") ssh.run("sudo umount /chef") print "Detaching volume" vol.detach() while vol.update() != "available": time.sleep(1) print "Creating snapshot" snap = vol.create_snapshot("DemoGrid Chef partition 0.2") snap.share(groups=['all']) vol.delete() print "The snapshot ID is %s" % snap.id print "Terminating instance" conn.terminate_instances([instance.id]) while instance.update() != "terminated": time.sleep(2) print "Instance terminated"
def run(self): log.init_logging(2) conn = create_ec2_connection() print "Creating instance" reservation = conn.run_instances(self.base_ami, min_count=1, max_count=1, instance_type='c1.medium', key_name=self.keypair) instance = reservation.instances[0] print "Instance %s created. Waiting for it to start..." % instance.id while instance.update() != "running": time.sleep(2) print "Instance running." ssh = SSH("ubuntu", instance.public_dns_name, self.keyfile) ssh.open() if self.snapshot != None: print "Creating volume + attaching." vol = conn.create_volume(1, instance.placement, self.snapshot) vol.attach(instance.id, '/dev/sdh') while vol.update() != "in-use": time.sleep(2) print "Volume created." print "Mounting volume." ssh.run("sudo mkdir /chef") ssh.run("sudo mount -t ext3 /dev/sdh /chef") ssh.run("sudo chown -R ubuntu /chef") else: print "Copying Chef files" ssh.run("sudo mkdir /chef") ssh.run("sudo chown -R ubuntu /chef") ssh.scp_dir("%s/chef" % self.demogrid_dir, "/chef") ssh.run("sudo apt-add-repository 'deb http://apt.opscode.com/ lucid main'") ssh.run("wget -qO - http://apt.opscode.com/[email protected] | sudo apt-key add -") ssh.run("sudo apt-get update") ssh.run("echo 'chef chef/chef_server_url string http://127.0.0.1:4000' | sudo debconf-set-selections") ssh.run("sudo apt-get -q=2 install chef") ssh.scp("%s/lib/ec2/chef.conf" % self.demogrid_dir, "/tmp/chef.conf") ssh.run("echo '{ \"run_list\": \"recipe[demogrid::ec2]\" }' > /tmp/chef.json") ssh.run("sudo chef-solo -c /tmp/chef.conf -j /tmp/chef.json") ssh.run("sudo update-rc.d nis disable") ssh.run("sudo update-rc.d chef-client disable") if self.snapshot != None: ssh.run("sudo umount /chef") print "Detaching volume" vol.detach() while vol.update() != "available": time.sleep(1) # Apparently instance.stop() will terminate # the instance (this is a known bug), so we # use stop_instances instead. print "Stopping instance" conn.stop_instances([instance.id]) while instance.update() != "stopped": time.sleep(2) print "Instance stopped" print "Creating AMI" # Doesn't actually return AMI. Have to make it public manually. ami = conn.create_image(instance.id, self.ami_name, description=self.ami_name) print "Cleaning up" print "Terminating instance" conn.terminate_instances([instance.id]) while instance.update() != "terminated": time.sleep(2) print "Instance terminated" if self.snapshot != None: vol.delete()