sh $ DEPLOY_HOST=host.amazonaws.com DEPLOY_PRIVATE_KEY="$(cat id_rsa)" npx https://gist.github.com/sebasjm/18588bf1f49c48204361f78e260e1bae <service-name> <image-name>
Last active
December 13, 2017 19:19
-
-
Save sebasjm/18588bf1f49c48204361f78e260e1bae to your computer and use it in GitHub Desktop.
update docker swark service through ssh tunnel
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
#!/usr/bin/env node | |
const net = require('net'); | |
const { Client } = require('ssh2'); | |
const Docker = require('dockerode'); | |
const util = require('util'); | |
const conn = new Client(); | |
const serviceName = process.argv[2] | |
if (!serviceName) { | |
console.log("missing service name") | |
return; | |
} | |
const imageName = process.argv[3] | |
if (!imageName) { | |
console.log("missing image name") | |
return; | |
} | |
const deployHost = process.env.DEPLOY_HOST; | |
if (!deployHost) { | |
console.log("missing DEPLOY_HOST env"); | |
return; | |
} | |
const deployKey = process.env.DEPLOY_KEY; | |
if (!deployKey) { | |
console.log("missing DEPLOY_KEY env"); | |
return; | |
} | |
const deployPrivateKey = [ | |
'-----BEGIN RSA PRIVATE KEY-----', | |
deployKey.replace(' ','\n'), | |
'-----END RSA PRIVATE KEY-----' | |
].join('\n') | |
const dockerAuthKey = process.env.DOCKER_AUTH_KEY; | |
console.log("updating",serviceName,"to image",imageName); | |
conn.on('ready', function () { | |
console.log('ssh connection ready...'); | |
const server = net.createServer((sock) => { | |
console.log('new connection...'); | |
conn.openssh_forwardOutStreamLocal('/var/run/docker.sock', (err, stream) => { | |
if (err) { | |
console.log('err', err); | |
conn.end(); | |
throw err; | |
} | |
sock.pipe(stream).pipe(sock); | |
}); | |
}) | |
server.listen('/tmp/ssh2.sock', () => { | |
console.log('local server listen at /tmp/ssh2.sock'); | |
const docker = new Docker({socketPath: '/tmp/ssh2.sock'}); | |
const dockerService = docker.getService(serviceName) | |
dockerService | |
.inspect() | |
.then( service => { | |
console.log('Service Inspect: '); | |
console.log(util.inspect(service, {depth: null})); | |
const opts = service.Spec; | |
opts.version = parseInt(service.Version.Index); | |
opts.TaskTemplate.ContainerSpec.Image = imageName; | |
opts.Labels['com.docker.stack.image'] = imageName; | |
if (dockerAuthKey) opts.authconfig = { key: dockerAuthKey } | |
return dockerService.update(opts) | |
}) | |
.catch( erro => { console.log(erro); }) | |
.then( () => { | |
server.close(); conn.end(); | |
}) | |
}); | |
}).connect({ | |
host: deployHost, | |
port: 22, | |
username: 'docker', | |
privateKey: deployPrivateKey | |
}); |
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
{ | |
"name": "deploy", | |
"version": "1.0.0", | |
"description": "update docker swark service through ssh tunnel", | |
"bin": "./index.js", | |
"author": "[email protected]", | |
"license": "MIT", | |
"dependencies": { | |
"dockerode": "2.5.3", | |
"ssh2": "0.5.5" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment