-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
DRY_RUN := true | ||
|
||
.PHONY: build | ||
build: | ||
PKR_VAR_dry_run=${DRY_RUN} PKR_VAR_install_docker=true \ | ||
packer build ./ami.pkr.hcl | ||
|
||
.PHONY: build-no-docker | ||
build-no-docker: | ||
PKR_VAR_dry_run=${DRY_RUN} PKR_VAR_install_docker=false \ | ||
packer build ./ami.pkr.hcl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
packer { | ||
required_version = ">= 1.8.0" | ||
} | ||
|
||
locals { | ||
timestamp = regex_replace(timestamp(), "[- TZ:]", "") | ||
ami_prefix = "nomad-${var.nomad_version}" | ||
} | ||
|
||
variable "aws_region" { | ||
type = string | ||
default = "ap-south-1" | ||
} | ||
|
||
variable "dry_run" { | ||
type = bool | ||
default = true | ||
} | ||
|
||
variable "nomad_version" { | ||
type = string | ||
default = "1.5.3" | ||
} | ||
|
||
variable "install_docker" { | ||
type = bool | ||
default = false | ||
} | ||
|
||
// Get the most recent Ubuntu AMI ID. | ||
data "amazon-ami" "autogenerated_1" { | ||
filters = { | ||
architecture = "x86_64" | ||
"block-device-mapping.volume-type" = "gp2" | ||
name = "ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*" | ||
root-device-type = "ebs" | ||
virtualization-type = "hvm" | ||
} | ||
|
||
most_recent = true | ||
owners = ["099720109477"] | ||
region = var.aws_region | ||
} | ||
|
||
source "amazon-ebs" "nomad" { | ||
skip_create_ami = var.dry_run | ||
|
||
# https://github.com/hashicorp/packer/issues/11656#issuecomment-1106564406 | ||
temporary_key_pair_type = "ed25519" | ||
|
||
ami_description = "AMI for Nomad agents based on Ubuntu" | ||
ami_name = var.install_docker ? "${local.ami_prefix}-docker-${local.timestamp}" : "${local.ami_prefix}-${local.timestamp}" | ||
|
||
instance_type = "t2.micro" | ||
shutdown_behavior = "terminate" | ||
force_deregister = true | ||
force_delete_snapshot = true | ||
tags = { | ||
nomad_version = var.nomad_version | ||
source_ami_id = "{{ .SourceAMI }}" | ||
source_ami_name = "{{ .SourceAMIName }}" | ||
Name = var.install_docker ? "${local.ami_prefix}-docker" : "${local.ami_prefix}" | ||
} | ||
|
||
region = var.aws_region | ||
source_ami = data.amazon-ami.autogenerated_1.id | ||
ssh_username = "ubuntu" | ||
} | ||
|
||
build { | ||
sources = ["source.amazon-ebs.nomad"] | ||
|
||
// wait for sometime for instance to properly initialise. | ||
provisioner "shell" { | ||
inline = ["sleep 15"] | ||
} | ||
|
||
provisioner "shell" { | ||
environment_vars = ["NOMAD_VERSION=${var.nomad_version}", "INSTALL_DOCKER=${var.install_docker}"] | ||
script = "./setup.sh" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
# Disable interactive apt prompts | ||
export DEBIAN_FRONTEND=noninteractive | ||
|
||
NOMAD_VERSION="${NOMAD_VERSION:-1.5.3}" | ||
CNI_VERSION="${CNI_VERSION:-v1.2.0}" | ||
INSTALL_DOCKER="${INSTALL_DOCKER:-false}" | ||
|
||
# Update packages | ||
sudo apt-get -y update | ||
|
||
# Install software-properties-common | ||
sudo apt-get install -y software-properties-common | ||
|
||
# Add HashiCorp GPG key | ||
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add - | ||
|
||
# Add HashiCorp repository | ||
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | ||
|
||
# Update packages again | ||
sudo apt-get -y update | ||
|
||
# Install Nomad | ||
sudo apt-get install -y nomad=${NOMAD_VERSION}-1 | ||
|
||
# Download and install CNI plugins | ||
curl -L -o cni-plugins.tgz "https://github.com/containernetworking/plugins/releases/download/${CNI_VERSION}/cni-plugins-linux-$( [ $(uname -m) = aarch64 ] && echo arm64 || echo amd64)"-"${CNI_VERSION}".tgz && \ | ||
sudo mkdir -p /opt/cni/bin && \ | ||
sudo tar -C /opt/cni/bin -xzf cni-plugins.tgz | ||
|
||
# Stop and disable Nomad service | ||
sudo systemctl stop nomad | ||
sudo systemctl disable nomad | ||
|
||
# Install Docker if INSTALL_DOCKER is true | ||
if [ "$INSTALL_DOCKER" = true ]; then | ||
sudo apt-get -y update | ||
sudo apt-get -y install \ | ||
ca-certificates \ | ||
curl \ | ||
gnupg | ||
sudo install -m 0755 -d /etc/apt/keyrings | ||
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg | ||
sudo chmod a+r /etc/apt/keyrings/docker.gpg | ||
echo \ | ||
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \ | ||
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \ | ||
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null | ||
sudo apt-get -y update | ||
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin | ||
|
||
# Create daemon.json if it doesn't exist | ||
if [ ! -f /etc/docker/daemon.json ]; then | ||
sudo touch /etc/docker/daemon.json | ||
fi | ||
|
||
# Add configuration to daemon.json | ||
echo '{ | ||
"log-driver": "json-file", | ||
"log-opts": { | ||
"max-size": "10m", | ||
"max-file": "10" | ||
} | ||
}' | sudo tee /etc/docker/daemon.json >/dev/null | ||
|
||
|
||
# Restart Docker | ||
sudo systemctl restart docker | ||
sudo usermod -aG docker ubuntu | ||
fi |