Skip to content

Commit

Permalink
Port mapping 80 tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
namse committed Nov 30, 2023
1 parent 94d56d2 commit f416c31
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
34 changes: 34 additions & 0 deletions oioi/agent/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ const GRACEFUL_SHUTDOWN_TIMEOUT_SECS: i64 = 30;
lazy_static::lazy_static! {
static ref GROUP_NAME: String = std::env::var("GROUP_NAME").expect("GROUP_NAME env var not set");
static ref EC2_INSTANCE_ID: String = std::env::var("EC2_INSTANCE_ID").expect("EC2_INSTANCE_ID env var not set");
static ref PORT_MAPPINGS: Vec<PortMapping> = std::env::var("PORT_MAPPINGS").map(|env_string| {
env_string.split(',').map(|mapping| {
let mut parts = mapping.split(&[',', '/']);
let container_port = parts.next().expect("container port not found").parse::<u16>().expect("container port is not a number");
let host_port = parts.next().expect("host port not found").parse::<u16>().expect("host port is not a number");
let protocol = parts.next().expect("protocol not found").to_string();

PortMapping {
container_port,
host_port,
protocol,
}
}).collect()
}).expect("PORT_MAPPINGS env var not set");
}

async fn real_main() -> Result<()> {
Expand Down Expand Up @@ -128,6 +142,20 @@ async fn run_new_container(docker: &Docker, image: &str) -> Result<()> {
("awslogs-create-group".to_string(), "true".to_string()),
])),
}),
port_bindings: Some(std::collections::HashMap::from_iter(
PORT_MAPPINGS
.iter()
.map(|mapping| {
(
format!("{}/{}", mapping.container_port, mapping.protocol),
Some(vec![bollard::models::PortBinding {
host_ip: None,
host_port: Some(mapping.host_port.to_string()),
}]),
)
})
.collect::<Vec<_>>(),
)),
..Default::default()
}),
..Default::default()
Expand Down Expand Up @@ -164,3 +192,9 @@ async fn docker_prune(docker: &Docker) -> Result<()> {

Ok(())
}

struct PortMapping {
container_port: u16,
host_port: u16,
protocol: String,
}
22 changes: 20 additions & 2 deletions oioi/cdk/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface OioiProps {
groupName: string;
image: string;
vpc?: cdk.aws_ec2.Vpc;
portMappings?: PortMapping[];
}

export class Oioi extends Construct {
Expand All @@ -31,7 +32,7 @@ export class Oioi extends Construct {
),
machineImage: cdk.aws_ec2.MachineImage.latestAmazonLinux2023({
cpuType: cdk.aws_ec2.AmazonLinuxCpuType.ARM_64,
userData: getUserData(props.groupName),
userData: getUserData(props),
}),
},
);
Expand All @@ -45,8 +46,24 @@ export class Oioi extends Construct {
}
}

function getUserData(groupName: string): cdk.aws_ec2.UserData {
type PortMapping = {
containerPort: number;
hostPort: number;
protocol: "tcp" | "udp";
};

function getUserData({
groupName,
portMappings,
}: OioiProps): cdk.aws_ec2.UserData {
const userData = cdk.aws_ec2.UserData.forLinux();
const portMappingsString =
portMappings
?.map(
({ containerPort, hostPort, protocol }) =>
`${hostPort ?? containerPort}:${containerPort}/${protocol}`,
)
.join(",") ?? "";

const dockerOptions = [
"-d",
Expand All @@ -59,6 +76,7 @@ function getUserData(groupName: string): cdk.aws_ec2.UserData {

`-e GROUP_NAME=${groupName}`,
`-e EC2_INSTANCE_ID=$EC2_INSTANCE_ID`,
`-e PORT_MAPPINGS=${portMappingsString}`,
"-v /var/run/docker.sock:/var/run/docker.sock docker",
].join(" ");

Expand Down
7 changes: 7 additions & 0 deletions oioi/oioi-test-cdk/lib/oioi-test-cdk-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export class OioiTestCdkStack extends cdk.Stack {
const { vpc, autoScalingGroup } = new oioi.Oioi(this, "Oioi", {
groupName: "test",
image,
portMappings: [
{
containerPort: 80,
hostPort: 80,
protocol: "tcp",
},
],
});

const albTargetGroup =
Expand Down

0 comments on commit f416c31

Please sign in to comment.