Created
October 3, 2016 15:40
-
-
Save brandonaaskov/021cd9bb977ae62d53f7a8c8cc353dd9 to your computer and use it in GitHub Desktop.
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 babel-node | |
var _ = require('lodash') | |
var exec = require('child_process').exec | |
var onDockerStopContainer = function (err, stdout, stderr) { | |
if (err) { | |
return console.error(err) | |
} | |
exec('docker rm ' + this.containerId, onDockerRemoveContainer.bind(this)) | |
} | |
var onDockerRemoveContainer = function (err, stdout, stderr) { | |
if (err) { | |
return console.error(err) | |
} | |
console.log('Removed ' + this.containerId + ': ' + this.names) | |
} | |
var stopContainers = function (json) { | |
_.forEach(json, function (container) { | |
exec('docker stop ' + container.containerId, onDockerStopContainer.bind(container)) | |
}) | |
} | |
exec('docker ps -a', function (err, stdout, stderr) { | |
var lines = stdout.split('\n') | |
var json = _.reduce(lines, function (total, line, count) { | |
var chunks = _.compact(line.split(' ')) | |
if (count === 0) return total | |
if (_.isEmpty(chunks)) return total | |
total.push({ | |
containerId: chunks[0], | |
imageId: chunks[1], | |
command: chunks[2], | |
created: chunks[3], | |
status: chunks[4], | |
names: chunks[5] | |
}) | |
return total | |
}, []) | |
stopContainers(json) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment