Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package builder #228

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions infrastructure/package_builder/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions infrastructure/package_builder/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
data "aws_ami" "debian_latest" {
most_recent = true
name_regex = "debian-11-arm64"
owners = ["136693071363"]

filter {
name = "virtualization-type"
values = ["hvm"]
}
}

resource "aws_iam_role" "package_builder" {
name = "package_builder"

assume_role_policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": ["ec2.amazonaws.com"]
},
"Effect": "Allow"
}
]
}
EOF
}

resource "aws_iam_instance_profile" "package_builder" {
name = "package_builder"
role = aws_iam_role.package_builder.name
}

resource "aws_iam_role_policy_attachment" "package_builder_ssm" {
role = aws_iam_role.package_builder.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

resource "aws_launch_template" "package_builder" {
name_prefix = "package-builder"
image_id = data.aws_ami.debian_latest.id
instance_type = "t4g.small"

iam_instance_profile {
name = aws_iam_instance_profile.package_builder.name
}

vpc_security_group_ids = [aws_security_group.package_builder.id]

user_data = base64encode(join("\n", [
"#cloud-config",
yamlencode({
write_files : [
{
path : "/opt/builder/build.sh",
content : file("${path.module}/templates/build.sh"),
permissions : "0755",
},
{
path : "/opt/ssm_agent.sh",
content : file("${path.module}/templates/ssm_agent.sh"),
permissions : "0755",
},
],
runcmd : [
["/opt/ssm_agent.sh", "/opt/builder/build.sh"]
],
})
]))

lifecycle {
create_before_destroy = true
}
}

resource "aws_security_group" "package_builder" {
name = "package-builder"
vpc_id = data.terraform_remote_state.core_infrastructure.outputs.vpc.vpc_id
}

resource "aws_security_group_rule" "package_builder_egress" {
type = "egress"
to_port = 0
protocol = "-1"
from_port = 0
security_group_id = aws_security_group.package_builder.id
cidr_blocks = ["0.0.0.0/0"]
}

resource "aws_autoscaling_group" "package_builder" {
name = "package-builder"

launch_template {
id = aws_launch_template.package_builder.id
version = aws_launch_template.package_builder.latest_version
}

vpc_zone_identifier = data.terraform_remote_state.core_infrastructure.outputs.vpc.private_subnets

max_size = 1
min_size = 0
desired_capacity = 0
wait_for_capacity_timeout = 0

tag {
key = "Name"
value = "package-builder"
propagate_at_launch = true
}

instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
triggers = ["tag"]
}

lifecycle {
create_before_destroy = true
}
}
22 changes: 22 additions & 0 deletions infrastructure/package_builder/templates/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash -x

sudo apt-get -y update
sudo apt-get install -y xz-utils devscripts cowbuilder git
sudo echo "ALLOWUNTRUSTED=yes" >> /etc/pbuilderrc

sudo mkdir -p /usr/src/freeswitch-debs
sudo git clone https://github.com/somleng/freeswitch.git -bbuild_deps /usr/src/freeswitch-debs/freeswitch

# sudo git clone https://github.com/freeswitch/spandsp.git /usr/src/freeswitch-debs/spandsp
# sudo git clone https://github.com/freeswitch/sofia-sip /usr/src/freeswitch-debs/sofia-sip
#
# Follow this article to build debs
# https://earthly.dev/blog/creating-and-hosting-your-own-deb-packages-and-apt-repo/

# Copy debs to s3 and sign

cd /usr/src/freeswitch-debs/freeswitch

sudo ./debian/util.sh build-all -cbullseye -mquicktest -aarm64

# https://earthly.dev/blog/creating-and-hosting-your-own-deb-packages-and-apt-repo/
7 changes: 7 additions & 0 deletions infrastructure/package_builder/templates/ssm_agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash -x
# https://docs.aws.amazon.com/systems-manager/latest/userguide/agent-install-deb.html

mkdir /tmp/ssm
cd /tmp/ssm
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_arm64/amazon-ssm-agent.deb
sudo dpkg -i amazon-ssm-agent.deb
22 changes: 22 additions & 0 deletions infrastructure/package_builder/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terraform {
backend "s3" {
bucket = "infrastructure.somleng.org"
key = "package_builder.tfstate"
encrypt = true
region = "ap-southeast-1"
}
}

provider "aws" {
region = var.aws_region
}

data "terraform_remote_state" "core_infrastructure" {
backend = "s3"

config = {
bucket = "infrastructure.somleng.org"
key = "core.tfstate"
region = var.aws_region
}
}
3 changes: 3 additions & 0 deletions infrastructure/package_builder/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "aws_region" {
default = "ap-southeast-1"
}