Skip to content

Commit

Permalink
Merge pull request #88 from spectrocloud/custom-ami
Browse files Browse the repository at this point in the history
PE-5764 - Add support to create custom AMI
  • Loading branch information
akhilesh2410 authored Dec 16, 2024
2 parents dc05950 + 5d0cf96 commit dd85596
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
33 changes: 33 additions & 0 deletions examples/custom-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Create Custom Image on Public Cloud

This script automates the creation of a custom image on a public cloud provider (e.g., AWS). The script reads configuration values, manages required credentials, and invokes appropriate cloud-specific build commands.

## Prerequisites

1. **Dependencies**
- Bash (Unix/Linux environment)
- Packer CLI (https://developer.hashicorp.com/packer/install?product_intent=packer)

2. **Access and Credentials**
- Ensure valid credentials for your target cloud provider.
- For AWS, configure the `aws_access_key` and `aws_secret_key` in the configuration file i.e. custom-image-config.
- Permissions to create and manage images for the chosen cloud provider.

3. **Configuration File**
- **Global Configuration (`custom-image-config`)**:
Contains details about the cloud provider's credentials.
- **Cloud-Specific Configuration (`<cloud-provider>/<os-version>.json`)**:
Specifies the instance details for the cloud provider.

## Usage

1. Prepare the Configuration Files:
Create the custom-image-config file in the project root directory with the required credentials.
Add the appropriate cloud-specific configuration file in the <cloud-provider>/ directory

2. Run the Build Script: Execute the build-custom-image.sh script with the desired cloud provider:
```bash
cd examples/custom-image
./build-custom-image.sh <cloud provider>

eg: ./build-custom-image.sh aws
27 changes: 27 additions & 0 deletions examples/custom-image/build-custom-image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash
set -x
# aws credentials
cloud_provider=$1
export PACKER_LOG=1
source ./custom-image-config
build_aws_ami() {
packer init cloud/aws/config.pkr.hcl
packer build --var-file=cloud/aws/ubuntu-2204.json cloud/aws/packer.json
}
# Not implemented yet
build_azure_vhd() {
packer init cloud/azure/config.pkr.hcl
packer build --var-file=cloud/azure/ubuntu-2204.json cloud/azure/packer.json
}

if [ "$cloud_provider" == "aws" ]; then
export AWS_BUILD_ACCESS_KEY=${aws_access_key}
export AWS_BUILD_SECRET_KEY=${aws_secret_key}
build_aws_ami
elif [ "$cloud_provider" == "azure" ]; then
export AZURE_BUILD_CLIENT_ID=${azure_client_id}
export AZURE_BUILD_CLIENT_SECRET=${azure_client_secret}
export AZURE_BUILD_TENANT_ID=${azure_tenant_id}
export AZURE_BUILD_SUBSCRIPTION_ID=${azure_subscription_id}
build_azure_vhd
fi
16 changes: 16 additions & 0 deletions examples/custom-image/cloud/aws/config.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
packer {
required_plugins {
amazon = {
version = ">= 1.2.8"
source = "github.com/hashicorp/amazon"
}
ansible = {
version = ">= 1.1.0"
source = "github.com/hashicorp/ansible"
}
goss = {
version = "~> 3"
source = "github.com/YaleUniversity/goss"
}
}
}
41 changes: 41 additions & 0 deletions examples/custom-image/cloud/aws/packer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"builders": [{
"type": "amazon-ebs",
"region": "{{ user `aws_region` }}",
"source_ami": "{{user `source_ami`}}",
"instance_type": "{{user `builder_instance_type`}}",
"ssh_username": "{{user `ssh_username`}}",
"ami_name": "{{user `ami_name`}}",
"source_ami_filter": {
"filters": {
"architecture": "x86_64",
"name": "{{user `ami_filter_name`}}",
"root-device-type": "ebs",
"virtualization-type": "hvm"
},
"most_recent": true,
"owners": "{{user `ami_filter_owners`}}"
},
"vpc_id": "{{ user `vpc_id` }}",
"subnet_id": "{{ user `subnet_id` }}"
}],

"provisioners": [
{
"type": "shell",
"inline": [
"set -e",
"sudo apt update -y || (echo 'APT Update Failed'; exit 1)",
"sudo apt install -y bash systemd rsync rsyslog jq zstd conntrack systemd-timesyncd || (echo 'APT Install Failed'; exit 1)"
]
},
{
"type": "shell",
"inline": [
"curl -fsSL -o /tmp/palette-agent-install.sh https://github.com/spectrocloud/agent-mode/releases/latest/download/palette-agent-install.sh",
"chmod +x /tmp/palette-agent-install.sh",
"sudo /tmp/palette-agent-install.sh"
]
}
]
}
16 changes: 16 additions & 0 deletions examples/custom-image/cloud/aws/ubuntu-2204.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"ami_filter_name": "ubuntu/images/*ubuntu-jammy-22.04-amd64-server-*",
"ami_filter_owners": "099720109477",
"build_name": "ubuntu-22.04",
"distribution": "Ubuntu",
"distribution_release": "jammy",
"distribution_version": "22.04",
"root_device_name": "/dev/sda1",
"source_ami": "",
"ssh_username": "ubuntu",
"aws_region": "us-east-2",
"ami_name": "spectro-agent-mode-ubuntu2204-ami-{{timestamp}}",
"builder_instance_type": "t2.medium",
"vpc_id": "vpc-xxxxxxx",
"subnet_id": "subnet-xxxxxxx"
}
10 changes: 10 additions & 0 deletions examples/custom-image/custom-image-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### aws
aws_access_key=""
aws_secret_key=""

#### azure
#### NOT SUPPORTED YET
azure_client_id=""
azure_client_secret=""
azure_tenant_id=""
azure_subscription_id=""

0 comments on commit dd85596

Please sign in to comment.