-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.ts
239 lines (224 loc) · 5.66 KB
/
run.ts
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
import yargs from "yargs";
import * as dotenv from "dotenv";
import LabeledProcessRunner from "./runner.js";
import { ServerlessStageDestroyer } from "@stratiformdigital/serverless-stage-destroyer";
import { execSync } from "child_process";
// load .env
dotenv.config();
const deployedServices = [
"database",
"app-api",
"stream-functions",
"ui-waflog-s3-bucket",
"ui",
"ui-auth",
"ui-waf-log-assoc",
"ui-src",
];
// Function to update .env files using 1Password CLI
function updateEnvFiles() {
try {
execSync("op inject -i .env.tpl -o .env -f", { stdio: "inherit" });
execSync("sed -i '' -e 's/# pragma: allowlist secret//g' .env");
} catch (error) {
// eslint-disable-next-line no-console
console.error("Failed to update .env files using 1Password CLI.");
process.exit(1);
}
}
// run_db_locally runs the local db
// @ts-ignore
async function run_db_locally(runner: LabeledProcessRunner) {
await runner.run_command_and_output(
"db yarn",
["yarn", "install"],
"services/database"
);
await runner.run_command_and_output(
"db svls",
["serverless", "dynamodb", "install", "--stage=local"],
"services/database"
);
await runner.run_command_and_output(
"db svls doc",
["serverless", "doctor"],
"services/database"
);
runner.run_command_and_output(
"db",
[
"serverless",
"offline",
"start",
"--stage",
"local",
"--lambdaPort",
"3003",
],
"services/database"
);
runner.run_command_and_output(
"db",
["serverless", "dynamodb", "start", "--stage=local"],
"services/database"
);
await new Promise((res) => setTimeout(res, 10 * 1000)); // The above runners need to all finish, not all can be awaited, they block
await runner.run_command_and_output(
"db",
[
"aws",
"lambda",
"invoke",
"/dev/null",
"--endpoint-url",
"http://localhost:3003",
"--function-name",
"database-local-seed",
],
"services/database"
);
}
// run_api_locally uses the serverless-offline plugin to run the api lambdas locally
// @ts-ignore
async function run_api_locally(runner: LabeledProcessRunner) {
await runner.run_command_and_output(
"api deps",
["yarn", "install"],
"services/app-api"
);
await runner.run_command_and_output(
"api svls doc",
["serverless", "doctor"],
"services/app-api"
);
runner.run_command_and_output(
"api",
[
"serverless",
"offline",
"start",
"--stage",
"local",
"--region",
"us-east-1",
"--httpPort",
"3030",
],
"services/app-api"
);
}
// run_fe_locally runs the frontend and its dependencies locally
// @ts-ignore
async function run_fe_locally(runner: LabeledProcessRunner) {
await runner.run_command_and_output(
"ui deps",
["yarn", "install"],
"services/ui-src"
);
await runner.run_command_and_output(
"ui svls doc",
["serverless", "doctor"],
"services/ui-src"
);
await runner.run_command_and_output(
"ui conf",
["./env.sh", "local"],
"services/ui-src"
);
runner.run_command_and_output("ui", ["npm", "start"], "services/ui-src");
}
// run_all_locally runs all of our services locally
async function run_all_locally() {
const runner = new LabeledProcessRunner();
run_db_locally(runner);
run_api_locally(runner);
run_fe_locally(runner);
}
async function install_deps(runner: LabeledProcessRunner, service: string) {
await runner.run_command_and_output(
"Installing dependencies",
["yarn", "install", "--frozen-lockfile"],
`services/${service}`
);
}
async function prepare_services(runner: LabeledProcessRunner) {
for (const service of deployedServices) {
await install_deps(runner, service);
}
}
async function deploy(options: { stage: string }) {
const stage = options.stage;
const runner = new LabeledProcessRunner();
await prepare_services(runner);
const deployCmd = ["sls", "deploy", "--stage", stage];
await runner.run_command_and_output("Serverless deploy", deployCmd, ".");
}
async function destroy_stage(options: {
stage: string;
service: string | undefined;
wait: boolean;
verify: boolean;
}) {
let destroyer = new ServerlessStageDestroyer();
let filters = [
{
Key: "PROJECT",
Value: `${process.env.PROJECT}`,
},
];
if (options.service) {
filters.push({
Key: "SERVICE",
Value: `${options.service}`,
});
}
await destroyer.destroy(`${process.env.REGION_A}`, options.stage, {
wait: options.wait,
filters: filters,
verify: options.verify,
});
}
// The command definitons in yargs
// All valid arguments to dev should be enumerated here, this is the entrypoint to the script
yargs(process.argv.slice(2))
.command("local", "run system locally", {}, () => {
run_all_locally();
})
.command(
"test",
"run all tests",
() => {},
() => {
console.log("Testing 1. 2. 3.");
}
)
.command(
"deploy",
"deploy the app with serverless compose to the cloud",
{
stage: { type: "string", demandOption: true },
},
deploy
)
.command(
"destroy",
"destroy serverless stage",
{
stage: { type: "string", demandOption: true },
service: { type: "string", demandOption: false },
wait: { type: "boolean", demandOption: false, default: true },
verify: { type: "boolean", demandOption: false, default: true },
},
destroy_stage
)
.command(
"update-env",
"update environment variables using 1Password",
() => {},
() => {
updateEnvFiles();
}
)
.scriptName("run")
.strict()
.demandCommand(1, "").argv; // this prints out the help if you don't call a subcommand