diff --git a/spug_api/apps/exec/executors.py b/spug_api/apps/exec/executors.py index f85e754..94b1147 100644 --- a/spug_api/apps/exec/executors.py +++ b/spug_api/apps/exec/executors.py @@ -14,8 +14,9 @@ def exec_worker_handler(job): class Job: - def __init__(self, key, name, hostname, port, username, pkey, command, interpreter, params=None, token=None): - self.ssh = SSH(hostname, port, username, pkey) + def __init__(self, key, name, hostname, port, username, pkey, command, interpreter, params=None, token=None, + term=None): + self.ssh = SSH(hostname, port, username, pkey, term=term) self.key = key self.command = self._handle_command(command, interpreter) self.token = token diff --git a/spug_api/apps/exec/views.py b/spug_api/apps/exec/views.py index 03bb8e4..a6db18c 100644 --- a/spug_api/apps/exec/views.py +++ b/spug_api/apps/exec/views.py @@ -92,9 +92,14 @@ class TaskView(View): @auth('exec.task.do') def patch(self, request): form, error = JsonParser( - Argument('token', help='参数错误') + Argument('token', help='参数错误'), + Argument('cols', type=int, required=False), + Argument('rows', type=int, required=False) ).parse(request.body) if error is None: + term = None + if form.cols and form.rows: + term = {'width': form.cols, 'height': form.rows} rds = get_redis_connection() task = ExecHistory.objects.get(digest=form.token) for host in Host.objects.filter(id__in=json.loads(task.host_ids)): @@ -108,7 +113,8 @@ class TaskView(View): username=host.username, command=task.command, pkey=host.private_key, - params=json.loads(task.params) + params=json.loads(task.params), + term=term ) rds.rpush(settings.EXEC_WORKER_KEY, json.dumps(data)) return json_response(error=error) diff --git a/spug_api/libs/ssh.py b/spug_api/libs/ssh.py index 7878de2..7ffa677 100644 --- a/spug_api/libs/ssh.py +++ b/spug_api/libs/ssh.py @@ -52,12 +52,13 @@ AuthHandler._finalize_pubkey_algorithm = _finalize_pubkey_algorithm class SSH: def __init__(self, hostname, port=22, username='root', pkey=None, password=None, default_env=None, - connect_timeout=10): + connect_timeout=10, term=None): self.stdout = None self.client = None self.channel = None self.sftp = None self.exec_file = None + self.term = term or {} self.eof = 'Spug EOF 2108111926' self.default_env = default_env self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?') @@ -179,7 +180,7 @@ class SSH: return self.channel counter = 0 - self.channel = self.client.invoke_shell() + self.channel = self.client.invoke_shell(**self.term) command = 'set +o history\nset +o zle\nset -o no_nomatch\nexport PS1= && stty -echo\n' command = self._handle_command(command, self.default_env) self.channel.sendall(command) diff --git a/spug_web/src/pages/exec/task/Output.js b/spug_web/src/pages/exec/task/Output.js index f104346..259f5a7 100644 --- a/spug_web/src/pages/exec/task/Output.js +++ b/spug_web/src/pages/exec/task/Output.js @@ -64,7 +64,9 @@ function OutView(props) { term.write(message) socket.send('ok'); fitPlugin.fit() - http.patch('/api/exec/do/', {token: store.token}) + const formData = fitPlugin.proposeDimensions() + formData.token = store.token + http.patch('/api/exec/do/', formData) } socket.onmessage = e => { if (e.data === 'pong') {