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

packer build #2806

Merged
merged 14 commits into from
Mar 15, 2024
Merged
60 changes: 0 additions & 60 deletions .github/packer/ubuntu-jammy-x86_64-public-ami.json

This file was deleted.

81 changes: 81 additions & 0 deletions .github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
packer {
required_plugins {
amazon = {
source = "github.com/hashicorp/amazon"
version = "~> 1"
}
ansible = {
source = "github.com/hashicorp/ansible"
version = "~> 1"
}
}
}

variable "skip_create_ami" {
type = string
default = "${env("SKIP_CREATE_AMI")}"
}

variable "tag" {
type = string
default = "${env("TAG")}"
}

variable "version" {
type = string
default = "jammy-22.04"
}

data "amazon-ami" "autogenerated_1" {
filters = {
architecture = "x86_64"
name = "ubuntu/images/*ubuntu-${var.version}-*-server-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
region = "us-east-1"
}

locals {
skip_create_ami = var.skip_create_ami == "True"
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
clean_name = regex_replace(timestamp(), "[^a-zA-Z0-9-]", "-")
}

source "amazon-ebs" "autogenerated_1" {
ami_groups = ["all"]
ami_name = "public-avalanche-ubuntu-${var.version}-${var.tag}-${local.timestamp}"
instance_type = "c5.large"
region = "us-east-1"
skip_create_ami = local.skip_create_ami
source_ami = "${data.amazon-ami.autogenerated_1.id}"
ssh_username = "ubuntu"
tags = {
Base_AMI_Name = "{{ .SourceAMIName }}"
Name = "public-avalanche-ubuntu-${var.version}-${var.tag}-${local.clean_name}"
Release = "${var.version}"
}
}

build {
sources = ["source.amazon-ebs.autogenerated_1"]

provisioner "shell" {
inline = ["while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done", "wait_apt=$(ps aux | grep apt | wc -l)", "while [ \"$wait_apt\" -gt \"1\" ]; do echo \"waiting for apt to be ready....\"; wait_apt=$(ps aux | grep apt | wc -l); sleep 5; done", "sudo apt-get -y update", "sudo apt-get install -y python3-boto3 golang"]
}

provisioner "ansible" {
extra_arguments = ["-e", "component=public-ami build=packer os_release=jammy tag=${var.tag}"]
playbook_file = ".github/packer/create_public_ami.yml"
roles_path = ".github/packer/roles/"
use_proxy = false
}

provisioner "shell" {
execute_command = "sudo bash -x {{ .Path }}"
script = ".github/packer/clean-public-ami.sh"
}

}
20 changes: 19 additions & 1 deletion .github/workflows/build-public-ami.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ on:
tags:
- "*"

env:
PACKER_VERSION: "1.10.2"
PYTHON3_BOTO3_VERSION: "1.20.34+dfsg-1"

jobs:
build-public-ami-and-upload:
runs-on: ubuntu-22.04
Expand All @@ -26,7 +30,7 @@ jobs:
- name: Install aws cli
run: |
sudo apt update
sudo apt-get -y install packer python3-boto3
sudo apt-get -y install python3-boto3=${PYTHON3_BOTO3_VERSION}

- name: Get the tag
id: get_tag
Expand All @@ -53,6 +57,20 @@ jobs:
aws-secret-access-key: ${{ secrets.MARKETPLACE_KEY }}
aws-region: us-east-1

- name: Setup `packer`
uses: hashicorp/setup-packer@main
id: setup
with:
version: ${{ env.PACKER_VERSION }}

- name: Run `packer init`
id: init
run: "packer init ./.github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl"

- name: Run `packer validate`
id: validate
run: "packer validate ./.github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl"

- name: Create AMI and upload to marketplace
run: |
./.github/workflows/update-ami.py
Expand Down
26 changes: 19 additions & 7 deletions .github/workflows/update-ami.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import uuid
import re
import subprocess
import sys

# Globals
amifile = '.github/workflows/amichange.json'
packerfile = ".github/packer/ubuntu-jammy-x86_64-public-ami.json"
packerfile = ".github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl"

# Environment Globals
product_id = os.getenv('PRODUCT_ID')
Expand All @@ -19,11 +20,16 @@

def packer_build(packerfile):
print("Running the packer build")
subprocess.run('/usr/local/bin/packer build ' + packerfile, shell=True)
output = subprocess.run('/usr/local/bin/packer build ' + packerfile, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if output.returncode != 0:
raise RuntimeError(f"Command returned with code: {output.returncode}")

def packer_build_update(packerfile):
print("Creating packer AMI image for Marketplace")
output = subprocess.run('/usr/local/bin/packer build ' + packerfile, shell=True, stdout=subprocess.PIPE)
output = subprocess.run('/usr/local/bin/packer build ' + packerfile, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if output.returncode != 0:
raise RuntimeError(f"Command returned with code: {output.returncode}")

found = re.findall('ami-[a-z0-9]*', str(output.stdout))

if found:
Expand Down Expand Up @@ -76,10 +82,16 @@ def update_ami(amifile, amiid):
print(f"An error occurred while updating AMI delivery options: {e}")

def main():
if skip_create_ami == "True":
packer_build(packerfile)
else:
update_ami(amifile, packer_build_update(packerfile))
try:
if skip_create_ami == "True":
packer_build(packerfile)
else:
update_ami(amifile, packer_build_update(packerfile))

print("Ran packer build and update ami successfully")
except Exception as e:
print(f"An error occurred while running packer")
sys.exit(5)

if __name__ == '__main__':
main()
Expand Down
Loading