-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
184 lines (152 loc) · 4.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var Docker = require('dockerode'),
dockgen = require('dockgen'),
temp = require('temp'),
fs = require('fs'),
spawn = require('child_process').spawn;
temp.track();
var Dockship = function(opts) {
this.opts = opts || {};
this.opts.image_prefix = opts.image_prefix || 'dockship_';
this.docker = new Docker({
host: this.opts.host || 'http://127.0.0.1',
port: this.opts.port || 4243,
});
}
// Image stuff
Dockship.prototype.listApps = function(appName, cb) {
var self = this;
this.docker.listImages(function(err, data) {
if(err) return cb(err);
cb(null, data.map(function(app) {
// Check if image starts with predefined prefix
if(app.RepoTags[0].indexOf(self.opts.image_prefix) === 0) {
// If appName is set, only return images matching that
if(! appName || self.opts.image_prefix + appName + ':latest' === app.RepoTags[0]) {
return {
name: self.getAppName(app.RepoTags[0]),
id: app.Id
};
}
}
}).clean());
});
};
Dockship.prototype.deleteApp = function(appId, cb) {
var self = this;
var app = this.docker.getImage(self.opts.image_prefix + appId + ':latest');
app.remove(cb);
};
Dockship.prototype.deployApp = function(appName, cb) {
var self = this;
temp.mkdir('dockship', function(err, dir) {
if(err) return cb(err);
fs.writeFileSync(dir + '/Dockerfile', dockgen(process.cwd()));
var tar_stream = spawn('tar', [
'cv', './',
'-C', dir, 'Dockerfile'
]);
tar_stream.stderr.pipe(process.stderr);
self.docker.buildImage(tar_stream.stdout, {t: self.opts.image_prefix + appName}, cb);
});
};
// Container stuff
Dockship.prototype.listAppInstances = function(cb) {
var self = this;
this.docker.listContainers({all: false}, function(err, instances) {
if(err) return cb(err);
instances = instances.map(function(instance) {
// Check if image starts with predefined prefix
if(instance.Image.indexOf(self.opts.image_prefix) === 0) {
// If appName is set, only return images matching that
instance.Image = self.getAppName(instance.Image);
return instance;
}
}).clean();
cb(null, instances);
});
};
Dockship.prototype.getAppIds = function(appName, cb) {
this.listAppInstances(function(err, data) {
if(err) return cb(err);
cb(null, data.map(function(container) {
return(container.Id);
}).clean());
});
};
Dockship.prototype.findAppIds = function(appName, cb) {
var self = this;
this.listAppInstances(function(err, data) {
if(err) return cb(err);
cb(null, data.map(function(container) {
if(container.Image === appName || appName === '--all') {
return(container.Id);
}
}).clean());
});
};
Dockship.prototype.getInstanceLogs = function(instanceId, stdout_stream, stderr_stream, cb) {
var container = this.docker.getContainer(instanceId);
container.attach({stdout: true, stderr: true, stream: true, logs: true, tty: false}, function(err, stream) {
if(err) {
cb(err);
} else {
//stream.pipe(process.stdout);
container.modem.demuxStream(stream, process.stdout, process.stdout);
cb();
}
});
}
Dockship.prototype.startApp = function(appId, cb) {
this.docker.createContainer({
Image: appId,
AttachStdout: true,
AttachStderr: true,
}, function(err, container) {
if(err) return cb(err);
container.inspect(function(err, containerDetails) {
if(err) return cb(err);
var cfg = {
PortBindings: {}
}
var exposedPorts = containerDetails.Config.ExposedPorts;
for(var i in exposedPorts) {
cfg.PortBindings[i] = [
{
HostIp: '0.0.0.0',
HostPort: i.split('/')[0]
}
]
}
container.start(cfg, function(err) {
if(err) return cb(err);
cb(null, container.id);
});
});
});
};
Dockship.prototype.getAppName = function(instanceName) {
return instanceName
.replace(new RegExp('^' + this.opts.image_prefix), '')
.replace(/:latest$/, '');
};
Dockship.prototype.getInstanceName = function(appName) {
return this.opts.image_prefix + appName + ':latest';
}
Dockship.prototype.stopApp = function(appId, cb) {
var container = this.docker.getContainer(appId);
container.stop(function(err) {
if(err) return cb(err);
container.remove(cb);
});
};
module.exports = Dockship;
// Helpers
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] === deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};