Skip to content

Commit 5eaa9c2

Browse files
packer build (#2806)
Co-authored-by: yevhenvolchenko <yevhenvolchenko@users.noreply.github.com>
1 parent 598018b commit 5eaa9c2

File tree

4 files changed

+119
-68
lines changed

4 files changed

+119
-68
lines changed

.github/packer/ubuntu-jammy-x86_64-public-ami.json

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
packer {
2+
required_plugins {
3+
amazon = {
4+
source = "github.com/hashicorp/amazon"
5+
version = "~> 1"
6+
}
7+
ansible = {
8+
source = "github.com/hashicorp/ansible"
9+
version = "~> 1"
10+
}
11+
}
12+
}
13+
14+
variable "skip_create_ami" {
15+
type = string
16+
default = "${env("SKIP_CREATE_AMI")}"
17+
}
18+
19+
variable "tag" {
20+
type = string
21+
default = "${env("TAG")}"
22+
}
23+
24+
variable "version" {
25+
type = string
26+
default = "jammy-22.04"
27+
}
28+
29+
data "amazon-ami" "autogenerated_1" {
30+
filters = {
31+
architecture = "x86_64"
32+
name = "ubuntu/images/*ubuntu-${var.version}-*-server-*"
33+
root-device-type = "ebs"
34+
virtualization-type = "hvm"
35+
}
36+
most_recent = true
37+
owners = ["099720109477"]
38+
region = "us-east-1"
39+
}
40+
41+
locals {
42+
skip_create_ami = var.skip_create_ami == "True"
43+
timestamp = regex_replace(timestamp(), "[- TZ:]", "")
44+
clean_name = regex_replace(timestamp(), "[^a-zA-Z0-9-]", "-")
45+
}
46+
47+
source "amazon-ebs" "autogenerated_1" {
48+
ami_groups = ["all"]
49+
ami_name = "public-avalanche-ubuntu-${var.version}-${var.tag}-${local.timestamp}"
50+
instance_type = "c5.large"
51+
region = "us-east-1"
52+
skip_create_ami = local.skip_create_ami
53+
source_ami = "${data.amazon-ami.autogenerated_1.id}"
54+
ssh_username = "ubuntu"
55+
tags = {
56+
Base_AMI_Name = "{{ .SourceAMIName }}"
57+
Name = "public-avalanche-ubuntu-${var.version}-${var.tag}-${local.clean_name}"
58+
Release = "${var.version}"
59+
}
60+
}
61+
62+
build {
63+
sources = ["source.amazon-ebs.autogenerated_1"]
64+
65+
provisioner "shell" {
66+
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"]
67+
}
68+
69+
provisioner "ansible" {
70+
extra_arguments = ["-e", "component=public-ami build=packer os_release=jammy tag=${var.tag}"]
71+
playbook_file = ".github/packer/create_public_ami.yml"
72+
roles_path = ".github/packer/roles/"
73+
use_proxy = false
74+
}
75+
76+
provisioner "shell" {
77+
execute_command = "sudo bash -x {{ .Path }}"
78+
script = ".github/packer/clean-public-ami.sh"
79+
}
80+
81+
}

.github/workflows/build-public-ami.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ on:
1010
tags:
1111
- "*"
1212

13+
env:
14+
PACKER_VERSION: "1.10.2"
15+
PYTHON3_BOTO3_VERSION: "1.20.34+dfsg-1"
16+
1317
jobs:
1418
build-public-ami-and-upload:
1519
runs-on: ubuntu-22.04
@@ -26,7 +30,7 @@ jobs:
2630
- name: Install aws cli
2731
run: |
2832
sudo apt update
29-
sudo apt-get -y install packer python3-boto3
33+
sudo apt-get -y install python3-boto3=${PYTHON3_BOTO3_VERSION}
3034
3135
- name: Get the tag
3236
id: get_tag
@@ -53,6 +57,20 @@ jobs:
5357
aws-secret-access-key: ${{ secrets.MARKETPLACE_KEY }}
5458
aws-region: us-east-1
5559

60+
- name: Setup `packer`
61+
uses: hashicorp/setup-packer@main
62+
id: setup
63+
with:
64+
version: ${{ env.PACKER_VERSION }}
65+
66+
- name: Run `packer init`
67+
id: init
68+
run: "packer init ./.github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl"
69+
70+
- name: Run `packer validate`
71+
id: validate
72+
run: "packer validate ./.github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl"
73+
5674
- name: Create AMI and upload to marketplace
5775
run: |
5876
./.github/workflows/update-ami.py

.github/workflows/update-ami.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import uuid
66
import re
77
import subprocess
8+
import sys
89

910
# Globals
1011
amifile = '.github/workflows/amichange.json'
11-
packerfile = ".github/packer/ubuntu-jammy-x86_64-public-ami.json"
12+
packerfile = ".github/packer/ubuntu-jammy-x86_64-public-ami.pkr.hcl"
1213

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

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

2427
def packer_build_update(packerfile):
2528
print("Creating packer AMI image for Marketplace")
26-
output = subprocess.run('/usr/local/bin/packer build ' + packerfile, shell=True, stdout=subprocess.PIPE)
29+
output = subprocess.run('/usr/local/bin/packer build ' + packerfile, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30+
if output.returncode != 0:
31+
raise RuntimeError(f"Command returned with code: {output.returncode}")
32+
2733
found = re.findall('ami-[a-z0-9]*', str(output.stdout))
2834

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

7884
def main():
79-
if skip_create_ami == "True":
80-
packer_build(packerfile)
81-
else:
82-
update_ami(amifile, packer_build_update(packerfile))
85+
try:
86+
if skip_create_ami == "True":
87+
packer_build(packerfile)
88+
else:
89+
update_ami(amifile, packer_build_update(packerfile))
90+
91+
print("Ran packer build and update ami successfully")
92+
except Exception as e:
93+
print(f"An error occurred while running packer")
94+
sys.exit(5)
8395

8496
if __name__ == '__main__':
8597
main()

0 commit comments

Comments
 (0)