-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuser-data.ts
85 lines (70 loc) · 2.67 KB
/
user-data.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
import { UserData } from "aws-cdk-lib/aws-ec2";
import type { S3DownloadOptions } from "aws-cdk-lib/aws-ec2";
import { Bucket } from "aws-cdk-lib/aws-s3";
import { GuDistributable } from "../../types";
import type { GuDistributableForEc2 } from "../../types";
import type { GuPrivateS3ConfigurationProps } from "../../utils/ec2";
import { GuDistributionBucketParameter } from "../core";
import type { AppIdentity, GuStack } from "../core";
export type GuUserDataPropsWithApp = GuUserDataProps & AppIdentity;
export interface GuUserDataProps {
distributable: GuDistributableForEc2;
configuration?: GuPrivateS3ConfigurationProps;
}
/**
* An abstraction over UserData to simplify its creation.
* Especially useful for simple user data where we:
* - (optional) download config
* - download distributable
* - execute distributable
*/
export class GuUserData {
private readonly _userData: UserData;
get userData(): UserData {
return this._userData;
}
private downloadDistributable(scope: GuStack, app: AppIdentity, props: GuDistributableForEc2) {
const bucketKey = GuDistributable.getObjectKey(scope, app, props);
const bucket = Bucket.fromBucketAttributes(scope, `DistributionBucket-${app.app}`, {
bucketName: GuDistributionBucketParameter.getInstance(scope).valueAsString,
});
this.addS3DownloadCommand({
bucket,
bucketKey,
localFile: `/${app.app}/${props.fileName}`,
});
}
private downloadConfiguration(scope: GuStack, app: string, props: GuPrivateS3ConfigurationProps) {
const bucket = Bucket.fromBucketAttributes(scope, `${app}ConfigurationBucket`, {
bucketName: props.bucket.valueAsString,
});
props.files.forEach((bucketKey) => {
const fileName = bucketKey.split("/").slice(-1)[0];
// `fileName` is typed as `string | undefined`. Throw if `fileName` is falsy.
if (!fileName) {
throw new Error("Failed to create configuration section in UserData");
}
this.addS3DownloadCommand({
bucket,
bucketKey,
localFile: `/etc/${app}/${fileName}`,
});
});
}
constructor(scope: GuStack, props: GuUserDataPropsWithApp) {
this._userData = UserData.forLinux();
if (props.configuration) {
this.downloadConfiguration(scope, props.app, props.configuration);
}
this.downloadDistributable(scope, props, props.distributable);
this.addCommands(props.distributable.executionStatement);
}
addCommands(...commands: string[]): GuUserData {
this._userData.addCommands(...commands);
return this;
}
addS3DownloadCommand(params: S3DownloadOptions): GuUserData {
this._userData.addS3DownloadCommand(params);
return this;
}
}