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 delete(gandi, background, force, resource): """Delete a PaaS instance. Resource can be a vhost, a hostname, or an ID """ output_keys = ["id", "type", "step"] possible_resources = gandi.paas.resource_list() for item in resource: if item not in possible_resources: gandi.echo("Sorry PaaS instance %s does not exist" % item) gandi.echo("Please use one of the following: %s" % possible_resources) return if not force: instance_info = "'%s'" % ", ".join(resource) proceed = click.confirm("Are you sure to delete PaaS instance %s?" % instance_info) if not proceed: return opers = gandi.paas.delete(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def delete(gandi, background, force, resource): """Delete a vlan. Resource can be a vlan name or an ID """ output_keys = ['id', 'type', 'step'] possible_resources = gandi.vlan.resource_list() for item in resource: if item not in possible_resources: gandi.echo('Sorry vlan %s does not exist' % item) gandi.echo('Please use one of the following: %s' % possible_resources) return if not force: vlan_info = "'%s'" % ', '.join(resource) proceed = click.confirm('Are you sure to delete vlan %s?' % vlan_info) if not proceed: return opers = gandi.vlan.delete(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def restart(gandi, resource, background, force): """Restart a PaaS instance. Resource can be a vhost, a hostname, or an ID """ output_keys = ['id', 'type', 'step'] possible_resources = gandi.paas.resource_list() for item in resource: if item not in possible_resources: gandi.echo('Sorry PaaS instance %s does not exist' % item) gandi.echo('Please use one of the following: %s' % possible_resources) return if not force: instance_info = "'%s'" % ', '.join(resource) proceed = click.confirm("Are you sure to restart PaaS instance %s?" % instance_info) if not proceed: return opers = gandi.paas.restart(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def keys_info(gandi, fqdn, key): """Display information about a domain key.""" key_info = gandi.dns.keys_info(fqdn, key) output_keys = ['uuid', 'algorithm', 'algorithm_name', 'ds', 'fingerprint', 'public_key', 'flags', 'tag', 'status'] output_generic(gandi, key_info, output_keys, justify=15) return key_info
def list(gandi, domain, zone_id, output): """List DNS zone records for a domain.""" output_keys = ['name', 'type', 'value', 'ttl'] if not zone_id: result = gandi.domain.info(domain) zone_id = result['zone_id'] if not zone_id: gandi.echo('No zone records found, domain %s doesn\'t seems to be ' 'managed at Gandi.' % domain) return records = gandi.record.list(zone_id) if not output: for num, rec in enumerate(records): if num: gandi.separator_line() output_generic(gandi, rec, output_keys, justify=12) else: zone_filename = domain + "_" + str(zone_id) if os.path.exists(zone_filename): open(zone_filename, 'w').close() for record in records: with open(zone_filename, 'ab') as zone_file: zone_file.write('%s %s IN %s %s\n' % (record['name'], record['ttl'], record['type'], record['value'])) gandi.echo('Your zone file have been writen in %s' % zone_filename) return records
def api(gandi): """Display information about API used.""" output_keys = ['api_version'] result = gandi.api.info() output_generic(gandi, result, output_keys) return result
def info(gandi, id): """Display information about an operation.""" output_keys = ['id', 'type', 'step', 'last_error'] oper = gandi.oper.info(id) output_generic(gandi, oper, output_keys) return oper
def api(gandi): """Display information about API used.""" output_keys = ["api_version"] result = gandi.api.info() output_generic(gandi, result, output_keys) return result
def keys_create(gandi, fqdn, flag): """Create key for a domain.""" key_info = gandi.dns.keys_create(fqdn, int(flag)) output_keys = ['uuid', 'algorithm', 'algorithm_name', 'ds', 'fingerprint', 'public_key', 'flags', 'tag', 'status'] output_generic(gandi, key_info, output_keys, justify=15) return key_info
def info(gandi): """Display infromation about hosting account. """ output_keys = ['handle', 'credits'] account = gandi.account.info() output_generic(gandi, account, output_keys) return account
def info(gandi, email): """Display information about a mailbox.""" login, domain = email output_keys = ["login", "aliases", "fallback_email", "quota", "responder"] mailbox = gandi.mail.info(domain, login) output_generic(gandi, mailbox, output_keys, justify=14) return mailbox
def probe(gandi, resource, enable, disable, test, host, interval, http_method, http_response, threshold, timeout, url, window): """ Manage a probe for a webaccelerator """ result = gandi.webacc.probe(resource, enable, disable, test, host, interval, http_method, http_response, threshold, timeout, url, window) output_keys = ['status', 'timeout'] output_generic(gandi, result, output_keys, justify=14) return result
def api(gandi): """Display information about API used.""" key_name = 'API version' result = gandi.api.info() result[key_name] = result.pop('api_version') output_generic(gandi, result, [key_name]) return result
def info(gandi, email): """Display information about a mailbox.""" login, domain = email output_keys = ['login', 'aliases', 'fallback_email', 'quota', 'responder'] mailbox = gandi.mail.info(domain, login) output_generic(gandi, mailbox, output_keys, justify=14) return mailbox
def keys_list(gandi, fqdn): """List domain keys.""" keys = gandi.dns.keys(fqdn) output_keys = ['uuid', 'algorithm', 'algorithm_name', 'ds', 'flags', 'status'] for num, key in enumerate(keys): if num: gandi.separator_line() output_generic(gandi, key, output_keys, justify=15) return keys
def info(gandi, resource): """Display information about a domain.""" output_keys = ["fqdn", "nameservers", "services", "zone_id", "tags"] contact_field = ["owner", "admin", "bill", "tech", "reseller"] result = gandi.domain.info(resource) output_contact_info(gandi, result["contacts"], contact_field, justify=12) output_generic(gandi, result, output_keys, justify=12) return result
def info(gandi, resource): """Display information about a domain.""" output_keys = ['fqdn', 'nameservers', 'services', 'zone_id', 'tags'] contact_field = ['owner', 'admin', 'bill', 'tech', 'reseller'] result = gandi.domain.info(resource) output_contact_info(gandi, result['contacts'], contact_field, justify=12) output_generic(gandi, result, output_keys, justify=12) return result
def datacenters(gandi, id): """List available datacenters.""" output_keys = ['iso', 'name', 'country'] if id: output_keys.append('id') result = gandi.datacenter.list() for dc in result: gandi.separator_line() output_generic(gandi, dc, output_keys) return result
def list(gandi, domain, name, zone_id, output, format, limit): """List DNS zone records for a domain.""" options = { 'items_per_page': limit, } if name is not None: options.update({'~name': '%{}%'.format(name)}) output_keys = ['name', 'type', 'value', 'ttl'] if not zone_id: result = gandi.domain.info(domain) zone_id = result['zone_id'] if not zone_id: gandi.echo('No zone records found, domain %s doesn\'t seems to be ' 'managed at Gandi.' % domain) return records = gandi.record.list(zone_id, options) if not output and not format: for num, rec in enumerate(records): if num: gandi.separator_line() output_generic(gandi, rec, output_keys, justify=12) elif output: zone_filename = domain + "_" + str(zone_id) if os.path.isfile(zone_filename): open(zone_filename, 'w').close() for record in records: format_record = ('%s %s IN %s %s' % (record['name'], record['ttl'], record['type'], record['value'])) with open(zone_filename, 'ab') as zone_file: zone_file.write(format_record + '\n') gandi.echo('Your zone file have been writen in %s' % zone_filename) elif format: if format == 'text': for record in records: format_record = ('%s %s IN %s %s' % (record['name'], record['ttl'], record['type'], record['value'])) gandi.echo(format_record) if format == 'json': format_record = json.dumps(records, sort_keys=True, indent=4, separators=(',', ': ')) gandi.echo(format_record) return records
def datacenters(gandi, id): """List available datacenters.""" output_keys = ["iso", "name", "country"] if id: output_keys.append("id") result = gandi.datacenter.list() for num, dc in enumerate(result): if num: gandi.separator_line() output_generic(gandi, dc, output_keys) return result
def start(gandi, background, resource): """Start a virtual machine. Resource can be a Hostname or an ID """ output_keys = ['id', 'type', 'step'] opers = gandi.iaas.start(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def stop(gandi, background, resource): """Stop a virtual machine. Resource can be a Hostname or an ID """ output_keys = ['id', 'type', 'step'] opers = gandi.iaas.stop(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def migrate(gandi, resource, force, background): """ Migrate a disk to another datacenter. """ # check it's not attached source_info = gandi.disk.info(resource) if source_info['vms_id']: click.echo('Cannot start the migration: disk %s is attached. ' 'Please detach the disk before starting the migration.' % resource) return disk_datacenter = source_info['datacenter_id'] dc_choices = gandi.datacenter.list_migration_choice(disk_datacenter) if not dc_choices: click.echo('No datacenter is available for migration') return elif len(dc_choices) == 1: # use the only one available datacenter_id = dc_choices[0]['id'] else: choice_list = [dc['dc_code'] for dc in dc_choices] dc_choice = click.Choice(choice_list) dc_chosen = click.prompt( 'Select a datacenter [%s]' % '|'.join(choice_list), # noqa type=dc_choice, show_default=True) datacenter_id = [ dc['id'] for dc in dc_choices if dc['dc_code'] == dc_chosen ][0] if not force: proceed = click.confirm('Are you sure you want to migrate disk %s ?' % resource) if not proceed: return datacenters = gandi.datacenter.list() dc_from = [ dc['dc_code'] for dc in datacenters if dc['id'] == disk_datacenter ][0] dc_to = [dc['dc_code'] for dc in datacenters if dc['id'] == datacenter_id][0] migration_msg = ('* Starting the migration of disk %s from datacenter %s ' 'to %s' % (resource, dc_from, dc_to)) gandi.echo(migration_msg) output_keys = ['id', 'type', 'step'] oper = gandi.disk.migrate(resource, datacenter_id, background) if background: output_generic(gandi, oper, output_keys) return oper
def list(gandi, domain, zone_id): """List DNS zone records for a domain.""" output_keys = ['name', 'type', 'value', 'ttl'] if not zone_id: result = gandi.domain.info(domain) zone_id = result['zone_id'] records = gandi.record.list(zone_id) for rec in records: gandi.separator_line() output_generic(gandi, rec, output_keys, justify=12) return records
def start(gandi, background, resource): """Start a virtual machine. Resource can be a Hostname or an ID """ output_keys = ["id", "type", "step"] resource = tuple(set(resource)) opers = gandi.iaas.start(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def stop(gandi, background, resource): """Stop a virtual machine. Resource can be a Hostname or an ID """ output_keys = ['id', 'type', 'step'] resource = sorted(tuple(set(resource))) opers = gandi.iaas.stop(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
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 reboot(gandi, background, resource): """Reboot a virtual machine. Resource can be a Hostname or an ID """ output_keys = ['id', 'type', 'step'] resource = sorted(tuple(set(resource))) opers = gandi.iaas.reboot(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def info(gandi, resource, format): """ Display information about a webaccelerator """ result = gandi.webacc.info(resource) if format: output_json(gandi, format, result) return result output_base = { 'name': result['name'], 'algorithm': result['lb']['algorithm'], 'datacenter': result['datacenter']['name'], 'state': result['state'], 'ssl': 'Disable' if result['ssl_enable'] is False else 'Enabled' } output_keys = ['name', 'state', 'datacenter', 'ssl', 'algorithm'] output_generic(gandi, output_base, output_keys, justify=14) gandi.echo('Vhosts :') for vhost in result['vhosts']: output_vhosts = ['vhost', 'ssl'] vhost['vhost'] = vhost['name'] vhost['ssl'] = 'None' if vhost['cert_id'] is None else 'Exists' output_sub_generic(gandi, vhost, output_vhosts, justify=14) gandi.echo('') gandi.echo('Backends :') for server in result['servers']: try: ip = gandi.ip.info(server['ip']) iface = gandi.iface.info(ip['iface_id']) server['name'] = gandi.iaas.info(iface['vm_id'])['hostname'] output_servers = ['name', 'ip', 'port', 'state'] except Exception: warningmsg = ('\tBackend with ip address %s no longer exists.' '\n\tYou should remove it.' % server['ip']) gandi.echo(warningmsg) output_servers = ['ip', 'port', 'state'] output_sub_generic(gandi, server, output_servers, justify=14) gandi.echo('') gandi.echo('Probe :') output_probe = [ 'state', 'host', 'interval', 'method', 'response', 'threshold', 'timeout', 'url', 'window' ] result['probe']['state'] = ('Disable' if result['probe']['enable'] is False else 'Enabled') output_sub_generic(gandi, result['probe'], output_probe, justify=14) return result
def delete(gandi, resource, force, background): """ Delete a disk. """ output_keys = ['name', 'disk_id', 'state', 'date_creation'] if not force: proceed = click.confirm('Are you sure to delete disk %s?' % resource) if not proceed: return opers = gandi.disk.delete(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def delete(gandi, resource, force, background): """ Delete a vhost. """ output_keys = ['name', 'paas_id', 'state', 'date_creation'] if not force: proceed = click.confirm('Are you sure to delete vhost %s?' % resource) if not proceed: return opers = gandi.vhost.delete(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def list(gandi, limit): """List operations.""" output_keys = ['id', 'type', 'step'] options = { 'step': ['BILL', 'WAIT', 'RUN'], 'items_per_page': limit, } result = gandi.oper.list(options) for oper in result: gandi.separator_line() output_generic(gandi, oper, output_keys) return result
def migrate(gandi, resource, force, background): """ Migrate a disk to another datacenter. """ # check it's not attached source_info = gandi.disk.info(resource) if source_info['vms_id']: click.echo('Cannot start the migration: disk %s is attached. ' 'Please detach the disk before starting the migration.' % resource) return disk_datacenter = source_info['datacenter_id'] dc_choices = gandi.datacenter.list_migration_choice(disk_datacenter) if not dc_choices: click.echo('No datacenter is available for migration') return elif len(dc_choices) == 1: # use the only one available datacenter_id = dc_choices[0]['id'] else: choice_list = [dc['dc_code'] for dc in dc_choices] dc_choice = click.Choice(choice_list) dc_chosen = click.prompt('Select a datacenter [%s]' % '|'.join(choice_list), # noqa type=dc_choice, show_default=True) datacenter_id = [dc['id'] for dc in dc_choices if dc['dc_code'] == dc_chosen][0] if not force: proceed = click.confirm('Are you sure you want to migrate disk %s ?' % resource) if not proceed: return datacenters = gandi.datacenter.list() dc_from = [dc['dc_code'] for dc in datacenters if dc['id'] == disk_datacenter][0] dc_to = [dc['dc_code'] for dc in datacenters if dc['id'] == datacenter_id][0] migration_msg = ('* Starting the migration of disk %s from datacenter %s ' 'to %s' % (resource, dc_from, dc_to)) gandi.echo(migration_msg) output_keys = ['id', 'type', 'step'] oper = gandi.disk.migrate(resource, datacenter_id, background) if background: output_generic(gandi, oper, output_keys) return oper
def info(gandi, resource, format): """ Display information about a webaccelerator """ result = gandi.webacc.info(resource) if format: output_json(gandi, format, result) return result output_base = { 'name': result['name'], 'algorithm': result['lb']['algorithm'], 'datacenter': result['datacenter']['name'], 'state': result['state'], 'ssl': 'Disable' if result['ssl_enable'] is False else 'Enabled' } output_keys = ['name', 'state', 'datacenter', 'ssl', 'algorithm'] output_generic(gandi, output_base, output_keys, justify=14) gandi.echo('Vhosts :') for vhost in result['vhosts']: output_vhosts = ['vhost', 'ssl'] vhost['vhost'] = vhost['name'] vhost['ssl'] = 'None' if vhost['cert_id'] is None else 'Exists' output_sub_generic(gandi, vhost, output_vhosts, justify=14) gandi.echo('') gandi.echo('Backends :') for server in result['servers']: try: ip = gandi.ip.info(server['ip']) iface = gandi.iface.info(ip['iface_id']) server['name'] = gandi.iaas.info(iface['vm_id'])['hostname'] output_servers = ['name', 'ip', 'port', 'state'] except Exception: warningmsg = ('\tBackend with ip address %s no longer exists.' '\n\tYou should remove it.' % server['ip']) gandi.echo(warningmsg) output_servers = ['ip', 'port', 'state'] output_sub_generic(gandi, server, output_servers, justify=14) gandi.echo('') gandi.echo('Probe :') output_probe = ['state', 'host', 'interval', 'method', 'response', 'threshold', 'timeout', 'url', 'window'] result['probe']['state'] = ('Disable' if result['probe']['enable'] is False else 'Enabled') output_sub_generic(gandi, result['probe'], output_probe, justify=14) return result
def list(gandi, domain, zone_id, output, format, limit): """List DNS zone records for a domain.""" options = { 'items_per_page': limit, } output_keys = ['name', 'type', 'value', 'ttl'] if not zone_id: result = gandi.domain.info(domain) zone_id = result['zone_id'] if not zone_id: gandi.echo('No zone records found, domain %s doesn\'t seems to be ' 'managed at Gandi.' % domain) return records = gandi.record.list(zone_id, options) if not output and not format: for num, rec in enumerate(records): if num: gandi.separator_line() output_generic(gandi, rec, output_keys, justify=12) elif output: zone_filename = domain + "_" + str(zone_id) if os.path.isfile(zone_filename): open(zone_filename, 'w').close() for record in records: format_record = ('%s %s IN %s %s' % (record['name'], record['ttl'], record['type'], record['value'])) with open(zone_filename, 'ab') as zone_file: zone_file.write(format_record + '\n') gandi.echo('Your zone file have been writen in %s' % zone_filename) elif format: if format == 'text': for record in records: format_record = ('%s %s IN %s %s' % (record['name'], record['ttl'], record['type'], record['value'])) gandi.echo(format_record) if format == 'json': format_record = json.dumps(records, sort_keys=True, indent=4, separators=(',', ': ')) gandi.echo(format_record) return records
def list(gandi, limit, step): """List operations.""" output_keys = ['id', 'type', 'step'] options = { 'step': step, 'items_per_page': limit, 'sort_by': 'date_created DESC' } result = gandi.oper.list(options) for num, oper in enumerate(reversed(result)): if num: gandi.separator_line() output_generic(gandi, oper, output_keys) return result
def list(gandi, limit, format): """ List webaccelerators """ options = { 'items_per_page': limit, } result = gandi.webacc.list(options) if format: output_json(gandi, format, result) return result output_keys = ['name', 'state', 'ssl'] for num, webacc in enumerate(result): if num: gandi.separator_line('-', 4) webacc['ssl'] = 'Enabled' if webacc['ssl_enable'] else 'Disable' output_generic(gandi, webacc, output_keys, justify=14) gandi.echo('Vhosts :') for num, vhost in enumerate(webacc['vhosts']): output_vhosts = ['vhost', 'ssl'] vhost['vhost'] = vhost['name'] vhost['ssl'] = 'Disable' if vhost['cert_id'] is None else 'Enabled' output_sub_generic(gandi, vhost, output_vhosts, justify=14) gandi.echo('') gandi.echo('Backends :') for server in webacc['servers']: try: ip = gandi.ip.info(server['ip']) iface = gandi.iface.info(ip['iface_id']) vm_info = gandi.iaas.info(iface['vm_id']) server['name'] = vm_info['hostname'] output_servers = ['name', 'ip', 'port', 'state'] except Exception: warningmsg = ('\tBackend with ip address %s no longer \ exists.\n\tYou should remove it.' % server['ip']) gandi.echo(warningmsg) output_servers = ['ip', 'port', 'state'] output_sub_generic(gandi, server, output_servers, justify=14) gandi.echo('') return result
def delete(gandi, resource, force, background): """ Delete a disk. """ output_keys = ['name', 'disk_id', 'state', 'date_creation'] resource = tuple(set(resource)) if not force: disk_info = "'%s'" % ', '.join(resource) proceed = click.confirm('Are you sure to delete disk %s?' % disk_info) if not proceed: return opers = gandi.disk.delete(resource, background) if background: for oper in opers: output_generic(gandi, oper, output_keys) return opers
def delete(gandi, background, force, resource): """Delete a virtual machine. Resource can be a Hostname or an ID """ output_keys = ['id', 'type', 'step'] resource = sorted(tuple(set(resource))) possible_resources = gandi.iaas.resource_list() for item in resource: if item not in possible_resources: gandi.echo('Sorry virtual machine %s does not exist' % item) gandi.echo('Please use one of the following: %s' % possible_resources) return if not force: instance_info = "'%s'" % ', '.join(resource) proceed = click.confirm("Are you sure to delete Virtual Machine %s?" % instance_info) if not proceed: return iaas_list = gandi.iaas.list() stop_opers = [] for item in resource: vm = next((vm for (index, vm) in enumerate(iaas_list) if vm['hostname'] == item), gandi.iaas.info(item)) if vm['state'] == 'running': if background: gandi.echo('Virtual machine not stopped, background option ' 'disabled') background = False oper = gandi.iaas.stop(item, background) if not background: stop_opers.append(oper) opers = gandi.iaas.delete(resource, background) if background: for oper in stop_opers + opers: output_generic(gandi, oper, output_keys) return opers
def migrate(gandi, resource, force, background, finalize): """ Migrate a virtual machine to another datacenter. """ if not gandi.iaas.check_can_migrate(resource): return if not force: proceed = click.confirm('Are you sure you want to migrate VM %s ?' % resource) if not proceed: return if finalize: gandi.iaas.need_finalize(resource) output_keys = ['id', 'type', 'step'] oper = gandi.iaas.migrate(resource, background, finalize=finalize) if background: output_generic(gandi, oper, output_keys) return oper