-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathVerificationServer.js
177 lines (144 loc) · 4.24 KB
/
VerificationServer.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
import fs from 'fs';
import path from 'path';
import dots from 'dot';
import os from 'os';
import { TEMPLATES_DIR } from '../../../constants.js';
export default class VerificationServer {
/**
*
* @param {πDocker} docker
* @param {dockerPull} dockerPull
* @param {StartedContainers} startedContainers
* @param {HomeDir} homeDir
*/
constructor(docker, dockerPull, startedContainers, homeDir) {
this.docker = docker;
this.dockerPull = dockerPull;
this.startedContainers = startedContainers;
this.homeDir = homeDir;
this.container = null;
this.configPath = null;
this.config = null;
}
/**
* Set up verification server
*
* @param {Config} config
* @param {string} validationUrl
* @param {string[]} validationContent
* @return {Promise<void>}
*/
async setup(config, validationUrl, validationContent) {
if (this.config) {
throw new Error('Server is already setup');
}
this.config = config;
dots.templateSettings.strip = false;
// Set up Gateway config
const configSubPath = path.join('platform', 'gateway');
const templatePath = path.join(TEMPLATES_DIR, configSubPath, '_zerossl_validation.yaml.dot');
const templateString = fs.readFileSync(templatePath, 'utf-8');
const template = dots.template(templateString);
const route = validationUrl.replace(`http://${config.get('externalIp')}`, '');
const body = validationContent.join('\\n');
const gatewayConfig = template({ route, body });
const configDir = this.homeDir.joinPath(config.getName(), 'platform', 'gateway');
const configName = path.basename(templatePath, '.dot');
this.configPath = path.join(configDir, configName);
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir);
}
fs.rmSync(this.configPath, { force: true });
fs.writeFileSync(this.configPath, gatewayConfig, 'utf8');
}
/**
* Start verification server
*
* @return {Promise<boolean>} - False if already started
*/
async start() {
if (!this.config) {
throw new Error('Setup server first');
}
if (this.container) {
return false;
}
const image = this.config.get('platform.gateway.docker.image');
const name = 'dashmate-zerossl-validation';
const { uid, gid } = os.userInfo();
const opts = {
name,
Image: image,
Tty: false,
Env: [`ENVOY_UID=${uid}`, `ENVOY_GID=${gid}`],
ExposedPorts: { '80/tcp': {} },
HostConfig: {
AutoRemove: true,
Binds: [`${this.configPath}:/etc/envoy/envoy.yaml:ro`],
PortBindings: { '80/tcp': [{ HostPort: '80' }] },
},
};
await this.dockerPull(image);
let retries = 0;
const MAX_RETRIES = 3;
while (!this.container && retries <= MAX_RETRIES) {
try {
this.container = await this.docker.createContainer(opts);
} catch (e) {
// Throw any other error except container name conflict
if (e.statusCode !== 409) {
throw e;
}
// Container name is already in use
// Remove container
const danglingContainer = await this.docker.getContainer(name);
await danglingContainer.remove({ force: true });
try {
await danglingContainer.wait();
} catch (waitError) {
// Throw any other error except container not found
if (waitError.statusCode !== 404) {
throw waitError;
}
// Skip error if container is already removed
}
}
retries++;
}
this.startedContainers.addContainer(opts.name);
await this.container.start();
return true;
}
/**
* Stop verification server
*
* @return {Promise<void>}
*/
async stop() {
if (!this.container) {
return;
}
await this.container.stop({ t: 3 });
try {
await this.container.wait();
} catch (e) {
// Skip error if container is already removed
if (e.statusCode !== 404) {
throw e;
}
}
this.container = null;
}
/**
* Destroy verification server files
*
* @return {Promise<void>}
*/
async destroy() {
if (!this.config) {
throw new Error('Setup server first');
}
fs.rmSync(this.configPath, { force: true });
this.config = null;
}
}