Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
bin
CLAUDE.md
Session.vim
892 changes: 720 additions & 172 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@aws-sdk/client-efs": "^3.758.0",
"@aws-sdk/client-elastic-load-balancing-v2": "^3.764.0",
"@aws-sdk/client-route-53": "^3.782.0",
"@aws-sdk/client-secrets-manager": "^3.758.0",
"@aws-sdk/client-servicediscovery": "^3.758.0",
"@studion/prettier-config": "^0.1.0",
"@types/node": "^22",
Expand Down
1 change: 1 addition & 0 deletions src/v2/components/ecs-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export class EcsService extends pulumi.ComponentResource {
return this.logGroup.name.apply(logGroupName => ({
...container,
readonlyRootFilesystem: false,
user: `${FIRST_POSIX_NON_ROOT_USER.userId}:${FIRST_POSIX_NON_ROOT_USER.groupId}`,
...(container.mountPoints && {
mountPoints: container.mountPoints.map(mountPoint =>
pulumi
Expand Down
176 changes: 176 additions & 0 deletions src/v2/components/mongo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import * as pulumi from '@pulumi/pulumi';
import { Password } from '../../../components/password';
import { EcsService } from '../ecs-service';

export namespace Mongo {
export type Container = Pick<
EcsService.Container,
'environment' | 'secrets'
> & {
username: pulumi.Input<string>;
/**
* Autogenerated by default.
*/
password?: pulumi.Input<string>;
/**
* @default 27017
*/
port?: pulumi.Input<number>;
/**
* @default "mongo:7.0.3@sha256:238b1636bdd7820c752b91bec8a669f92568eb313ad89a1fc4a92903c1b40489"
*/
image?: EcsService.Container['image'];
/**
* @default { sourceVolume: 'mongo', containerPath: '/data/db', readOnly: false, }
*/
mountPoints?: EcsService.Container['mountPoints'];
};

export type EcsConfig = Pick<
EcsService.Args,
| 'cluster'
| 'vpc'
| 'desiredCount'
| 'size'
| 'taskExecutionRoleInlinePolicies'
| 'taskRoleInlinePolicies'
| 'tags'
> & {
/**
* @default "mongo"
*/
volumes?: EcsService.Args['volumes'];
// TODO: If autoscaling is enabled, is minCount and maxCount set to 1?
/**
* @default { enabled: true }
*/
autoscaling?: EcsService.Args['autoscaling'];
};

export type Args = EcsConfig & Container;
}

export class Mongo extends pulumi.ComponentResource {
readonly name: string;
readonly username: pulumi.Output<string>;
readonly port: pulumi.Output<number>;
readonly host: pulumi.Output<string>;
readonly service: EcsService;
readonly password: Password;
readonly container: EcsService.Container;
readonly ecsConfig: Mongo.EcsConfig;
readonly volumes: pulumi.Output<EcsService.PersistentStorageVolume[]>;

constructor(
name: string,
args: Mongo.Args,
opts: pulumi.ComponentResourceOptions = {},
) {
super('studion:Mongo', name, args, opts);

const defaultImage =
'mongo:7.0.3@sha256:238b1636bdd7820c752b91bec8a669f92568eb313ad89a1fc4a92903c1b40489';
const defaultPort = 27017;

this.name = name;
this.username = pulumi.output(args.username);
this.port = pulumi.output(args.port || defaultPort);
this.host = pulumi.output(`${name}.${name}`);

this.password = new Password(
`${this.name}-mongo-password`,
{ value: args.password },
{ parent: this },
);

const containerEnvironment = pulumi.output(args.environment).apply(env => [
{
name: 'MONGO_INITDB_ROOT_USERNAME',
value: args.username,
},
...(env || []),
]);
const containerSecrets = pulumi.output(args.secrets).apply(secrets => [
{
name: 'MONGO_INITDB_ROOT_PASSWORD',
valueFrom: this.password.secret.arn,
},
...(secrets || []),
]);
this.container = {
name: this.name,
image: args.image || defaultImage,
portMappings: [EcsService.createTcpPortMapping(this.port)],
mountPoints: this.getMountPoints(args.mountPoints),
environment: containerEnvironment,
secrets: containerSecrets,
};

this.ecsConfig = {
vpc: args.vpc,
cluster: args.cluster,
desiredCount: args.desiredCount || 1,
autoscaling: args.autoscaling || { enabled: false },
size: args.size,
taskExecutionRoleInlinePolicies: args.taskExecutionRoleInlinePolicies,
taskRoleInlinePolicies: args.taskRoleInlinePolicies,
tags: args.tags,
volumes: args.volumes,
};

this.volumes = this.getVolumes(args.volumes);
this.service = this.createEcsService();

this.registerOutputs();
}

private getVolumes(
volumes?: pulumi.Input<pulumi.Input<EcsService.PersistentStorageVolume>[]>,
): pulumi.Output<EcsService.PersistentStorageVolume[]> {
return pulumi.output(volumes).apply(passedVolumes => {
if (!passedVolumes?.length) {
return [{ name: 'mongo' }];
}
return passedVolumes;
});
}

private getMountPoints(
mountPoints?: EcsService.PersistentStorageMountPoint[],
): EcsService.PersistentStorageMountPoint[] {
return (
mountPoints || [
{
sourceVolume: 'mongo',
containerPath: '/data/db',
readOnly: false,
},
]
);
}

// TODO: Pass params such as container
private createEcsService(): EcsService {
return new EcsService(
`${this.name}-ecs`,
{
...this.ecsConfig,
volumes: this.volumes,
containers: [
{
...this.container,
command: [
'mongod',
'--port',
pulumi.output(this.port).apply(port => port.toString()),
],
essential: true,
},
],
enableServiceAutoDiscovery: true,
assignPublicIp: false,
},
{ parent: this },
);
}
}
1 change: 1 addition & 0 deletions src/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { EcsService } from './components/ecs-service';
export { WebServer } from './components/web-server';
export { WebServerBuilder } from './components/web-server/builder';
export { WebServerLoadBalancer } from './components/web-server/load-balancer';
export { Mongo } from './components/mongo';

import { OtelCollectorBuilder } from './otel/builder';
import { OtelCollector } from './otel';
Expand Down
Loading