def start(cls, client, *roles): """Start a server for the specified client with the given roles and connect the requested services. Roles are passed as either a name or a tuple (name, parameter). """ config = Configuration(client) _logger.info("New server for %s on %s with roles %s", config.client, config.host, roles) roles = [("bits", None)] + list(roles) role_adders = cls.get_role_adders(*roles) # Work out the correct region to use and connect to it region = config.region for role_adder in role_adders: region = role_adder.region() or region cnx = ec2_connect(config, region) # Work out the machine size to launch and set default run args run_args = {"key_name": get_keyname(config, cnx), "instance_type": "t1.micro"} for role_adder in role_adders: run_args["instance_type"] = role_adder.size() or run_args["instance_type"] # Calculate how many bits the AMI should be using bits = None for role_adder in role_adders: bits = role_adder.bits(run_args["instance_type"]) or bits # Find the AMI to use ami = None for role_adder in role_adders: ami = role_adder.ami(region, bits, run_args["instance_type"]) or ami # Work out the other run arguments we need for role_adder in role_adders: run_args = role_adder.run_kwargs(run_args) # Start the machine image = cnx.get_all_images(ami)[0] reservation = image.run(**run_args) _logger.debug("Have reservation %s for new server with instances %s", reservation, reservation.instances) # Now we can make the server instance and add the roles server = cls(config, reservation, reservation.instances[0]) for role in role_adders: role.started(server) # Wait for it to start up while server.instance.state == "pending": _logger.info("Waiting 10s for instance to start...") time.sleep(10) server.instance.update() _logger.info("Instance state now %s with name %s.", server.instance.state, server.instance.dns_name) # Upgrade it and configure it server.dist_upgrade() server.add_roles(role_adders) return server
def get_private_key_filename(config, cnx): """Fetch the key filename for this configuration and connection. """ hostname = socket.gethostname() foldername = os.path.expanduser('~/.profab/%s/%s' % ( config.client, cnx.region.name)) pathname = '%s/%s.pem' % (foldername, hostname) if not os.access(foldername, os.F_OK): os.mkdir(foldername) keys = cnx.get_all_key_pairs() for key in keys: if key.name == hostname: _logger.debug("Already found key on EC2 for hostname %s", hostname) return pathname _logger.info("No key pair found for host %s on EC2." " Creating new key pair.", hostname) pem = cnx.create_key_pair(hostname) pem.save(foldername) return pathname
def get_private_key_filename(config, cnx): """Fetch the key filename for this configuration and connection. """ hostname = socket.gethostname() foldername = os.path.expanduser('~/.profab/%s/%s' % (config.client, cnx.region.name)) pathname = '%s/%s.pem' % (foldername, hostname) if not os.access(foldername, os.F_OK): os.mkdir(foldername) keys = cnx.get_all_key_pairs() for key in keys: if key.name == hostname: _logger.debug("Already found key on EC2 for hostname %s", hostname) return pathname _logger.info( "No key pair found for host %s on EC2." " Creating new key pair.", hostname) pem = cnx.create_key_pair(hostname) pem.save(foldername) return pathname
def start(cls, client, *roles): """Start a server for the specified client with the given roles and connect the requested services. Roles are passed as either a name or a tuple (name, parameter). """ config = Configuration(client) _logger.info("New server for %s on %s with roles %s", config.client, config.host, roles) roles = [('ami.lucid', None), ('bits', None)] + list(roles) role_adders = cls.get_role_adders(*roles) # Work out the correct region to use and connect to it region = config.region for role_adder in role_adders: region = role_adder.region() or region cnx = ec2_connect(config, region) # Work out the machine size to launch and set default run args run_args = { 'key_name': get_keyname(config, cnx), 'instance_type': 't1.micro', } for role_adder in role_adders: run_args['instance_type'] = role_adder.size() or \ run_args['instance_type'] # Calculate how many bits the AMI should be using bits = None for role_adder in role_adders: bits = role_adder.bits(run_args['instance_type']) or bits # Find the AMI to use ami = None for role_adder in role_adders: ami = role_adder.ami(region, bits, run_args['instance_type']) or ami # Work out the other run arguments we need for role_adder in role_adders: run_args = role_adder.run_kwargs(run_args) # Start the machine image = cnx.get_all_images(ami)[0] reservation = image.run(**run_args) _logger.debug("Have reservation %s for new server with instances %s", reservation, reservation.instances) # Now we can make the server instance and add the roles server = cls(config, reservation, reservation.instances[0]) for role in role_adders: role.started(server) # Wait for it to start up while server.instance.state == 'pending': _logger.info("Waiting 10s for instance to start...") time.sleep(10) server.instance.update() _logger.info("Instance state now %s with name %s.", server.instance.state, server.instance.dns_name) # Upgrade it and configure it server.dist_upgrade() server.add_roles(role_adders) return server