Skip to content

Commit

Permalink
azure: Add packer template for azure
Browse files Browse the repository at this point in the history
Signed-off-by: Itxaka <[email protected]>
  • Loading branch information
Itxaka committed Jul 15, 2021
1 parent b799ee3 commit e154a36
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/build.yaml.gomplate
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ jobs:
COS_VERSION=$(yq r packages/cos/collection.yaml 'packages.[0].version')
export COS_VERSION="${COS_VERSION/+/-}"
export PKR_VAR_cos_version="${COS_VERSION}"
export PKR_VAR_aws_cos_install_args="cos-deploy {{{ if (ne $flavor "opensuse") }}}--no-verify {{{ end }}}--docker-image quay.io/costoolkit/{{{$config.repository}}}-{{{ $flavor }}}:cos-system-${COS_VERSION}"
export PKR_VAR_cos_deploy_args="cos-deploy {{{ if (ne $flavor "opensuse") }}}--no-verify {{{ end }}}--docker-image quay.io/costoolkit/{{{$config.repository}}}-{{{ $flavor }}}:cos-system-${COS_VERSION}"
export PKR_VAR_flavor={{{ $flavor }}}
export PKR_VAR_git_sha="${GITHUB_SHA}"
make packer-aws
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-master.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ jobs:
COS_VERSION=$(yq r packages/cos/collection.yaml 'packages.[0].version')
export COS_VERSION="${COS_VERSION/+/-}"
export PKR_VAR_cos_version="${COS_VERSION}"
export PKR_VAR_aws_cos_install_args="cos-deploy --docker-image quay.io/costoolkit/releases-opensuse:cos-system-${COS_VERSION}"
export PKR_VAR_cos_deploy_args="cos-deploy --docker-image quay.io/costoolkit/releases-opensuse:cos-system-${COS_VERSION}"
export PKR_VAR_flavor=opensuse
export PKR_VAR_git_sha="${GITHUB_SHA}"
make packer-aws
Expand Down
43 changes: 38 additions & 5 deletions packer/images.json.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ source "amazon-ebs" "cos" {
}
}

source "azure-arm" "cos" {
client_id = var.azure_client_id
tenant_id = var.azure_tenant_id
client_secret = var.azure_client_secret
subscription_id = var.azure_subscription_id
custom_managed_image_resource_group_name = var.azure_custom_managed_image_resource_group_name
custom_managed_image_name = var.azure_custom_managed_image_name
managed_image_name = "${var.name}-${replace(var.cos_version, "+", "-")}-${formatdate("DDMMYYYY", timestamp())}-${var.flavor}"
managed_image_resource_group_name = var.azure_managed_image_resource_group_name
user_data_file = var.azure_user_data_file
location = var.azure_location
os_type = "Linux"
os_disk_size_gb = var.azure_os_disk_size_gb
vm_size = var.azure_vm_size
communicator = "ssh"
# root username is not allowed!
ssh_username = "packer"
ssh_password = "cos"
azure_tags = {
name = var.name
version = var.cos_version
}
}

source "qemu" "cos" {
accelerator = "${var.accelerator}"
boot_wait = "${var.sleep}"
Expand Down Expand Up @@ -73,22 +97,22 @@ source "virtualbox-iso" "cos" {
build {
description = "cOS"

sources = ["source.amazon-ebs.cos", "source.qemu.cos", "source.virtualbox-iso.cos"]
sources = ["source.amazon-ebs.cos", "source.qemu.cos", "source.virtualbox-iso.cos", "source.azure-arm.cos"]

provisioner "file" {
except = ["amazon-ebs.cos"]
except = ["amazon-ebs.cos", "azure-arm.cos"]
destination = "/90_custom.yaml"
source = "config.yaml"
}

provisioner "file" {
except = ["amazon-ebs.cos"]
except = ["amazon-ebs.cos", "azure-arm.cos"]
destination = "/vagrant.yaml"
source = "vagrant.yaml"
}

provisioner "shell" {
except = ["amazon-ebs.cos"]
except = ["amazon-ebs.cos", "azure-arm.cos"]
inline = ["INTERACTIVE=false cos-installer --config /90_custom.yaml /dev/sda",
"if [ -n \"${var.feature}\" ]; then mount /dev/disk/by-label/COS_OEM /oem; cos-feature enable ${var.feature}; fi"
]
Expand All @@ -98,7 +122,16 @@ build {
provisioner "shell" {
only = ["amazon-ebs.cos"]
inline = [
"${var.aws_cos_install_args}",
"${var.aws_cos_deploy_args}",
"sync"
]
pause_after = "30s"
}

provisioner "shell" {
only = ["azure-arm.cos"]
inline = [
"${var.azure_cos_deploy_args}",
"sync"
]
pause_after = "30s"
Expand Down
2 changes: 1 addition & 1 deletion packer/aws/setup-disk.yaml → packer/user-data/aws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ stages:
filesystem: ext4
- fsLabel: COS_PERSISTENT
pLabel: persistent
filesystem: ext4
filesystem: ext4
27 changes: 27 additions & 0 deletions packer/user-data/azure.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: "Default deployment"
stages:
rootfs.after:
- if: '[ -f "/run/cos/recovery_mode" ]'
name: "Repart image"
layout:
device:
label: COS_RECOVERY
add_partitions:
- fsLabel: COS_STATE
size: 9192
pLabel: state
filesystem: ext4
- fsLabel: COS_PERSISTENT
pLabel: persistent
filesystem: ext4
# Azure does not allow to ssh with root user so we need to create an user to provision the image correctly
network:
- if: '[ -f "/run/cos/recovery_mode" ]'
commands:
- useradd "packer" -m -u 1500 # Add packer user
- echo "packer:cos" | chpasswd # change packer pass
- usermod -a -G root packer # add packer to root group
- if: '[ -f "/run/cos/recovery_mode" ]'
files:
- path: /etc/sudoers.d/wheel # set passwordless sudo
content: "packer ALL = (ALL) NOPASSWD: ALL"
75 changes: 67 additions & 8 deletions packer/variables.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ variable "aws_source_ami_filter_virtualization-type" {
description = "Type of virtualization type to filter the search for the base AMI"
}


variable "aws_cos_install_args" {
type = string
default = "cos-deploy"
description = "Arguments to execute while provisioning the aws ami with packer. This will use the shell provisioner"
}

variable "aws_launch_volume_name" {
type = string
default = "/dev/sda1"
Expand All @@ -83,10 +76,76 @@ variable "aws_launch_volume_size" {

variable "aws_user_data_file" {
type = string
default = "aws/setup-disk.yaml"
default = "user-data/aws.yaml"
description = "Path to the user-data file to boot the base AMI with"
}

variable "azure_client_id" {
type = string
default = env("AZURE_CLIENT_ID")
}

variable "azure_tenant_id" {
type = string
default = env("AZURE_TENANT_ID")
}

variable "azure_client_secret" {
type = string
default = env("AZURE_CLIENT_SECRET")
}

variable "azure_subscription_id" {
type = string
default = env("AZURE_SUBSCRIPTION_ID")
}

variable "azure_custom_managed_image_resource_group_name" {
type = string
default = "cos-testing"
}

variable "azure_custom_managed_image_name" {
type = string
default = "cos_0.5.7-recovery"
}

variable "azure_managed_image_resource_group_name" {
type = string
default = "cos-testing"
}

variable "azure_location" {
type = string
default = "westeurope"
}

variable "azure_os_disk_size_gb" {
type = number
default = 16
}

variable "azure_vm_size" {
type = string
default = "Standard_B2s"
}

variable "azure_user_data_file" {
type = string
default = "user-data/azure.yaml"
description = "Path to the user-data file to boot the base Image with"
}

variable "azure_cos_deploy_args" {
default = "sudo /usr/sbin/cos-deploy"
}

variable "aws_cos_deploy_args" {
type = string
default = "cos-deploy"
description = "Arguments to execute while provisioning cloud images with packer. This will use the shell provisioner"
}

variable "cos_version" {
type = string
default = "latest"
Expand Down

0 comments on commit e154a36

Please sign in to comment.