diff --git a/examples/custom-image/README.md b/examples/custom-image/README.md new file mode 100644 index 0000000..dbd95bd --- /dev/null +++ b/examples/custom-image/README.md @@ -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 (`/.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 / 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 + + eg: ./build-custom-image.sh aws diff --git a/examples/custom-image/build-custom-image.sh b/examples/custom-image/build-custom-image.sh new file mode 100755 index 0000000..d192e66 --- /dev/null +++ b/examples/custom-image/build-custom-image.sh @@ -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 \ No newline at end of file diff --git a/examples/custom-image/cloud/aws/config.pkr.hcl b/examples/custom-image/cloud/aws/config.pkr.hcl new file mode 100644 index 0000000..0829648 --- /dev/null +++ b/examples/custom-image/cloud/aws/config.pkr.hcl @@ -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" + } + } +} diff --git a/examples/custom-image/cloud/aws/packer.json b/examples/custom-image/cloud/aws/packer.json new file mode 100644 index 0000000..dc47823 --- /dev/null +++ b/examples/custom-image/cloud/aws/packer.json @@ -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/download/v4.5.11-rc.1/palette-agent-install.sh", + "chmod +x /tmp/palette-agent-install.sh", + "sudo /tmp/palette-agent-install.sh" + ] + } +] +} diff --git a/examples/custom-image/cloud/aws/ubuntu-2204.json b/examples/custom-image/cloud/aws/ubuntu-2204.json new file mode 100644 index 0000000..523171b --- /dev/null +++ b/examples/custom-image/cloud/aws/ubuntu-2204.json @@ -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" +} diff --git a/examples/custom-image/custom-image-config b/examples/custom-image/custom-image-config new file mode 100644 index 0000000..e52f4ec --- /dev/null +++ b/examples/custom-image/custom-image-config @@ -0,0 +1,10 @@ +### aws +aws_access_key="ASIASO6J6QHXBB6MEXOL" +aws_secret_key="T27xJQWo9Jj3bxOWGHyXIEqIjVcs2IoA0hZpuf5z" + +#### azure +#### NOT SUPPORTED YET +azure_client_id="" +azure_client_secret="" +azure_tenant_id="" +azure_subscription_id="" \ No newline at end of file