U 批量执行输出内容自适应屏幕宽度

pull/586/head
vapao 2022-07-10 00:27:28 +08:00
parent 93a5beb43d
commit 9ca6e1dee8
4 changed files with 17 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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') {