diff --git a/spug_api/apps/exec/executors.py b/spug_api/apps/exec/executors.py index 2c8c884..de61db6 100644 --- a/spug_api/apps/exec/executors.py +++ b/spug_api/apps/exec/executors.py @@ -14,9 +14,9 @@ def exec_worker_handler(job): class Job: - def __init__(self, hostname, port, username, pkey, command, token=None): + def __init__(self, key, hostname, port, username, pkey, command, token=None): self.ssh = SSH(hostname, port, username, pkey) - self.key = f'{hostname}:{port}' + self.key = key self.command = command self.token = token self.rds_cli = None diff --git a/spug_api/apps/exec/views.py b/spug_api/apps/exec/views.py index 5553984..6bd5020 100644 --- a/spug_api/apps/exec/views.py +++ b/spug_api/apps/exec/views.py @@ -56,6 +56,7 @@ def do_task(request): token, rds = uuid.uuid4().hex, get_redis_connection() for host in Host.objects.filter(id__in=form.host_ids): data = dict( + key=host.id, token=token, hostname=host.hostname, port=host.port, diff --git a/spug_web/src/pages/exec/task/ExecConsole.js b/spug_web/src/pages/exec/task/ExecConsole.js index 0a87201..362ed1a 100644 --- a/spug_web/src/pages/exec/task/ExecConsole.js +++ b/spug_web/src/pages/exec/task/ExecConsole.js @@ -37,6 +37,9 @@ class ExecConsole extends React.Component { const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; this.socket = new WebSocket(`${protocol}//${window.location.host}/api/ws/exec/${store.token}/?x-token=${X_TOKEN}`); this.socket.onopen = () => { + for (let key of Object.keys(store.outputs)) { + this.handleWrite(key, '\x1b[36m### Waiting for scheduling ...\x1b[0m\r\n') + } this.socket.send('ok'); }; this.socket.onmessage = e => { @@ -46,13 +49,7 @@ class ExecConsole extends React.Component { const {key, data, status} = JSON.parse(e.data); if (status !== undefined) store.outputs[key].status = status; if (data) { - if (this.terms[key]) { - this.terms[key].write(data) - } else if (this.outputs[key]) { - this.outputs[key] += data - } else { - this.outputs[key] = data - } + this.handleWrite(key, data) } } }; @@ -60,9 +57,9 @@ class ExecConsole extends React.Component { for (let key of Object.keys(store.outputs)) { store.outputs[key]['status'] = 'websocket error' if (this.terms[key]) { - this.terms[key].write('\u001b[31mWebsocket connection failed!\u001b[0m') + this.terms[key].write('\x1b[31mWebsocket connection failed!\x1b[0m') } else { - this.outputs[key] = '\u001b[31mWebsocket connection failed!\u001b[0m' + this.outputs[key] = '\x1b[31mWebsocket connection failed!\x1b[0m' } } } @@ -73,6 +70,16 @@ class ExecConsole extends React.Component { store.isFullscreen = false; } + handleWrite = (key, data) => { + if (this.terms[key]) { + this.terms[key].write(data) + } else if (this.outputs[key]) { + this.outputs[key] += data + } else { + this.outputs[key] = data + } + } + genExtra = (status) => { if (status === -2) { return ; diff --git a/spug_web/src/pages/exec/task/index.js b/spug_web/src/pages/exec/task/index.js index c6e8c67..0f7c0f4 100644 --- a/spug_web/src/pages/exec/task/index.js +++ b/spug_web/src/pages/exec/task/index.js @@ -24,6 +24,10 @@ class TaskIndex extends React.Component { } } + componentWillUnmount() { + store.host_ids = [] + } + handleSubmit = () => { this.setState({loading: true}); const host_ids = store.host_ids; diff --git a/spug_web/src/pages/exec/task/store.js b/spug_web/src/pages/exec/task/store.js index b05d4d8..16ff3c1 100644 --- a/spug_web/src/pages/exec/task/store.js +++ b/spug_web/src/pages/exec/task/store.js @@ -29,9 +29,8 @@ class Store { } else { for (let id of this.host_ids) { const host = hostStore.idMap[id]; - const key = `${host.hostname}:${host.port}`; - this.outputs[key] = { - title: `${host.name}(${key})`, + this.outputs[host.id] = { + title: `${host.name}(${host.hostname}:${host.port})`, status: -2 } }