-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
204 lines (185 loc) · 5.83 KB
/
utils.ts
File metadata and controls
204 lines (185 loc) · 5.83 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
import { S3BucketCorsConfigurationCorsRule } from '@cdktf/provider-aws/lib/s3-bucket-cors-configuration';
import { TerraformVariable } from 'cdktf';
import { S3BucketObjectOwnership } from './constructs/bucket';
export const GRAASP_ROOT_DOMAIN = 'graasp.org';
export const Environment = {
DEV: 'dev',
PRODUCTION: 'production',
} as const;
export type EnvironmentOptions = (typeof Environment)[keyof typeof Environment];
export const AllowedRegion = {
Frankfurt: 'eu-central-1',
Zurich: 'eu-central-2',
} as const;
export type AllowedRegionOptions =
(typeof AllowedRegion)[keyof typeof AllowedRegion];
const InfraState = {
/**
* The infrastructure operates in "normal" mode, all services are up, and there is no filtering on requests
*/
Running: 'running',
/**
* The infrastructure is operating in "restricted" mode, only requests that are performed with specific headers are allowed.
* This mode is useful for testing that everything works after a large change.
*/
Restricted: 'restricted',
/**
* The infrastructure is mostly down, all services are stopped, only the database is still accessible, the migration service is started and migrations can be performed
*/
DBOnly: 'db-only',
/**
* The infrastructure is completely down, no services are running, the database is turned off.
* This is a state that should be used during the night and the weekends to reduce costs on low-usage environnements (i.e. dev)
* or, while we do not have new changes in "staging".
*/
Stopped: 'stopped',
} as const;
export type InfraStateOptions = (typeof InfraState)[keyof typeof InfraState];
export const SpotPreference = {
OnlySpot: 'OnlySpot',
NoSpot: 'NoSpot',
UpscaleWithSpot: 'UpscaleWithSpot',
} as const;
export type SpotPreferenceOptions =
(typeof SpotPreference)[keyof typeof SpotPreference];
export type EnvironmentConfig = {
env: EnvironmentOptions;
subdomain?: string;
region: AllowedRegionOptions;
infraState: InfraStateOptions;
hostedZoneId: string;
};
export type GraaspWebsiteConfig = {
exposeDNS: boolean;
functionAssociationArn?: string;
apexDomain?: boolean;
s3StaticSite?: boolean;
corsConfig: S3BucketCorsConfigurationCorsRule[];
bucketOwnership?: S3BucketObjectOwnership;
};
export function subdomainForEnv(subdomain: string, env: EnvironmentConfig) {
return env.subdomain
? `${subdomain}.${env.subdomain}.${GRAASP_ROOT_DOMAIN}`
: `${subdomain}.${GRAASP_ROOT_DOMAIN}`;
}
export function envDomain(env: EnvironmentConfig) {
return env.subdomain
? `${env.subdomain}.${GRAASP_ROOT_DOMAIN}`
: `${GRAASP_ROOT_DOMAIN}`;
}
export function envEmail(name: string, env: EnvironmentConfig) {
return env.subdomain
? `${name}.${env.subdomain}@${GRAASP_ROOT_DOMAIN}`
: `${name}@${GRAASP_ROOT_DOMAIN}`;
}
export function envCorsRegex(env: EnvironmentConfig) {
const subdomain = env.subdomain ? `${env.subdomain}\\.` : '';
return `^https?:\\/\\/(([a-z0-9]+\\.)+)?${subdomain}graasp\\.org$`;
}
export function envName(env: EnvironmentConfig) {
return env.subdomain ?? 'prod';
}
const VALID_INFRA_STATES = Object.values(InfraState) as string[];
export function validateInfraState(
infraState: string | undefined,
): InfraStateOptions {
// default if nothing is provided
if (infraState === undefined) {
return InfraState.Running;
}
// if a state is provided it should match one of the allowed states
if (!VALID_INFRA_STATES.includes(infraState)) {
throw new Error(
`INFRA_STATE should be one of: ${Object.values(InfraState).join(', ')}. Provided: ${infraState}`,
);
}
return infraState as InfraStateOptions;
}
export function isServiceActive(environment: EnvironmentConfig): {
maintenance: boolean;
database: boolean;
administration: boolean;
graasp: boolean;
migration: boolean;
collab: boolean;
} {
const { infraState } = environment;
switch (infraState) {
case InfraState.Stopped:
return {
maintenance: true,
database: false,
administration: false,
graasp: false,
migration: false,
collab: false,
};
case InfraState.DBOnly:
return {
maintenance: true,
database: true,
administration: true,
graasp: false,
migration: true,
collab: true,
};
case InfraState.Restricted:
return {
maintenance: true,
database: true,
administration: true,
graasp: true,
migration: false,
collab: true,
};
case InfraState.Running:
default:
return {
maintenance: false,
database: true,
administration: true,
graasp: true,
migration: false,
collab: true,
};
}
}
export function getMaintenanceHeaderPair(
environment: EnvironmentConfig,
): { name: string; value: string } | undefined {
if (isServiceActive(environment).maintenance === false) {
return undefined;
}
const name = process.env.MAINTENANCE_HEADER_NAME;
const value = process.env.MAINTENANCE_HEADER_SECRET;
if (!name || !value) {
throw new Error('Expected to have a maintenance header name and value');
}
return { name, value };
}
export function toEnvVar(tfVar: TerraformVariable) {
// this helps to transform a tfvar value into a value that can be used as a string in env var for containers for example.
// it has to do with how terraform handles tokens inside.
return `\$\{${tfVar.value}\}`;
}
export function buildPostgresConnectionString({
protocol,
host,
port,
name,
username,
password,
applicationName,
}: {
protocol?: string;
host: string;
port: string;
name: string;
username: string;
password: string;
applicationName?: string;
}) {
const proto = protocol ?? 'postgres';
const appName = applicationName ?? 'backend';
return `${proto}://${username}:${password}@${host}:${port}/${name}?application_name=${appName}`;
}