From 4032c03bb5dfbb3c7a9f1db0391ff992ab2824b9 Mon Sep 17 00:00:00 2001 From: namse Date: Sun, 3 Dec 2023 10:43:10 +0000 Subject: [PATCH] Fit types --- oioi/cdk/lib/index.ts | 45 ++++++++++--------- oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts | 9 +--- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/oioi/cdk/lib/index.ts b/oioi/cdk/lib/index.ts index 0152cc4f9..c2d04e5c8 100644 --- a/oioi/cdk/lib/index.ts +++ b/oioi/cdk/lib/index.ts @@ -3,21 +3,7 @@ import { Construct } from "constructs"; export interface OioiProps { groupName: string; - image: - | { - type: "ecr"; - account: string; - region: string; - repository: string; - tag: string; - } - | { - type: "normal"; - /** - * @example public.ecr.aws/o4b6l4b3/oioi:latest - */ - uri: string; - }; + image: cdk.aws_ecr_assets.DockerImageAsset | OioiDockerNormalImage; vpc?: cdk.aws_ec2.Vpc; alb?: cdk.aws_elasticloadbalancingv2.ApplicationLoadBalancer; portMappings?: PortMapping[]; @@ -82,11 +68,17 @@ export class Oioi extends Construct { const imageUri = (() => { const image = props.image; - if (image.type === "ecr") { - return `${image.account}.dkr.ecr.${image.region}.amazonaws.com/${image.repository}:${image.tag}`; - } else { + if (image instanceof cdk.aws_ecr_assets.DockerImageAsset) { + const account = image.imageUri.split(".")[0].split("/")[0]; + const region = image.imageUri.split(".")[3]; + const repository = image.imageUri.split("/")[1]; + const tag = image.imageUri.split(":")[1]; + return `${account}.dkr.ecr.${region}.amazonaws.com/${repository}:${tag}`; + } + if (image instanceof OioiDockerNormalImage) { return image.uri; } + throw new Error("Invalid image"); })(); const imageParameter = new cdk.aws_ssm.StringParameter( @@ -100,11 +92,12 @@ export class Oioi extends Construct { const dockerLoginScript = (() => { const image = props.image; - if (image.type === "ecr") { - return `aws ecr get-login-password --region ${image.region} | docker login --username AWS --password-stdin ${image.account}.dkr.ecr.${image.region}.amazonaws.com`; - } else { - return ""; + if (image instanceof cdk.aws_ecr_assets.DockerImageAsset) { + const account = image.imageUri.split(".")[0].split("/")[0]; + const region = image.imageUri.split(".")[3]; + return `aws ecr get-login-password --region ${region} | docker login --username AWS --password-stdin ${account}.dkr.ecr.${region}.amazonaws.com`; } + return ""; })(); const initConfigs: Record = { @@ -273,3 +266,11 @@ type PortMapping = { hostPort: number; protocol: "tcp" | "udp"; }; + +export class OioiDockerNormalImage { + public readonly uri: string; + + constructor(uri: string) { + this.uri = uri; + } +} diff --git a/oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts b/oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts index ab887c34e..30230a9bf 100644 --- a/oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts +++ b/oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts @@ -13,11 +13,9 @@ export class OioiTestCdkStack extends cdk.Stack { outputs: ["type=docker"], }); - console.log("image.imageUri", image.imageUri); - const { vpc, autoScalingGroup, alb } = new oioi.Oioi(this, "Oioi", { groupName: "test", - image: image.imageUri, + image, portMappings: [ { containerPort: 80, @@ -25,11 +23,6 @@ export class OioiTestCdkStack extends cdk.Stack { protocol: "tcp", }, ], - dockerLoginScript: `aws ecr get-login-password --region ${ - this.region - } | docker login --username AWS --password-stdin ${ - image.repository.repositoryUri.split("/")[0] - }`, }); const albTargetGroup =