Skip to content

Commit

Permalink
fix(INFRA-3587): migrate packer config and fix workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
vineetguptadev committed Mar 9, 2024
1 parent d003d29 commit b1f928e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
78 changes: 78 additions & 0 deletions .github/packer/ubuntu-jammy-x86_64-public-ami.json.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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
}

variable "tag" {
type = string
}

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 = lower(var.skip_create_ami) == "true" ? true : false
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

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}-${regex_replace(local.timestamp, "[- T:]", "")}"
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"
}

}
14 changes: 10 additions & 4 deletions .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.6.6+ds1-4ubuntu0.22.04.2"
PYTHON3_BOTO3_VERSION: "1.20.34+dfsg-1"

jobs:
build-public-ami-and-upload:
runs-on: ubuntu-22.04
Expand All @@ -26,16 +30,16 @@ jobs:
- name: Install aws cli
run: |
sudo apt update
sudo apt-get -y install packer python3-boto3
sudo apt-get -y install packer=${PACKER_VERSION} python3-boto3=${PYTHON3_BOTO3_VERSION}
- name: Get the tag
id: get_tag
run: |
if [[ ${{ github.event_name }} == 'push' ]];
then
echo "TAG=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
echo "PKR_VAR_tag=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_ENV
else
echo "TAG=${{ inputs.tag }}" >> $GITHUB_ENV
echo "PKR_VAR_tag=${{ inputs.tag }}" >> $GITHUB_ENV
fi
shell: bash

Expand All @@ -55,8 +59,10 @@ jobs:

- name: Create AMI and upload to marketplace
run: |
packer init ./.github/packer/
./.github/workflows/update-ami.py
env:
TAG: ${{ env.TAG }}
SKIP_CREATE_AMI: ${{ env.SKIP_CREATE_AMI }}
TAG: ${{ env.PKR_VAR_tag }}
PRODUCT_ID: ${{ secrets.MARKETPLACE_PRODUCT }}
ROLE_ARN: ${{ secrets.MARKETPLACE_ROLE }}
7 changes: 5 additions & 2 deletions .github/workflows/update-ami.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
#!/usr/bin/env python3
import json
import os
import os
import boto3
import uuid
import re
import subprocess

# 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.json.pkr.hcl"

# Environment Globals
product_id = os.getenv('PRODUCT_ID')
role_arn = os.getenv('ROLE_ARN')
vtag = os.getenv('TAG')
tag = vtag.replace('v', '')
skip_create_ami = os.getenv('SKIP_CREATE_AMI', "True")
os.environ["PKR_VAR_skip_create_ami"] = skip_create_ami
os.environ["PKR_VAR_tag"] = tag


def packer_build(packerfile):
print("Running the packer build")
Expand Down

0 comments on commit b1f928e

Please sign in to comment.