-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.orig.js
279 lines (256 loc) · 10.3 KB
/
app.orig.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
'use strict';
// Modules
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const toObject = require('./../../lib/utils').toObject;
const utils = require('./lib/utils');
const warnings = require('./lib/warnings');
// Helper to get http ports
const getHttpPorts = data => _.get(data, 'Config.Labels["io.lando.http-ports"]', '80,443').split(',');
const getHttpsPorts = data => _.get(data, 'Config.Labels["io.lando.https-ports"]', '443').split(',');
// Helper to get scannable or not scannable services
const getScannable = (app, scan = true) => _.filter(app.info, service => {
return _.get(app, `config.services.${service.service}.scanner`, true) === scan;
});
// Helper to set the LANDO_LOAD_KEYS var
const getKeys = (keys = true) => {
if (_.isArray(keys)) return keys.join(' ');
return keys.toString();
};
// Helper to bind exposed ports to the correct address
const normalizeBind = (bind, address = '127.0.0.1') => {
// If bind is not a string, return right away
if (!_.isString(bind)) return bind;
// Otherwise attempt to do stuff
const pieces = _.toString(bind).split(':');
// If we have three pieces then honor the users choice
if (_.size(pieces) === 3) return bind;
// Unshift the address to the front and return
else if (_.size(pieces) === 2) {
pieces.unshift(address);
return pieces.join(':');
}
// Otherwise we can just return the address prefixed to the bind
return `${address}::${bind}`;
};
// Update built against
const updateBuiltAgainst = (app, version = 'unknown') => {
app.meta = _.merge({}, app.meta, {builtAgainst: version});
return app.meta;
};
module.exports = (app, lando) => {
// Add localhost info to our containers if they are up
_.forEach(['post-init', 'post-start'], event => {
app.events.on(event, () => {
app.log.verbose('attempting to find open services...');
return app.engine.list({project: app.project})
// Return running containers
.filter(container => app.engine.isRunning(container.id))
// Make sure they are still a defined service (eg if the user changes their lando yml)
.filter(container => _.includes(app.services, container.service))
// Inspect each and add new URLS
.map(container => app.engine.scan(container))
// Scan all the http ports
.map(data => utils.getUrls(data, getHttpPorts(data), getHttpsPorts(data), lando.config.bindAddress))
.map(data => _.find(app.info, {service: data.service}).urls = data.urls);
});
});
// Refresh all our certs
app.events.on('post-init', () => {
const buildServices = _.get(app, 'opts.services', app.services);
app.log.verbose('refreshing certificates...', buildServices);
app.events.on('post-start', 9999, () => lando.Promise.each(buildServices, service => {
return app.engine.run({
id: `${app.project}_${service}_1`,
cmd: 'mkdir -p /certs && /helpers/refresh-certs.sh > /certs/refresh.log',
compose: app.compose,
project: app.project,
opts: {
detach: true,
mode: 'attach',
user: 'root',
services: [service],
},
})
.catch(err => {
app.addWarning(warnings.serviceNotRunningWarning(service), err);
});
}));
});
// Run a secondary user perm sweep on services that cannot run as root eg mysql
app.events.on('post-init', () => {
if (!_.isEmpty(app.nonRoot)) {
app.log.verbose('perm sweeping flagged non-root containers ...', app.nonRoot);
app.events.on('post-start', 1, () => lando.Promise.each(app.nonRoot, service => {
return app.engine.run({
id: `${app.project}_${service}_1`,
cmd: '/helpers/user-perms.sh --silent',
compose: app.compose,
project: app.project,
opts: {
detach: true,
mode: 'attach',
user: 'root',
services: [service],
},
})
.catch(err => {
app.addWarning(warnings.serviceNotRunningWarning(service), err);
});
}));
}
});
// Assess our key situation so we can warn users who may have too many
app.events.on('post-init', () => {
// Get keys on host
const sshDir = path.resolve(lando.config.home, '.ssh');
const keys = _(fs.readdirSync(sshDir))
.filter(file => !_.includes(['config', 'known_hosts'], file))
.filter(file => path.extname(file) !== '.pub')
.value();
// Determine the key size
const keySize = _.size(_.get(app, 'config.keys', keys));
app.log.verbose('analyzing user ssh keys... using %s of %s', keySize, _.size(keys));
app.log.debug('key config... ', _.get(app, 'config.keys', 'none'));
app.log.silly('users keys', keys);
// Add a warning if we have more keys than the warning level
if (keySize > lando.config.maxKeyWarning) {
app.addWarning(warnings.maxKeyWarning());
}
});
// Collect info so we can inject LANDO_INFO
//
// @TODO: this is not currently the full lando info because a lot of it requires
// the app to be on
app.events.on('post-init', 10, () => {
const info = toObject(_.map(app.info, 'service'), {});
_.forEach(info, (value, key) => {
info[key] = _.find(app.info, {service: key});
});
app.log.verbose('setting LANDO_INFO...');
app.env.LANDO_INFO = JSON.stringify(info);
});
// Analyze an apps compose files so we can set the default bind address
// correctly
//
// @TODO: i feel like there has to be a better way to do this than this mega loop right?
app.events.on('post-init', 9999, () => {
_.forEach(app.composeData, service => {
_.forEach(service.data, datum => {
_.forEach(datum.services, props => {
if (!_.isEmpty(props.ports)) {
app.log.debug('ensuring exposed ports on %s are bound to %s', service.id, lando.config.bindAddress);
props.ports = _(props.ports).map(port => normalizeBind(port, lando.config.bindAddress)).value();
}
});
});
});
});
// Add some logic that extends start until healthchecked containers report as healthy
app.events.on('post-start', 1, () => lando.engine.list({project: app.project})
// Filter out containers without a healthcheck
.filter(container => _.has(_.find(app.info, {service: container.service}), 'healthcheck'))
// Map to info
.map(container => _.find(app.info, {service: container.service}))
// Map to a retry of the healthcheck command
.map(info => lando.Promise.retry(() => {
return app.engine.run({
id: `${app.project}_${info.service}_1`,
cmd: info.healthcheck,
compose: app.compose,
project: app.project,
opts: {
user: 'root',
cstdio: 'pipe',
silent: true,
noTTY: true,
services: [info.service],
},
})
.catch(err => {
console.log('Waiting until %s service is ready...', info.service);
app.log.debug('running healthcheck %s for %s...', info.healthcheck, info.service);
// app.log.silly(err);
return Promise.reject(info.service);
});
}, {max: 25, backoff: 1000})
.catch(service => {
info.healthy = false;
app.addWarning(warnings.serviceUnhealthyWarning(service), Error(`${service} reported as unhealthy.`));
})));
// If the app already is installed but we can't determine the builtAgainst, then set it to something bogus
app.events.on('pre-start', () => {
if (!_.has(app.meta, 'builtAgainst')) {
return lando.engine.list({project: app.project, all: true}).then(containers => {
if (!_.isEmpty(containers)) {
lando.cache.set(app.metaCache, updateBuiltAgainst(app), {persist: true});
}
});
}
});
// If we don't have a builtAgainst already then we must be spinning up for the first time and its safe to set this
app.events.on('post-start', () => {
if (!_.has(app.meta, 'builtAgainst')) {
lando.cache.set(app.metaCache, updateBuiltAgainst(app, app._config.version), {persist: true});
}
if (app.meta.builtAgainst !== app._config.version) {
app.addWarning(warnings.rebuildWarning());
}
});
// Check for docker compat warnings and surface them nicely as well
app.events.on('post-start', () => {
_.forEach(_(lando.versions).filter(version => version.dockerVersion).value(), thing => {
if (!thing.satisfied) app.addWarning(warnings.unsupportedVersionWarning(thing));
});
});
// Scan urls
app.events.on('post-start', 10, () => {
// Message to let the user know it could take a bit
console.log('Scanning to determine which services are ready... Please stand by...');
// Filter out any services where the scanner might be disabled
return app.scanUrls(_.flatMap(getScannable(app), 'urls'), {max: 16}).then(urls => {
// Get data about our scanned urls
app.urls = urls;
// Add in unscannable ones if we have them
if (!_.isEmpty(getScannable(app, false))) {
app.urls = app.urls.concat(_.map(_.flatMap(getScannable(app, false), 'urls'), url => ({
url,
status: true,
color: 'yellow',
})));
}
});
});
// Reset app info on a stop, this helps prevent wrong/duplicate information being reported on a restart
app.events.on('post-stop', () => lando.utils.getInfoDefaults(app));
// Otherwise set on rebuilds
// NOTE: We set this pre-rebuild because post-rebuild runs after post-start because you would need to
// do two rebuilds to remove the warning since appWarning is already set by the time we get here.
// Running pre-rebuild ensures the warning goes away but concedes a possible warning tradeoff between
// this and a build step failure
app.events.on('pre-rebuild', () => {
lando.cache.set(app.metaCache, updateBuiltAgainst(app, app._config.version), {persist: true});
});
// Remove meta cache on destroy
app.events.on('post-destroy', () => {
app.log.debug('removing metadata cache...');
lando.cache.remove(app.metaCache);
});
// REturn defualts
return {
env: {
LANDO_APP_PROJECT: app.project,
LANDO_APP_NAME: app.name,
LANDO_APP_ROOT: app.root,
LANDO_APP_ROOT_BIND: app.root,
LANDO_APP_COMMON_NAME: _.truncate(app.project, {length: 64}),
LANDO_LOAD_KEYS: getKeys(_.get(app, 'config.keys')),
BITNAMI_DEBUG: 'true',
},
labels: {
'io.lando.src': app.configFiles.join(','),
'io.lando.http-ports': '80,443',
},
};
};