def create_keypair(): """Create a new keypair and upload it to Ravello.""" cfgdir = util.get_config_dir() privname = os.path.join(cfgdir, 'id_ravello') pubname = privname + '.pub' keyname = 'ravello@%s' % socket.gethostname() # Prefer to generate the key locallly with ssh-keygen because # that gives us more privacy. If ssh-keygen is not available, ask # for a key through the API. sshkeygen = util.which('ssh-keygen') if sshkeygen: try: console.info("Generating keypair using 'ssh-keygen'...") subprocess.call(['ssh-keygen', '-q', '-t', 'rsa', '-C', keyname, '-b', '2048', '-N', '', '-f', privname]) except subprocess.CalledProcessError as e: error.raise_error('ssh-keygen returned with error status {0}', e.returncode) with file(pubname) as fin: pubkey = fin.read() keyparts = pubkey.strip().split() else: keyname = 'ravello@api-generated' console.info('Requesting a new keypair via the API...') keypair = env.api.create_keypair() with file(privname, 'w') as fout: fout.write(keypair['privateKey']) with file(pubname, 'w') as fout: fout.write(keypair['publicKey'].rstrip()) fout.write(' {0} (generated remotely)\n'.format(keyname)) pubkey = keypair['publicKey'].rstrip() keyparts = pubkey.split() keyparts[2:] = [keyname] # Create the pubkey in the API under a unique name pubkeys = env.api.get_pubkeys() keyname = util.get_unused_name(keyname, pubkeys) keyparts[2] = keyname keydata = '{0} {1} {2}\n'.format(*keyparts) pubkey = {'name': keyname} pubkey['publicKey'] = keydata pubkey = env.api.create_pubkey(pubkey) with file(pubname, 'w') as fout: fout.write(keydata) env.public_key = pubkey env.private_key_file = privname return pubkey
def do_save(args, env): """The "ravello save" command.""" with env.let(quiet=True): login.default_login() keypair.default_keypair() manif = manifest.default_manifest(required=False) app = application.default_application(args.application) appname = app["name"] project, defname, instance = appname.split(":") state = application.get_application_state(app) if state not in ("STOPPED", "STARTED"): error.raise_error( "Application `{0}:{1}` is currently in state {2}.\n" "Can only create blueprint when STOPPED or STARTED.", defname, instance, state, ) if state == "STARTED" and not env.always_confirm: console.info("Application `{0}:{1}` is currently running.", defname, instance) result = console.confirm("Do you want to continue with a live snapshot") if not result: console.info("Not confirmed.") return error.EX_OK template = "{0}:{1}".format(project, defname) bpname = util.get_unused_name(template, cache.get_blueprints()) parts = bpname.split(":") console.info("Saving blueprint as `{0}:{1}`.", parts[1], parts[2]) blueprint = env.api.create_blueprint(bpname, app) env.blueprint = blueprint # for tests console.info("Blueprint creation process started.") console.info("Use 'ravtest ps -b' to monitor progress.") return error.EX_OK
def create_new_application(appdef, name_is_template=True): """Create a new application based on ``appdef``.""" if name_is_template: project = env.manifest['project'] template = '{0}:{1}'.format(project['name'], appdef['name']) name = util.get_unused_name(template, cache.get_applications()) else: name = appdef['name'] app = { 'name': name } bpname = appdef.get('blueprint') if bpname: blueprint = cache.get_blueprint(name=bpname) app['blueprintId'] = blueprint['id'] else: vms = [] for vmdef in appdef.get('vms', []): vm = create_new_vm(vmdef) vms.append(vm) app['vms'] = vms app = env.api.create_application(app) app = cache.get_application(app['id']) # update cache return app
def new_blueprint_name(template): """Return a new blueprint name based on *template*.""" name = util.get_unused_name(template, cache.get_blueprints()) return name
def new_application_name(template): """Return a new application name based on *template*.""" name = util.get_unused_name(template, cache.get_applications()) return name