-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
113 lines (101 loc) · 4.73 KB
/
app.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
'use strict';
// Modules
const _ = require('lodash');
const fs = require('fs');
const keys = require('./lib/keys');
const lagoonConf = require('./lib/config');
const path = require('path');
const warnings = require('./lib/warnings');
const {getLandoServices} = require('./lib/services');
const LagoonApi = require('./lib/api');
// Only do this on lagoon recipes
module.exports = (app, lando) => {
if (_.get(app, 'config.recipe') === 'lagoon') {
// Indicate awareness
app.log.verbose('identified a lagoon app');
// Start by loading in all the laggon files we can
app.lagoon = {config: lagoonConf.loadConfigFiles(app.root)};
// And then augment with a few other things
app.lagoon.domain = `${app.name}.${app._config.domain}`;
app.lagoon.containers = _.keys(_.get(app.lagoon, 'config.compose.services', {})),
app.log.silly('loaded lagoon config files', app.lagoon);
// Set the app caches, validate keys and update key cache
_.forEach(['pull', 'push'], command => {
app.events.on(`post-${command}`, (config, answers) => {
// Only run if answer.auth is set, this allows these commands to all be
// overriden without causing a failure here
if (keys.getPreferredKey(answers)) {
const api = new LagoonApi(keys.getPreferredKey(answers), lando);
return api.auth().then(() => api.whoami().then(me => {
// if this is a generated key lets move it
if (_.has(answers, 'auth-generate')) {
// Get the generated key
const auth = answers['auth-generate'];
const generatedKey = _.first(auth.split('@'));
// Get the new key
const newKey = path.join(lando.config.userConfRoot, 'keys', `lagoon-${me.id}`);
// Move the key
fs.renameSync(generatedKey, newKey);
answers['auth-generate'] = _.replace(auth, generatedKey, newKey);
// Remove older stuff
fs.unlinkSync(`${generatedKey}.pub`);
}
// Update lando's store of lagoon keys
const newKey = {date: _.toInteger(_.now() / 1000), key: keys.getPreferredKey(answers), email: me.email};
lando.cache.set(app.lagoonKeyCache, keys.sortKeys(app.lagoonKeys, [newKey]), {persist: true});
// Update the app metadata cache
const metaData = lando.cache.get(app.metaCache);
lando.cache.set(app.metaCache, _.merge({}, metaData, newKey), {persist: true});
// Wipe out the apps tooling cache to reset with the new key
lando.cache.remove(`${app.name}.tooling.cache`);
}));
}
});
});
/*
* This event is intended to parse and interpret the lagoon config files
* loaded above into things we can use elsewhere, eg if there is any useful
* non-trivial data mutation that needs to happen ANYWHERE else in the
* recipe it probably should happen here
*/
app.events.on('pre-init', 1, () => {
// Error if we don't have at least one docker service or any lagoon config
if (_.isEmpty(app.lagoon.containers) || _.isEmpty(app.lagoon.config.lagoon)) {
throw Error(`The lagoon recipe requires a .lagoon.yml in ${app.root} but we couldn't find one! ` +
`Check out https://docs.lando.dev/config/lagoon.html to see how all the magic works!`);
}
// Get the raw lagoon config
const lagoonConfig = app.lagoon.config;
// Reset the lando app name to be the project name
app.name = _.get(lagoonConfig, 'lagoon.project', app.name);
// Add the parsed services config
app.lagoon.services = lagoonConf.parseServices(lagoonConfig.compose.services, app.config);
app.log.verbose('parsed lagoon services');
app.log.silly('lagoon services ares', app.lagoon.services);
// Load in other stuff keys tokens and other meta at the most opportune moment
app.lagoonKeyCache = 'lagoon.keys';
app.lagoonKeys = lando.cache.get(app.lagoonKeyCache) || [];
});
/*
* Warn user of unsupported services
* This event exists to
*/
app.events.on('post-start', 9, () => {
const allServices = _.map(app.lagoon.services, 'name');
const supportedServices = _.map(getLandoServices(app.lagoon.services), 'name');
const unsupportedServices = _.difference(allServices, supportedServices);
if (!_.isEmpty(unsupportedServices)) {
app.addWarning(warnings.unsupportedServices(unsupportedServices.join(', ')));
}
});
// Fix pullable/local services for lagoon things
app.events.on('pre-rebuild', 9, () => {
_.forEach(_.get(app, 'config.services', {}), (config, name) => {
if (_.has(config, 'lagoon.build')) {
_.remove(app.opts.pullable, item => item === name);
app.opts.local.push(name);
}
});
});
}
};