def update_key(request): """Associate/disassociate a keypair with a machine, or get private key. """ params = request.json_body if params["action"] == "associate" or params["action"] == "disassociate": key_id = params["key_id"] backend_id = params["backend_id"] machine_id = params["machine_id"] if params["action"] == "associate": ret = associate_key(request, key_id, backend_id, machine_id) else: ret = disassociate_key(request, key_id, backend_id, machine_id) elif params["action"] == "get_private_key": ret = get_private_key(request) else: ret = Response("Key action not supported", 405) return ret
def update_key(request): """Associate/disassociate a keypair with a machine, or get private key. """ params = request.json_body if params['action'] == 'associate' or params['action'] == 'disassociate': key_id = params['key_id'] backend_id = params['backend_id'] machine_id = params['machine_id'] if params['action'] == 'associate': ret = associate_key(request, key_id, backend_id, machine_id) else: ret = disassociate_key(request, key_id, backend_id, machine_id) elif params['action'] == 'get_private_key': ret = get_private_key(request) else: ret = Response('Key action not supported', 405) return ret
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_id = request.json_body["key"] except: key_id = None try: keypairs = request.environ["beaker.session"]["keypairs"] except: keypairs = request.registry.settings.get("keypairs", {}) if key_id: keypair = get_keypair_by_name(keypairs, key_id) 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) associate_key(request, key_id, backend_id, node.id, deploy=False) except Exception as e: return Response("Failed to create machine in Rackspace: %s" % e, 500) elif conn.type in EC2_PROVIDERS and public_key and private_key: imported_key = import_key(conn, public_key, key_id) 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, ssh_alternate_usernames=["ec2-user", "ubuntu"], max_tries=1, ex_keyname=key_id, ex_securitygroup=EC2_SECURITYGROUP["name"], ) associate_key(request, key_id, backend_id, node.id, deploy=False) except Exception as e: return Response("Failed to create machine 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 and private_key: auth = NodeAuthSSHKey(public_key) (tmp_key, tmp_key_path) = tempfile.mkstemp() key_fd = os.fdopen(tmp_key, "w+b") key_fd.write(private_key) key_fd.close() deploy_script = ScriptDeployment(script) try: node = conn.deploy_node( name=machine_name, image=image, size=size, deploy=deploy_script, location=location, auth=auth, ssh_key=tmp_key_path, ) associate_key(request, key_id, backend_id, node.id, deploy=False) except Exception as e: return Response("Failed to create machine in Linode: %s" % e, 500) # remove temp file with private key try: os.remove(tmp_key_path) except: pass else: return Response("Cannot create a machine without a keypair", 400) return { "id": node.id, "name": node.name, "extra": node.extra, "public_ips": node.public_ips, "private_ips": node.private_ips, }