Created
November 7, 2016 20:54
-
-
Save jonasfj/4ea4a3d42a41f212ae419c53679e0f88 to your computer and use it in GitHub Desktop.
Connect to a one-click-loaner shell using local terminal emulator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"private": true, | |
"dependencies": { | |
"docker-exec-websocket-server": "^1.3.1" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let https = require('https') | |
let URL = require('url'); | |
let querystring = require('querystring'); | |
let DockerClient = require('docker-exec-websocket-server').DockerExecClient; | |
https.get(process.argv[2], res => { | |
if (res.statusCode != 303) { | |
console.log("Bad request"); | |
} | |
let socketUrl = querystring.parse(URL.parse(res.headers.location).query).socketUrl; | |
let client = new DockerClient({ | |
url: socketUrl, | |
tty: true, | |
command: [ | |
'sh', '-c', [ | |
'if [ -f "/etc/taskcluster-motd" ]; then cat /etc/taskcluster-motd; fi;', | |
'if [ -z "$TERM" ]; then export TERM=xterm; fi;', | |
'if [ -z "$HOME" ]; then export HOME=/root; fi;', | |
'if [ -z "$USER" ]; then export USER=root; fi;', | |
'if [ -z "$LOGNAME" ]; then export LOGNAME=root; fi;', | |
'if [ -z `which "$SHELL"` ]; then export SHELL=bash; fi;', | |
'if [ -z `which "$SHELL"` ]; then export SHELL=sh; fi;', | |
'if [ -z `which "$SHELL"` ]; then export SHELL="/.taskclusterutils/busybox sh"; fi;', | |
'SPAWN="$SHELL";', | |
'if [ "$SHELL" = "bash" ]; then SPAWN="bash -li"; fi;', | |
'if [ -f "/bin/taskcluster-interactive-shell" ]; then SPAWN="/bin/taskcluster-interactive-shell"; fi;', | |
'exec $SPAWN;' | |
].join('') | |
] | |
}); | |
client.execute().then(() => { | |
process.stdin.setRawMode(true); | |
process.on('exit', () => { | |
process.stdin.setRawMode(false); | |
}); | |
process.stdout.on('resize', () => { | |
client.resize(process.stdout.rows, process.stdout.columns); | |
}); | |
process.stdin.pipe(client.stdin); | |
client.stdout.pipe(process.stdout); | |
client.stderr.pipe(process.stderr); | |
client.on('exit', (exitCode) => { | |
process.exit(exitCode); | |
}); | |
client.resize(process.stdout.rows, process.stdout.columns); | |
}).catch(err => { | |
console.log("Error: %s", err); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment