def main(profile, domain_name, cluster_name, pod_name, zone_name, keyword_filter, only_routers, only_routers_to_be_upgraded, no_routers, router_nic_count, nic_count_is_minimum, nic_count_is_maximum, router_max_version, router_min_version, project_name, only_project, ignore_domains, calling_credentials, only_summary, no_summary, log_file): """List VMs""" click_log.basic_config() if log_file: logger = logging.getLogger() logger.addHandler(log_module.FileHandler(log_file)) if project_name and domain_name: logging.error("The project and domain options can't be used together") sys.exit(1) co = CosmicOps(profile=profile, dry_run=False) if ignore_domains: ignore_domains = ignore_domains.replace(' ', '').split(',') logging.info(f"Ignoring domains: {str(ignore_domains)}") if calling_credentials: table_headers = [ 'VM', 'Storage', 'Template', 'Memory', 'Cores', 'Instance', 'Host', 'Domain', 'Account', 'Created', 'LastRebootVersion' ] table_data = [] if only_project: vms = co.get_all_project_vms(list_all=False) else: vms = co.get_all_vms(list_all=False) with click_spinner.spinner(): for vm in vms: if vm['domain'] in ignore_domains: continue storage_size = sum([volume['size'] for volume in vm.get_volumes()]) project_name = vm.get('project', None) vm_account = f"Proj: {project_name}" if project_name else vm['account'] table_data.append([ vm['name'], humanfriendly.format_size(storage_size, binary=True), vm['templatedisplaytext'], humanfriendly.format_size(vm['memory'] * 1024 ** 2, binary=True), vm['cpunumber'], vm['instancename'], vm['hostname'], vm['domain'], vm_account, vm['created'], vm['laststartversion'] ]) logging.info(tabulate(table_data, headers=table_headers, tablefmt='pretty')) sys.exit(0) if domain_name: domain = co.get_domain(name=domain_name) if domain is None or domain == []: logging.error(f"The domain '{str(domain_name)}' could not be found!") sys.exit(1) else: domain = None if project_name: project = co.get_project(name=project_name) if project is None or project == []: logging.error(f"The project '{str(project_name)}' could not be found!") sys.exit(1) else: project = None if pod_name: pod = co.get_pod(name=pod_name) if pod is None or pod == []: logging.error(f"The pod '{str(pod_name)}' could not be found!") sys.exit(1) else: pod = None if zone_name: zone = co.get_zone(name=zone_name) if zone is None or zone == []: logging.error(f"The zone '{str(zone_name)}' could not be found!") sys.exit(1) else: zone = None if cluster_name: clusters = [co.get_cluster(name=cluster_name)] if clusters[0] is None: logging.error(f"The cluster '{str(cluster_name)}' could not be found!") sys.exit(1) elif pod: clusters = co.get_all_clusters(pod=pod) elif zone: clusters = co.get_all_clusters(zone=zone) else: clusters = co.get_all_clusters() total_host_counter = 0 total_vm_counter = 0 total_host_memory = 0 total_vm_memory = 0 total_storage = 0 total_cores = 0 for cluster in clusters: hosts = cluster.get_all_hosts() if not hosts: logging.warning(f"No hosts found on cluster '{cluster['name']}'") continue cluster_host_counter = 0 cluster_vm_counter = 0 cluster_host_memory = 0 cluster_vm_memory = 0 cluster_storage = 0 cluster_cores = 0 cluster_table_headers = [ 'VM', 'Storage', 'Template', 'Router nic count', 'Router version', 'Memory', 'Cores', 'Instance', 'Host', 'Domain', 'Account', 'Created', 'LastRebootVersion' ] cluster_table_data = [] for host in hosts: cluster_host_counter += 1 cluster_host_memory += host['memorytotal'] if not only_routers: if project or only_project: vms = host.get_all_project_vms(project=project) else: vms = host.get_all_vms(domain=domain, keyword_filter=keyword_filter) for vm in vms: if vm['domain'] in ignore_domains: continue cluster_vm_counter += 1 storage_size = sum([volume['size'] for volume in vm.get_volumes()]) cluster_storage += storage_size cluster_vm_memory += vm['memory'] cluster_cores += vm['cpunumber'] vm_project_name = vm.get('project', None) vm_account = f"Proj: {vm['project']}" if vm_project_name else vm['account'] cluster_table_data.append([ vm['name'], humanfriendly.format_size(storage_size, binary=True), vm['templatedisplaytext'], '-', '-', humanfriendly.format_size(vm['memory'] * 1024 ** 2, binary=True), vm['cpunumber'], vm['instancename'], vm['hostname'], vm['domain'], vm_account, vm['created'], vm['laststartversion'] ]) if no_routers: continue if project or only_project: routers = host.get_all_project_routers(project=project) else: routers = host.get_all_routers(domain=domain) for router in routers: if router['domain'] in ignore_domains: continue if router_min_version and LooseVersion(router['version']) < LooseVersion(router_min_version): continue if router_max_version and LooseVersion(router['version']) > LooseVersion(router_max_version): continue if router_nic_count and nic_count_is_minimum: if router_nic_count > len(router['nic']): continue elif router_nic_count and nic_count_is_maximum: if router_nic_count < len(router['nic']): continue elif router_nic_count: if router_nic_count != len(router['nic']): continue if only_routers_to_be_upgraded and not router['requiresupgrade']: continue cluster_vm_counter += 1 service_offering = co.get_service_offering(id=router['serviceofferingid'], system=True) if service_offering: router['memory'] = service_offering['memory'] router['cpunumber'] = service_offering['cpunumber'] cluster_vm_memory += router['memory'] cluster_cores += router['cpunumber'] else: router['memory'] = 'Unknown' router['cpunumber'] = 'Unknown' if router['isredundantrouter']: redundant_state = router['redundantstate'] elif router['vpcid']: redundant_state = 'VPC' else: redundant_state = 'SINGLE' if router['vpcid']: network = co.get_vpc(id=router['vpcid']) else: network = co.get_network(id=router['guestnetworkid']) if network: display_name = network['name'] else: display_name = router['name'] display_name = f"{display_name} ({redundant_state.lower()})" if router['requiresupgrade']: display_name = f"{display_name} [ReqUpdate!]" router_project_name = router.get('project', None) router_account = f"Proj: {router['project']}" if router_project_name else router['account'] cluster_table_data.append([ display_name, '-', '-', len(router['nic']), router['version'], humanfriendly.format_size(router['memory'] * 1024 ** 2, binary=True) if router[ 'memory'] != 'Unknown' else router['memory'], router['cpunumber'], router['name'], router['hostname'], router['domain'], router_account, router['created'], router['laststartversion'] ]) total_host_counter += cluster_host_counter total_host_memory += cluster_host_memory total_vm_memory += cluster_vm_memory total_vm_counter += cluster_vm_counter total_storage += cluster_storage total_cores += cluster_cores if not only_summary: # pragma: no cover logging.info(tabulate(cluster_table_data, headers=cluster_table_headers, tablefmt='pretty')) if not no_summary: # pragma: no cover logging.info(f"\nSummary for '{cluster['name']}':") logging.info(f"Number of VMs: {cluster_vm_counter}") logging.info(f"Number of hosts: {cluster_host_counter}") logging.info( f"Allocated memory: {humanfriendly.format_size(cluster_vm_memory * 1024 ** 2, binary=True)} / {humanfriendly.format_size(cluster_host_memory, binary=True)}") logging.info(f"Allocated cores: {cluster_cores}") logging.info(f"Allocated storage: {humanfriendly.format_size(cluster_storage, binary=True)}") if not no_summary: # pragma: no cover logging.info('\n================== Grand Totals ===============') logging.info(f"Total number of VMs: {total_vm_counter}") logging.info(f"Total number of hosts: {total_host_counter}") logging.info( f"Total allocated memory: {humanfriendly.format_size(total_vm_memory * 1024 ** 2, binary=True)} / {humanfriendly.format_size(total_host_memory, binary=True)}") logging.info(f"Total allocated cores: {total_cores}") logging.info(f"Total allocated storage: {humanfriendly.format_size(total_storage, binary=True)}")
def main(profile, is_project_vm, dry_run, vm, cluster, destination_dc, destination_so): """Offline migrate VM to CLUSTER""" click_log.basic_config() log_to_slack = True logging.task = 'Offline Migrate VM' logging.slack_title = 'Domain' if dry_run: log_to_slack = False logging.warning('Running in dry-run mode, will only show changes') co = CosmicOps(profile=profile, dry_run=dry_run, log_to_slack=log_to_slack) cs = CosmicSQL(server=profile, dry_run=dry_run) target_cluster = co.get_cluster(name=cluster) if not target_cluster: sys.exit(1) vm = co.get_vm(name=vm, is_project_vm=is_project_vm) if not vm: sys.exit(1) if destination_dc and destination_dc not in DATACENTERS: logging.error(f"Unknown datacenter '{destination_dc}', should be one of {str(DATACENTERS)}") sys.exit(1) logging.instance_name = vm['instancename'] logging.slack_value = vm['domain'] logging.vm_name = vm['name'] logging.zone_name = vm['zonename'] target_storage_pool = None try: # Get CLUSTER scoped volume (no NVMe or ZONE-wide) while target_storage_pool is None or target_storage_pool['scope'] != 'CLUSTER': target_storage_pool = choice(target_cluster.get_storage_pools()) except IndexError: logging.error(f"No storage pools found for cluster '{target_cluster['name']}") sys.exit(1) if vm['state'] == 'Running': need_to_stop = True auto_start_vm = True else: need_to_stop = False auto_start_vm = False if destination_dc: for datacenter in DATACENTERS: if datacenter == destination_dc: continue if datacenter in vm['serviceofferingname']: new_offering = vm['serviceofferingname'].replace(datacenter, destination_dc) logging.info( f"Replacing '{vm['serviceofferingname']}' with '{new_offering}'") cs.update_service_offering_of_vm(vm['instancename'], new_offering) vm = co.get_vm(name=vm['instancename'], is_project_vm=is_project_vm) break if destination_so: logging.info( f"Replacing '{vm['serviceofferingname']}' with '{destination_so}'") cs.update_service_offering_of_vm(vm['instancename'], destination_so) vm = co.get_vm(name=vm['instancename'], is_project_vm=is_project_vm) vm_service_offering = co.get_service_offering(id=vm['serviceofferingid']) if vm_service_offering: storage_tags = vm_service_offering['tags'] if 'tags' in vm_service_offering else '' if not storage_tags: logging.warning('VM service offering has no storage tags') else: if storage_tags not in target_storage_pool['tags']: logging.error( f"Can't migrate '{vm['name']}', storage tags on target cluster ({target_storage_pool['tags']}) to not contain the tags on the VM's service offering ({storage_tags})'") sys.exit(1) if need_to_stop: if not vm.stop(): sys.exit(1) volumes = vm.get_volumes() for volume in volumes: if volume['storage'] == target_storage_pool['name']: logging.warning( f"Volume '{volume['name']}' ({volume['id']}) already on cluster '{target_cluster['name']}', skipping...") continue source_storage_pool = co.get_storage_pool(name=volume['storage']) if not source_storage_pool: sys.exit(1) if source_storage_pool['scope'] == 'ZONE': logging.warning(f"Scope of volume '{volume['name']}' ({volume['id']}) is ZONE, skipping...") continue if not volume.migrate(target_storage_pool): sys.exit(1) with click_spinner.spinner(): while True: volume.refresh() if volume['state'] == 'Ready': break logging.warning( f"Volume '{volume['name']}' ({volume['id']}) is in '{volume['state']}' state instead of 'Ready', sleeping...") time.sleep(60) if auto_start_vm: destination_host = target_cluster.find_migration_host(vm) if not destination_host: sys.exit(1) if not vm.start(destination_host): sys.exit(1) else: logging.info(f"Not starting VM '{vm['name']}' as it was not running", log_to_slack)
class TestCosmicOps(TestCase): def setUp(self): cs_patcher = patch('cosmicops.ops.CloudStack') self.mock_cs = cs_patcher.start() self.addCleanup(cs_patcher.stop) self.cs_instance = self.mock_cs.return_value slack_patcher = patch('cosmicops.log.Slack') self.mock_slack = slack_patcher.start() self.addCleanup(slack_patcher.stop) sleep_patcher = patch('time.sleep', return_value=None) self.mock_sleep = sleep_patcher.start() self.addCleanup(sleep_patcher.stop) self.co = CosmicOps(endpoint='https://localhost', key='key', secret='secret') @patch('cosmicops.ops._load_cloud_monkey_profile') def test_init_with_profile(self, mock_load): mock_load.return_value = ('profile_endpoint', 'profile_key', 'profile_secret') CosmicOps(profile='config') self.mock_cs.assert_called_with('profile_endpoint', 'profile_key', 'profile_secret', 60) @tempdir() def test_load_cloud_monkey_profile(self, tmp): config = (b"[testprofile]\n" b"url = http://localhost:8000/client/api\n" b"apikey = test_api_key\n" b"secretkey = test_secret_key\n") tmp.makedir('.cloudmonkey') tmp.write('.cloudmonkey/config', config) with patch('pathlib.Path.home') as path_home_mock: path_home_mock.return_value = Path(tmp.path) (endpoint, key, secret) = _load_cloud_monkey_profile('testprofile') self.assertEqual('http://localhost:8000/client/api', endpoint) self.assertEqual('test_api_key', key) self.assertEqual('test_secret_key', secret) @tempdir() def test_load_cloud_monkey_profile_with_default(self, tmp): config = (b"[core]\n" b"profile = profile2\n\n" b"[profile1]\n" b"url = http://localhost:8000/client/api/1\n" b"apikey = test_api_key_1\n" b"secretkey = test_secret_key_1\n\n" b"[profile2]\n" b"url = http://localhost:8000/client/api/2\n" b"apikey = test_api_key_2\n" b"secretkey = test_secret_key_2\n") tmp.makedir('.cloudmonkey') tmp.write('.cloudmonkey/config', config) with patch('pathlib.Path.home') as path_home_mock: path_home_mock.return_value = Path(tmp.path) (endpoint, key, secret) = _load_cloud_monkey_profile('config') self.assertEqual('http://localhost:8000/client/api/2', endpoint) self.assertEqual('test_api_key_2', key) self.assertEqual('test_secret_key_2', secret) def test_cs_get_single_result(self): self.cs_instance.listFunction.return_value = [{ 'id': 'id_field', 'name': 'name_field' }] result = self.co._cs_get_single_result('listFunction', {'name': 'name_field'}, CosmicObject, 'type') self.cs_instance.listFunction.assert_called_with(fetch_list=True, name='name_field') self.assertIsInstance(result, CosmicObject) self.assertDictEqual({ 'id': 'id_field', 'name': 'name_field' }, result._data) def test_get_get_single_result_failure(self): self.cs_instance.listFunction.return_value = [] self.assertIsNone( self.co._cs_get_single_result('listFunction', {}, CosmicObject, 'type')) self.cs_instance.listFunction.return_value = [{}, {}] self.assertIsNone( self.co._cs_get_single_result('listFunction', {}, CosmicObject, 'type')) def test_cs_get_all_results(self): self.cs_instance.listFunction.return_value = [{ 'id': 'id1', 'name': 'name1' }, { 'id': 'id2', 'name': 'name2' }] result = self.co._cs_get_all_results('listFunction', {}, CosmicObject, 'type') self.cs_instance.listFunction.assert_called_with(fetch_list=True) for i, item in enumerate(result): self.assertIsInstance(item, CosmicObject) self.assertDictEqual({ 'id': f'id{i + 1}', 'name': f'name{i + 1}' }, item._data) def test_get_vm(self): self.co._cs_get_single_result = Mock() self.co.get_vm(name='vm1') self.assertDictEqual({ 'name': 'vm1', 'listall': True }, self.co._cs_get_single_result.call_args[0][1]) self.co.get_vm(name='i-VM-1') self.assertDictEqual({ 'keyword': 'i-VM-1', 'listall': True }, self.co._cs_get_single_result.call_args[0][1]) self.co.get_vm(name='project_vm1', is_project_vm=True) self.assertDictEqual( { 'name': 'project_vm1', 'projectid': '-1', 'listall': True }, self.co._cs_get_single_result.call_args[0][1]) def test_get_project_vm(self): self.co._cs_get_single_result = Mock() self.co.get_project_vm(name='project_vm1') self.assertDictEqual( { 'name': 'project_vm1', 'projectid': '-1', 'listall': True }, self.co._cs_get_single_result.call_args[0][1]) def test_get_router(self): self.co._cs_get_single_result = Mock() self.co.get_router(name='router1') self.assertDictEqual({ 'name': 'router1', 'listall': True }, self.co._cs_get_single_result.call_args[0][1]) self.co.get_router(name='project_router1', is_project_router=True) self.assertDictEqual( { 'name': 'project_router1', 'projectid': '-1', 'listall': True }, self.co._cs_get_single_result.call_args[0][1]) def test_get_cluster(self): self.co._cs_get_single_result = Mock() self.co.get_zone = Mock( return_value=CosmicZone(Mock(), { 'id': 'z1', 'name': 'zone1' })) self.co.get_cluster(name='cluster1') self.assertDictEqual({'name': 'cluster1'}, self.co._cs_get_single_result.call_args[0][1]) self.co.get_cluster(name='cluster1', zone='zone1') self.assertDictEqual({ 'name': 'cluster1', 'zoneid': 'z1' }, self.co._cs_get_single_result.call_args[0][1]) self.co.get_zone.return_value = None self.assertIsNone(self.co.get_cluster(name='cluster1', zone='zone1')) def test_get_all_clusters(self): self.co._cs_get_all_results = Mock() self.co.get_all_clusters() self.assertDictEqual({}, self.co._cs_get_all_results.call_args[0][1]) self.co.get_all_clusters(zone=CosmicZone(Mock(), {'id': 'z1'})) self.assertDictEqual({'zoneid': 'z1'}, self.co._cs_get_all_results.call_args[0][1]) self.co.get_all_clusters(pod=CosmicPod(Mock(), {'id': 'p1'})) self.assertDictEqual({'podid': 'p1'}, self.co._cs_get_all_results.call_args[0][1]) def test_get_service_offering(self): self.co._cs_get_single_result = Mock() self.co.get_service_offering(name='so1') self.assertDictEqual({'name': 'so1'}, self.co._cs_get_single_result.call_args[0][1]) self.co.get_service_offering(name='so1', system=True) self.assertDictEqual({ 'name': 'so1', 'issystem': True }, self.co._cs_get_single_result.call_args[0][1]) def test_get_all_vms(self): self.co._cs_get_all_results = Mock() self.co.get_all_vms() self.assertDictEqual({'listall': True}, self.co._cs_get_all_results.call_args[0][1]) def test_get_all_project_vms(self): self.co._cs_get_all_results = Mock() self.co.get_all_project_vms() self.assertDictEqual({ 'projectid': '-1', 'listall': True }, self.co._cs_get_all_results.call_args[0][1]) def test_wait_for_job(self): self.cs_instance.queryAsyncJobResult.return_value = {'jobstatus': '1'} self.assertTrue(self.co.wait_for_job('job')) def test_wait_for_job_cloud_stack_exception(self): self.cs_instance.queryAsyncJobResult.side_effect = CloudStackException( response=Mock()) self.assertRaises(CloudStackException, self.co.wait_for_job, 'job') def test_wait_for_job_connection_error(self): self.cs_instance.queryAsyncJobResult.side_effect = ConnectionError self.assertRaises(ConnectionError, self.co.wait_for_job, 'job') def test_wait_for_job_retries(self): self.cs_instance.queryAsyncJobResult.side_effect = [ CloudStackException('multiple JSON fields named jobstatus', response=Mock()), ConnectionError('Connection aborted'), ConnectionError('Connection aborted') ] self.assertFalse(self.co.wait_for_job('job', 3)) def test_wait_for_job_failure(self): self.cs_instance.queryAsyncJobResult.return_value = {'jobstatus': '2'} self.assertFalse(self.co.wait_for_job('job'))