Skip to content

Commit

Permalink
Fit types
Browse files Browse the repository at this point in the history
  • Loading branch information
namse committed Dec 3, 2023
1 parent 8fd8408 commit 4032c03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
45 changes: 23 additions & 22 deletions oioi/cdk/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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(
Expand All @@ -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<string, cdk.aws_ec2.InitConfig> = {
Expand Down Expand Up @@ -273,3 +266,11 @@ type PortMapping = {
hostPort: number;
protocol: "tcp" | "udp";
};

export class OioiDockerNormalImage {
public readonly uri: string;

constructor(uri: string) {
this.uri = uri;
}
}
9 changes: 1 addition & 8 deletions oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,16 @@ 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,
hostPort: 80,
protocol: "tcp",
},
],
dockerLoginScript: `aws ecr get-login-password --region ${
this.region
} | docker login --username AWS --password-stdin ${
image.repository.repositoryUri.split("/")[0]
}`,
});

const albTargetGroup =
Expand Down

0 comments on commit 4032c03

Please sign in to comment.