def get(self, request): filter_keyword = request.GET.get('filter_keyword', '') filter_select = request.GET.get('filter_select', '') host = request.GET.get('host', '') server_type_id = ServerType.objects.get( server_type='file').server_type_id server = Server.objects.get(server_type_id=server_type_id, host=host) path_root = server.file_path_root path = request.GET.get('path') cmd_tail = 'tail -' + lines_for_view + ' ' + path_root + path if filter_select == 'Content' and filter_keyword != '': cmd_tail = cmd_tail + ' | grep ' + filter_keyword text_content = exec_command_over_ssh( server.host, server.port, server.username, server.password, cmd=cmd_tail, ) try: text_content = text_content.decode(encoding='utf8') except Exception as e: logging.error(e) text_content = 'can not decode file content' log_user = request.session.get('username') log_detail = log_user + ' viewer ' + path log_record(log_user=log_user, log_detail=log_detail) context = { 'host': host, 'path': path, 'text_content': text_content, 'filter_select': filter_select, 'filter_keyword': filter_keyword } return render(request, 'text_viewer.html', context)
def get_container_list(self): container_list = [] try: cmd = 'docker ps -a | grep -v IMAGE' stdout = exec_command_over_ssh(self.host, self.port, self.username, self.password, cmd) container_infos = stdout.decode().split('\n') for i in range(0, len(container_infos) - 1): container_info = re.split(' +', container_infos[i]) for j in range(0, len(container_infos[i])): container = Container() container.host = self.host container.host_port = self.port container.container_id = container_info[0] container.image = container_info[1] container.command = container_info[2] container.created = container_info[3] container.status = container_info[4] if 'up' in container.status.lower(): container.statename = 'running' if 'exited' in container.status.lower(): container.statename = 'exited' container.port = container_info[5] container.name = container_info[-1] if container_info[-1] == '': container.name = container_info[-2] container_list.append(container) return (container_list) except Exception as e: logging.error(e) return None
def get_file_list(self, path='/tmp/'): try: cmd = 'ls -alh ' + path stdout = exec_command_over_ssh(self.host, self.host_port, self.host_username, self.host_password, cmd) stdout = stdout.decode(encoding='utf8').split('\n')[2:-1] return (stdout) except Exception as e: logging.error(e) return []
def get_process_list(self): process_list = [] try: cmd = 'supervisorctl status' stdout = exec_command_over_ssh(self.host, self.port, self.username, self.password, cmd) process_infos = stdout.decode().split('\n') for i in range(0, len(process_infos) - 1): process_info = re.split(' +', process_infos[i]) for j in range(0, len(process_infos[i])): process = Process() process.host = self.host process.host_port = self.port process.name = process_info[0] process.statename = process_info[1] process.description = process_info[2] process_list.append(process) return (process_list) except Exception as e: logging.error(e) return None
def container_opt(self, container_opt): cmd = 'docker ' + container_opt + ' ' + self.container_id stdout = exec_command_over_ssh(self.host, self.host_port, self.host_username, self.host_password, cmd) return (stdout)
def process_opt(self, process_opt): cmd = 'supervisorctl ' + process_opt + ' ' + self.name stdout = exec_command_over_ssh(self.host, self.host_port, self.host_username, self.host_password, cmd) return (stdout)