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()
def run2(self): node = self.node instance = self.instance log.info("Setting up instance %s. Hostname: %s" % (instance.id, instance.public_dns_name), node) if self.launcher.config.has_snap(): log.debug("Creating Chef volume.", node) vol = self.launcher.conn.create_volume(1, instance.placement, self.launcher.config.get_snap()) log.debug("Created Chef volume %s. Attaching." % vol.id, node) vol.attach(instance.id, '/dev/sdh') log.debug("Chef volume attached. Waiting for it to become in-use.", node) self.launcher.wait_state(vol, "in-use") log.debug("Volume is in-use", node) self.launcher.vols.append(vol) self.check_continue() if self.launcher.loglevel in (0,1): ssh_out = None ssh_err = None elif self.launcher.loglevel == 2: ssh_out = sys.stdout ssh_err = sys.stderr log.debug("Establishing SSH connection", node) ssh = SSH("ubuntu", instance.public_dns_name, self.launcher.config.get_keyfile(), ssh_out, ssh_err) ssh.open() log.debug("SSH connection established", node) self.check_continue() if self.launcher.config.has_snap(): log.debug("Mounting Chef volume", node) ssh.run("sudo mount -t ext3 /dev/sdh /chef", expectnooutput=True) # Upload host file and update hostname log.debug("Uploading host file and updating hostname", node) ssh.scp("%s/hosts_ec2" % self.launcher.generated_dir, "/chef/cookbooks/demogrid/files/default/hosts") ssh.run("sudo cp /chef/cookbooks/demogrid/files/default/hosts /etc/hosts", expectnooutput=True) ssh.run("sudo bash -c \"echo %s > /etc/hostname\"" % node.hostname, expectnooutput=True) ssh.run("sudo /etc/init.d/hostname restart") self.check_continue() # Upload topology file log.debug("Uploading topology file", node) ssh.scp("%s/topology_ec2.rb" % self.launcher.generated_dir, "/chef/cookbooks/demogrid/attributes/topology.rb") # Copy certificates log.debug("Copying certificates", node) ssh.scp_dir("%s/certs" % self.launcher.generated_dir, "/chef/cookbooks/demogrid/files/default/") self.check_continue() if self.launcher.loglevel == 0: print " \033[1;37m%s\033[0m: Basic setup is done. Installing Grid software now." % node.hostname.split(".")[0] # Run chef log.debug("Running chef", node) ssh.scp("%s/lib/ec2/chef.conf" % self.launcher.demogrid_dir, "/tmp/chef.conf") ssh.run("echo '{ \"run_list\": \"role[%s]\" }' > /tmp/chef.json" % node.role, expectnooutput=True) ssh.run("sudo chef-solo -c /tmp/chef.conf -j /tmp/chef.json") self.check_continue() # The Chef recipes will overwrite the hostname, so # we need to set it again. ssh.run("sudo bash -c \"echo %s > /etc/hostname\"" % node.hostname, expectnooutput=True) ssh.run("sudo /etc/init.d/hostname restart") if self.launcher.config.has_snap(): ssh.run("sudo umount /chef", expectnooutput=True) vol.detach() self.launcher.wait_state(vol, "available") vol.delete() self.launcher.vols.remove(vol) ssh.run("sudo update-rc.d nis enable") log.info("Configuration done.", node) if self.launcher.loglevel == 0: print " \033[1;37m%s\033[0m is ready." % node.hostname.split(".")[0]
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"