def net_check_remote(port, addr=None, machine_addr=None, ssh_creds=None, force=False): """Checks the availability of a port from outside using another machine (over SSH)""" from utils.ssh import SSHClient port = int(port) if not addr: addr = my_ip_address() if port not in _ports[addr] or force: if not machine_addr: machine_addr = urlparse.urlparse(store.base_url).hostname if not ssh_creds: ssh = SSHClient(hostname=machine_addr) else: ssh = SSHClient(hostname=machine_addr, username=ssh_creds['username'], password=ssh_creds['password']) with ssh: # on exception => fails with return code 1 cmd = '''python -c " import sys, socket addr = socket.gethostbyname('%s') socket.create_connection((addr, %d), timeout=10) sys.exit(0) "''' % (addr, port) ret, out = ssh.run_command(cmd) if ret == 0: _ports[addr][port] = True else: _ports[addr][port] = False return _ports[addr][port]
def disable_forgery_protection(): starttime = time.time() ssh_client = SSHClient() logger.info('Turning off "allow_forgery_protection"') ssh_client.run_command( "sed -i \'s/allow_forgery_protection = true/allow_forgery_protection = false/\' " "/var/www/miq/vmdb/config/environments/production.rb") ssh_client.run_command("service evmserverd restart") ssh_client.close() timediff = time.time() - starttime logger.info( 'Turned off "allow_forgery_protection" in: {}'.format(timediff)) yield starttime = time.time() ssh_client = SSHClient() logger.info('Turning on "allow_forgery_protection"') ssh_client.run_command( "sed -i \'s/allow_forgery_protection = false/allow_forgery_protection = true/\' " "/var/www/miq/vmdb/config/environments/production.rb") ssh_client.run_command("service evmserverd restart") ssh_client.close() timediff = time.time() - starttime logger.info('Turned on "allow_forgery_protection" in: {}'.format(timediff))
def set_yaml_config(config_name, data_dict, hostname=None): """Given a yaml name, dictionary and hostname, set the configuration yaml on the server The configuration yamls must be inserted into the DB using the ruby console, so this function uses SSH, not the database. It makes sense to be included here as a counterpart to :py:func:`get_yaml_config` Args: config_name: Name of the yaml configuration file data_dict: Dictionary with data to set/change hostname: Hostname/address of the server that we want to set up (default ``None``) Note: If hostname is set to ``None``, the default server set up for this session will be used. See :py:class:``utils.ssh.SSHClient`` for details of the default setup. Warning: Manually editing the config yamls is potentially dangerous. Furthermore, the rails runner doesn't return useful information on the outcome of the set request, so errors that arise from the newly loading config file will go unreported. Usage: # Update the appliance name, for example vmbd_yaml = get_yaml_config('vmdb') vmdb_yaml['server']['name'] = 'EVM IS AWESOME' set_yaml_config('vmdb', vmdb_yaml, '1.2.3.4') """ # CFME does a lot of things when loading a configfile, so # let their native conf loader handle the job # If hostname is defined, connect to the specified server if hostname is not None: _ssh_client = SSHClient(hostname=hostname) # Else, connect to the default one set up for this session else: _ssh_client = SSHClient() # Build & send new config temp_yaml = NamedTemporaryFile() dest_yaml = '/tmp/conf.yaml' yaml.dump(data_dict, temp_yaml, default_flow_style=False) _ssh_client.put_file(temp_yaml.name, dest_yaml) # Build and send ruby script dest_ruby = '/tmp/load_conf.rb' ruby_template = data_path.join('utils', 'cfmedb_load_config.rbt') ruby_replacements = {'config_name': config_name, 'config_file': dest_yaml} temp_ruby = load_data_file(ruby_template.strpath, ruby_replacements) _ssh_client.put_file(temp_ruby.name, dest_ruby) # Run it _ssh_client.run_rails_command(dest_ruby)
def test_verify_revert_snapshot(test_vm, provider, soft_assert, register_event, request): """Tests revert snapshot Metadata: test_flag: snapshot, provision """ snapshot1 = new_snapshot(test_vm) ip = snapshot1.vm.provider.mgmt.get_ip_address(snapshot1.vm.name) ssh_kwargs = { 'username': credentials[provider.data['full_template']['creds']]['username'], 'password': credentials[provider.data['full_template']['creds']]['password'], 'hostname': ip } with SSHClient(**ssh_kwargs) as ssh_client: ssh_client.run_command('touch snapshot1.txt') snapshot1.create() ssh_client.run_command('touch snapshot2.txt') snapshot2 = new_snapshot(test_vm) snapshot2.create() snapshot1.revert_to() # Wait for the snapshot to become active logger.info('Waiting for vm %s to become active', snapshot1.name) wait_for(snapshot1.wait_for_snapshot_active, num_sec=300, delay=20, fail_func=sel.refresh) test_vm.wait_for_vm_state_change(desired_state=test_vm.STATE_OFF, timeout=720) register_event(target_type='VmOrTemplate', target_name=test_vm.name, event_type='request_vm_start') register_event(target_type='VmOrTemplate', target_name=test_vm.name, event_type='vm_start') test_vm.power_control_from_cfme(option=test_vm.POWER_ON, cancel=False) navigate_to(test_vm.provider, 'Details') test_vm.wait_for_vm_state_change(desired_state=test_vm.STATE_ON, timeout=900) soft_assert(test_vm.find_quadicon().state == 'currentstate-on') soft_assert(test_vm.provider.mgmt.is_vm_running(test_vm.name), "vm not running") with SSHClient(**ssh_kwargs) as ssh_client: try: wait_for(lambda: ssh_client.run_command('test -e snapshot2.txt')[1] == 0, fail_condition=False) logger.info('Revert to snapshot %s successful', snapshot1.name) except: logger.info('Revert to snapshot %s Failed', snapshot1.name)
def test_verify_revert_snapshot(test_vm, provider, soft_assert, register_event, request): """Tests revert snapshot Metadata: test_flag: snapshot, provision """ snapshot1 = new_snapshot(test_vm) ip = snapshot1.vm.provider_crud.get_mgmt_system().get_ip_address( snapshot1.vm.name) print ip ssh_kwargs = { 'username': credentials[provider.data['full_template']['creds']]['username'], 'password': credentials[provider.data['full_template']['creds']]['password'], 'hostname': ip } ssh = SSHClient(**ssh_kwargs) ssh.run_command('touch snapshot1.txt') snapshot1.create() ssh.run_command('touch snapshot2.txt') snapshot2 = new_snapshot(test_vm) snapshot2.create() snapshot1.revert_to() # Wait for the snapshot to become active logger.info('Waiting for vm %s to become active', snapshot1.name) wait_for(snapshot1.wait_for_snapshot_active, num_sec=300, delay=20, fail_func=sel.refresh) test_vm.wait_for_vm_state_change(desired_state=Vm.STATE_OFF, timeout=720) register_event(test_vm.provider_crud.get_yaml_data()['type'], "vm", test_vm.name, ["vm_power_on_req", "vm_power_on"]) test_vm.power_control_from_cfme(option=Vm.POWER_ON, cancel=False) pytest.sel.force_navigate('infrastructure_provider', context={'provider': test_vm.provider_crud}) test_vm.wait_for_vm_state_change(desired_state=Vm.STATE_ON, timeout=900) soft_assert(test_vm.find_quadicon().state == 'currentstate-on') soft_assert( test_vm.provider_crud.get_mgmt_system().is_vm_running(test_vm.name), "vm not running") client = SSHClient(**ssh_kwargs) request.addfinalizer(test_vm.delete_from_provider) try: wait_for(lambda: client.run_command('test -e snapshot2.txt')[1] == 0, fail_condition=False) logger.info('Revert to snapshot %s successful', snapshot1.name) except: logger.info('Revert to snapshot %s Failed', snapshot1.name)
def test_idle_default(request): """Runs an appliance at idle for specific amount of time. Memory Monitor creates graphs and summary at the end of the scenario.""" from_ts = int(time.time() * 1000) ssh_client = SSHClient() clean_appliance(ssh_client) quantifiers = {} scenario_data = { 'appliance_ip': cfme_performance['appliance']['ip_address'], 'appliance_name': cfme_performance['appliance']['appliance_name'], 'test_dir': 'workload-idle', 'test_name': 'Idle with Default Roles', 'appliance_roles': get_server_roles_workload_idle_default(separator=', '), 'scenario': { 'name': 'default' } } monitor_thread = SmemMemoryMonitor(SSHClient(), scenario_data) def cleanup_workload(from_ts, quantifiers, scenario_data): starttime = time.time() to_ts = int(starttime * 1000) g_url = get_default_dashboard_url(from_ts, to_ts) logger.debug('Started cleaning up monitoring thread.') monitor_thread.grafana_url = g_url monitor_thread.signal = False monitor_thread.join() add_workload_quantifiers(quantifiers, scenario_data) timediff = time.time() - starttime logger.info( 'Finished cleaning up monitoring thread in {}'.format(timediff)) request.addfinalizer( lambda: cleanup_workload(from_ts, quantifiers, scenario_data)) monitor_thread.start() wait_for_miq_server_workers_started(poll_interval=2) # No need to set server roles as we are using the default set of roles s_time = cfme_performance['workloads']['test_idle_default']['total_time'] logger.info('Idling appliance for {}s'.format(s_time)) time.sleep(s_time) quantifiers['Elapsed_Time'] = s_time logger.info('Test Ending...')
def setSSHConnection(self): self.ssh_conn = SSHClient(log_path=self.output_dir + "/cumulus_" + self.userid + ".ssh.log") self.ssh_conn.connect(hostname=self.server, username=self.userid, password=self.password)
def sat6_unregister(): with SSHClient() as ssh: ssh.run_command('subscription-manager remove --all') ssh.run_command('subscription-manager unregister') ssh.run_command('subscription-manager clean') ssh.run_command('mv -f /etc/rhsm/rhsm.conf.kat-backup /etc/rhsm/rhsm.conf') ssh.run_command('rpm -qa | grep katello-ca-consumer | xargs rpm -e')
def setup_external_auth_openldap(**data): """Sets up the appliance for an external authentication with OpenLdap. Keywords: get_groups: Get User Groups from External Authentication (httpd). ipaserver: IPA server address. iparealm: Realm. credentials: Key of the credential in credentials.yaml """ connect_kwargs = { 'username': credentials['host_default']['username'], 'password': credentials['host_default']['password'], 'hostname': data['ipaddress'], } appliance_obj = appliance.IPAppliance() appliance_name = 'cfmeappliance{}'.format(fauxfactory.gen_alpha(7).lower()) appliance_address = appliance_obj.address appliance_fqdn = '{}.{}'.format(appliance_name, data['domain_name']) with SSHClient(**connect_kwargs) as ldapserver_ssh: # updating the /etc/hosts is a workaround due to the # https://bugzilla.redhat.com/show_bug.cgi?id=1360928 command = 'echo "{}\t{}" >> /etc/hosts'.format(appliance_address, appliance_fqdn) ldapserver_ssh.run_command(command) ldapserver_ssh.get_file(remote_file=data['cert_filepath'], local_path=conf_path.strpath) ensure_browser_open() login_admin() auth = ExternalAuthSetting(get_groups=data.pop("get_groups", True)) auth.setup() appliance_obj.configure_appliance_for_openldap_ext_auth(appliance_fqdn) logout()
def get_appliance(provider): '''Fixture to provision appliance to the provider being tested if necessary''' global appliance_list global appliance_vm_name if provider not in appliance_list: if ('appliances_provider' not in cfme_data['basic_info'].keys() or provider != cfme_data['basic_info']['appliances_provider']): appliance_list[provider] = provision_appliance(provider) else: appliance_list[provider] = re.findall(r'[0-9]+(?:\.[0-9]+){3}', conf.env['base_url'])[0] prov_data = cfme_data['management_systems'][provider] if prov_data['type'] == 'virtualcenter': # ssh in and see if vddk already present, if not, install ssh_kwargs = { 'username': conf.credentials['ssh']['username'], 'password': conf.credentials['ssh']['password'], 'hostname': appliance_list[provider] } # Init SSH client client = SSHClient(**ssh_kwargs) if int( client.run_command("ldconfig -p | grep vix | wc -l") [1]) < 1: install_vddk(appliance_list[provider]) client.close() elif prov_data['type'] == 'rhevm': add_rhev_direct_lun_disk(provider, appliance_vm_name) return appliance_list[provider]
def setup_external_auth_ipa(**data): """Sets up the appliance for an external authentication with IPA. Keywords: get_groups: Get User Groups from External Authentication (httpd). ipaserver: IPA server address. iparealm: Realm. credentials: Key of the credential in credentials.yaml """ ssh = SSHClient() ensure_browser_open() login_admin() if data["ipaserver"] not in get_ntp_servers(): set_ntp_servers(data["ipaserver"]) sleep(120) auth = ExternalAuthSetting(get_groups=data.pop("get_groups", False)) auth.setup() logout() creds = credentials.get(data.pop("credentials"), {}) data.update(**creds) rc, out = ssh.run_command( "appliance_console_cli --ipaserver {ipaserver} --iparealm {iparealm} " "--ipaprincipal {principal} --ipapassword {password}".format(**data)) assert rc == 0, out assert "failed" not in out.lower( ), "External auth setup failed:\n{}".format(out) login_admin()
def db_restore(temp_appliance_extended_db): app = temp_appliance_extended_db app.stop_evm_service() app.drop_database() db_storage_hostname = conf.cfme_data['bottlenecks']['hostname'] db_storage = SSHClient(hostname=db_storage_hostname, **conf.credentials['bottlenecks']) with db_storage as ssh: # Different files for different versions ver = "_56" if current_version() < '5.7' else "" rand_filename = "/tmp/v2_key_{}".format(fauxfactory.gen_alphanumeric()) ssh.get_file("/home/backups/otsuman_db_bottlenecks/v2_key{}".format(ver), rand_filename) dump_filename = "/tmp/db_dump_{}".format(fauxfactory.gen_alphanumeric()) ssh.get_file("/home/backups/otsuman_db_bottlenecks/db.backup{}".format(ver), dump_filename) region_filename = "/tmp/REGION_{}".format(fauxfactory.gen_alphanumeric()) ssh.get_file("/home/backups/otsuman_db_bottlenecks/REGION{}".format(ver), region_filename) guid_filename = "/tmp/GUID_{}".format(fauxfactory.gen_alphanumeric()) ssh.get_file("/home/backups/otsuman_db_bottlenecks/GUID{}".format(ver), guid_filename) with app.ssh_client as ssh: ssh.put_file(rand_filename, "/var/www/miq/vmdb/certs/v2_key") ssh.put_file(dump_filename, "/tmp/evm_db.backup") ssh.put_file(region_filename, "/var/www/miq/vmdb/REGION") ssh.put_file(guid_filename, "/var/www/miq/vmdb/GUID") app.restore_database() app.start_evm_service() app.wait_for_web_ui()
def make_ssh_client(ip, sshname, sshpass): connect_kwargs = { 'username': sshname, 'password': sshpass, 'hostname': ip } return SSHClient(**connect_kwargs)
def test_navigate_explorer(): """Initial Example Workload, Add Provider, turn on some things, initiate navigations on the WebUI from python. Currently lets disable cleaning the appliance etc.""" from_ts = int(time.time() * 1000) ssh_client = SSHClient() # clean_appliance(ssh_client) cfme_ip = cfme_performance['appliance']['ip_address'] cfme_web_ui_user = cfme_performance['appliance']['web_ui']['username'] cfme_web_ui_password = cfme_performance['appliance']['web_ui']['password'] url = "https://{}/".format(cfme_ip) params = {"user_name": cfme_web_ui_user, "user_password": cfme_web_ui_password } with requests.Session() as sess: r = sess.get("{}{}".format(url, "api/auth?requester_type=ui"), auth=HTTPBasicAuth(cfme_web_ui_user, cfme_web_ui_password), verify=False, allow_redirects=False) r = sess.post("{}{}".format(url, "dashboard/authenticate"), params=params, verify=False, allow_redirects=False) dump_r(r) # Get a protected page now: #r = sess.get(url + 'dashboard/show', verify=False, allow_redirects=False) for i in range(10): r = sess.get("{}{}".format(url, "dashboard/show"), verify=False) r = sess.get("{}{}".format(url, "vm_infra/explorer"), verify=False)
def main(): parser = argparse.ArgumentParser( epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('address', help='hostname or ip address of target appliance') args = parser.parse_args() ssh_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': args.address } # Init SSH client client = SSHClient(**ssh_kwargs) # generate installed rpm list status, out = client.run_command( 'rpm -qa | sort > /tmp/installed_rpms.txt') client.get_file('/tmp/installed_rpms.txt', 'installed_rpms.txt') # compress logs dir status, out = client.run_command( 'cd /var/www/miq/vmdb; tar zcvf /tmp/appliance_logs.tgz log') client.get_file('/tmp/appliance_logs.tgz', 'appliance_logs.tgz')
def get_vm_config_modified_time(name, vm_name, datastore_url, provider_key): try: providers_data = cfme_data.get("management_systems", {}) hosts = providers_data[provider_key]['hosts'] host_creds = providers_data[provider_key].get('host_credentials', 'host_default') hostname = [host['name'] for host in hosts if name in host['name']] if not hostname: hostname = re.findall(r'[0-9]+(?:\.[0-9]+){3}', name) connect_kwargs = { 'username': credentials[host_creds]['username'], 'password': credentials[host_creds]['password'], 'hostname': hostname[0] } datastore_path = re.findall(r'([^ds:`/*].*)', str(datastore_url)) ssh_client = SSHClient(**connect_kwargs) command = 'find ~/{}/{} -name {} | xargs date -r'.format( datastore_path[0], str(vm_name), str(vm_name) + '.vmx') exit_status, output = ssh_client.run_command(command) ssh_client.close() modified_time = parser.parse(output.rstrip()) modified_time = modified_time.astimezone( pytz.timezone(str(get_localzone()))) return modified_time.replace(tzinfo=None) except Exception as e: logger.error(e) return False
def make_ssh_client(ssh_host, ssh_user, ssh_pass): connect_kwargs = { 'username': ssh_user, 'password': ssh_pass, 'hostname': ssh_host } return SSHClient(**connect_kwargs)
def main(): parser = argparse.ArgumentParser( epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('address', help='hostname or ip address of target appliance') args = parser.parse_args() ssh_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': args.address } with SSHClient(**ssh_kwargs) as ssh: print("Setting appliance's time. Please wait.") servers_str = " ".join( ["'%s'" % server for server in cfme_data["clock_servers"]]) status, out = ssh.run_command("ntpdate " + servers_str) if status != 0: print( "Could not set the time. Check the output of the command, please:" ) print(out.strip()) return 1 print( "Time was set. Now it should be safe to log in and test on the appliance." ) return 0
def __init__(self, provider): provider_cfme_data = provider.get_yaml_data() self.hostname = provider_cfme_data['hostname'] creds = provider_cfme_data.get('ssh_creds') if not creds: raise Exception( 'Could not find ssh_creds in provider\'s cfme data.') if isinstance(creds, dict): self.username = creds.get('username') self.password = creds.get('password') else: self.username = credentials[creds].get('username') self.password = credentials[creds].get('password') with SSHClient(hostname=self.hostname, username=self.username, password=self.password, look_for_keys=True) as ssh_client: self.ssh_client = ssh_client self.ssh_client.load_system_host_keys() self._command_counter = 0 self.log_line_limit = 500
def test_idle(request, scenario): """Runs an appliance at idle with specific roles turned on for specific amount of time. Memory Monitor creates graphs and summary at the end of the scenario.""" from_ts = int(time.time() * 1000) ssh_client = SSHClient() logger.debug('Scenario: {}'.format(scenario['name'])) clean_appliance(ssh_client) quantifiers = {} scenario_data = { 'appliance_ip': cfme_performance['appliance']['ip_address'], 'appliance_name': cfme_performance['appliance']['appliance_name'], 'test_dir': 'workload-idle', 'test_name': 'Idle with {} Roles'.format(scenario['name']), 'appliance_roles': ', '.join(scenario['roles']), 'scenario': scenario } monitor_thread = SmemMemoryMonitor(SSHClient(), scenario_data) def cleanup_workload(from_ts, quantifiers, scenario_data): starttime = time.time() to_ts = int(starttime * 1000) g_urls = get_scenario_dashboard_urls(scenario, from_ts, to_ts) logger.debug('Started cleaning up monitoring thread.') monitor_thread.grafana_urls = g_urls monitor_thread.signal = False monitor_thread.join() add_workload_quantifiers(quantifiers, scenario_data) timediff = time.time() - starttime logger.info( 'Finished cleaning up monitoring thread in {}'.format(timediff)) request.addfinalizer( lambda: cleanup_workload(from_ts, quantifiers, scenario_data)) monitor_thread.start() wait_for_miq_server_workers_started(poll_interval=2) set_server_roles(ssh_client, scenario['roles']) s_time = scenario['total_time'] logger.info('Idling appliance for {}s'.format(s_time)) time.sleep(s_time) quantifiers['Elapsed_Time'] = s_time logger.info('Test Ending...')
def main(): parser = argparse.ArgumentParser( epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('address', help='hostname or ip address of target appliance') args = parser.parse_args() ssh_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': args.address } # Init SSH client with SSHClient(**ssh_kwargs) as ssh_client: snmp_path = scripts_data_path.join("snmp") # Copy print("Copying files") ssh_client.put_file( snmp_path.join("snmp_listen.rb").strpath, "/root/snmp_listen.rb") ssh_client.put_file( snmp_path.join("snmp_listen.sh").strpath, "/root/snmp_listen.sh") # Enable after startup print("Enabling after startup") status = ssh_client.run_command( "grep 'snmp_listen[.]sh' /etc/rc.local")[0] if status != 0: ssh_client.run_command( "echo 'cd /root/ && ./snmp_listen.sh start' >> /etc/rc.local") assert ssh_client.run_command("grep 'snmp_listen[.]sh' /etc/rc.local")[0] == 0, \ "Could not enable!" # Run! print("Starting listener") assert ssh_client.run_command("cd /root/ && ./snmp_listen.sh start")[0] == 0, \ "Could not start!" # Open the port if not opened print("Opening the port in iptables") status = ssh_client.run_command( "grep '--dport 8765' /etc/sysconfig/iptables")[0] if status != 0: # append after the 5432 entry ssh_client.run_command( "sed -i '/--dport 5432/a -A INPUT -p tcp -m tcp --dport 8765 -j ACCEPT' " "/etc/sysconfig/iptables") ssh_client.run_command("systemctl restart iptables") # last ssh command, close # Check if accessible try: requests.get("http://{}:8765/".format(args.address)) except requests.exceptions.ConnectionError: print("Could not detect running listener!") exit(2)
def setup_collectd(perf_data): command_str = "until ping -c1 " + str(perf_data['appliance']['ip_address'] ) + " &>/dev/null; do sleep 5; done" print subprocess.Popen(command_str, shell=True, stdout=subprocess.PIPE).stdout.read() id_pub = subprocess.Popen("ssh-keygen -y -f ~/.ssh/id_rsa_t", shell=True, stdout=subprocess.PIPE).stdout.read() commandstring = "echo \"" + str(id_pub) + "\" > ~/.ssh/authorized_keys" ssh_client = SSHClient() ssh_client.run_command(commandstring) version_string = get_current_version_string().replace(".", "") appliance_name_update = perf_data['appliance']['appliance_name'].replace( "LATEST", version_string) perf_data['appliance']['appliance_name'] = appliance_name_update stream = open("cfme-performance/conf/data.yml", "r") datayml = yaml.load(stream) perf_data['tools']['grafana']['ip_address'] = datayml['grafana']['ip'] perf_data['tools']['grafana']['enabled'] = 'true' hosts_local = "[monitorhost]\n" + str( perf_data['tools']['grafana']['ip_address']) + "\n\n" hosts_local = hosts_local + "[cfme-vmdb]\n" + perf_data['appliance'][ 'appliance_name'] + "\n\n" hosts_local = hosts_local + "[cfme-worker]\n\n[cfme-worker]\n\n[cfme-all-in-one]\n\n[rhevm]\n" hostfile = open("ansible/hosts.local", "w") hostfile.write(hosts_local) hostfile.close() cstr = "\n\tIdentityFile ~/.ssh/id_rsa_t\n\tStrictHostKeyChecking no\n\tUserKnownHostsFile=/dev/null" ssh_config = "Host " + perf_data['appliance'][ 'appliance_name'] + "\n\tHostname " + perf_data['appliance'][ 'ip_address'] + cstr ssh_config = ssh_config + "\nHost " + datayml['grafana'][ 'host'] + "\n\tHostname " + datayml['grafana']['ip'] + cstr #print ssh_config sshfile = open('ansible/ssh-config.local', 'w') sshfile.write(ssh_config) sshfile.close() stream = open("cfme-performance/conf/all.yml", "r") allstream = yaml.load(stream) allstream['appliances'][perf_data['appliance']['appliance_name']] = {} allstream['appliances'][perf_data['appliance'][ 'appliance_name']] = allstream['appliances']['CF-B2B-R0000-test'] del allstream['appliances']['CF-B2B-R0000-test'] with open('ansible/group_vars/all.local.yml', 'w') as outfile: yaml.dump(allstream, outfile, default_flow_style=False) subprocess.Popen("sleep 300", shell=True) print subprocess.Popen( "ansible-playbook -i hosts.local configure/postdeploy.yml -vvv", shell=True, stdout=subprocess.PIPE, cwd="ansible").stdout.read()
def is_registration_complete(used_repo_or_channel): with SSHClient() as ssh: ret, out = ssh.run_command('yum repolist enabled') # Check that the specified (or default) repo (can be multiple, separated by a space) # is enabled and that there are packages available for repo_or_channel in used_repo_or_channel.split(' '): if (repo_or_channel not in out) or (not re.search(r'repolist: [^0]', out)): return False return True
def disable_external_auth_ipa(): """Unconfigure external auth.""" ssh = SSHClient() ensure_browser_open() login_admin() auth = DatabaseAuthSetting() auth.update() rc, out = ssh.run_command("appliance_console_cli --uninstall-ipa") assert rc == 0, out
def make_ssh_client(provider_mgmt): creds = credentials[provider_mgmt.kwargs.get('ssh_creds', None)] connect_kwargs = { 'username': creds['username'], 'password': creds['password'], 'hostname': provider_mgmt.kwargs.get('ipaddress') } return SSHClient(**connect_kwargs)
def ssh_client(): hostname = "localhost" username = "******" password = "******" ssh_port = 22 ssh = SSHClient(hostname, username, password, ssh_port) return ssh
def configure_appliance_for_event_testing(request, listener_info): """ This fixture ensures that the appliance is configured for event testing. """ event_testing = request.config.pluginmanager.getplugin("event_testing") # If we did not enable the event testing, do not setup if event_testing.listener is None: return return setup_for_event_testing(SSHClient(), cfmedb(), listener_info, providers.list_infra_providers())
def restart_appliance(address): print('Restarting evmserverd on {}'.format(address)) with SSHClient(hostname=address, **ssh_creds) as client: status, out = client.run_command('systemctl restart evmserverd') if status != 0: print("Restarting evmserverd failed on {}".format(address)) sys.exit(1) else: print("Restarting succeeded on {}".format(address))
def main(): parser = argparse.ArgumentParser( epilog=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('address', help='hostname or ip address of target appliance') parser.add_argument('db_address', help='hostname or ip address of external database') parser.add_argument('--database', default='vmdb_production', help='name of the external database') parser.add_argument('--region', default=0, type=int, help='region to assign to the new DB') parser.add_argument('--username', default=credentials['database']['username'], help='username for external database') parser.add_argument('--password', default=credentials['database']['password'], help='password for external database') args = parser.parse_args() ssh_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': args.address } rbt_repl = { 'miq_lib': '/var/www/miq/lib', 'host': args.db_address, 'database': args.database, 'region': args.region, 'username': args.username, 'password': args.password } # Find and load our rb template with replacements base_path = os.path.dirname(__file__) rbt = datafile.data_path_for_filename('enable-external-db.rbt', base_path) rb = datafile.load_data_file(rbt, rbt_repl) # Init SSH client and sent rb file over to /tmp remote_file = '/tmp/%s' % generate_random_string() client = SSHClient(**ssh_kwargs) client.put_file(rb.name, remote_file) # Run the rb script, clean it up when done print 'Initializing Appliance External DB' status, out = client.run_command('ruby %s' % remote_file) client.run_command('rm %s' % remote_file) if status != 0: print 'Enabling DB failed with error:' print out sys.exit(1) else: print 'DB Enabled, evm watchdog should start the UI shortly.'
def get_ssh_client(hostname): """ Returns fresh ssh client connected to given server using given credentials """ hostname = urlparse('scheme://' + hostname).netloc connect_kwargs = { 'username': credentials['ssh']['username'], 'password': credentials['ssh']['password'], 'hostname': hostname, } return SSHClient(**connect_kwargs)