def everything(ctx): """Destroy everything you own.""" click.confirm(" Are you sure you want to destroy your entire lab?", abort=True) click.confirm( " Really sure? You cannot undo this, just like you cannot un-bake a cake.", abort=True) consume_task(ctx.obj.vlab_api, endpoint='/api/1/inf/power', body={ 'machine': 'all', 'power': 'off' }, message='Powering down your lab', timeout=300, pause=5) consume_task(ctx.obj.vlab_api, endpoint='/api/1/inf/inventory', message='Destroying inventory', method='DELETE') resp = consume_task(ctx.obj.vlab_api, endpoint='/api/2/inf/vlan', message='Determining what networks you own', method='GET') vlans = resp.json()['content'] tasks = {} with Spinner('Deleting networks'): for vlan in vlans.keys(): resp = ctx.obj.vlab_api.delete('/api/2/inf/vlan', json={'vlan-name': vlan}) tasks[vlan] = resp.links['status']['url'] block_on_tasks(ctx.obj.vlab_api, tasks, pause=1)
def delete_cluster(vlab_api, cluster): """Destroy an entire OneFS cluster""" data = consume_task(vlab_api, endpoint='/api/2/inf/onefs', message='Looking up OneFS cluster {}'.format(cluster), method='GET').json() nodes = _find_cluster_nodes(cluster, all_nodes=data['content'].keys()) if not nodes: raise click.ClickException('No cluster named {} found'.format(cluster)) tasks = {} with Spinner("Deleting cluster {}".format(cluster)): for node in nodes: body = {'name': node} resp = vlab_api.delete('/api/2/inf/onefs', json=body) tasks[node] = '/api/2/inf/onefs/task/{}'.format( resp.json()['content']['task-id']) block_on_tasks(vlab_api, tasks) with Spinner('Deleting port mapping rules'): for node in nodes: all_ports = vlab_api.get('/api/1/ipam/portmap', params={ 'name': node }).json()['content']['ports'] for port in all_ports.keys(): vlab_api.delete('/api/1/ipam/portmap', json={'conn_port': int(port)}) click.echo('OK!')
def init_lab(vlab_api, username, wan, switch, config, log): """Initialize the inventory, default networks, and gateway/firewall :Returns: None :param vlab_api: A valid API connection to vLab :type vlab_api: vlab_cli.lib.api.vLabApi :param username: The name of the user deleting their lab :type username: String :param switch: The name of the network switch their networks are connected to :type switch: String :param config: The parsed configuration file used by the vLab CLI :type config: configparser.ConfigParser :param log: A logging object :type log: logging.Logger """ if not config: bad_config = True elif set(config.sections()) != CONFIG_SECTIONS: bad_config = True else: bad_config = False if bad_config: try: new_info = invoke_config() except Exception as doh: log.debug(doh, exc_info=True) raise click.ClickException(doh) else: set_config(new_info) with Spinner('Initializing your lab'): tasks = {} resp1 = vlab_api.post('/api/1/inf/inventory', auto_check=False) tasks['inventory'] = resp1.links['status']['url'] body2 = {'vlan-name': 'frontend', 'switch-name': switch} resp2 = vlab_api.post('/api/2/inf/vlan', json=body2) tasks['frontend_network'] = resp2.links['status']['url'] body3 = {'vlan-name': 'backend', 'switch-name': switch} resp3 = vlab_api.post('/api/2/inf/vlan', json=body3) tasks['backend_network'] = resp3.links['status']['url'] block_on_tasks(vlab_api, tasks, auto_check=False, pause=1) body4 = {'wan': wan, 'lan': 'frontend'.format(username)} consume_task(vlab_api, endpoint='/api/2/inf/gateway', message='Deploying gateway', method='POST', body=body4, timeout=1500, pause=5, auto_check=False) invoke_init_done_help()
def nuke_lab(vlab_api, username, wan, switch, config, log): """Delete all VMs and Networks a user owns, and create a new one. :Returns: None :param vlab_api: A valid API connection to vLab :type vlab_api: vlab_cli.lib.api.vLabApi :param username: The name of the user deleting their lab :type username: String :param switch: The name of the network switch their networks are connected to :type switch: String :param wan: The name of the Wide Area Network their new lab should connect to :type wan: String :param config: The parsed configuration file used by the vLab CLI :type config: configparser.ConfigParser :param log: A logging object :type log: logging.Logger """ consume_task(vlab_api, endpoint='/api/1/inf/power', body={ 'machine': 'all', 'power': 'off' }, message='Powering down your lab', timeout=300, pause=5) consume_task(vlab_api, endpoint='/api/1/inf/inventory', message='Destroying inventory', method='DELETE') resp = consume_task(vlab_api, endpoint='/api/2/inf/vlan', message='Determining what networks you own', method='GET') vlans = resp.json()['content'] tasks = {} with Spinner('Deleting networks'): for vlan in vlans.keys(): resp = vlab_api.delete('/api/2/inf/vlan', json={'vlan-name': vlan}) tasks[vlan] = resp.links['status']['url'] block_on_tasks(vlab_api, tasks, pause=1) typewriter('Finished deleting old lab. Initializing a new lab.') init_lab(vlab_api, username, wan, switch, config=config, log=log)
def create_nodes(username, name, image, external, internal, node_count, ram, cpu_count, vlab_api): """Concurrently make all nodes :Returns: Dictionary :param username: The user who owns the new nodes :type username: String :param image: The image/version of OneFS node to create :type image: String :param external: The base name of the external network to connect the node(s) to :type external: String :param internal: The base name of the internal network to connect the node(s) to :type external: String :param node_count: The number of OneFS nodes to make :type node_count: Integer :param ram: The number of GB of ram/memory to create a OneFS node with :type ram: Integer :param cpu_count: The number of CPU cores to allocate to the vOneFS node :type cpu_count: Integer :param vlab_api: An instantiated connection to the vLab server :type vlab_api: vlab_cli.lib.api.vLabApi """ tasks = {} node_v_nodes = 'node' if node_count == 1 else 'nodes' with Spinner('Deploying {} {} running {}'.format(node_count, node_v_nodes, image)): for idx in range(node_count): node_name = '{}-{}'.format(name, idx +1) # +1 so we don't have node-0 body = {'name' : node_name, 'image': image, 'frontend': external, 'backend': internal, 'ram': ram, 'cpu-count': cpu_count, } resp = vlab_api.post('/api/2/inf/onefs', json=body) tasks[node_name] = '/api/2/inf/onefs/task/{}'.format(resp.json()['content']['task-id']) info = block_on_tasks(vlab_api, tasks) return info