def _create_machine_linode(conn, key_name, private_key, public_key, machine_name, image, size, location): """Create a machine in Linode. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ auth = NodeAuthSSHKey(public_key) with get_temp_file(private_key) as tmp_key_path: try: node = conn.create_node( name=machine_name, image=image, size=size, location=location, auth=auth, ssh_key=tmp_key_path, ex_private=True ) except Exception as e: raise MachineCreationError("Linode, got exception %s" % e, e) return node
def _create_machine_openstack(conn, private_key, public_key, machine_name, image, size, location, networks, user_data): """Create a machine in Openstack. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ key = str(public_key).replace('\n', '') try: server_key = '' keys = conn.ex_list_keypairs() for k in keys: if key == k.public_key: server_key = k.name break if not server_key: server_key = conn.ex_import_keypair_from_string(name=machine_name, key_material=key) server_key = server_key.name except: server_key = conn.ex_import_keypair_from_string( name='mistio' + str(random.randint(1, 100000)), key_material=key) server_key = server_key.name # select the right OpenStack network object available_networks = conn.ex_list_networks() try: chosen_networks = [] for net in available_networks: if net.id in networks: chosen_networks.append(net) except: chosen_networks = [] with get_temp_file(private_key) as tmp_key_path: try: node = conn.create_node( name=machine_name, image=image, size=size, location=location, ssh_key=tmp_key_path, ssh_alternate_usernames=['ec2-user', 'ubuntu'], max_tries=1, ex_keyname=server_key, networks=chosen_networks, ex_userdata=user_data) except Exception as e: raise MachineCreationError("OpenStack, got exception %s" % e, e) return node
def _create_machine_ec2(conn, key_name, private_key, public_key, machine_name, image, size, location, user_data): """Create a machine in Amazon EC2. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ with get_temp_file(public_key) as tmp_key_path: try: # create keypair with key name and pub key conn.ex_import_keypair(name=key_name, keyfile=tmp_key_path) except: # get existing key with that pub key try: keypair = conn.ex_find_or_import_keypair_by_key_material( pubkey=public_key ) key_name = keypair['keyName'] except Exception as exc: raise CloudUnavailableError("Failed to import key") # create security group name = config.EC2_SECURITYGROUP.get('name', '') description = config.EC2_SECURITYGROUP.get('description', '') try: log.info("Attempting to create security group") conn.ex_create_security_group(name=name, description=description) conn.ex_authorize_security_group_permissive(name=name) except Exception as exc: if 'Duplicate' in exc.message: log.info('Security group already exists, not doing anything.') else: raise InternalServerError("Couldn't create security group", exc) try: node = conn.create_node( name=machine_name, image=image, size=size, location=location, max_tries=1, ex_keyname=key_name, ex_securitygroup=config.EC2_SECURITYGROUP['name'], ex_userdata=user_data ) except Exception as e: raise MachineCreationError("EC2, got exception %s" % e, e) return node
def _create_machine_digital_ocean(conn, key_name, private_key, public_key, machine_name, image, size, location, user_data): """Create a machine in Digital Ocean. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ key = public_key.replace('\n', '') # on API v1 list keys returns only ids, without actual public keys # So the check fails. If there's already a key with the same pub key, # create key call will fail! try: server_key = '' keys = conn.ex_list_ssh_keys() for k in keys: if key == k.pub_key: server_key = k break if not server_key: server_key = conn.ex_create_ssh_key(machine_name, key) except: try: server_key = conn.ex_create_ssh_key('mistio' + str( random.randint(1, 100000)), key) except: # on API v1 if we can't create that key, means that key is already # on our account. Since we don't know the id, we pass all the ids server_keys = [str(k.id) for k in keys] if not server_key: ex_ssh_key_ids = server_keys else: ex_ssh_key_ids = [str(server_key.id)] # check if location allows the private_networking setting private_networking = False try: locations = conn.list_locations() for loc in locations: if loc.id == location.id: if 'private_networking' in loc.extra: private_networking = True break except: # do not break if this fails for some reason pass with get_temp_file(private_key) as tmp_key_path: try: node = conn.create_node( name=machine_name, image=image, size=size, ex_ssh_key_ids=ex_ssh_key_ids, location=location, ssh_key=tmp_key_path, private_networking=private_networking, user_data=user_data ) except Exception as e: raise MachineCreationError( "Digital Ocean, got exception %s" % e, e ) return node
def _create_machine_nephoscale(conn, key_name, private_key, public_key, machine_name, image, size, location, ips): """Create a machine in Nephoscale. Here there is no checking done, all parameters are expected to be sanitized by create_machine. """ machine_name = machine_name[:64].replace(' ', '-') # name in NephoScale must start with a letter, can contain mixed # alpha-numeric characters, hyphen ('-') and underscore ('_') # characters, cannot exceed 64 characters, and can end with a # letter or a number." # Hostname must start with a letter, can contain mixed alpha-numeric # characters and the hyphen ('-') character, cannot exceed 15 characters, # and can end with a letter or a number. key = public_key.replace('\n', '') # NephoScale has 2 keys that need be specified, console and ssh key # get the id of the ssh key if it exists, otherwise add the key try: server_key = '' keys = conn.ex_list_keypairs(ssh=True, key_group=1) for k in keys: if key == k.public_key: server_key = k.id break if not server_key: server_key = conn.ex_create_keypair(machine_name, public_key=key) except: server_key = conn.ex_create_keypair( 'mistio' + str(random.randint(1, 100000)), public_key=key ) # mist.api does not support console key add through the wizzard. # Try to add one try: console_key = conn.ex_create_keypair( 'mistio' + str(random.randint(1, 100000)), key_group=4 ) except: console_keys = conn.ex_list_keypairs(key_group=4) if console_keys: console_key = console_keys[0].id if size.name and size.name.startswith('D'): baremetal = True else: baremetal = False with get_temp_file(private_key) as tmp_key_path: try: node = conn.create_node( name=machine_name, hostname=machine_name[:15], image=image, size=size, zone=location.id, server_key=server_key, console_key=console_key, ssh_key=tmp_key_path, baremetal=baremetal, ips=ips ) except Exception as e: raise MachineCreationError("Nephoscale, got exception %s" % e, e) return node