Skip to content

Commit

Permalink
feat(packer): add configs for DigitalOcean
Browse files Browse the repository at this point in the history
  • Loading branch information
raisedadead committed Oct 20, 2024
1 parent facb834 commit 8b1f531
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packer/digitalocean/scripts/ansible/install-common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name: Install common packages on Ubuntu
hosts: all

tasks:
- name: Update apt package index and install common packages
ansible.builtin.apt:
name:
- build-essential
- software-properties-common
- curl
- git
- tar
- unzip
- zip
- vim
- neovim
- htop
- glances
- ncdu
state: present
update_cache: true
cache_valid_time: 3600
autoclean: true
autoremove: true

- name: Wait for apt lock to be released
ansible.builtin.shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done
changed_when: false

- name: Remove unattended-upgrades
ansible.builtin.apt:
name: unattended-upgrades
state: absent

- name: Wait for apt lock to be released
ansible.builtin.shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done
changed_when: false

- name: Upgrade all packages
ansible.builtin.apt:
upgrade: full
autoremove: true
autoclean: true
61 changes: 61 additions & 0 deletions packer/digitalocean/scripts/ansible/install-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
- name: Install Docker and Docker Compose on Ubuntu
hosts: all
become: yes

tasks:
- name: Update apt cache
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600

- name: Install prerequisites
ansible.builtin.apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
state: present

- name: Wait for apt lock to be released
ansible.builtin.shell: while fuser /var/lib/dpkg/lock >/dev/null 2>&1; do sleep 5; done
changed_when: false

- name: Add Docker GPG key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add Docker repository
ansible.builtin.apt_repository:
repo: "deb [arch={{ ansible_architecture }}] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present

- name: Update apt cache (forced)
ansible.builtin.apt:
update_cache: yes
force_apt_get: yes

- name: Debug - List available Docker packages
ansible.builtin.shell: apt-cache search docker-ce
register: docker_packages
changed_when: false

- name: Debug - Show available Docker packages
ansible.builtin.debug:
var: docker_packages.stdout_lines

- name: Install Docker CE, CLI, Containerd and Compose
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
state: present

- name: Install Docker Module for Python
ansible.builtin.pip:
name: docker
12 changes: 12 additions & 0 deletions packer/digitalocean/scripts/ansible/reboot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Reboot
hosts: all

tasks:
- name: Reboot
ansible.builtin.reboot:
connect_timeout: 5
reboot_timeout: 300
pre_reboot_delay: 30
post_reboot_delay: 180
test_command: uptime
116 changes: 116 additions & 0 deletions packer/digitalocean/ubuntu.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
packer {
required_plugins {
digitalocean = {
version = ">= 1.4.0"
source = "github.com/digitalocean/digitalocean"
}
ansible = {
source = "github.com/hashicorp/ansible"
version = ">= 1.1.0"
}
}
}

variable "scripts_dir" { default = "digitalocean/scripts" }

locals { image_version = "${formatdate("YYYYMMDD.hhmm", timestamp())}" }
variable "do_api_token" {
type = string
default = env("DO_API_TOKEN")

validation {
condition = length(var.do_api_token) > 0
error_message = "The DO_API_TOKEN environment variable must be set or the -var do_api_token=xxxxx must be used to set the token value."
}
}

variable "do_size" { default = "s-2vcpu-2gb" }
variable "do_region" { default = "nyc3" }
variable "do_image" { default = "ubuntu-24-04-x64" }
variable "do_image_description" { default = "Ubuntu 24.04 LTS" }
variable "do_os_version" { default = "24.04" }
variable "do_os_flavor" { default = "ubuntu" }

source "digitalocean" "ubuntu" {
api_token = "${var.do_api_token}"
image = var.do_image
region = var.do_region
size = var.do_size
snapshot_name = "ami-${var.do_os_flavor}-${var.do_os_version}-${local.image_version}"
ssh_username = "root"
}

build {
name = "ubuntu"
sources = ["source.digitalocean.ubuntu"]

provisioner "ansible" {
playbook_file = "${var.scripts_dir}/ansible/install-common.yml"
user = "root"
use_proxy = false
ansible_env_vars = [
"ANSIBLE_HOST_KEY_CHECKING=False",
"ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3",
"ANSIBLE_STDOUT_CALLBACK=yaml"
]
extra_arguments = [
"-v"
]
}

provisioner "ansible" {
playbook_file = "${var.scripts_dir}/ansible/reboot.yml"
user = "root"
use_proxy = false
ansible_env_vars = [
"ANSIBLE_HOST_KEY_CHECKING=False",
"ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3",
"ANSIBLE_STDOUT_CALLBACK=yaml"
]
extra_arguments = [
"-v"
]
}

provisioner "ansible" {
playbook_file = "${var.scripts_dir}/ansible/install-docker.yml"
user = "root"
use_proxy = false
ansible_env_vars = [
"ANSIBLE_HOST_KEY_CHECKING=False",
"ANSIBLE_PYTHON_INTERPRETER=/usr/bin/python3",
"ANSIBLE_STDOUT_CALLBACK=yaml"
]
extra_arguments = [
"-v"
]
}

post-processor "manifest" {
output = "manifest.json"
strip_path = true
}

hcp_packer_registry {
bucket_name = "digitalocean-ubuntu"

description = <<EOT
An Ubuntu LTS - Server image with Docker installed.
EOT

bucket_labels = {
"do_size" = var.do_size
"do_region" = var.do_region
"os_flavor" = var.do_os_flavor
"os_version" = var.do_os_version
}

build_labels = {
"os_ami_id" = "ami-${var.do_os_flavor}-${var.do_os_version}-${local.image_version}"
"os_base_image" = var.do_image
"os_flavor" = var.do_os_flavor
"os_version" = var.do_os_version
}
}
}

0 comments on commit 8b1f531

Please sign in to comment.