console/deployment/utils/cloudflare.ts

212 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-05-18 07:26:57 +00:00
import { readFileSync } from 'fs';
import { resolve } from 'path';
2022-12-28 19:22:54 +00:00
import * as cf from '@pulumi/cloudflare';
import * as pulumi from '@pulumi/pulumi';
2024-03-04 12:56:12 +00:00
import { S3 } from '../services/s3';
2022-05-18 07:26:57 +00:00
export class CloudflareCDN {
constructor(
2022-10-07 10:08:29 +00:00
private config: {
envName: string;
zoneId: string;
cdnDnsRecord: string;
2024-03-04 12:56:12 +00:00
sentryDsn: string | pulumi.Output<string>;
2022-10-07 10:08:29 +00:00
release: string;
2024-03-04 12:56:12 +00:00
s3: S3;
s3Mirror: S3;
},
2022-05-18 07:26:57 +00:00
) {}
deploy() {
const kvStorage = new cf.WorkersKvNamespace('hive-ha-storage', {
2022-10-07 10:08:29 +00:00
title: `hive-ha-cdn-${this.config.envName}`,
2022-05-18 07:26:57 +00:00
});
const script = new cf.WorkerScript('hive-ha-worker', {
content: readFileSync(
// eslint-disable-next-line no-process-env
process.env.CDN_WORKER_ARTIFACT_PATH ||
resolve(__dirname, '../../packages/services/cdn-worker/dist/index.worker.mjs'),
'utf-8',
),
2022-10-07 10:08:29 +00:00
name: `hive-storage-cdn-${this.config.envName}`,
module: true,
2022-05-18 07:26:57 +00:00
kvNamespaceBindings: [
{
// HIVE_DATA is in use in cdn-script.js as well, its the name of the global variable
name: 'HIVE_DATA',
namespaceId: kvStorage.id,
},
],
analyticsEngineBindings: [
{
name: 'USAGE_ANALYTICS',
2022-12-19 09:34:58 +00:00
dataset: `hive_ha_cdn_usage_${this.config.envName}`,
},
{
name: 'ERROR_ANALYTICS',
2022-12-19 09:34:58 +00:00
dataset: `hive_ha_cdn_error_${this.config.envName}`,
},
{
name: 'KEY_VALIDATION_ANALYTICS',
dataset: `hive_ha_cdn_key_validation_${this.config.envName}`,
},
2023-09-14 10:30:36 +00:00
{
name: 'R2_ANALYTICS',
dataset: `hive_ha_cdn_r2_${this.config.envName}`,
},
{
name: 'S3_ANALYTICS',
dataset: `hive_ha_cdn_s3_${this.config.envName}`,
},
2023-09-14 10:30:36 +00:00
{
name: 'RESPONSE_ANALYTICS',
dataset: `hive_ha_cdn_response_${this.config.envName}`,
},
],
2022-05-18 07:26:57 +00:00
secretTextBindings: [
2022-10-07 10:08:29 +00:00
{
name: 'SENTRY_DSN',
text: this.config.sentryDsn,
},
{
name: 'SENTRY_ENVIRONMENT',
text: this.config.envName,
},
{
name: 'SENTRY_RELEASE',
text: this.config.release,
2022-05-18 07:26:57 +00:00
},
{
name: 'S3_ENDPOINT',
2024-03-04 12:56:12 +00:00
text: this.config.s3.secret.raw.endpoint,
},
{
name: 'S3_ACCESS_KEY_ID',
2024-03-04 12:56:12 +00:00
text: this.config.s3.secret.raw.accessKeyId,
},
{
name: 'S3_SECRET_ACCESS_KEY',
2024-03-04 12:56:12 +00:00
text: this.config.s3.secret.raw.secretAccessKey,
},
{
name: 'S3_BUCKET_NAME',
2024-03-04 12:56:12 +00:00
text: this.config.s3.secret.raw.bucket,
},
{
name: 'S3_MIRROR_ENDPOINT',
text: this.config.s3Mirror.secret.raw.endpoint,
},
{
name: 'S3_MIRROR_ACCESS_KEY_ID',
text: this.config.s3Mirror.secret.raw.accessKeyId,
},
{
name: 'S3_MIRROR_SECRET_ACCESS_KEY',
text: this.config.s3Mirror.secret.raw.secretAccessKey,
},
{
name: 'S3_MIRROR_BUCKET_NAME',
text: this.config.s3Mirror.secret.raw.bucket,
},
2022-05-18 07:26:57 +00:00
],
});
2022-10-07 10:08:29 +00:00
const workerBase = this.config.cdnDnsRecord;
2022-05-18 07:26:57 +00:00
const workerUrl = `https://${workerBase}`;
new cf.WorkerRoute('cf-hive-worker', {
scriptName: script.name,
pattern: `${workerBase}/*`,
2022-10-07 10:08:29 +00:00
zoneId: this.config.zoneId,
2022-05-18 07:26:57 +00:00
});
return {
workerBaseUrl: workerUrl,
cfStorageNamespaceId: kvStorage.id,
};
}
}
2022-11-07 13:27:19 +00:00
export class CloudflareBroker {
constructor(
private config: {
envName: string;
zoneId: string;
cdnDnsRecord: string;
secretSignature: pulumi.Output<string>;
2024-03-04 12:56:12 +00:00
sentryDsn: string | pulumi.Output<string>;
2022-11-07 13:27:19 +00:00
release: string;
loki: null | {
endpoint: string;
username: string;
password: pulumi.Output<string>;
};
},
2022-11-07 13:27:19 +00:00
) {}
deploy() {
const secretTextBindings = [
{
name: 'SIGNATURE',
text: this.config.secretSignature,
},
{
name: 'SENTRY_DSN',
text: this.config.sentryDsn,
},
{
name: 'SENTRY_ENVIRONMENT',
text: this.config.envName,
},
{
name: 'SENTRY_RELEASE',
text: this.config.release,
},
];
if (this.config.loki) {
secretTextBindings.push(
{
name: 'LOKI_PASSWORD',
text: this.config.loki.password,
},
{
name: 'LOKI_USERNAME',
text: this.config.loki.username,
},
{
name: 'LOKI_ENDPOINT',
text: this.config.loki.endpoint,
},
);
}
const script = new cf.WorkerScript('hive-broker-worker', {
content: readFileSync(
// eslint-disable-next-line no-process-env
process.env.BROKER_WORKER_ARTIFACT_PATH ||
2023-07-27 12:28:10 +00:00
resolve(__dirname, '../../packages/services/broker-worker/dist/index.worker.mjs'),
'utf-8',
),
2023-07-27 12:28:10 +00:00
module: true,
name: `hive-broker-${this.config.envName}`,
secretTextBindings,
2022-11-07 13:27:19 +00:00
});
const workerBase = this.config.cdnDnsRecord;
const workerUrl = `https://${workerBase}`;
new cf.WorkerRoute('cf-hive-broker-worker', {
scriptName: script.name,
pattern: `${workerBase}/*`,
zoneId: this.config.zoneId,
});
return {
secretSignature: this.config.secretSignature,
workerBaseUrl: workerUrl,
};
}
}