-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
executable file
·209 lines (184 loc) · 6.72 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const {Docker} = require('node-docker-api');
const blessed = require('blessed');
const Compose = require('./lib/compose');
const UIHelpers = require('./ui/helper');
const Settings = require('./lib/settings');
const path = require('path');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
(async () => {
const settings = new Settings();
let periodicTableRefreshTimeout;
const baseComposeFilePath = await settings.getComposePath();
const compose = new Compose(baseComposeFilePath);
const nameOverrides = await settings.getNameOverrides();
let composeFileSettings = await settings.getComposeSettings(baseComposeFilePath);
await compose.parse();
// If this is the first time, enable all services
if (!composeFileSettings.services || Object.keys(composeFileSettings.services).length === 0) {
composeFileSettings = await settings.toggleServices(baseComposeFilePath, Object.keys(compose.getDefinition().services));
}
/**
* Gets table information suitable for display
*/
const getTableInfo = async () => {
const containers = await docker.container.list({ all: true });
const images = await docker.image.list();
return UIHelpers.getList(
compose.getDefinition(),
containers.map(a => a.data),
images.map(a => a.data),
composeFileSettings.services,
nameOverrides,
path.dirname(baseComposeFilePath[0]).split('/').pop(),
);
};
/**
* Re-Renders the table
* @param {object} table Table Object
*/
const renderTable = async (table) => {
if (periodicTableRefreshTimeout) {
clearTimeout(periodicTableRefreshTimeout);
periodicTableRefreshTimeout = null;
}
const currentData = await getTableInfo();
const idx = table.selected;
table.setData(currentData.display);
table.select(idx);
screen.render();
screen.emit('resize');
periodicTableRefreshTimeout = setTimeout(() => {
renderTable(table);
}, 5000);
};
const screen = blessed.screen({
fastCSR: true,
});
const titleText = await settings.getTitle();
screen.title = titleText;
const { table, statusText } = require('./ui')(screen, titleText);
// Listeners
const listeners = require('./lib/listeners');
listeners.listen(screen, table, nameOverrides);
/**
* Gets the active/inactive Docker container object given a Docker Compose service
* @param {string} serviceName Compose Service Name
*/
const getContainer = async (serviceName) => {
const ti = await getTableInfo();
const correspondingContainer = (ti.info.find(a => a.serviceName === serviceName) || {}).container;
if (correspondingContainer) {
const allRealContainers = await docker.container.list({ all: true });
const realContainer = allRealContainers.find(a => a.data.Id === correspondingContainer.Id);
if (realContainer) {
return realContainer;
}
}
return false;
}
listeners.on('exit', () => process.exit(0));
listeners.on('shell', async (serviceName) => {
const container = await getContainer(serviceName);
if (container) {
screen.leave();
const command = ['docker', 'exec', '-ti', container.data.Id, 'sh'];
console.log(`Running ${command.join(' ')}`);
const child = screen.spawn(command[0], command.slice(1));
child.on('close', function () { });
}
});
listeners.on('enableToggle', async (serviceName) => {
composeFileSettings = await settings.toggleServices(baseComposeFilePath, [serviceName]);
await renderTable(table);
});
listeners.on('remove', async (serviceName) => {
const container = await getContainer(serviceName);
if (container) {
statusText.setContent(`{blue-bg}Removing Container{/}`);
screen.render();
await container.stop();
await container.delete();
statusText.setContent(``);
await renderTable(table);
}
});
listeners.on('stopContainer', async (serviceName) => {
const container = await getContainer(serviceName);
if (container) {
statusText.setContent(`{blue-bg}Stopping Container{/}`);
screen.render();
await container.stop();
statusText.setContent(``);
await renderTable(table);
}
});
listeners.on('up', async () => {
const tmpComposePath = `${path.dirname(baseComposeFilePath[0])}/_tmp_docker_compose_vis.yml`;
await compose.writeTmpCompose(
tmpComposePath,
composeFileSettings,
);
screen.leave();
const command = ['docker-compose', '-f', tmpComposePath, 'up', '-d'];
console.log(`Running ${command.join(' ')}`);
const child = screen.spawn(command[0], command.slice(1));
child.on('close', async (exitCode) => {
if (exitCode !== 0) { process.exit(exitCode); }
await compose.cleanupTmpComposeFile(tmpComposePath);
});
});
listeners.on('down', async () => {
const tmpComposePath = `${path.dirname(baseComposeFilePath[0])}/_tmp_docker_compose_vis.yml`;
await compose.writeTmpCompose(
tmpComposePath,
composeFileSettings,
);
screen.leave();
const command = ['docker-compose', '-f', tmpComposePath, 'down'];
console.log(`Running ${command.join(' ')}`);
const child = screen.spawn(command[0], command.slice(1));
child.on('close', async (exitCode) => {
if (exitCode !== 0) { process.exit(exitCode); }
await compose.cleanupTmpComposeFile(tmpComposePath);
});
});
listeners.on('logs', async (serviceName) => {
const container = await getContainer(serviceName);
if (container) {
screen.leave();
const command = ['docker', 'logs', '-f', container.data.Id, '--tail', '250'];
console.log(`Running ${command.join(' ')}`);
const child = screen.spawn(command[0], command.slice(1));
child.on('close', async () => { process.exit(0); });
}
await renderTable(table);
});
listeners.on('build', async (serviceName) => {
const tmpComposePath = `${path.dirname(baseComposeFilePath[0])}/_tmp_docker_compose_vis.yml`;
await compose.writeTmpCompose(
tmpComposePath,
composeFileSettings,
);
screen.leave();
const command = ['docker-compose', '-f', tmpComposePath, 'build', serviceName];
console.log(`Running ${command.join(' ')}`);
const child = screen.exec(command[0], command.slice(1));
child.on('close', async (exitCode) => {
if (exitCode !== 0) { process.exit(exitCode); }
await compose.cleanupTmpComposeFile(tmpComposePath);
});
});
await renderTable(table);
screen.render();
// Unhandled errors due to our terrible code
process.on('unhandledRejection', (err) => {
if (screen) { screen.destroy(); }
console.error(err);
process.exit(1);
});
process.on('uncaughtException', (err) => {
if (screen) { screen.destroy(); }
console.error(err);
process.exit(1);
});
})();