-
Notifications
You must be signed in to change notification settings - Fork 0
/
DockerClass.js
172 lines (126 loc) · 5.51 KB
/
DockerClass.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
const CERT = '/cert/cert.pem';
//const OpenSSLListen = ;
import colog from 'colog';
import { PathParser, ExternalIP } from "utils";
import { NODE, RERUN } from './CONFIG.js';
import { init } from "./SSH.js";
//import Cert from './cert.js';
import { Commands, Names, CommandFactory } from './Command.js';
import { Hash, Pull, StopSocatStartExpress, Script, } from './DockerUtils.js';
const { IsMain, } = PathParser(
import.meta.url);
export default class DockerClass {
constructor(docker, Params) {
const { PublicURL } = docker;
Params.host = new URL(PublicURL).hostname;
if (!Params.root)
Params.root = '/home/' + Params.username;
console.log(Params);
const Command = (...args) => {
const Return = CommandFactory(Params.root, ...args);
Return.Env.push('PUBLIC_URL=' + PublicURL);
Params.cache && Return.Env.push('CACHE=' + Params.cache);
return Return;
};
this.Commands = Commands.map(Command);
/*if (Name === hostname()) {
const ExpressCommand = this.Commands.find(({ name }) => name === 'express');
delete ExpressCommand.ExposedPorts;
const { HostConfig } = ExpressCommand;
delete HostConfig.PortBindings;
HostConfig.Binds = [
HostConfig.Binds.find(x => x.endsWith('Docker.sh')),
'/volatile/node_modules:/root/node_modules:ro',
];
console.log('Command modified', ExpressCommand);
}*/
Object.assign(this, { PublicURL, docker, Params, Command, });
}
command([container]) {
const { docker } = this;
return container === 'script' ? Script(docker) : this.main([docker.getContainer(container)]);
}
main(Containers, ExpressRunning) {
const { docker } = this;
Promise.all(Containers.map(async x => {
const { Remove } = x;
try {
(Remove === undefined || Remove) && await x.remove({ force: true });
}
catch (error) {
console.warn(error);
}
Remove === undefined && console.warn("Remove is undefined");
const Command = this.Commands.find(Command => Command.name === x.id);
if (!Command)
return Promise.reject(`Container "${x}" does not exsists`);
Command.Labels.COMMAND = Hash(Command);
return docker.createContainer(Command).then(() => () => x.start().then(() => Command.name));
}))
.then(Starts => {
if (!ExpressRunning) {
const Express = Containers.find(({ id }) => id === 'express');
Express.start = StopSocatStartExpress.bind(undefined, docker);
}
return Promise.all(Starts.map(x => x()));
})
.then((...args) => colog.success('Successfully started: ' + args));
}
async ListContainers() {
const { docker, Params, } = this;
let ping = false;
try {
await docker.ping();
ping = true;
console.log('ping success');
}
catch (error) {
if (error.statusCode === 502)
console.warn('ping', error);
else
throw error;
}
ping || await init(Params);
console.log("List containers");
return docker.listContainers({
all: true,
filters: { name: [...Names, "script"] },
});
}
Run() {
const { docker, Command, Commands, } = this;
return this.ListContainers()
.then(Script.bind(undefined, docker, Command))
.then(Data => {
console.info("Filter");
const Containers = Names.map(x => docker.getContainer(x));
const Filtered = Containers.filter((Container, i) => {
const { id } = Container;
const data = Data.find(x => x.Names[0].substring(1) == id);
Container.Remove = Boolean(data);
if (!data)
return true;
if (RERUN.includes(id)) {
console.warn('Container', Container.id, 'to be force removed');
return true;
}
//console.log(data.Labels.COMMAND, Hash(Commands[i]));
if (data.Labels.COMMAND !== Hash(Commands[i]))
return true;
return id === 'script' ? false : data.State !== "running";
});
if (!Filtered.length)
return colog.success('All containers are running. No action required.');
colog.answer("Trying container(s):" + Filtered.reduce((accum, { id }) => accum + ' ' + id, ''));
const Express = Data.find(({ Names: [Name] }) => Name === '/express');
console.log('Express state:', Express && Express.State);
return Pull(docker)
.then(() => this.main(Filtered, Express && Express.State === 'running'));
})
.finally(() => console.info("Finally"));
}
}
export const DefaultDockerClass = () =>
import('./PortainerDocker.js')
.then(({ DefaultPortainerDocker }) => new DockerClass(DefaultPortainerDocker(), NODE));
IsMain && DefaultDockerClass().then(dockerClass => dockerClass.Run());