def create_from_saved_state(cls, config, saved_state, timeout=600): parsed_config = EC2Configuration.create(config) state = EC2State.create(saved_state) connection = _connect_to_ec2( region=state.region, credentials=parsed_config.credentials ) instance = connection.start_instances( instance_ids=state.instance_id)[0] instance.update() while instance.state != "running" and timeout > 1: log_yellow("Instance state: %s" % instance.state) sleep(10) timeout = timeout - 10 instance.update() # and make sure we don't return until the instance is fully up wait_for_ssh(instance.ip_address) return cls( connection=connection, instance=instance, config=parsed_config, state=state )
def create_server_rackspace(connection, distribution, disk_name, disk_size, ami, region, key_pair, instance_type, instance_name, tags={}, security_groups=None): """ Creates Rackspace Instance and saves it state in a local json file """ log_yellow("Creating Rackspace instance...") flavor = connection.flavors.find(name=instance_type) image = connection.images.find(name=ami) server = connection.servers.create(name=instance_name, flavor=flavor.id, image=image.id, region=region, availability_zone=region, key_name=key_pair) while server.status == 'BUILD': log_yellow("Waiting for build to finish...") sleep(5) server = connection.servers.get(server.id) # check for errors if server.status != 'ACTIVE': log_red("Error creating rackspace instance") exit(1) # the server was assigned IPv4 and IPv6 addresses, locate the IPv4 address ip_address = server.accessIPv4 if ip_address is None: log_red('No IP address assigned') exit(1) wait_for_ssh(ip_address) log_green('New server with IP address {0}.'.format(ip_address)) return server
def create_from_saved_state(cls, config, saved_state, timeout=600): parsed_config = EC2Configuration.create(config) state = EC2State.create(saved_state) connection = _connect_to_ec2(region=state.region, credentials=parsed_config.credentials) instance = connection.start_instances( instance_ids=state.instance_id)[0] instance.update() while instance.state != "running" and timeout > 1: log_yellow("Instance state: %s" % instance.state) sleep(10) timeout = timeout - 10 instance.update() # and make sure we don't return until the instance is fully up wait_for_ssh(instance.ip_address) return cls(connection=connection, instance=instance, config=parsed_config, state=state)
def up_ec2(connection, region, instance_id, wait_for_ssh_available=True, log=False, timeout=600): """ boots an existing ec2_instance """ # boot the ec2 instance instance = connection.start_instances(instance_ids=instance_id)[0] instance.update() while instance.state != "running" and timeout > 1: log_yellow("Instance state: %s" % instance.state) if log: log_yellow("Instance state: %s" % instance.state) sleep(10) timeout = timeout - 10 instance.update() # and make sure we don't return until the instance is fully up if wait_for_ssh_available: wait_for_ssh(instance.ip_address)
def _create_server_ec2(connection, region, disk_name, disk_size, ami, key_pair, instance_type, tags={}, security_groups=None, delete_on_termination=True, log=False, wait_for_ssh_available=True): """ Creates EC2 Instance """ if log: log_green("Started...") log_yellow("...Creating EC2 instance...") ebs_volume = EBSBlockDeviceType() ebs_volume.size = disk_size bdm = BlockDeviceMapping() bdm[disk_name] = ebs_volume # get an ec2 ami image object with our choosen ami image = connection.get_all_images(ami)[0] # start a new instance reservation = image.run(1, 1, key_name=key_pair, security_groups=security_groups, block_device_map=bdm, instance_type=instance_type) # and get our instance_id instance = reservation.instances[0] # and loop and wait until ssh is available while instance.state == u'pending': if log: log_yellow("Instance state: %s" % instance.state) sleep(10) instance.update() if log: log_green("Instance state: %s" % instance.state) if wait_for_ssh_available: wait_for_ssh(instance.public_dns_name) # update the EBS volumes to be deleted on instance termination if delete_on_termination: for dev, bd in instance.block_device_mapping.items(): instance.modify_attribute('BlockDeviceMapping', ["%s=%d" % (dev, 1)]) # add a tag to our instance if tags: connection.create_tags([instance.id], tags) if log: log_green("Public dns: %s" % instance.public_dns_name) # returns our new instance return instance