def disassociate_key_to_machine(request): '''Disassociate a key from a machine. Receives a key name, and a machine/backend id pair and removes the machine from that keypair''' params = request.json_body key_name = params.get('key_name', '') machine_id = params.get('machine_id', '') backend_id = params.get('backend_id', '') try: keypairs = request.environ['beaker.session']['keypairs'] except: keypairs = request.registry.settings.get('keypairs', {}) keypair = {} if key_name in keypairs.keys(): keypair = keypairs[key_name] else: return Response('Keypair not found', 404) machine_backend = [backend_id, machine_id] for pair in keypair['machines']: if pair == machine_backend: keypair['machines'].remove(pair) save_keypairs(request, keypair) break return {}
def associate_key_to_machine(request): '''Associate a key with a machine. Receives a key name, and a machine/backend id''' params = request.json_body key_name = params.get('key_name', '') machine_id = params.get('machine_id', '') backend_id = params.get('backend_id', '') try: keypairs = request.environ['beaker.session']['keypairs'] except: keypairs = request.registry.settings.get('keypairs', {}) keypair = {} if key_name in keypairs.keys(): keypair = keypairs[key_name] else: return Response('Keypair not found', 404) machine_backend = [backend_id, machine_id] if not machine_backend in keypair['machines']: keypair['machines'].append(machine_backend) save_keypairs(request, keypair) return {}
def associate_key_to_machines(request): '''Associate a key with list of machines. Receives a key name, and a list of machine/backend ids''' params = request.json_body key_name = params.get('key_name', '') machine_backend_list = params.get('machine_backend_list', '') try: keypairs = request.environ['beaker.session']['keypairs'] except: keypairs = request.registry.settings.get('keypairs', {}) keypair = {} if key_name in keypairs.keys(): keypair = keypairs[key_name] else: return Response('Keypair not found', 404) #machine_backend_list = [[machine1_id, backend1_id], [machine2_id, backend2_id], [machine3_id, backend3_id]] if keypair: keypair['machines'] = [] for pair in machine_backend_list: try: machine_id = pair[0] backend_id = pair[1] except: continue keypair['machines'].append(pair) save_keypairs(request, keypair) return {}
def create_machine(request): """Creates a new virtual machine on the specified backend. If the backend is Rackspace it attempts to deploy the node with an ssh key provided in config. the method used is the only one working in the old Rackspace backend. create_node(), from libcloud.compute.base, with 'auth' kwarg doesn't do the trick. Didn't test if you can upload some ssh related files using the 'ex_files' kwarg from openstack 1.0 driver. In Linode creation is a bit different. There you can pass the key file directly during creation. The Linode API also requires to set a disk size and doesn't get it from size.id. So, send size.disk from the client and use it in all cases just to avoid provider checking. Finally, Linode API does not support association between a machine and the image it came from. We could set this, at least for machines created through mist.io in ex_comment, lroot or lconfig. lroot seems more appropriate. However, liblcoud doesn't support linode.config.list at the moment, so no way to get them. Also, it will create inconsistencies for machines created through mist.io and those from the Linode interface. """ try: conn = connect(request) except: return Response('Backend not found', 404) backend_id = request.matchdict['backend'] try: key_name = request.json_body['key'] except: key_name = None try: keypairs = request.environ['beaker.session']['keypairs'] except: keypairs = request.registry.settings.get('keypairs', {}) if key_name: keypair = get_keypair_by_name(keypairs, key_name) else: keypair = get_keypair(keypairs) if keypair: private_key = keypair['private'] public_key = keypair['public'] else: private_key = public_key = None try: machine_name = request.json_body['name'] location_id = request.json_body['location'] image_id = request.json_body['image'] size_id = request.json_body['size'] #deploy_script received as unicode, but ScriptDeployment wants str script = str(request.json_body.get('script', '')) # these are required only for Linode, passing them anyway image_extra = request.json_body['image_extra'] disk = request.json_body['disk'] except Exception as e: return Response('Invalid payload', 400) size = NodeSize(size_id, name='', ram='', disk=disk, bandwidth='', price='', driver=conn) image = NodeImage(image_id, name='', extra=image_extra, driver=conn) if conn.type in EC2_PROVIDERS: locations = conn.list_locations() for loc in locations: if loc.id == location_id: location = loc break else: location = NodeLocation(location_id, name='', country='', driver=conn) if conn.type in [Provider.RACKSPACE_FIRST_GEN, Provider.RACKSPACE] and\ public_key: key = SSHKeyDeployment(str(public_key)) deploy_script = ScriptDeployment(script) msd = MultiStepDeployment([key, deploy_script]) try: node = conn.deploy_node(name=machine_name, image=image, size=size, location=location, deploy=msd) if keypair: machines = keypair.get('machines', None) if machines and len(machines): keypair['machines'].append([backend_id, node.id]) else: keypair['machines'] = [[backend_id, node.id],] save_keypairs(request, keypair) except Exception as e: return Response('Something went wrong with node creation in RackSpace: %s' % e, 500) elif conn.type in EC2_PROVIDERS and public_key: imported_key = import_key(conn, public_key, key_name) created_security_group = create_security_group(conn, EC2_SECURITYGROUP) deploy_script = ScriptDeployment(script) (tmp_key, tmp_key_path) = tempfile.mkstemp() key_fd = os.fdopen(tmp_key, 'w+b') key_fd.write(private_key) key_fd.close() #deploy_node wants path for ssh private key if imported_key and created_security_group: try: node = conn.deploy_node(name=machine_name, image=image, size=size, deploy=deploy_script, location=location, ssh_key=tmp_key_path, ex_keyname=key_name, ex_securitygroup=EC2_SECURITYGROUP['name']) if keypair: machines = keypair.get('machines', None) if machines and len(machines): keypair['machines'].append([backend_id, node.id]) else: keypair['machines'] = [[backend_id, node.id],] save_keypairs(request, keypair) except Exception as e: return Response('Something went wrong with node creation in EC2: %s' % e, 500) #remove temp file with private key try: os.remove(tmp_key_path) except: pass elif conn.type is Provider.LINODE and public_key: auth = NodeAuthSSHKey(public_key) deploy_script = ScriptDeployment(script) try: node = conn.create_node(name=machine_name, image=image, size=size, deploy=deploy_script, location=location, auth=auth) if keypair: machines = keypair.get('machines', None) if machines and len(machines): keypair['machines'].append([backend_id, node.id]) else: keypair['machines'] = [[backend_id, node.id],] save_keypairs(request, keypair) except: return Response('Something went wrong with Linode creation', 500) else: try: node = conn.create_node(name=machine_name, image=image, size=size, location=location) except Exception as e: return Response('Something went wrong with generic node creation: %s' % e, 500) return {'id': node.id, 'name': node.name, 'extra': node.extra, 'public_ips': node.public_ips, 'private_ips': node.private_ips, }
def create_machine(request): """Creates a new virtual machine on the specified backend. If the backend is Rackspace it attempts to deploy the node with an ssh key provided in config. the method used is the only one working in the old Rackspace backend. create_node(), from libcloud.compute.base, with 'auth' kwarg doesn't do the trick. Didn't test if you can upload some ssh related files using the 'ex_files' kwarg from openstack 1.0 driver. In Linode creation is a bit different. There you can pass the key file directly during creation. The Linode API also requires to set a disk size and doesn't get it from size.id. So, send size.disk from the client and use it in all cases just to avoid provider checking. Finally, Linode API does not support association between a machine and the image it came from. We could set this, at least for machines created through mist.io in ex_comment, lroot or lconfig. lroot seems more appropriate. However, liblcoud doesn't support linode.config.list at the moment, so no way to get them. Also, it will create inconsistencies for machines created through mist.io and those from the Linode interface. """ try: conn = connect(request) except: return Response('Backend not found', 404) backend_id = request.matchdict['backend'] try: key_name = request.json_body['key'] except: key_name = None try: keypairs = request.environ['beaker.session']['keypairs'] except: keypairs = request.registry.settings.get('keypairs', {}) if key_name: keypair = get_keypair_by_name(keypairs, key_name) else: keypair = get_keypair(keypairs) if keypair: private_key = keypair['private'] public_key = keypair['public'] else: private_key = public_key = None try: machine_name = request.json_body['name'] location_id = request.json_body['location'] image_id = request.json_body['image'] size_id = request.json_body['size'] #deploy_script received as unicode, but ScriptDeployment wants str script = str(request.json_body.get('script', '')) # these are required only for Linode, passing them anyway image_extra = request.json_body['image_extra'] disk = request.json_body['disk'] except Exception as e: return Response('Invalid payload', 400) size = NodeSize(size_id, name='', ram='', disk=disk, bandwidth='', price='', driver=conn) image = NodeImage(image_id, name='', extra=image_extra, driver=conn) if conn.type in EC2_PROVIDERS: locations = conn.list_locations() for loc in locations: if loc.id == location_id: location = loc break else: location = NodeLocation(location_id, name='', country='', driver=conn) if conn.type in [Provider.RACKSPACE_FIRST_GEN, Provider.RACKSPACE] and\ public_key: key = SSHKeyDeployment(str(public_key)) deploy_script = ScriptDeployment(script) msd = MultiStepDeployment([key, deploy_script]) try: node = conn.deploy_node(name=machine_name, image=image, size=size, location=location, deploy=msd) if keypair: machines = keypair.get('machines', None) if machines and len(machines): keypair['machines'].append([backend_id, node.id]) else: keypair['machines'] = [ [backend_id, node.id], ] save_keypairs(request, keypair) except Exception as e: return Response( 'Something went wrong with node creation in RackSpace: %s' % e, 500) elif conn.type in EC2_PROVIDERS and public_key: imported_key = import_key(conn, public_key, key_name) created_security_group = create_security_group(conn, EC2_SECURITYGROUP) deploy_script = ScriptDeployment(script) (tmp_key, tmp_key_path) = tempfile.mkstemp() key_fd = os.fdopen(tmp_key, 'w+b') key_fd.write(private_key) key_fd.close() #deploy_node wants path for ssh private key if imported_key and created_security_group: try: node = conn.deploy_node( name=machine_name, image=image, size=size, deploy=deploy_script, location=location, ssh_key=tmp_key_path, ex_keyname=key_name, ex_securitygroup=EC2_SECURITYGROUP['name']) if keypair: machines = keypair.get('machines', None) if machines and len(machines): keypair['machines'].append([backend_id, node.id]) else: keypair['machines'] = [ [backend_id, node.id], ] save_keypairs(request, keypair) except Exception as e: return Response( 'Something went wrong with node creation in EC2: %s' % e, 500) #remove temp file with private key try: os.remove(tmp_key_path) except: pass elif conn.type is Provider.LINODE and public_key: auth = NodeAuthSSHKey(public_key) deploy_script = ScriptDeployment(script) try: node = conn.create_node(name=machine_name, image=image, size=size, deploy=deploy_script, location=location, auth=auth) if keypair: machines = keypair.get('machines', None) if machines and len(machines): keypair['machines'].append([backend_id, node.id]) else: keypair['machines'] = [ [backend_id, node.id], ] save_keypairs(request, keypair) except: return Response('Something went wrong with Linode creation', 500) else: try: node = conn.create_node(name=machine_name, image=image, size=size, location=location) except Exception as e: return Response( 'Something went wrong with generic node creation: %s' % e, 500) return { 'id': node.id, 'name': node.name, 'extra': node.extra, 'public_ips': node.public_ips, 'private_ips': node.private_ips, }