Skip to content

Commit

Permalink
wip: filemanager docker function
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalenic committed Mar 26, 2024
1 parent 99256ff commit 56dba74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Construct } from 'constructs';
import { Duration } from 'aws-cdk-lib';
import { AssetStaging, BundlingFileAccess, Duration } from 'aws-cdk-lib';
import { ISecurityGroup, IVpc, SecurityGroup, SubnetType } from 'aws-cdk-lib/aws-ec2';
import { Architecture, Version } from 'aws-cdk-lib/aws-lambda';
import { ManagedPolicy, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
Expand All @@ -8,6 +8,8 @@ import { RustFunction } from 'cargo-lambda-cdk';
import path from 'path';
import { exec } from 'cargo-lambda-cdk/lib/util';
import { randomUUID } from 'node:crypto';
import { print } from 'aws-cdk/lib/logging';
import * as fs from 'fs';

/**
* Properties for the database.
Expand Down Expand Up @@ -94,28 +96,60 @@ export class Function extends Construct {
`/` +
`${props.databaseSecret.secretValueFromJson('dbname').unsafeUnwrap()}`;

const manifestPath = path.join(__dirname, '..', '..', '..');
const workspacePath = path.join(__dirname, '..', '..', '..');
const manifestPath = path.join(workspacePath, props.package, 'Cargo.toml');
const uuid = randomUUID();

print(`running filemanager docker-run with project name: ${uuid}`);
// This starts the container running postgres in order to compile queries using sqlx.
// It needs to be executed outside `beforeBundling`, because `beforeBundling` runs inside
// the container context, and docker compose needs to run outside of this context.
const output = exec(
'make',
['-s', 'docker-run', `DOCKER_PROJECT_NAME=${randomUUID()}`],
{ cwd: manifestPath, shell: true }
['-s', 'docker-run', `DOCKER_PROJECT_NAME=${uuid}`],
{ cwd: workspacePath, shell: true }
);
// Grab the last line only in case there are other outputs.
const address = output.stdout.toString().trim().match('.*$')?.pop();
const localDatabaseUrl = `postgresql://filemanager:filemanager@${address}/filemanager`; // pragma: allowlist secret
print(`the local filemanager database url is: ${localDatabaseUrl}`);
console.log(JSON.stringify(address));
const a = output.stdout.toString().trim();
console.log(JSON.stringify(a));

this._function = new RustFunction(this, 'RustFunction', {
manifestPath,
manifestPath: manifestPath,
binaryName: props.package,
bundling: {
environment: {
...props.buildEnvironment,
// Avoid permission issues by creating another target directory.
CARGO_TARGET_DIR: "target-cdk-docker-bundling",
// The bundling container needs to be able to connect to the container running postgres.
DATABASE_URL: `postgresql://filemanager:filemanager@${address}/filemanager`, // pragma: allowlist secret
DATABASE_URL: localDatabaseUrl,
},
commandHooks: {
beforeBundling(inputDir: string, outputDir: string): string[] {
return [];
},
afterBundling(inputDir: string, outputDir: string): string[] {
console.log(manifestPath);
console.log(`input: ${inputDir}`);
console.log(`output: ${outputDir}`);

// console.log(AssetStaging.BUNDLING_OUTPUT_DIR);
fs.readdirSync('/tmp').forEach(file => {
if (fs.existsSync(file) && fs.lstatSync(file).isDirectory()) {
fs.readdirSync(file).forEach(inner => {
console.log(inner);
})
}
console.log(file);
});

console.log(`dir: ${__dirname}`);
return [];
}
}
},
memorySize: 128,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@aws-cdk/aws-lambda-python-alpha": "2.126.0-alpha.0",
"aws-cdk-lib": "^2.133.0",
"cargo-lambda-cdk": "^0.0.19",
"cargo-lambda-cdk": "^0.0.20",
"cdk-nag": "^2.28.27",
"constructs": "^10.2.69",
"dotenv": "^16.3.1",
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1680,15 +1680,15 @@ __metadata:
languageName: node
linkType: hard

"cargo-lambda-cdk@npm:^0.0.19":
version: 0.0.19
resolution: "cargo-lambda-cdk@npm:0.0.19"
"cargo-lambda-cdk@npm:^0.0.20":
version: 0.0.20
resolution: "cargo-lambda-cdk@npm:0.0.20"
dependencies:
js-toml: ^0.1.1
peerDependencies:
aws-cdk-lib: ^2.1.0
aws-cdk-lib: ^2.63.0
constructs: ^10.0.5
checksum: a2ad75973055b5b7ab3b4f7233643677befec10ea9b6a2e4a565dd61783c4247f383f6455b19986dd2ca8bf050c656bd0ebf89161ea46e7626e875585b9d606a
checksum: 407b3d9226dbee1b76a4a80c485a329da466eefaeb9afc71cd624bfedfe2f85adfeb107e4540d92ce16c4a799a15d622e3c6a48f3f879af63e564d892fb96fba
languageName: node
linkType: hard

Expand Down Expand Up @@ -3892,7 +3892,7 @@ __metadata:
"@typescript-eslint/parser": ^6.19.1
aws-cdk: ^2.133.0
aws-cdk-lib: ^2.133.0
cargo-lambda-cdk: ^0.0.19
cargo-lambda-cdk: ^0.0.20
cdk-nag: ^2.28.27
constructs: ^10.2.69
dotenv: ^16.3.1
Expand Down

0 comments on commit 56dba74

Please sign in to comment.