Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
Signed-off-by: jayfranco999 <[email protected]>
  • Loading branch information
jayfranco999 committed Oct 2, 2024
1 parent 1f36849 commit a928364
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ manifest.json
creds.json
vars.json

AWSCLIV2.pkg
13 changes: 0 additions & 13 deletions Jenkinsfile_k8s
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,6 @@ pipeline {
}
}
excludes {
/**
Since https://github.com/jenkins-infra/helpdesk/issues/3421 and https://github.com/jenkins-infra/helpdesk/issues/3502, EC2 is only used for Linux + `arm64` VMs.
**/
exclude {
axis {
name 'cpu_architecture'
values 'amd64'
}
axis {
name 'compute_type'
values 'amazon-ebs'
}
}
// Only build Ubuntu images for arm64 CPU in AWS (notValues)
exclude {
axis {
Expand Down
5 changes: 5 additions & 0 deletions build-jenkins-agent-ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ build {
name = "ubuntu"
}

source "amazon-ebs.base" {
name = "ubuntu"
ssh_username = "ubuntu"
}

source "azure-arm.base" {
name = "ubuntu"
# List available offers and publishers with the command `az vm image list --output table`
Expand Down
22 changes: 22 additions & 0 deletions build-jenkins-agent-windows.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
build {
source "amazon-ebs.base" {
name = "windows"
communicator = "winrm"
user_data_file = "./provisioning/setupWinRM.ps1"
winrm_insecure = true
winrm_timeout = "20m"
winrm_use_ssl = true
winrm_username = local.windows_winrm_user[var.image_type]
}

source "azure-arm.base" {
name = "windows"
communicator = "winrm"
Expand Down Expand Up @@ -130,4 +140,16 @@ build {
"while($true) { $imageState = Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Setup\\State | Select ImageState; if($imageState.ImageState -ne 'IMAGE_STATE_GENERALIZE_RESEAL_TO_OOBE') { Write-Output $imageState.ImageState; Start-Sleep -s 10 } else { break } }"
]
}

# This provisioner must be the last for AWS EBS builds, after reboots
provisioner "powershell" {
only = ["amazon-ebs.windows"]
elevated_user = local.windows_winrm_user[var.image_type]
elevated_password = build.Password

inline = [
"& 'C:/Program Files/Amazon/EC2Launch/ec2launch' reset --block",
"& 'C:/Program Files/Amazon/EC2Launch/ec2launch' sysprep --block",
]
}
}
34 changes: 34 additions & 0 deletions datasources.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Data sources are always treated BEFORE locals and sources.
data "amazon-ami" "ubuntu-22_04" {
filters = {
name = format("ubuntu/images/hvm-ssd/ubuntu-*-22.04-%s-server-*", var.architecture)
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["099720109477"]
region = var.aws_region
}

data "amazon-ami" "windows-2019" {
filters = {
# https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2launch-v2.html
name = "EC2LaunchV2-Windows_Server-2019-English-Core-Base-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
region = var.aws_region
}

data "amazon-ami" "windows-2022" {
filters = {
name = "Windows_Server-2022-English-Core-Base-*"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
region = var.aws_region
}
7 changes: 7 additions & 0 deletions locals.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ locals {
image_name = format("jenkins-agent-%s-%s-%s", var.agent_os_type, var.agent_os_version, var.architecture)
unique_image_name = format("%s-%s", local.image_name, local.now_unix_timestamp)
# List available SKUs with the command `az vm image list-skus --offer 0001-com-ubuntu-server-jammy --location eastus --publisher canonical --output table`
aws_spot_instance_types = {
# 4 vCPU x86 / 16 GB / $0.1504 - https://aws.amazon.com/fr/ec2/instance-types/t3/#Product_Details
"amd64" = ["t3.xlarge", "t3a.xlarge", "t2.xlarge", "m6a.xlarge"]
# 4 vCPU ARM64 (Gravitnb)/ 16 GB / $0.1344 - https://aws.amazon.com/fr/ec2/instance-types/t4/#Product_Details
"arm64" = ["t4g.xlarge", "m7g.xlarge"]
}
az_instance_image_sku = {
"amd64" = "${local.agent_os_version_safe}-lts-gen2"
"arm64" = "${local.agent_os_version_safe}-lts-arm64"
}
windows_winrm_user = {
"azure-arm" = "packer"
"docker" = "packer"
"amazon-ebs" = "Administrator" # In AWS EC2, WinRM super admin must be the "Administrator" account
}
azure_vm_size = {
"amd64" = "Standard_D4ads_v5" # 4 CPU / 16 GB / Huge size required to avoid https:#docs.microsoft.com/en-us/azure/virtual-machines/linux/image-builder-troubleshoot#sysprep-timing and avoid full disk (DS2v2 only have 14 Gb SSD for system)
Expand Down
4 changes: 4 additions & 0 deletions main.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
packer {
required_version = ">= 1.7.2, < 2"
required_plugins {
amazon = {
version = "1.2.2"
source = "github.com/hashicorp/amazon"
}
windows-update = {
version = "0.16.8"
source = "github.com/rgl/windows-update"
Expand Down
35 changes: 35 additions & 0 deletions sources.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
# This source defines all the common settings for any AWS AMI (whatever Operating System)
source "amazon-ebs" "base" {
ami_name = "${local.image_name}-${var.architecture}-${local.now_unix_timestamp}"
instance_type = local.aws_instance_type[var.architecture]

# Define custom rootfs for build to avoid later filesystem extension during agent startups
launch_block_device_mappings {
delete_on_termination = true
device_name = "/dev/sda1"
volume_size = local.windows_disk_size_gb
volume_type = "gp2"
}

# Where to build the VM
region = var.aws_region

# Where to export the AMI
ami_regions = [
var.aws_region
]

# Egg-and-chicken: what is the base image to start from (eg. what is my egg)?
source_ami = data.amazon-ami["${var.agent_os_type}-${local.agent_os_version_safe}"].id

# To improve audit and garbage collecting, we provide tags
tags = {
imageplatform = var.architecture
imagetype = local.image_name
timestamp = local.now_unix_timestamp
version = var.image_version
scm_ref = var.scm_ref
build_type = var.build_type
}
}

# This source defines all the common settings for any Azure image (whatever Operating System)
source "azure-arm" "base" {
managed_image_resource_group_name = local.azure_destination_resource_group
Expand Down
50 changes: 50 additions & 0 deletions updatecli/updatecli.d/packer-plugins/amazon.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
name: Bump packer amazon plugin version

scms:
default:
kind: github
spec:
user: "{{ .github.user }}"
email: "{{ .github.email }}"
owner: "{{ .github.owner }}"
repository: "{{ .github.repository }}"
token: "{{ requiredEnv .github.token }}"
username: "{{ .github.username }}"
branch: "{{ .github.branch }}"

sources:
lastReleaseVersion:
kind: githubrelease
name: Get the latest packer's amazon plugin version
spec:
owner: "hashicorp"
repository: "packer-plugin-amazon"
token: "{{ requiredEnv .github.token }}"
username: "{{ .github.username }}"
versionfilter:
kind: semver
transformers:
- trimprefix: v

targets:
updateVersion:
name: "Update the amazon plugin version in main.pkr.hcl"
sourceid: lastReleaseVersion
kind: file
spec:
file: main.pkr.hcl
matchpattern: 'amazon = \{((\r\n|\r|\n)(\s+))version = ".*"'
replacepattern: >-
amazon = {${1}version = "{{ source "lastReleaseVersion" }}"
scmid: default

actions:
default:
kind: github/pullrequest
title: Bump packer amazon plugin version to {{ source "lastReleaseVersion" }}
scmid: default
spec:
labels:
- dependencies
- packer-amazon-plugin
6 changes: 5 additions & 1 deletion variables.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ variable "architecture" {
description = "CPU architecture ID of the build with the following possible values: [amd64 (default), arm64]"
default = "amd64"
}
variable "aws_region" {
type = string
default = "us-east-2"
}
variable "azure_client_id" {
type = string
default = env("AZURE_CLIENT_ID")
Expand All @@ -35,7 +39,7 @@ variable "image_version" {
}
variable "image_type" {
type = string
description = "Which kind of Packer builder to use (e.g. cloud platform): [azure-arm (default), docker]"
description = "Which kind of Packer builder to use (e.g. cloud platform): [amazon-ebs (default), azure-arm, docker]"
default = "azure-arm"
}
variable "build_type" {
Expand Down

0 comments on commit a928364

Please sign in to comment.