def get_transport(self, *args, **kwargs): if self.connected: logger.vdebug('reusing ssh transport') else: logger.vdebug('connecting new ssh transport') self.connect() return super(SSHClient, self).get_transport(*args, **kwargs)
def refresh_providers_bulk(provider_ids): """Refresh the specified providers with one request via the REST API""" starttime = time.time() appliance = cfme_performance['appliance']['ip_address'] resources = [] for provider_id in provider_ids: resources.append({ "href": "https://" + appliance + "/api/providers/" + str(provider_id) }) data_dict = { "action": "refresh", "resources": resources } data_json = json.dumps(data_dict) response = requests.post("https://" + appliance + "/api/providers/", data=data_json, auth=(cfme_performance['appliance']['rest_api']['username'], cfme_performance['appliance']['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) if response.status_code != 200: logger.debug(response.text) logger.vdebug('Queued Refresh {} Provider(s) in: {}s'.format(len(provider_ids), round(time.time() - starttime, 2)))
def get_vm_details(vm_id): """ Returns the name, type, vendor, host_id, and power_state associated with the vm_id. """ logger.vdebug('Retrieving the VM details for ID: {}'.format(vm_id)) details = {} response = requests.get(url="https://" + cfme_appliance['ip_address'] + "/api/vms/" + str(vm_id), auth=(cfme_appliance['rest_api']['username'], cfme_appliance['rest_api']['password']), verify=False) details_json = response.json() details['id'] = details_json['id'] if 'ems_id' in details_json: details['ems_id'] = details_json['ems_id'] details['name'] = str(details_json['name']) details['type'] = str(details_json['type']) details['vendor'] = str(details_json['vendor']) if 'host_id' in details_json: details['host_id'] = str(details_json['host_id']) details['power_state'] = str(details_json['power_state']) return details
def scan_provider_vms_bulk(vm_ids): """ Performs Smart State Analysis on the specified VMs with one request via the REST API. """ starttime = time.time() appliance = cfme_appliance['ip_address'] resources = [] for vm_id in vm_ids: resources.append( {"href": "https://" + appliance + "/api/vms/" + str(vm_id)}) data_dict = {"action": "scan", "resources": resources} data_json = json.dumps(data_dict) response = requests.post("https://" + appliance + "/api/vms/", data=data_json, auth=(cfme_appliance['rest_api']['username'], cfme_appliance['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) if response.status_code != 200: logger.debug(response.text) logger.vdebug('Queued Scan {} VM(s) in: {}s'.format( len(vm_ids), round(time.time() - starttime, 2)))
def refresh_providers(provider_ids): """Refresh the specified providers with one request per provider via the REST API""" starttime = time.time() for provider in provider_ids: refresh_provider(provider) logger.vdebug('Queued Refresh {} Provider(s) in: {}s'.format(len(provider_ids), round(time.time() - starttime, 2)))
def scan_provider_vms(vm_ids): """Performs Smart State Analysis on the specified VMs with one request per VM via the REST API""" starttime = time.time() for vm in vm_ids: scan_provider_vm(vm) logger.vdebug('Queued Scan {} VM(s) in: {}s'.format(len(vm_ids), round(time.time() - starttime, 2)))
def scan_provider_vms(vm_ids): """Performs Smart State Analysis on the specified VMs with one request per VM via the REST API""" starttime = time.time() for vm in vm_ids: scan_provider_vm(vm) logger.vdebug('Queued Scan {} VM(s) in: {}s'.format( len(vm_ids), round(time.time() - starttime, 2)))
def connect(self, hostname=None, **kwargs): """See paramiko.SSHClient.connect""" logger.vdebug('ssh.connect') if hostname and hostname != self._connect_kwargs['hostname']: self._connect_kwargs['hostname'] = hostname self.close() if not self.connected: self._connect_kwargs.update(kwargs) return super(SSHClient, self).connect(**self._connect_kwargs)
def __call__(self, **connect_kwargs): logger.vdebug('ssh.__call__') # Update a copy of this instance's connect kwargs with passed in kwargs, # then return a new instance with the updated kwargs new_connect_kwargs = dict(self._connect_kwargs) new_connect_kwargs.update(connect_kwargs) # pass the key state if the hostname is the same, under the assumption that the same # host will still have keys installed if they have already been new_client = SSHClient(**new_connect_kwargs) return new_client
def refresh_provider_vm(vm_id): """Refresh the specified VM via the REST API""" appliance = cfme_performance['appliance']['ip_address'] response = requests.post("https://" + appliance + "/api/vms/" + str(vm_id), data=json.dumps({"action": "refresh"}), auth=(cfme_performance['appliance']['rest_api']['username'], cfme_performance['appliance']['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) if response.status_code != 200: logger.debug(response.text) logger.vdebug('Refreshed VM: {}, Response: {}'.format(vm_id, response))
def suspend_vm(vm_id): """Suspend the specified VM via the REST API""" logger.vdebug('Suspending VM with ID: {}'.format(vm_id)) appliance = cfme_appliance['ip_address'] response = requests.post("https://" + appliance + "/api/vms/" + str(vm_id), data=json.dumps({"action": "suspend"}), auth=(cfme_appliance['rest_api']['username'], cfme_appliance['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) if response.status_code != 200: logger.debug(response.text) logger.debug('Suspended VM: {}, Response: {}'.format(vm_id, response))
def __init__(self, stream_output=False, **connect_kwargs): logger.vdebug('ssh.__init__') super(SSHClient, self).__init__() self._streaming = stream_output default_connect_kwargs = { 'username': cfme_performance['appliance']['ssh']['username'], 'password': cfme_performance['appliance']['ssh']['password'], 'hostname': cfme_performance['appliance']['ip_address'], 'port': 22, 'timeout': 10, 'allow_agent': False, 'look_for_keys': False, 'gss_auth': False } # Overlay defaults with any passed-in kwargs and store default_connect_kwargs.update(connect_kwargs) self._connect_kwargs = default_connect_kwargs self.set_missing_host_key_policy(paramiko.AutoAddPolicy()) _client_session.append(self)
def get_vm_details(vm_id): """Returns the name, type, vendor, host_id, and power_state associated with the vm_id""" logger.vdebug('Retrieving the VM details for ID: {}'.format(vm_id)) details = {} response = requests.get( url="https://" + cfme_performance['appliance']['ip_address'] + "/api/vms/" + str(vm_id), auth=(cfme_performance['appliance']['rest_api']['username'], cfme_performance['appliance']['rest_api']['password']), verify=False ) details_json = response.json() details['id'] = details_json['id'] if 'ems_id' in details_json: details['ems_id'] = details_json['ems_id'] details['name'] = str(details_json['name']) details['type'] = str(details_json['type']) details['vendor'] = str(details_json['vendor']) if 'host_id' in details_json: details['host_id'] = str(details_json['host_id']) details['power_state'] = str(details_json['power_state']) return details
def __enter__(self): logger.vdebug('ssh.__enter__') self.connect() return self
def open_sftp(self, *args, **kwargs): logger.vdebug('ssh.open_sftp') self.connect() return super(SSHClient, self).open_sftp(*args, **kwargs)
def add_provider(provider): """Adds a provider via the REST API.""" logger.vdebug('Adding Provider: {}, Type: {}'.format( provider['name'], provider['type'])) data_dict = { "action": "create", "resources": [{ "name": provider['name'], "type": provider['type'], "credentials": [{ "userid": provider['credentials']['username'], "password": provider['credentials']['password'] }] }] } if 'ip_address' in provider: data_dict['resources'][0]['hostname'] = provider['ip_address'] types = [ 'ManageIQ::Providers::Amazon::CloudManager', 'ManageIQ::Providers::Google::CloudManager' ] if provider['type'] in types: region = provider['provider_region'] data_dict['resources'][0]['provider_region'] = region if 'metrics_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['metrics_credentials']['username'], "password": provider['metrics_credentials']['password'], "auth_type": "metrics" }) elif 'password_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['password_credentials']['username'], "password": provider['password_credentials']['password'], "auth_type": "password" }) elif 'bearer_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['bearer_credentials']['username'], "password": provider['bearer_credentials']['password'], "auth_type": "bearer" }) elif 'amqp_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['amqp_credentials']['username'], "password": provider['amqp_credentials']['password'], "auth_type": "amqp" }) elif 'ssh_keypair_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['ssh_keypair_credentials']['username'], "password": provider['ssh_keypair_credentials']['password'], "auth_type": "ssh_keypair" }) json_data = json.dumps(data_dict) appliance = cfme_appliance['ip_address'] response = requests.post("https://" + appliance + "/api/providers", data=json_data, auth=(cfme_appliance['rest_api']['username'], cfme_appliance['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) logger.debug('Added Provider: {}, Response: {}'.format( provider['name'], response)) # Workaround for Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1351253 if provider['type'] == "ManageIQ::Providers::Microsoft::InfraManager": msg = 'Running rails command for Microsoft InfraManager Workaround' logger.debug(msg) ssh_client = SSHClient() command = ('m = ExtManagementSystem.find_by_name \'{}\';' 'attributes = {{:security_protocol => \'{}\'}};' 'm.update_authentication(:default => ' '{{:userid => \'{}\', :password => \'{}\'}});' 'm.update_attributes(attributes);' 'm.save;' 'm.authentication_check;'.format( provider['name'], provider['security_protocol'], provider['credentials']['username'].replace( '\\', '\\\\'), provider['credentials']['password'])) ssh_client.run_rails_console(command, timeout=None, log_less=True) # Workaround for Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1351253 if provider['type'] == 'ManageIQ::Providers::Openstack::CloudManager': msg = 'Running rails command for Openstack CloudManager Workaround' logger.debug(msg) ssh_client = SSHClient() command = ( 'e = ExtManagementSystem.find_by_name \'{}\'; ' 'attributes = {{:security_protocol => \'{}\', :port => \'{}\'}}; ' 'e.update_authentication(:default => ' '{{:userid => \'{}\', :password => \'{}\'}}); ' 'e.update_attributes(attributes); ' 'e.save; ' 'e.authentication_check;'.format( provider['name'], provider['credentials']['security_protocol'], provider['credentials']['port'], provider['credentials']['username'], provider['credentials']['password'])) if 'amqp_credentials' in provider: default_ip = provider['ip_address'] default_port = provider['credentials']['port'] default_user = provider['credentials']['username'] default_pass = provider['credentials']['password'] default_protocol = provider['credentials']['security_protocol'] # Optionalize amqp endpoint as separate endpoint if specified in # provider definition if 'ip_address' in provider['amqp_credentials']: amqp_ip = provider['amqp_credentials']['ip_address'] else: amqp_ip = provider['ip_address'] amqp_port = provider['amqp_credentials']['port'] amqp_user = provider['amqp_credentials']['username'] amqp_pass = provider['amqp_credentials']['password'] amqp_protocol = provider['amqp_credentials']['security_protocol'] # In order to properly set the connection_configurations, # it must first change twice # To do so, we set the userid to FOOBAR, # then set it to the correct userid command = ( 'e = ExtManagementSystem.find_by_name \'{0}\'; ' 'attributes = {{:security_protocol => \'{5}\', :port => {2}}};' 'e.update_authentication(:default => {{:userid => \'{3}\', ' ':password => \'{4}\'}});' 'e.update_attributes(attributes); ' 'configurations = [{{:endpoint => {{:role => :default, ' ':hostname => \'{1}\', ' ':port => {2}, :security_protocol => \'{5}\'}}, ' ':authentication => {{:role => :default, :userid => \'{3}\', ' ':password => \'{4}\', :save => true }}}}, ' '{{:endpoint => {{:role => :amqp, :hostname => \'{6}\', ' ':port => {7}, ' ':security_protocol => \'{10}\'}}, :authentication => ' '{{:role => :amqp, ' ':userid => \'FOOBAR\', :password => \'{9}\', ' ':save => true}}}}]; ' 'e.connection_configurations = configurations; ' 'configurations = [{{:endpoint => {{:role => :default, ' ':hostname => \'{1}\', ' ':port => {2}, :security_protocol => \'{5}\'}}, ' ':authentication => {{:role => :default, :userid => \'{3}\', ' ':password => \'{4}\', :save => true }}}}, ' '{{:endpoint => {{:role => :amqp, :hostname => \'{6}\', ' ':port => {7}, ' ':security_protocol => \'{10}\'}}, :authentication => ' '{{:role => :amqp, ' ':userid => \'{8}\', :password => \'{9}\', :save => true}}}}];' 'e.connection_configurations = configurations; ' 'e.save; ' 'e.authentication_check;'.format(provider['name'], default_ip, default_port, default_user, default_pass, default_protocol, amqp_ip, amqp_port, amqp_user, amqp_pass, amqp_protocol)) ssh_client.run_rails_console(command, timeout=None, log_less=True)
def add_provider(provider): """Adds a provider via the REST API.""" logger.vdebug('Adding Provider: {}, Type: {}'.format(provider['name'], provider['type'])) data_dict = { "action": "create", "resources": [{ "name": provider['name'], "type": provider['type'], "credentials": [{ "userid": provider['credentials']['username'], "password": provider['credentials']['password'] }] }] } if 'ip_address' in provider: data_dict['resources'][0]['hostname'] = provider['ip_address'] if (provider['type'] == 'ManageIQ::Providers::Amazon::CloudManager' or provider['type'] == 'ManageIQ::Providers::Google::CloudManager'): data_dict['resources'][0]['provider_region'] = provider['provider_region'] if 'metrics_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['metrics_credentials']['username'], "password": provider['metrics_credentials']['password'], "auth_type": "metrics" }) elif 'password_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['password_credentials']['username'], "password": provider['password_credentials']['password'], "auth_type": "password" }) elif 'bearer_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['bearer_credentials']['username'], "password": provider['bearer_credentials']['password'], "auth_type": "bearer" }) elif 'amqp_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['amqp_credentials']['username'], "password": provider['amqp_credentials']['password'], "auth_type": "amqp" }) elif 'ssh_keypair_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['ssh_keypair_credentials']['username'], "password": provider['ssh_keypair_credentials']['password'], "auth_type": "ssh_keypair" }) json_data = json.dumps(data_dict) appliance = cfme_performance['appliance']['ip_address'] response = requests.post("https://" + appliance + "/api/providers", data=json_data, auth=(cfme_performance['appliance']['rest_api']['username'], cfme_performance['appliance']['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) logger.debug('Added Provider: {}, Response: {}'.format(provider['name'], response)) # Workaround for Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1351253 if provider['type'] == "ManageIQ::Providers::Microsoft::InfraManager": logger.debug('Running rails command for Microsoft InfraManager Workaround') ssh_client = SSHClient() command = ( 'm = ExtManagementSystem.find_by_name \'{}\';' 'attributes = {{:security_protocol => \'{}\'}};' 'm.update_authentication(:default => {{:userid => \'{}\', :password => \'{}\'}});' 'm.update_attributes(attributes);' 'm.save;' 'm.authentication_check;'.format(provider['name'], provider['security_protocol'], provider['credentials']['username'].replace('\\', '\\\\'), provider['credentials']['password'])) ssh_client.run_rails_console(command, timeout=None, log_less=True) # Workaround for Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1351253 if provider['type'] == 'ManageIQ::Providers::Openstack::CloudManager': logger.debug('Running rails command for Openstack CloudManager Workaround') ssh_client = SSHClient() command = ( 'e = ExtManagementSystem.find_by_name \'{}\'; ' 'attributes = {{:security_protocol => \'{}\', :port => \'{}\'}}; ' 'e.update_authentication(:default => {{:userid => \'{}\', :password => \'{}\'}}); ' 'e.update_attributes(attributes); ' 'e.save; ' 'e.authentication_check;'.format( provider['name'], provider['credentials']['security_protocol'], provider['credentials']['port'], provider['credentials']['username'], provider['credentials']['password'])) if 'amqp_credentials' in provider: default_ip = provider['ip_address'] default_port = provider['credentials']['port'] default_user = provider['credentials']['username'] default_pass = provider['credentials']['password'] default_protocol = provider['credentials']['security_protocol'] # Optionalize amqp endpoint as separate endpoint if specified in provider definition if 'ip_address' in provider['amqp_credentials']: amqp_ip = provider['amqp_credentials']['ip_address'] else: amqp_ip = provider['ip_address'] amqp_port = provider['amqp_credentials']['port'] amqp_user = provider['amqp_credentials']['username'] amqp_pass = provider['amqp_credentials']['password'] amqp_protocol = provider['amqp_credentials']['security_protocol'] # In order to properly set the connection_configurations, it must first change twice # To do so, we set the userid to FOOBAR, then set it to the correct userid command = ( 'e = ExtManagementSystem.find_by_name \'{0}\'; ' 'attributes = {{:security_protocol => \'{5}\', :port => {2}}}; ' 'e.update_authentication(:default => {{:userid => \'{3}\', :password => \'{4}\'}});' 'e.update_attributes(attributes); ' 'configurations = [{{:endpoint => {{:role => :default, :hostname => \'{1}\', ' ':port => {2}, :security_protocol => \'{5}\'}}, ' ':authentication => {{:role => :default, :userid => \'{3}\', ' ':password => \'{4}\', :save => true }}}}, ' '{{:endpoint => {{:role => :amqp, :hostname => \'{6}\', :port => {7}, ' ':security_protocol => \'{10}\'}}, :authentication => {{:role => :amqp, ' ':userid => \'FOOBAR\', :password => \'{9}\', :save => true}}}}]; ' 'e.connection_configurations = configurations; ' 'configurations = [{{:endpoint => {{:role => :default, :hostname => \'{1}\', ' ':port => {2}, :security_protocol => \'{5}\'}}, ' ':authentication => {{:role => :default, :userid => \'{3}\', ' ':password => \'{4}\', :save => true }}}}, ' '{{:endpoint => {{:role => :amqp, :hostname => \'{6}\', :port => {7}, ' ':security_protocol => \'{10}\'}}, :authentication => {{:role => :amqp, ' ':userid => \'{8}\', :password => \'{9}\', :save => true}}}}]; ' 'e.connection_configurations = configurations; ' 'e.save; ' 'e.authentication_check;'.format( provider['name'], default_ip, default_port, default_user, default_pass, default_protocol, amqp_ip, amqp_port, amqp_user, amqp_pass, amqp_protocol)) ssh_client.run_rails_console(command, timeout=None, log_less=True)
def add_provider(provider): """Adds a provider via the REST API.""" logger.vdebug('Adding Provider: {}, Type: {}'.format(provider['name'], provider['type'])) data_dict = { "action": "create", "resources": [{ "name": provider['name'], "type": provider['type'], "credentials": [{ "userid": provider['credentials']['username'], "password": provider['credentials']['password'] }] }] } if 'ip_address' in provider: data_dict['resources'][0]['hostname'] = provider['ip_address'] if (provider['type'] == 'ManageIQ::Providers::Amazon::CloudManager' or provider['type'] == 'ManageIQ::Providers::Google::CloudManager'): data_dict['resources'][0]['provider_region'] = provider['provider_region'] if 'metrics_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['metrics_credentials']['username'], "password": provider['metrics_credentials']['password'], "auth_type": "metrics" }) elif 'password_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['password_credentials']['username'], "password": provider['password_credentials']['password'], "auth_type": "password" }) elif 'bearer_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['bearer_credentials']['username'], "password": provider['bearer_credentials']['password'], "auth_type": "bearer" }) elif 'amqp_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['amqp_credentials']['username'], "password": provider['amqp_credentials']['password'], "auth_type": "amqp" }) elif 'ssh_keypair_credentials' in provider: data_dict['resources'][0]['credentials'].append({ "userid": provider['ssh_keypair_credentials']['username'], "password": provider['ssh_keypair_credentials']['password'], "auth_type": "ssh_keypair" }) json_data = json.dumps(data_dict) appliance = cfme_performance['appliance']['ip_address'] response = requests.post("https://" + appliance + "/api/providers", data=json_data, auth=(cfme_performance['appliance']['rest_api']['username'], cfme_performance['appliance']['rest_api']['password']), verify=False, headers={"content-type": "application/json"}, allow_redirects=False) logger.debug('Added Provider: {}, Response: {}'.format(provider['name'], response))