def prepare_scripts(self, host, is_server): if self.scripts_prepared_for_host.get(host, False): return gateway_host = SshConn.get_gateway_host(host) # copy scripts to host scripts_dir = os.path.join(self.env_monitoring_config['app_path'], self.APP_SCRIPTS_FOLDER) script_files = [f for f in os.listdir(scripts_dir) if os.path.isfile(os.path.join(scripts_dir, f))] script_mode = stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | \ stat.S_IROTH | stat.S_IXOTH target_host = host if is_server else gateway_host self.make_remote_dir(target_host, self.REMOTE_SCRIPTS_FOLDER) for file_name in script_files: remote_path = os.path.join(self.REMOTE_SCRIPTS_FOLDER, file_name) local_path = os.path.join(scripts_dir, file_name) if not os.path.isfile(local_path): continue if is_server: ssh = self.get_ssh(target_host, for_sftp=True) ssh.copy_file(local_path, remote_path, mode=script_mode) else: self.copy_to_remote_host(target_host, local_path, remote_path, mode=script_mode, make_remote_dir=False) self.scripts_prepared_for_host[host] = True
def run(self, cmd, ssh_to_host="", enable_cache=True, on_gateway=False, ssh=None, use_sudo=True): ssh_conn = ssh if ssh else SshConn(ssh_to_host) commands = self.adapt_cmd_to_env(ssh_conn, cmd, use_sudo, on_gateway, ssh_to_host) out = '' for c in commands: out += self.run_single_command(c, ssh_conn, ssh_to_host, enable_cache=enable_cache) return out
def get_ssh(self, host, is_container=False, for_sftp=False): ssh = SshConnection.get_ssh(host, for_sftp) if not ssh: conf = self.env_monitoring_config if is_container or host == conf['server_ip']: host = conf['server_ip'] port = int(conf['ssh_port']) user = conf['ssh_user'] pwd = conf['ssh_password'] ssh = SshConnection(host, user, _pwd=pwd, _port=port, for_sftp=for_sftp) else: ssh = SshConn(host, for_sftp=for_sftp) return ssh
def deploy_ssl_files(self, hosts: list): try: monitoring_server = self.env_monitoring_config['server_ip'] gateway_host = SshConn.get_gateway_host(hosts[0]) temp_dir = tempfile.TemporaryDirectory() for file_path in self.fetch_ssl_files: # copy SSL files from the monitoring server file_name = os.path.basename(file_path) local_path = os.path.join(temp_dir.name, file_name) self.get_file(monitoring_server, file_path, local_path) # first copy the files to the gateway self.write_to_remote_host(gateway_host, local_path, remote_path=file_path) ssl_path = os.path.commonprefix(self.fetch_ssl_files) for host in hosts: self.copy_from_gateway_to_host(host, ssl_path, ssl_path) except SshError: self.had_errors = True
def is_gateway_host(ssh_to_host): ssh_conn = SshConn(ssh_to_host) return ssh_conn.is_gateway_host(ssh_to_host)