-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path_lando.js
More file actions
213 lines (184 loc) · 7.02 KB
/
_lando.js
File metadata and controls
213 lines (184 loc) · 7.02 KB
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
'use strict';
// Modules
const _ = require('lodash');
const fs = require('fs');
const os = require('os');
const path = require('path');
const write = require('../utils/write-file');
const {color} = require('listr2');
const {nanoid} = require('nanoid');
/*
* The lowest level lando service, this is where a lot of the deep magic lives
*/
module.exports = {
name: '_lando',
parent: '_compose',
builder: parent => class LandoLando extends parent {
constructor(
id,
{
name,
healthcheck,
type,
userConfRoot,
version,
// app = '',
confDest = '',
confSrc = '',
config = {},
data = `data_${name}`,
dataHome = `home_${name}`,
entrypoint = '/lando-entrypoint.sh',
home = '',
moreHttpPorts = [],
info = {},
legacy = [],
meUser = 'www-data',
patchesSupported = false,
pinPairs = {},
ports = [],
project = '_lando_',
overrides = {},
refreshCerts = false,
remoteFiles = {},
scripts = [],
scriptsDir = '',
sport = '443',
ssl = false,
sslExpose = true,
supported = ['custom'],
supportedIgnore = false,
root = '',
// webroot = '/app',
} = {},
...sources
) {
// Add custom to list of supported
supported.push('custom');
// If this version is not supported throw an error
// @TODO: get this someplace else for unit tezting
if (!supportedIgnore && !_.includes(supported, version)) {
if (!patchesSupported
|| !_.includes(require('../utils/strip-wild')(supported), require('../utils/strip-patch')(version))) {
throw Error(`${type} version ${version} is not supported`);
}
}
if (_.includes(legacy, version)) {
console.error(color.yellow(`${type} version ${version} is a legacy version! We recommend upgrading.`));
}
// normalize scripts dir if needed
if (!path.isAbsolute(scriptsDir)) scriptsDir = path.resolve(root, scriptsDir);
// Get some basic locations
const globalScriptsDir = path.join(userConfRoot, 'scripts');
const serviceScriptsDir = path.join(userConfRoot, 'helpers', project, type, name);
const entrypointScript = path.join(globalScriptsDir, 'lando-entrypoint.sh');
const addCertsScript = path.join(globalScriptsDir, 'add-cert.sh');
const refreshCertsScript = path.join(globalScriptsDir, 'refresh-certs.sh');
// Move our config into the userconfroot if we have some
// NOTE: we need to do this because on macOS and Windows not all host files
// are shared into the docker vm
if (fs.existsSync(confSrc)) require('../utils/move-config')(confSrc, confDest);
// ditto for service helpers
if (fs.existsSync(scriptsDir)) require('../utils/move-config')(scriptsDir, serviceScriptsDir);
// Handle Environment
const environment = {
LANDO_SERVICE_NAME: name,
LANDO_SERVICE_TYPE: type,
};
// Handle labels
const labels = {
'io.lando.http-ports': _.uniq(['80', '443'].concat(moreHttpPorts)).join(','),
'io.lando.https-ports': _.uniq(['443'].concat([sport])).join(','),
};
// Set a reasonable log size
const logging = {driver: 'json-file', options: {'max-file': '3', 'max-size': '10m'}};
// Handle volumes
const volumes = [
`${userConfRoot}:/lando:cached`,
`${globalScriptsDir}:/helpers`,
`${entrypointScript}:/lando-entrypoint.sh`,
`${dataHome}:/var/www`,
];
// add in service helpers if we have them
if (fs.existsSync(serviceScriptsDir)) volumes.push(`${serviceScriptsDir}:/etc/lando/service/helpers`);
// Handle ssl
if (ssl) {
// also expose the sport
if (sslExpose) ports.push(sport);
// certs
const certname = `${id}.${project}.crt`;
const keyname = `${id}.${project}.key`;
environment.LANDO_SERVICE_CERT = `/lando/certs/${certname}`;
environment.LANDO_SERVICE_KEY = `/lando/certs/${keyname}`;
volumes.push(`${addCertsScript}:/scripts/000-add-cert`);
volumes.push(`${path.join(userConfRoot, 'certs', certname)}:/certs/cert.crt`);
volumes.push(`${path.join(userConfRoot, 'certs', keyname)}:/certs/cert.key`);
}
// Add in some more dirz if it makes sense
if (home) volumes.push(`${home}:/user:cached`);
// Handle cert refresh
// @TODO: this might only be relevant to the proxy, if so let's move it there
if (refreshCerts) volumes.push(`${refreshCertsScript}:/scripts/999-refresh-certs`);
// Add in any custom pre-runscripts
_.forEach(scripts, script => {
const local = path.resolve(root, script);
const remote = path.join('/scripts', path.basename(script));
volumes.push(`${local}:${remote}`);
});
// rebase remoteFiles
remoteFiles = _.merge({}, {'_lando_': '/tmp/rooster'}, remoteFiles);
// Handle custom config files
_.forEach(config, (local, remote) => {
// if this is special type then get it from remoteFile
remote = _.has(remoteFiles, remote) ? remoteFiles[remote] : path.resolve('/', remote);
// if file is an imported string lets just get the file path instead
if (local?.constructor?.name === 'ImportString') {
const meta = local.getMetadata();
if (meta.file) local = meta.file;
else local = local.toString();
}
// if file is still a multiline string then dump to tmp and use that
if (typeof local === 'string' && local.split('\n').length > 1) {
const contents = local;
local = path.join(os.tmpdir(), nanoid());
write(local, contents, {forcePosixLineEndings: true});
}
volumes.push(`${path.resolve(root, local)}:${remote}`);
});
// Add named volumes and other thingz into our primary service
const namedVols = {};
_.set(namedVols, data, {});
_.set(namedVols, dataHome, {});
sources.push({
services: _.set({}, name, {
entrypoint,
environment,
extra_hosts: ['host.lando.internal:host-gateway'],
labels,
logging,
ports,
volumes,
}),
volumes: namedVols,
});
// Add a final source if we need to pin pair
if (_.includes(_.keys(pinPairs), version)) {
sources.push({services: _.set({}, name, {image: _.get(pinPairs, version, version)})});
}
// Add our overrides at the end
sources.push({services: _.set({}, name, require('../utils/normalize-overrides')(overrides, root))});
// Add some info basics
info.config = config;
info.service = name;
info.type = type;
info.version = version;
info.meUser = meUser;
info.hasCerts = ssl;
info.api = 3;
// Add the healthcheck if it exists
if (healthcheck) info.healthcheck = healthcheck;
// Pass it down
super(id, info, ...sources);
}
},
};