def configure_secondary_interface(self, args, extra_vars, subnet_cidr): logging.info("[app] Configuring second NIC") self.wait_for_ssh_port(extra_vars["ssh_host"], args.search_pattern, extra_vars["ssh_port"]) subnet_network, subnet_netmask = subnet_cidr.split('/') # Copy and run script to configure routes scp_to_tmp(get_datafile_path('configure_nic.sh'), extra_vars["ssh_host"], extra_vars["ssh_user"], extra_vars["ssh_port"], args.private_key_file) cmd = ("sudo /tmp/configure_nic.sh " "--subnet_network {} --subnet_netmask {} --cloud {}").format( subnet_network, subnet_netmask, self.name) rc, stdout, stderr = remote_exec_command(extra_vars["ssh_host"], extra_vars["ssh_port"], extra_vars["ssh_user"], args.private_key_file, cmd) if rc: raise YBOpsRuntimeError( "Could not configure second nic {} {}".format(stdout, stderr)) # Since this is on start, wait for ssh on default port # Reboot instance remote_exec_command(extra_vars["ssh_host"], extra_vars["ssh_port"], extra_vars["ssh_user"], args.private_key_file, 'sudo reboot') self.wait_for_ssh_port(extra_vars["ssh_host"], args.search_pattern, extra_vars["ssh_port"]) # Verify that the command ran successfully: rc, stdout, stderr = remote_exec_command(extra_vars["ssh_host"], extra_vars["ssh_port"], extra_vars["ssh_user"], args.private_key_file, 'ls /tmp/dhclient-script-*') if rc: raise YBOpsRuntimeError("Second nic not configured at start up")
def callback(self, args): host_info = self.cloud.get_host_info(args) if not host_info: raise YBOpsRuntimeError("Instance: {} does not exist, cannot run preflight checks" .format(args.search_pattern)) results = {} logging.info("Running {} preflight checks for instance: {}".format( args.precheck_type, args.search_pattern)) self.update_ansible_vars_with_args(args) self.update_ansible_vars_with_host_info(host_info, args.custom_ssh_port) try: is_configure = args.precheck_type == "configure" self.wait_for_host(args, default_port=is_configure) except YBOpsRuntimeError as e: logging.info("Failed to connect to node {}: {}".format(args.search_pattern, e)) # No point continuing test if ssh fails. results["SSH Connection"] = False print(json.dumps(results, indent=2)) return scp_result = scp_to_tmp( get_datafile_path('preflight_checks.sh'), self.extra_vars["private_ip"], self.extra_vars["ssh_user"], self.extra_vars["ssh_port"], args.private_key_file) results["SSH Connection"] = scp_result == 0 ansible_status = self.cloud.setup_ansible(args).run("test_connection.yml", self.extra_vars, host_info, print_output=False) results["Try Ansible Command"] = ansible_status == 0 cmd = "/tmp/preflight_checks.sh --type {} --yb_home_dir {} --mount_points {}".format( args.precheck_type, YB_HOME_DIR, self.cloud.get_mount_points_csv(args)) if args.install_node_exporter: cmd += " --install_node_exporter" if args.air_gap: cmd += " --airgap" self.update_ansible_vars_with_args(args) self.update_ansible_vars_with_host_info(host_info, args.custom_ssh_port) rc, stdout, stderr = remote_exec_command( self.extra_vars["private_ip"], self.extra_vars["ssh_port"], self.extra_vars["ssh_user"], args.private_key_file, cmd) if rc != 0: results["Preflight Script Error"] = stderr else: # stdout will be returned as a list of lines, which should just be one line of json. stdout = json.loads(stdout[0]) stdout = {k: v == "true" for k, v in stdout.iteritems()} results.update(stdout) output = json.dumps(results, indent=2) print(output)
def verify_startup_script(self, args, host_info): cmd = "cat /etc/yb-boot-script-complete" rc, stdout, stderr = remote_exec_command(host_info['ssh_host'], host_info['ssh_port'], host_info['ssh_user'], args.private_key_file, cmd) if rc != 0: raise YBOpsRuntimeError( 'Failed to read /etc/yb-boot-script-complete {}\nSTDOUT: {}\nSTDERR: {}\n' .format(args.search_pattern, stdout, stderr)) if len(stdout) > 0: if stdout[0].rstrip(os.linesep) != args.boot_script_token: raise YBOpsRuntimeError( '/etc/yb-boot-script-complete on {} has incorrect token {}' .format(args.search_pattern, stdout))
def wait_for_startup_script(self, args, host_info): if self._wait_for_startup_script_command: rc, stdout, stderr = remote_exec_command( host_info['ssh_host'], host_info['ssh_port'], host_info['ssh_user'], args.private_key_file, self._wait_for_startup_script_command) if rc != 0: logging.error( 'Failed to wait for startup script completion on {}:'. format(args.search_pattern)) if stdout: logging.error('STDOUT: {}'.format(stdout)) if stderr: logging.error('STDERR: {}'.format(stderr)) return rc == 0 return True
def execute_boot_script(self, args, extra_vars): dest_path = os.path.join("/tmp", os.path.basename(args.boot_script)) # Make it executable, in case it isn't one. st = os.stat(args.boot_script) os.chmod(args.boot_script, st.st_mode | stat.S_IEXEC) scp_to_tmp( args.boot_script, extra_vars["ssh_host"], extra_vars["ssh_user"], extra_vars["ssh_port"], args.private_key_file) cmd = "sudo {}".format(dest_path) rc, stdout, stderr = remote_exec_command( extra_vars["ssh_host"], extra_vars["ssh_port"], extra_vars["ssh_user"], args.private_key_file, cmd) if rc: raise YBOpsRuntimeError( "[app] Could not run bootscript {} {}".format(stdout, stderr))
def callback(self, args): host_info = self.cloud.get_host_info(args) if not host_info: raise YBOpsRuntimeError( "Instance: {} does not exist, cannot run preflight checks". format(args.search_pattern)) results = {} logging.info("Running {} preflight checks for instance: {}".format( args.precheck_type, args.search_pattern)) self.update_ansible_vars_with_args(args) self.update_ansible_vars_with_host_info(host_info, args.custom_ssh_port) try: is_configure = args.precheck_type == "configure" self.wait_for_host(args, default_port=is_configure) except YBOpsRuntimeError as e: logging.info("Failed to connect to node {}: {}".format( args.search_pattern, e)) # No point continuing test if ssh fails. results["SSH Connection"] = False print(json.dumps(results, indent=2)) return scp_result = scp_to_tmp(get_datafile_path('preflight_checks.sh'), self.extra_vars["private_ip"], self.extra_vars["ssh_user"], self.extra_vars["ssh_port"], args.private_key_file) results["SSH Connection"] = scp_result == 0 ssh_options = { "ssh_user": "******", "ssh_host": self.extra_vars["private_ip"], "ssh_port": self.extra_vars["ssh_port"], "private_key_file": args.private_key_file } if args.root_cert_path is not None: self.verify_certificates("Server", args.root_cert_path, args.server_cert_path, args.server_key_path, ssh_options, args.skip_cert_validation, results) if args.root_cert_path_client_to_server is not None: self.verify_certificates("Server clientRootCA", args.root_cert_path_client_to_server, args.server_cert_path_client_to_server, args.server_key_path_client_to_server, ssh_options, args.skip_cert_validation, results) if args.client_cert_path is not None: root_cert_path = args.root_cert_path_client_to_server \ if args.root_cert_path_client_to_server is not None else args.root_cert_path self.verify_certificates( "Client", root_cert_path, args.client_cert_path, args.client_key_path, ssh_options, 'HOSTNAME', # not checking hostname for that serts results) sudo_pass_file = '/tmp/.yb_sudo_pass.sh' self.extra_vars['sudo_pass_file'] = sudo_pass_file ansible_status = self.cloud.setup_ansible(args).run( "send_sudo_pass.yml", self.extra_vars, host_info, print_output=False) results["Try Ansible Command"] = ansible_status == 0 ports_to_check = ",".join([ str(p) for p in [ args.master_http_port, args.master_rpc_port, args.tserver_http_port, args.tserver_rpc_port, args.cql_proxy_http_port, args.cql_proxy_rpc_port, args.ysql_proxy_http_port, args.ysql_proxy_rpc_port, args.redis_proxy_http_port, args.redis_proxy_rpc_port, args.node_exporter_http_port ] if p is not None ]) cmd = "/tmp/preflight_checks.sh --type {} --yb_home_dir {} --mount_points {} " \ "--ports_to_check {} --sudo_pass_file {} --cleanup".format( args.precheck_type, YB_HOME_DIR, self.cloud.get_mount_points_csv(args), ports_to_check, sudo_pass_file) if args.install_node_exporter: cmd += " --install_node_exporter" if args.air_gap: cmd += " --airgap" self.update_ansible_vars_with_args(args) self.update_ansible_vars_with_host_info(host_info, args.custom_ssh_port) rc, stdout, stderr = remote_exec_command(self.extra_vars["private_ip"], self.extra_vars["ssh_port"], self.extra_vars["ssh_user"], args.private_key_file, cmd) if rc != 0: results["Preflight Script Error"] = stderr else: # stdout will be returned as a list of lines, which should just be one line of json. stdout = json.loads(stdout[0]) stdout = {k: v == "true" for k, v in iteritems(stdout)} results.update(stdout) output = json.dumps(results, indent=2) print(output)