def clusters(cls, session): """ Method will prompt user to select a cluster Args: session (obj): Auth session object Returns: cluster (str): Name of selected cluster """ clusters_choices = Query.create_container( session, session.content.rootFolder, [vim.ComputeResource], True ) clusters = Query.list_obj_attrs(clusters_choices, 'name') clusters.sort() for num, opt in enumerate(clusters, start=1): print('%s: %s' % (num, opt)) while True: val = int(raw_input('\nPlease select number: ').strip()) if int(val) <= len(clusters): # need to substract 1 since we start enumeration at 1. val = int(val) - 1 selected_cluster = clusters[val] break else: print('Invalid number.') continue cls.logger.info(selected_cluster) return selected_cluster
def clusters(cls, session): """ Method will prompt user to select a cluster Args: session (obj): Auth session object Returns: cluster (str): Name of selected cluster """ clusters_choices = Query.create_container(session, session.content.rootFolder, [vim.ComputeResource], True) clusters = Query.list_obj_attrs(clusters_choices, 'name') clusters.sort() for num, opt in enumerate(clusters, start=1): print('%s: %s' % (num, opt)) while True: val = int(raw_input('\nPlease select number: ').strip()) if int(val) <= len(clusters): # need to substract 1 since we start enumeration at 1. val = int(val) - 1 selected_cluster = clusters[val] break else: print('Invalid number.') continue cls.logger.info(selected_cluster) return selected_cluster
def main(self): """ This is the main method, which parses all the argparse options and runs the necessary code blocks if True. """ try: call_count = 0 self.auth = Auth(self.opts.host) self.auth.login( self.opts.user, self.opts.passwd, self.opts.domain, self.opts.passwd_file ) self.opts.passwd = None self.logger.debug(self.opts) virtual_machines_container = Query.create_container( self.auth.session, self.auth.session.content.rootFolder, [vim.VirtualMachine], True ) self.vmcfg = VMConfigHelper(self.auth, self.opts, argparser.dotrc) self.clustercfg = ClusterConfig(self.auth, self.opts, argparser.dotrc) call_count = self.auth.session.content.sessionManager.currentSession.callCount if not self.opts.datacenter: self.opts.datacenter = Prompts.datacenters(self.auth.session) if self.opts.cmd == 'create': if self.opts.config: for cfg in self.opts.config: spec = self.vmcfg.dict_merge( argparser.dotrc, yaml.load(cfg, Loader=yaml.FullLoader) ) cfgcheck_update = CfgCheck.cfg_checker(spec, self.auth, self.opts) spec['vmconfig'].update( self.vmcfg.dict_merge(spec['vmconfig'], cfgcheck_update) ) spec = self.vmcfg.pre_create_hooks(**spec) spec = self.vmcfg.create_wrapper(**spec) self.vmcfg.post_create_hooks(**spec) filename = spec['vmconfig']['name'] + '.yaml' server_cfg = {} server_cfg['vmconfig'] = {} server_cfg['vmconfig'].update(spec['vmconfig']) if spec.get('mkbootiso', None): server_cfg['mkbootiso'] = {} server_cfg['mkbootiso'].update(spec['mkbootiso']) print( yaml.dump(server_cfg, default_flow_style=False), file=open(os.path.join(os.environ['OLDPWD'], filename), 'w') ) if self.opts.cmd == 'mount': self.vmcfg.mount_wrapper(self.opts.datastore, self.opts.path, *self.opts.name) if self.opts.cmd == 'power': self.vmcfg.power_wrapper(self.opts.power, *self.opts.name) if self.opts.cmd == 'umount': self.vmcfg.umount_wrapper(*self.opts.name) if self.opts.cmd == 'upload': self.vmcfg.upload_wrapper( self.opts.datastore, self.opts.dest, self.opts.verify_ssl, *self.opts.iso ) if self.opts.cmd == 'add': hostname = Query.get_obj(virtual_machines_container.view, self.opts.name) # nics if self.opts.device == 'nic': self.vmcfg.add_nic_recfg(hostname) if self.opts.cmd == 'reconfig': host = Query.get_obj(virtual_machines_container.view, self.opts.name) if self.opts.cfgs: self.logger.info( 'reconfig: %s cfgs: %s', host.name, ' '.join('%s=%s' % (k, v) for k, v in self.opts.cfgs.items()) ) self.vmcfg.reconfig(host, **self.opts.cfgs) if self.opts.folder: self.vmcfg.folder_recfg() if self.opts.device == 'disk': self.vmcfg.disk_recfg() if self.opts.device == 'nic': self.vmcfg.nic_recfg() if self.opts.cmd == 'drs': if not self.opts.cluster: self.opts.cluster = Prompts.clusters(self.auth.session) self.clustercfg.drs_rule() if self.opts.cmd == 'query': datacenters_container = Query.create_container( self.auth.session, self.auth.session.content.rootFolder, [vim.Datacenter], True ) clusters_container = Query.create_container( self.auth.session, self.auth.session.content.rootFolder, [vim.ClusterComputeResource], True ) if self.opts.anti_affinity_rules: if self.opts.cluster: anti_affinity_rules = Query.return_anti_affinity_rules( clusters_container.view, self.opts.cluster ) else: cluster = Prompts.clusters(self.auth.session) anti_affinity_rules = Query.return_anti_affinity_rules( clusters_container.view, cluster ) if not anti_affinity_rules: print('No antiaffinity rules defined.') else: print('Antiaffinity rules:') for key, val in sorted(anti_affinity_rules.items()): print('{0}: {1}'.format(key, ' '.join(sorted(val)))) if self.opts.datastores: if self.opts.cluster: datastores = Query.return_datastores( clusters_container.view, self.opts.cluster ) else: cluster = Prompts.clusters(self.auth.session) datastores = Query.return_datastores(clusters_container.view, cluster) for row in datastores: print('{0:30}\t{1:10}\t{2:10}\t{3:6}\t{4:10}\t{5:6}'.format(*row)) if self.opts.folders: if self.opts.datacenter: folders = Query.list_vm_folders( datacenters_container.view, self.opts.datacenter ) folders.sort() for folder in folders: print(folder) else: datacenter = Prompts.datacenters(self.auth.session) folders = Query.list_vm_folders(datacenters_container.view, datacenter) folders.sort() for folder in folders: print(folder) if self.opts.clusters: clusters = Query.list_obj_attrs(clusters_container, 'name') clusters.sort() for cluster in clusters: print(cluster) if self.opts.networks: if self.opts.cluster: cluster = Query.get_obj(clusters_container.view, self.opts.cluster) networks = Query.list_obj_attrs(cluster.network, 'name', view=False) networks.sort() for net in networks: print(net) else: cluster_name = Prompts.clusters(self.auth.session) cluster = Query.get_obj(clusters_container.view, cluster_name) networks = Query.list_obj_attrs(cluster.network, 'name', view=False) networks.sort() for net in networks: print(net) if self.opts.vms: vms = Query.list_vm_info(datacenters_container.view, self.opts.datacenter) for key, value in vms.items(): print(key, value) if self.opts.vmconfig: for name in self.opts.vmconfig: virtmachine = Query.get_obj(virtual_machines_container.view, name) self.logger.debug(virtmachine.config) if self.opts.createcfg: print( yaml.dump( Query.vm_config( virtual_machines_container.view, name, self.opts.createcfg ), default_flow_style=False ) ) else: print( yaml.dump( Query.vm_config(virtual_machines_container.view, name), default_flow_style=False ) ) if self.opts.vm_by_datastore: if self.opts.cluster and self.opts.datastore: vms = Query.vm_by_datastore( clusters_container.view, self.opts.cluster, self.opts.datastore ) for vm_name in vms: print(vm_name) else: if not self.opts.cluster: cluster = Prompts.clusters(self.auth.session) if not self.opts.datastore: datastore = Prompts.datastores(self.auth.session, cluster) print() vms = Query.vm_by_datastore(clusters_container.view, cluster, datastore) for vm_name in vms: print(vm_name) if self.opts.vm_guest_ids: for guest_id in Query.list_guestids(): print(guest_id) self.auth.logout() self.logger.debug('Call count: {0}'.format(call_count)) except ValueError as err: self.logger.error(err, exc_info=False) self.auth.logout() self.logger.debug('Call count: {0}'.format(call_count)) sys.exit(3) except vim.fault.InvalidLogin as loginerr: self.logger.error(loginerr.msg, exc_info=False) sys.exit(2) except ssl.CertificateError as err: self.logger.error(err, exc_info=False) sys.exit(2) except KeyboardInterrupt as err: self.logger.error(err, exc_info=False) self.auth.logout() self.logger.debug('Call count: {0}'.format(call_count)) sys.exit(1)
def main(self): """ This is the main method, which parses all the argparse options and runs the necessary code blocks if True. """ try: self.auth = Auth(self.opts.host) self.auth.login(self.opts.user, self.opts.passwd, self.opts.domain, self.opts.passwd_file) self.opts.passwd = None self.logger.debug(self.opts) virtual_machines_container = Query.create_container( self.auth.session, self.auth.session.content.rootFolder, [vim.VirtualMachine], True) self.vmcfg = VMConfigHelper(self.auth, self.opts, argparser.dotrc) self.clustercfg = ClusterConfig(self.auth, self.opts, argparser.dotrc) call_count = self.auth.session.content.sessionManager.currentSession.callCount if self.opts.cmd == 'create': if self.opts.config: for cfg in self.opts.config: spec = self.vmcfg.dict_merge(argparser.dotrc, yaml.load(cfg)) cfgcheck_update = CfgCheck.cfg_checker( spec, self.auth, self.opts) spec['vmconfig'].update( self.vmcfg.dict_merge(spec['vmconfig'], cfgcheck_update)) spec = self.vmcfg.pre_create_hooks(**spec) spec = self.vmcfg.create_wrapper(**spec) self.vmcfg.post_create_hooks(**spec) filename = spec['vmconfig']['name'] + '.yaml' server_cfg = {} server_cfg['vmconfig'] = {} server_cfg['vmconfig'].update(spec['vmconfig']) if spec.get('mkbootiso', None): server_cfg['mkbootiso'] = {} server_cfg['mkbootiso'].update(spec['mkbootiso']) print(yaml.dump(server_cfg, default_flow_style=False), file=open(filename, 'w')) if self.opts.cmd == 'mount': self.vmcfg.mount_wrapper(self.opts.datastore, self.opts.path, *self.opts.name) if self.opts.cmd == 'power': self.vmcfg.power_wrapper(self.opts.power, *self.opts.name) if self.opts.cmd == 'umount': self.vmcfg.umount_wrapper(*self.opts.name) if self.opts.cmd == 'upload': self.vmcfg.upload_wrapper(self.opts.datastore, self.opts.dest, self.opts.verify_ssl, *self.opts.iso) if self.opts.cmd == 'add': hostname = Query.get_obj(virtual_machines_container.view, self.opts.name) # nics if self.opts.device == 'nic': self.vmcfg.add_nic_recfg(hostname) if self.opts.cmd == 'reconfig': host = Query.get_obj(virtual_machines_container.view, self.opts.name) if self.opts.cfgs: self.logger.info( 'reconfig: %s cfgs: %s', host.name, ' '.join('%s=%s' % (k, v) for k, v in self.opts.cfgs.iteritems())) self.vmcfg.reconfig(host, **self.opts.cfgs) if self.opts.folder: self.vmcfg.folder_recfg() if self.opts.device == 'disk': self.vmcfg.disk_recfg() if self.opts.device == 'nic': self.vmcfg.nic_recfg() if self.opts.cmd == 'drs': if not self.opts.cluster: self.opts.cluster = Prompts.clusters(self.auth.session) self.clustercfg.drs_rule() if self.opts.cmd == 'query': datacenters_container = Query.create_container( self.auth.session, self.auth.session.content.rootFolder, [vim.Datacenter], True) clusters_container = Query.create_container( self.auth.session, self.auth.session.content.rootFolder, [vim.ClusterComputeResource], True) if self.opts.anti_affinity_rules: if self.opts.cluster: anti_affinity_rules = Query.return_anti_affinity_rules( clusters_container.view, self.opts.cluster) else: cluster = Prompts.clusters(self.auth.session) anti_affinity_rules = Query.return_anti_affinity_rules( clusters_container.view, cluster) if not anti_affinity_rules: print('No antiaffinity rules defined.') else: print('Antiaffinity rules:') for key, val in sorted( anti_affinity_rules.iteritems()): print('{0}: {1}'.format(key, ' '.join(sorted(val)))) if self.opts.datastores: if self.opts.cluster: datastores = Query.return_datastores( clusters_container.view, self.opts.cluster) else: cluster = Prompts.clusters(self.auth.session) datastores = Query.return_datastores( clusters_container.view, cluster) for row in datastores: print('{0:30}\t{1:10}\t{2:10}\t{3:6}\t{4:10}\t{5:6}'. format(*row)) if self.opts.folders: if self.opts.datacenter: folders = Query.list_vm_folders( datacenters_container.view, self.opts.datacenter) folders.sort() for folder in folders: print(folder) else: datacenter = Prompts.datacenters(self.auth.session) folders = Query.list_vm_folders( datacenters_container.view, datacenter) folders.sort() for folder in folders: print(folder) if self.opts.clusters: clusters = Query.list_obj_attrs(clusters_container, 'name') clusters.sort() for cluster in clusters: print(cluster) if self.opts.networks: if self.opts.cluster: cluster = Query.get_obj(clusters_container.view, self.opts.cluster) networks = Query.list_obj_attrs(cluster.network, 'name', view=False) networks.sort() for net in networks: print(net) else: cluster_name = Prompts.clusters(self.auth.session) cluster = Query.get_obj(clusters_container.view, cluster_name) networks = Query.list_obj_attrs(cluster.network, 'name', view=False) networks.sort() for net in networks: print(net) if self.opts.vms: vms = Query.list_vm_info(datacenters_container.view, self.opts.datacenter) for key, value in vms.iteritems(): print(key, value) if self.opts.vmconfig: for name in self.opts.vmconfig: virtmachine = Query.get_obj( virtual_machines_container.view, name) self.logger.debug(virtmachine.config) if self.opts.createcfg: print( yaml.dump(Query.vm_config( virtual_machines_container.view, name, self.opts.createcfg), default_flow_style=False)) else: print( yaml.dump(Query.vm_config( virtual_machines_container.view, name), default_flow_style=False)) if self.opts.vm_by_datastore: if self.opts.cluster and self.opts.datastore: vms = Query.vm_by_datastore(clusters_container.view, self.opts.cluster, self.opts.datastore) for vm_name in vms: print(vm_name) else: if not self.opts.cluster: cluster = Prompts.clusters(self.auth.session) if not self.opts.datastore: datastore = Prompts.datastores( self.auth.session, cluster) print() vms = Query.vm_by_datastore(clusters_container.view, cluster, datastore) for vm_name in vms: print(vm_name) if self.opts.vm_guest_ids: for guest_id in Query.list_guestids(): print(guest_id) self.auth.logout() self.logger.debug('Call count: {0}'.format(call_count)) except ValueError as err: self.logger.error(err, exc_info=False) self.auth.logout() self.logger.debug('Call count: {0}'.format(call_count)) sys.exit(3) except vim.fault.InvalidLogin as loginerr: self.logger.error(loginerr.msg, exc_info=False) sys.exit(2) except KeyboardInterrupt as err: self.logger.error(err, exc_info=False) self.auth.logout() self.logger.debug('Call count: {0}'.format(call_count)) sys.exit(1)
def networks(cls, net_obj): """ Method will prompt user to select a networks. Since multiple networks can be added to a VM, it will prompt the user to exit or add more. The networks should be selected in the order of which they want the interfaces set on the VM. For example, the first network selected will be configured on eth0 on the VM. Args: session (obj): Auth session object net_obj (cls): class has network managed object attribute multiple (bool): Allow for method to accept multiple inputs, otherwise it will return the first selection Returns: selected_networks (list): A list of selected networks """ if getattr(net_obj, 'network'): networks = Query.list_obj_attrs(net_obj.network, 'name', view=False) networks.sort() else: raise ValueError('network managed object not found in %s' % (type(net_obj))) print('\n') print('%s Networks Found.\n' % (len(networks))) for num, opt in enumerate(networks, start=1): print('%s: %s' % (num, opt)) selected_networks = [] while True: if selected_networks: print('selected: ' + ','.join(selected_networks)) val = raw_input( '\nPlease select number:\n(Q)uit (S)how Networks\n' ).strip() # need to test whether selection is an integer or not. try: if int(val) <= len(networks): # need to substract 1 since we start enumeration at 1. val = int(val) - 1 selected_networks.append(networks[val]) continue else: print('Invalid number.') continue except ValueError: if val == 'Q': break elif val == 'S': for num, opt in enumerate(networks, start=1): print('%s: %s' % (num, opt)) else: print('Invalid option.') continue cls.logger.info(selected_networks) return selected_networks
def networks(cls, net_obj): """ Method will prompt user to select a networks. Since multiple networks can be added to a VM, it will prompt the user to exit or add more. The networks should be selected in the order of which they want the interfaces set on the VM. For example, the first network selected will be configured on eth0 on the VM. Args: session (obj): Auth session object net_obj (cls): class has network managed object attribute multiple (bool): Allow for method to accept multiple inputs, otherwise it will return the first selection Returns: selected_networks (list): A list of selected networks """ if getattr(net_obj, 'network'): networks = Query.list_obj_attrs(net_obj.network, 'name', view=False) networks.sort() else: raise ValueError('network managed object not found in %s' % (type(net_obj))) print('\n') print('%s Networks Found.\n' % (len(networks))) for num, opt in enumerate(networks, start=1): print('%s: %s' % (num, opt)) selected_networks = [] while True: if selected_networks: print('selected: ' + ','.join(selected_networks)) val = raw_input( '\nPlease select number:\n(Q)uit (S)how Networks\n').strip() # need to test whether selection is an integer or not. try: if int(val) <= len(networks): # need to substract 1 since we start enumeration at 1. val = int(val) - 1 selected_networks.append(networks[val]) continue else: print('Invalid number.') continue except ValueError: if val == 'Q': break elif val == 'S': for num, opt in enumerate(networks, start=1): print('%s: %s' % (num, opt)) else: print('Invalid option.') continue cls.logger.info(selected_networks) return selected_networks