-
-
Notifications
You must be signed in to change notification settings - Fork 555
/
Copy pathgcp-setup.js
165 lines (144 loc) · 5.4 KB
/
gcp-setup.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
/* SPDX-FileCopyrightText: 2014-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import envars from "envars";
import { execa as spawn } from "execa";
import { URL } from "node:url";
import { $, argv, chalk, fs, path, question } from "zx";
import { rootDir } from "./utils.js";
/**
* This script can be used as a lightweight alternative to Terraform
* for bootstrapping a new Google Cloud (GCP) project. Usage example:
*
* $ yarn gcp:setup --env=test
* $ yarn gcp:setup --env=prod
*/
// Load the environment variables
const envName = argv.env ?? "test";
const env = envars.config({ env: envName });
const project = env.GOOGLE_CLOUD_PROJECT;
const region = env.GOOGLE_CLOUD_REGION;
const cwd = process.cwd();
await question(
[
chalk.grey(`Setting up the Google Cloud environment`),
``,
` ${chalk.bold(`Project`)}: ${chalk.green(project)}`,
` ${chalk.bold(`Region`)}: ${chalk.green(region)}`,
``,
chalk.grey(`Click ${chalk.bold(`[Enter]`)} to continue...\n`),
].join("\n"),
);
// Get the GCP project number
const projectNum = await spawn("gcloud", [
...["projects", "list", `--filter`, project],
...["--format", "value(project_number)"],
]).then((cmd) => cmd.stdout.toString());
// The list of Google Cloud services that needs to be enabled
const services = [
"iamcredentials.googleapis.com",
"compute.googleapis.com",
"cloudfunctions.googleapis.com",
"logging.googleapis.com",
"run.googleapis.com",
"sqladmin.googleapis.com",
"pubsub.googleapis.com",
"cloudbuild.googleapis.com",
"artifactregistry.googleapis.com",
"sourcerepo.googleapis.com",
"identitytoolkit.googleapis.com",
];
for (const service of services) {
await $`gcloud services enable ${service} --project=${project}`;
}
let cmd = await spawn(`gsutil`, [`kms`, `serviceaccount`, `-p`, projectNum]);
// The list of IAM service accounts
const appAccount = `service@${project}.iam.gserviceaccount.com`; // GCS, URL signing
const computeAccount = `${projectNum}[email protected]`; // GCF
const pubSubAccount = `service-${projectNum}@gcp-sa-pubsub.iam.gserviceaccount.com`;
const storageAccount = cmd.stdout.toString();
// Fetch the list of IAM service accounts
const serviceAccounts = await spawn("gcloud", [
...["iam", "service-accounts", "list"],
...["--project", project, "--format", "value(email)"],
]).then((cmd) => cmd.stdout.toString().split("\n").filter(Boolean));
// Create a custom service account for the app if not exists
if (!serviceAccounts.includes(appAccount)) {
await $`gcloud iam service-accounts create ${appAccount.split("@")[0]} ${[
...["--project", project, "--display-name", "App Service"],
]}`;
}
async function addRole(iamAccount, role) {
await $`gcloud projects add-iam-policy-binding ${project} ${[
`--member=serviceAccount:${iamAccount}`,
`--role=${role}`,
`--format=none`,
]}`;
}
await addRole(pubSubAccount, "roles/iam.serviceAccountTokenCreator");
await addRole(storageAccount, "roles/pubsub.publisher");
await addRole(computeAccount, "roles/eventarc.eventReceiver");
await addRole(computeAccount, "roles/iam.serviceAccountTokenCreator");
await addRole(appAccount, "roles/iam.serviceAccountTokenCreator");
await addRole(appAccount, "roles/storage.objectAdmin");
// Fetch the list of service account keys
cmd = await spawn("gcloud", [
...["iam", "service-accounts", "keys", "list"],
...["--iam-account", appAccount, "--managed-by", "user"],
]);
// Create a new service account (JSON) key if not exists
if (!cmd.stdout.toString()) {
await $`gcloud iam service-accounts keys create ${[
path.resolve(rootDir, `env/gcp-key.${envName}.json`),
`--iam-account=${appAccount}`,
]}`;
}
// Get the primary domain name (from the production environment)
const prodEnv = envars.config({ env: "prod" });
const domain = new URL(prodEnv.APP_ORIGIN).hostname;
// Ensure that the domain name is verified
while (true) {
cmd = await spawn("gcloud", ["domains", "list-user-verified"]);
const verifiedDomains = cmd.stdout.toString().split("\n").slice(1);
if (verifiedDomains.includes(domain)) break;
await $`gcloud domains verify ${domain} --project ${project}`;
await question(chalk.grey(`Click ${chalk.bold(`[Enter]`)} to continue...\n`));
}
// Fetch the list of existing GCS buckets
cmd = await spawn("gcloud", ["alpha", "storage", "ls", "--project", project]);
const corsFile = path.relative(cwd, path.join(rootDir, ".cache/cors.json"));
const existingBuckets = cmd.stdout.toString().split("\n");
const buckets = Object.keys(env)
.filter((key) => key.endsWith("_BUCKET"))
.filter((key) => envName === "prod" || env[key] !== prodEnv[key])
.map((key) => env[key]);
// Create missing GCS buckets if any
for (const bucket of buckets) {
if (!existingBuckets.includes(`gs://${bucket}/`)) {
await $`gsutil mb ${[
...["-p", project, "-l", region.split("-")[0], "-b", "on"],
...["-c", "standard", `gs://${bucket}/`],
]}`;
}
// Write CORS settings to a temporary file
await fs.writeFile(
corsFile,
JSON.stringify([
{
origin: [
env.APP_ORIGIN,
envName !== "prod" && "http://localhost:5173",
].filter(Boolean),
responseHeader: ["Content-Type"],
method: ["GET"],
maxAgeSeconds: 3600,
},
]),
{ encoding: "utf-8" },
);
// Apply CORS settings to the target bucket
try {
await $`gsutil cors set ${corsFile} ${`gs://${bucket}`}`;
} finally {
await fs.unlink(corsFile);
}
}