-
Notifications
You must be signed in to change notification settings - Fork 7
/
google.js
executable file
·94 lines (82 loc) · 3.15 KB
/
google.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
// Google methods
import omit from 'lodash.omit';
import shell from 'shelljs';
import winston from 'winston';
import yaml from 'js-yaml';
import fs from 'fs';
export default class AppEngineInstance {
constructor({
settingsFile,
dockerFile,
appFile,
workingDir,
ci,
env,
}) {
this.meteorSettings = omit(settingsFile, 'meteor-google-cloud');
this.dockerFile = dockerFile;
this.appSettings = appFile;
this.workingDir = workingDir;
this.googleCloudSettings = settingsFile['meteor-google-cloud'];
this.ci = ci;
this.env = env;
}
prepareBundle() {
// We add the Meteor settings now to avoid it being compiled to YAML
const compactSettings = JSON
.stringify(this.meteorSettings || {}, null, 0)
// It will remove all non-printable characters.
// This are all characters NOT within the ASCII HEX space 0x20-0x7E.
.replace(/[^\x20-\x7E]/gmi, '')
.replace(/[\n\r]+/g, '');
// make sure the env_variables are set
this.appSettings.env_variables = this.env;
// We will use shell sed command to replace the variables
Object.assign(this.appSettings.env_variables, {
METEOR_SETTINGS: '{{ METEOR_SETTINGS }}',
});
// Create app.yaml file
const app = yaml.safeDump(this.appSettings);
shell.exec(`echo '${app}' >${this.workingDir}/bundle/app.yaml`);
shell.sed('-i', '{{ METEOR_SETTINGS }}', `'${compactSettings}'`, `${this.workingDir}/bundle/app.yaml`);
const resultAppYaml = yaml.safeLoad(fs.readFileSync(`${this.workingDir}/bundle/app.yaml`));
resultAppYaml.env_variables.MONGO_URL = '*****';
resultAppYaml.env_variables.MONGO_OPLOG_URL = '*****';
winston.debug(`the following app.yaml will be used:\n${JSON.stringify(resultAppYaml)}`);
const nodeVersion = shell.exec(
`meteor node -v ${this.ci ? '--allow-superuser' : ''}`,
{ silent: true },
).stdout.trim();
const npmVersion = shell.exec(
`meteor npm -v ${this.ci ? '--allow-superuser' : ''}`,
{ silent: true },
).stdout.trim();
winston.debug(`set Node to ${nodeVersion}`);
winston.debug(`set NPM to ${npmVersion}`);
// Create Dockerfile
const docker = this.dockerFile
.replace('{{ nodeVersion }}', nodeVersion)
.replace('{{ npmVersion }}', npmVersion);
shell.exec(`echo '${docker}' >${this.workingDir}/bundle/Dockerfile`);
winston.debug(`the following Dockerfile will be used:\n${JSON.stringify(yaml.safeLoad(
fs.readFileSync(`${this.workingDir}/bundle/Dockerfile`),
))}`);
}
async deployBundle() {
winston.debug('deploy to App Engine');
// Allow users to pass any option to gcloud app deploy
const settings = this.googleCloudSettings;
const flags = Object.keys(settings).map((key) => {
if (key !== 'env_variables') {
const value = settings[key];
// Only some flags actually require a value (e.g. stop-previous-version)
if (value) {
return `--${key}=${settings[key]}`;
}
return `--${key}`;
}
}).join(' ');
winston.debug(`set flags for deploy: ${flags}`);
shell.exec(`cd ${this.workingDir}/bundle && gcloud app deploy -q ${flags}`);
}
}