await Promise.all(containers.map(async function (containerInfo) { if (containerInfo.Image == TAG) { console.log(`deleting existing container ${ containerInfo.Id }`); await docker.getContainer(containerInfo.Id).denodeify.remove({force: true}); } }));
backupContainer(containerName, function(err) { if (err) { console.log('backup failed, not stopping this container now'); delete stoppingContainerWaiters[containerName]; } else { var container = docker.getContainer(containerName); container.stop(function(err) { var waiters = stoppingContainerWaiters[containerName]; delete stoppingContainerWaiters[containerName]; if (err) { console.log('failed to stop container', containerName, err); updateContainerList(); } else { delete startedContainers[containerName]; console.log('stopped container', containerName); } if (waiters.length) { container.start(function(err, res) { for (var i=0; i<waiters.length; i++) { waiters[i](err); } }); } }); } });
return new Promise(async (resolve, reject) => { try { const user = await authenticate(username, token); const deployment = await DeploymentModel.findOne({ name, username: user.username }); const logs = []; const container = docker.getContainer(deployment.id); const logStream = new stream.PassThrough(); logStream.on('data', (chunk) => { logs.push(chunk.toString('utf8')); }); const cStream = await container.logs({ follow: true, stdout: true, stderr: true }); container.modem.demuxStream(cStream, logStream, logStream); cStream.on('end', function(){ return resolve(logs); }); setTimeout(function() { cStream.destroy(); }, 2000); } catch(ex) { return reject(ex); } });
function syncContainer (message) { if (message.status !== 'start') { storage.removeImage(message.id); return; } var container = docker.getContainer(message.id); container.inspect(function (err, data) { if (err) { return logger.error(err); } var config = getConfig(message, data); var hosts = getHosts(config); var services = getServices(config); var i; for (i = 0; i < hosts.length; i++) { storage.addVhost(hosts[i]); } for (i = 0; i < services.length; i++) { storage.addService(services[i]); } }); }
/** * removes a specific container, will stop and cleanup all necessary files * @method remove * @memberof Deployment * @param {String} name - the name of the container * @param {String} options.token - the token for the user who owns the deployment * @param {String} options.username - the username associated with this deployment * @return {Promise} */ static async remove({ subdomain, token, username }) { let user = await authenticate(username, token); let deployment = await DeploymentModel.findOne({ subdomain: subdomain, username: user.username }); try { const container = docker.getContainer(deployment.id); await container.kill(); let info = await container.inspect(); await docker.getImage(info.Image).remove({ force: true }); await container.remove(); } catch(ex) { if(ex.reason !== 'no such container') { throw new Error(ex); } } return deployment; }
function signupMail(username, password){ var container = docker.getContainer(mailId); container.exec({cmd: 'poste email:create ' + username + ' ' + password}) cp.exec('docker exec ' + mailId + ' poste email:create ' + username + ' ' + password, function(err, stdout, stderr){ console.log(stdout); }); }
containers.attach = function (containerId, stdin, stdout) { var container = docker.getContainer(containerId); var attach_opts = { stream: true, stdin: true, stdout: true, stderr: true }; container.attach(attach_opts, function (err, stream) { stdin.pipe(stream).pipe(stdout); stdin.on('close', function () { container.stop({}, function(err, data) { console.log("container ", container.id, " has been stopped."); }); container.remove({}, function(err, data) { console.log("container ", container.id, " has been removed."); }); }); container.start(function (err, data) { console.log("container ", container.id, " has been attached."); }); }); };
function forwardTerminalData(containerId, ws) { var container = docker.getContainer(containerId); var options = { "AttachStdin": true, "AttachStdout": true, "AttachStderr": true, "Tty": true, Cmd: ['bash'] }; container.exec(options, function(err, exec) { if (err) { throw err; } exec.start({stdin: true},function(err, execStream) { if (err) { throw err; } var output = new stream.PassThrough(); container.modem.demuxStream(execStream, output, output); output.on('data', function(chunk) { ws.send(chunk.toString()); }); ws.on('message', function(msg) { execStream.write(msg); }); }); }); }
exports.remove = function(options, callback) { console.log('whatt....'); console.log(options); var container = docker.getContainer(options.container); container.kill(function(err) { console.log('killed', err); if (err) { callback(err); } else { container.remove(function(err) { console.log('removed', err); if (err) { callback(err); } else { var image = docker.getImage(options.image); image.remove(function(err) { if (err) { callback(err); } else { console.log('image r', err); callback(err); } }); } }); } }); };
containerInfo.Names.map(name => { if (name.includes(containerName)) { let container = docker.getContainer(containerInfo.Id); container.remove({ force: true }, function (err, data) {}) } });
function inspectContainer(containerName, callback) { docker.getContainer(containerName).inspect(function handler(err, res) { var ipaddr = res.NetworkSettings.IPAddress; callback(err, { ipaddr: ipaddr, lastAccessed: new Date().getTime() }); }); }
DockerInspect.prototype.inspect = function (id, cb) { if (!cache[this.container]) { docker.getContainer(id).inspect(this.inspectHandler.bind({ callback: cb, container: id })) } else { cb(null, cache[this.container]) } }
return new Promise( (resolve, reject) => { container = docker.getContainer(id); container.remove({force: true},(err,data) => { if(err) { console.log("[remove]" + err); reject(err); return; } resolve(); }); });
containers.forEach(function(info) { var container = docker.getContainer(info.Id); container.stop(function(err) { if (err) { return console.error(err.stack); } container.remove(function() {}); }); });
splitEvents.pipe(through.obj(function(chunk, enc, cb) { var _this = this; var data = JSON.parse(chunk); var container = docker.getContainer(data.id); container.inspect(function(err, containerData) { if (!err) { _this.push(toEmit(data, containerData)); } cb(); }); })).pipe(result, {end: false});
containers.forEach(function(containerInfo) { var name = ctn.name.parse(containerInfo.Names[0]); var shouldPurge = name === null || !ctn.name.isKalaboxName(name); if (shouldPurge) { docker.getContainer(containerInfo.Id).remove(function(err, data) { if (cbRemove) { cbRemove(containerInfo); } }); } });
.then(cont => { debug('start:select', 'ID is', cont.id); let container = docker.getContainer(cont.id) container.stop(err => { if(err) { return res.error('INTERNAL_CONTAINER_FAILED_TO_STOP'); } return res.success('CONTAINER_STOPPED'); }) })
getContainerStatus(options.createOptions.name, function(err, containerStatus) { if (err) { callback(err); } else if (containerStatus.started) { callback(null, containerStatus); } else if (containerStatus.exists) { container = docker.getContainer(options.createOptions.name); startContainer(container, options.createOptions.name, callback); } else { createAndStart(options, callback); } });
/** * starts a container or all containers * @method start * @memberof Deployment * @param {String=} name - to start a specific container a name property is needed * @param {String} options.token - the token for the user who owns the deployment * @param {String} options.username - the username associated with this deployment * @return {Promise} */ static async start({ name, username, token }) { let opts = {}; // TODO: check username and token to make sure the request is authenticated if(username && token) { opts.username = username; } const deployments = await DeploymentModel.find(opts); if(deployments.length === 0) return; for(var i = 0; i < deployments.length; i++) { const deployment = deployments[i]; if(name && deployment.name == name) { await docker.getContainer(deployment.id).start(); } if(!name) { await docker.getContainer(deployment.id).start(); } } return deployments; }
docker.containerExistsByName(domain).then(function(container){ if(container.Status.indexOf('Exit') > -1){ var new_container = docker.getContainer(container.Id); new_container.start(function(err,data){ if(err){ reject(err); }else{ resolve(new_container); } }); }else{ reject(['already started',container]); } }).fail( function(){
containers.forEach(function (container) { if (container.Created < time) { console.log('removing', container.Id); docker.getContainer(container.Id).remove({ force: true }, function (err) { console.log('removed', container.Id); if (err) { console.log(err); } }); } else { console.log(container.Created, time); } });
return new Promise((resolve, reject) => { const container = this.docker.getContainer(id); container.stop((err, data) => { if (err) { return reject(err); } setTimeout(() => { container.remove((err, data) => { if (err) { return reject(err); } resolve(); }); }, STOP_GRACE_PERIOD); }); });
//function to get container information required to construct //container url function getContainerInfo(containerId,tutorialOutput, response){ var container = docker.getContainer(containerId); container.inspect(function (err, data) { logger.info("Container Information:") logger.info("Ip Address: " + data.NetworkSettings.IPAddress); var portdetails = JSON.stringify(data.NetworkSettings.Ports); var res = portdetails.replace("8081/tcp","port"); var portJson = JSON.parse(res); var containerPort = portJson.port[0].HostPort; logger.info("Container Port : "+ portJson.port[0].HostPort); var url1 = 'http://'+env.dockerHost+':'+containerPort+'/api/deploy/3'; postTutorialOutput(tutorialOutput, url1, response); }) }
function smartStartContainer(domain, localDataPath, callback) { var config = configReader.getConfig(domain); docker.getContainer(domain).start(function(err, res) { if (err && err.statusCode === 404) { createContainer(domain, config.application, config.backendEnv, localDataPath, function(err, container) { if (err) { callback(err); } else{ container.start(callback); } }); } else { callback(err, res); } }); }
/** * decorates a deployment with the correct data on get * @method decorate * @memberof Deployment * @param {Deployment} deployment - a deployment instance * @return {Promise} */ static async decorate(deployment) { let requests = await count({ subdomain: deployment.subdomain }); deployment.requests = requests || 0; const container = docker.getContainer(deployment.id); try { let info = await container.inspect(); deployment.status = info.State.Status; } catch(ex) { deployment.status = 'error'; } return deployment; }
.then( function(container){ if(container.Status.indexOf('Exit') != -1){ docker.getContainer(container.Id).remove( function(err){ if(err) reject(err); resolve('domain deleted'); }); }else{ self.stop(domain) .then(function(res){ docker.getContainer(container.Id).remove( function(err){ if(err) reject(err); resolve('domain deleted'); }) }) } })
function getContainerStatus(containerName, callback) { docker.getContainer(containerName).inspect(function(err, res) { if (err) { if (err.statusCode === 404) { callback(null, { started: false, exists: false }); } else { callback(err); } } else { var ipaddr = res.NetworkSettings.IPAddress; callback(err, { ipaddr: ipaddr, started: res.State.Running, exists: true }); } }); }
exec(containerName, preShutDownCommand, function(err) { if (err) { callback('backup failed, not stopping this container now'); delete stoppingContainerWaiters[containerName]; } else { var container = docker.getContainer(containerName); container.stop(function(err) { var waiters = stoppingContainerWaiters[containerName]; delete stoppingContainerWaiters[containerName]; callback(err); if (waiters.length) { container.start(function(err, res) { for (var i=0; i<waiters.length; i++) { waiters[i](err); } }); } }); } });
function backupContainer(containerName, callback) { docker.getContainer(containerName).exec({ Cmd: [ 'sh', '/backup.sh' ] }, function(err, exec) { exec.start(function(err, stream) { if (err) { if (callback) { callback(err); } } else { stream.setEncoding('utf8'); stream.pipe(process.stdout); stream.on('end', function() { console.log('done with sh /backup.sh inside the container'); if (callback) { callback(null); } }); } }); }); }
redisclient.get(hits._source.program,function(err,res){ var container = docker.getContainer(res); console.log(container); var params = { room: 'Alert', // Found in the JSON response from the call above from: 'System', message: 'Restarted' + JSON.stringify(container), color: 'red' }; HC.postMessage(params, function(data) { console.log(data); }); container.restart(function(err){ if(err){ console.log(err); } }); request.post('https://qpush.me/pusher/push_site/', {form:{"name":"twistedogic","code":"262156","sig":"","cache":"false","msg[text]":'Restarted' + JSON.stringify(container)} }); });