def create(gandi, name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey): """Create a new PaaS instance and initialize associated git repository. you can specify a configuration entry named 'sshkey' containing path to your sshkey file $ gandi config -g sshkey ~/.ssh/id_rsa.pub or getting the sshkey "my_key" from your gandi ssh keyring $ gandi config -g sshkey my_key to know which PaaS instance type to use as type $ gandi types """ if not name: name = randomstring() result = gandi.paas.create(name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey) return result
def create(gandi, name, vm, size, snapshotprofile, datacenter, source, background): """ Create a new disk. """ try: gandi.datacenter.is_opened(datacenter, 'iaas') except DatacenterLimited as exc: gandi.echo('/!\ Datacenter %s will be closed on %s, ' 'please consider using another datacenter.' % (datacenter, exc.date)) if vm: vm_dc = gandi.iaas.info(vm) vm_dc_id = vm_dc['datacenter_id'] dc_id = int(gandi.datacenter.usable_id(datacenter)) if vm_dc_id != dc_id: gandi.echo('/!\ VM %s datacenter will be used instead of %s.' % (vm, datacenter)) datacenter = vm_dc_id output_keys = ['id', 'type', 'step'] name = name or randomstring('vdi') disk_type = 'data' oper = gandi.disk.create(name, vm, size, snapshotprofile, datacenter, source, disk_type, background) if background: output_generic(gandi, oper, output_keys) return oper
def create(gandi, name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey, ssl, private_key, poll_cert): """Create a new PaaS instance and initialize associated git repository. you can specify a configuration entry named 'sshkey' containing path to your sshkey file $ gandi config -g sshkey ~/.ssh/id_rsa.pub or getting the sshkey "my_key" from your gandi ssh keyring $ gandi config -g sshkey my_key to know which PaaS instance type to use as type $ gandi paas types """ if not password: password = click.prompt('password', hide_input=True, confirmation_prompt=True) if not name: name = randomstring('vm') if vhosts and not gandi.hostedcert.activate_ssl(vhosts, ssl, private_key, poll_cert): return result = gandi.paas.create(name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey) return result
def create(gandi): """ Create a new contact. """ contact = {} for field, label, checks in FIELDS: ask_field(gandi, contact, field, label, checks) default_pwd = randomstring(16) contact['password'] = click.prompt('Please enter your password', hide_input=True, confirmation_prompt=True, default=default_pwd) result = True while result: result = gandi.contact.create_dry_run(contact) # display errors for err in result: gandi.echo(err['reason']) field = err['field'] if field not in FIELDS_POSITION: return desc = FIELDS[FIELDS_POSITION.get(field)] ask_field(gandi, contact, *desc) result = gandi.contact.create(contact) handle = result['handle'] gandi.echo('Please activate you public api access from gandi website, and ' 'get the apikey.') gandi.echo('Your handle is %s, and the password is the one you defined.' % handle) # open new browser window webbrowser.open('https://www.gandi.net/admin/api_key') # just to avoid missing the next question in webbrowser stderr time.sleep(1) # get the apikey from shell apikey = None while not apikey: apikey = click.prompt('What is your production apikey') caller = gandi.get('api.key') # save apikey in the conf if none defined else display an help on how to # use it if caller: gandi.echo('You already have an apikey defined, if you want to use the' ' newly created contact, use the env var : ') gandi.echo('export API_KEY=%s' % apikey) else: gandi.echo('Will save your apikey into the config file.') gandi.configure(True, 'api.key', apikey) return handle
def snapshot(gandi, name, resource, background): """ Create a snapshot on the fly. """ name = name or randomstring('snp') source_info = gandi.disk.info(resource) datacenter = source_info['datacenter_id'] result = gandi.disk.create(name, None, None, None, datacenter, resource, 'snapshot', background) if background: gandi.pretty_echo(result) return result
def create(gandi, name, vm, size, snapshotprofile, datacenter, source, background): """ Create a new disk. """ output_keys = ['id', 'type', 'step'] name = name or randomstring('vdi') disk_type = 'data' oper = gandi.disk.create(name, vm, size, snapshotprofile, datacenter, source, disk_type, background) if background: output_generic(gandi, oper, output_keys) return oper
def create(gandi, name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey, ssl, private_key, poll_cert): """Create a new PaaS instance and initialize associated git repository. you can specify a configuration entry named 'sshkey' containing path to your sshkey file $ gandi config set [-g] sshkey ~/.ssh/id_rsa.pub or getting the sshkey "my_key" from your gandi ssh keyring $ gandi config set [-g] sshkey my_key to know which PaaS instance type to use as type $ gandi paas types """ try: gandi.datacenter.is_opened(datacenter, 'paas') except DatacenterLimited as exc: gandi.echo('/!\ Datacenter %s will be closed on %s, ' 'please consider using another datacenter.' % (datacenter, exc.date)) if not password: password = click.prompt('password', hide_input=True, confirmation_prompt=True) if not name: name = randomstring('paas') if vhosts and not gandi.hostedcert.activate_ssl(vhosts, ssl, private_key, poll_cert): return result = gandi.paas.create(name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey) return result
def create(gandi, name, vm, size, snapshotprofile, datacenter, background): """ Create a new disk. """ try: snapshotprofile = int(snapshotprofile) if snapshotprofile else None except ValueError: gandi.echo('--snapshotprofile must be an existing profile.') gandi.echo('get all existing profiles with :') gandi.echo(' gandi snapshotprofile list') return name = name or randomstring() result = gandi.disk.create(name, vm, size, snapshotprofile, datacenter, background) if background: gandi.pretty_echo(result) return result
def create(gandi, name, vm, size, snapshotprofile, datacenter, source, background): """ Create a new disk. """ try: gandi.datacenter.is_opened(datacenter, 'iaas') except DatacenterLimited as exc: gandi.echo('/!\ Datacenter %s will be closed on %s, ' 'please consider using another datacenter.' % (datacenter, exc.date)) output_keys = ['id', 'type', 'step'] name = name or randomstring('vdi') disk_type = 'data' oper = gandi.disk.create(name, vm, size, snapshotprofile, datacenter, source, disk_type, background) if background: output_generic(gandi, oper, output_keys) return oper
def create(gandi, name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey, ssl, private_key, poll_cert): """Create a new PaaS instance and initialize associated git repository. you can specify a configuration entry named 'sshkey' containing path to your sshkey file $ gandi config set [-g] sshkey ~/.ssh/id_rsa.pub or getting the sshkey "my_key" from your gandi ssh keyring $ gandi config set [-g] sshkey my_key to know which PaaS instance type to use as type $ gandi paas types """ if not password: password = click.prompt('password', hide_input=True, confirmation_prompt=True) if not name: name = randomstring('vm') if vhosts and not gandi.hostedcert.activate_ssl(vhosts, ssl, private_key, poll_cert): return result = gandi.paas.create(name, size, type, quantity, duration, datacenter, vhosts, password, snapshotprofile, background, sshkey) return result
def create(cls, datacenter, memory, cores, ip_version, bandwidth, login, password, hostname, image, run, background, sshkey, size, vlan, ip, script, script_args, ssh): """Create a new virtual machine.""" from gandi.cli.modules.network import Ip, Iface if not background and not cls.intty(): background = True datacenter_id_ = int(Datacenter.usable_id(datacenter)) if not hostname: hostname = randomstring('vm') disk_name = 'sys_%s' % hostname[2:] else: disk_name = 'sys_%s' % hostname.replace('.', '') vm_params = { 'hostname': hostname, 'datacenter_id': datacenter_id_, 'memory': memory, 'cores': cores, } if login: vm_params['login'] = login if run: vm_params['run'] = run if password: vm_params['password'] = password if ip_version: vm_params['ip_version'] = ip_version vm_params['bandwidth'] = bandwidth if script: with open(script) as fd: vm_params['script'] = fd.read() if script_args: vm_params['script_args'] = script_args vm_params.update(cls.convert_sshkey(sshkey)) # XXX: name of disk is limited to 15 chars in ext2fs, ext3fs # but api allow 255, so we limit to 15 for now disk_params = {'datacenter_id': vm_params['datacenter_id'], 'name': disk_name[:15]} if size: if isinstance(size, tuple): prefix, size = size disk_params['size'] = size sys_disk_id_ = int(Image.usable_id(image, datacenter_id_)) ip_summary = [] if ip_version == 4: ip_summary = ['v4', 'v6'] elif ip_version == 6: ip_summary = ['v6'] if vlan: ip_ = None ip_summary.append('private') if ip: try: ip_ = Ip.info(ip) except Exception: pass else: if not Ip.check_and_detach(ip_, None, force=False): return if ip_: iface_id = ip_['iface_id'] else: ip_create = Ip.create(4, vm_params['datacenter_id'], bandwidth, None, vlan, ip) iface_id = ip_create['iface_id'] # if there is a public ip, will attach this one later, else give # the iface to vm.create if not ip_version: vm_params['iface_id'] = iface_id result = cls.call('hosting.vm.create_from', vm_params, disk_params, sys_disk_id_) cls.echo('* Configuration used: %d cores, %dMb memory, ip %s, ' 'image %s, hostname: %s, datacenter: %s' % (cores, memory, '+'.join(ip_summary), image, hostname, datacenter)) # background mode, bail out now (skip interactive part) if background and (not vlan or not ip_version): return result # interactive mode, run a progress bar cls.echo('Creating your Virtual Machine %s.' % hostname) cls.display_progress(result) cls.echo('Your Virtual Machine %s has been created.' % hostname) vm_id = None for oper in result: if oper.get('vm_id'): vm_id = oper.get('vm_id') break if vlan and ip_version: attach = Iface._attach(iface_id, vm_id) if background: return attach if 'ssh_key' not in vm_params and 'keys' not in vm_params: return if vm_id and ip_version: cls.wait_for_sshd(vm_id) if ssh: cls.ssh_keyscan(vm_id) cls.ssh(vm_id, 'root', None)
def create(cls, datacenter, memory, cores, ip_version, bandwidth, login, password, hostname, image, run, background, sshkey, size, vlan, ip, script, script_args, ssh): """Create a new virtual machine.""" from gandi.cli.modules.network import Ip, Iface if not background and not cls.intty(): background = True datacenter_id_ = int(Datacenter.usable_id(datacenter)) if not hostname: hostname = randomstring('vm') disk_name = 'sys_%s' % hostname[2:] else: disk_name = 'sys_%s' % hostname.replace('.', '') vm_params = { 'hostname': hostname, 'datacenter_id': datacenter_id_, 'memory': memory, 'cores': cores, } if login: vm_params['login'] = login if run: vm_params['run'] = run if password: vm_params['password'] = password if ip_version: vm_params['ip_version'] = ip_version vm_params['bandwidth'] = bandwidth if script: with open(script) as fd: vm_params['script'] = fd.read() if script_args: vm_params['script_args'] = script_args vm_params.update(cls.convert_sshkey(sshkey)) # XXX: name of disk is limited to 15 chars in ext2fs, ext3fs # but api allow 255, so we limit to 15 for now disk_params = { 'datacenter_id': vm_params['datacenter_id'], 'name': disk_name[:15] } if size: if isinstance(size, tuple): prefix, size = size disk_params['size'] = size sys_disk_id_ = int(Image.usable_id(image, datacenter_id_)) ip_summary = [] if ip_version == 4: ip_summary = ['v4', 'v6'] elif ip_version == 6: ip_summary = ['v6'] if vlan: ip_ = None ip_summary.append('private') if ip: try: ip_ = Ip.info(ip) except Exception: pass else: if not Ip._check_and_detach(ip_, None): return if ip_: iface_id = ip_['iface_id'] else: ip_create = Ip.create(4, vm_params['datacenter_id'], bandwidth, None, vlan, ip) iface_id = ip_create['iface_id'] # if there is a public ip, will attach this one later, else give # the iface to vm.create if not ip_version: vm_params['iface_id'] = iface_id result = cls.call('hosting.vm.create_from', vm_params, disk_params, sys_disk_id_) cls.echo( '* Configuration used: %d cores, %dMb memory, ip %s, ' 'image %s, hostname: %s, datacenter: %s' % (cores, memory, '+'.join(ip_summary), image, hostname, datacenter)) # background mode, bail out now (skip interactive part) if background and (not vlan or not ip_version): return result # interactive mode, run a progress bar cls.echo('Creating your Virtual Machine %s.' % hostname) cls.display_progress(result) cls.echo('Your Virtual Machine %s has been created.' % hostname) vm_id = None for oper in result: if oper.get('vm_id'): vm_id = oper.get('vm_id') break if vlan and ip_version: attach = Iface._attach(iface_id, vm_id) if background: return attach if 'ssh_key' not in vm_params and 'keys' not in vm_params: return if vm_id and ip_version: cls.wait_for_sshd(vm_id) if ssh: cls.ssh_keyscan(vm_id) cls.ssh(vm_id, 'root', None)
def create(cls, datacenter, memory, cores, ip_version, bandwidth, login, password, hostname, image, run, background, sshkey, size, script): """Create a new virtual machine.""" if not background and not cls.intty(): background = True datacenter_id_ = int(Datacenter.usable_id(datacenter)) if not hostname: hostname = randomstring() disk_name = 'sys_%s' % hostname[4:] else: disk_name = 'sys_%s' % hostname.replace('.', '') vm_params = { 'hostname': hostname, 'datacenter_id': datacenter_id_, 'memory': memory, 'cores': cores, 'ip_version': ip_version, 'bandwidth': bandwidth, } if login: vm_params['login'] = login if run: vm_params['run'] = run if password: vm_params['password'] = password vm_params.update(cls.convert_sshkey(sshkey)) # XXX: name of disk is limited to 15 chars in ext2fs, ext3fs # but api allow 255, so we limit to 15 for now disk_params = {'datacenter_id': vm_params['datacenter_id'], 'name': disk_name[:15]} if size: disk_params['size'] = size sys_disk_id_ = int(Image.usable_id(image, datacenter_id_)) result = cls.call('hosting.vm.create_from', vm_params, disk_params, sys_disk_id_) ip_summary = 'ip v4+v6' if ip_version == 6: ip_summary = 'ip v6' cls.echo('* Configuration used: %d cores, %dMb memory, %s, ' 'image %s, hostname: %s' % (cores, memory, ip_summary, image, hostname)) # background mode, bail out now (skip interactive part) if background: return result # interactive mode, run a progress bar cls.echo('Creating your Virtual Machine %s.' % hostname) cls.display_progress(result) cls.echo('Your Virtual Machine %s has been created.' % hostname) if 'ssh_key' not in vm_params and 'keys' not in vm_params: return vm_id = None for oper in result: if oper.get('vm_id'): vm_id = oper.get('vm_id') break if vm_id: cls.wait_for_sshd(vm_id) cls.ssh_keyscan(vm_id) if script: ret = cls.scp(vm_id, 'root', None, script, '/var/tmp/gscript') if not ret: cls.error('Failed to scp script %s to VM %s (id: %s)' % (script, hostname, vm_id)) ret = cls.ssh(vm_id, 'root', None, script and ['/var/tmp/gscript']) if not ret and (script and ['/var/tmp/gscript']): cls.error('Failed to execute script %s on VM %s (id: %s)' % ('/var/tmp/gscript', hostname, vm_id))
def create(cls, datacenter, memory, cores, ip_version, bandwidth, login, password, hostname, image, run, background, sshkey, size): """Create a new virtual machine.""" if not background and not cls.intty(): background = True datacenter_id_ = int(Datacenter.usable_id(datacenter)) if not hostname: hostname = randomstring() disk_name = 'sys_%s' % hostname[4:] else: disk_name = 'sys_%s' % hostname.replace('.', '') vm_params = { 'hostname': hostname, 'datacenter_id': datacenter_id_, 'memory': memory, 'cores': cores, 'ip_version': ip_version, 'bandwidth': bandwidth, } if login: vm_params['login'] = login if run: vm_params['run'] = run if password: vm_params['password'] = password vm_params.update(cls.convert_sshkey(sshkey)) # XXX: name of disk is limited to 15 chars in ext2fs, ext3fs # but api allow 255, so we limit to 15 for now disk_params = {'datacenter_id': vm_params['datacenter_id'], 'name': disk_name[:15]} if size: disk_params['size'] = size sys_disk_id_ = int(Image.usable_id(image, datacenter_id_)) result = cls.call('hosting.vm.create_from', vm_params, disk_params, sys_disk_id_) if ip_version == 4: ip_summary = 'ip v4+v6' else: ip_summary = 'ip v6' cls.echo('* Configuration used: %d cores, %dMb memory, %s, ' 'image %s, hostname: %s' % (cores, memory, ip_summary, image, hostname)) if background: return result # interactive mode, run a progress bar cls.echo('Creating your Virtual Machine %s.' % hostname) cls.display_progress(result) cls.echo('Your Virtual Machine %s has been created.' % hostname) if 'ssh_key' not in vm_params and 'keys' not in vm_params: return vm_id = None for oper in result: if oper.get('vm_id'): vm_id = oper.get('vm_id') break if vm_id: time.sleep(5) cls.ssh(oper['vm_id'], login='******', identity=None, wipe_key=True)
def create(cls, datacenter, memory, cores, ip_version, bandwidth, login, password, hostname, image, run, background, sshkey): """Create a new virtual machine.""" if not background and not cls.intty(): background = True datacenter_id_ = int(Datacenter.usable_id(datacenter)) if not hostname: hostname = randomstring() disk_name = 'sys_%s' % hostname[4:] else: disk_name = 'sys_%s' % hostname.replace('.', '') vm_params = { 'hostname': hostname, 'datacenter_id': datacenter_id_, 'memory': memory, 'cores': cores, 'ip_version': ip_version, 'bandwidth': bandwidth, } if login: vm_params['login'] = login if run: vm_params['run'] = run if password: vm_params['password'] = password vm_params.update(cls.convert_sshkey(sshkey)) # XXX: name of disk is limited to 15 chars in ext2fs, ext3fs # but api allow 255, so we limit to 15 for now disk_params = {'datacenter_id': vm_params['datacenter_id'], 'name': disk_name[:15]} sys_disk_id_ = int(Image.usable_id(image, datacenter_id_)) result = cls.call('hosting.vm.create_from', vm_params, disk_params, sys_disk_id_) if ip_version == 4: ip_summary = 'ip v4+v6' else: ip_summary = 'ip v6' cls.echo('* Configuration used: %d cores, %dMb memory, %s, ' 'image %s, hostname: %s' % (cores, memory, ip_summary, image, hostname)) if background: return result # interactive mode, run a progress bar cls.echo('Creating your Virtual Machine.') cls.display_progress(result) cls.echo('Your Virtual Machine %s has been created.' % hostname) if 'ssh_key' not in vm_params and 'keys' not in vm_params: return vm_id = None for oper in result: if oper.get('vm_id'): vm_id = oper.get('vm_id') break if vm_id: time.sleep(5) cls.ssh(oper['vm_id'], login='******', identity=None, wipe_key=True)