Skip to content

Commit

Permalink
packer: convert JSON packer template to HCL (caddyserver#75)
Browse files Browse the repository at this point in the history
* packer: convert JSON packer template to HCL

* packer: delete the JSON template

* packer: make it beautiful

* packer: remove the autogenerated disclaimer

Co-authored-by: Dave Henderson <[email protected]>

Co-authored-by: Dave Henderson <[email protected]>
  • Loading branch information
mohammed90 and hairyhenderson authored Sep 17, 2021
1 parent 2f23e8a commit 7e1546d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 50 deletions.
4 changes: 2 additions & 2 deletions packer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ This helps build the DigitalOcean droplet image for their marketplace.

- Packer 1.6 or newer
- Ansible
- The latest tags from the `caddyserver/caddy` repo (`git fetch --tags`)

To run the script:

```bash
DIGITALOCEAN_TOKEN=foobar packer build do-marketplace-image.json
packer init
DIGITALOCEAN_TOKEN=foobar packer build do-marketplace-image.pkr.hcl
```

Be sure to replace `foobar` with your DigitalOcean API key.
Expand Down
48 changes: 0 additions & 48 deletions packer/do-marketplace-image.json

This file was deleted.

66 changes: 66 additions & 0 deletions packer/do-marketplace-image.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
packer {
required_plugins {
digitalocean = {
version = ">= 1.0.0"
source = "github.com/hashicorp/digitalocean"
}
}
}

variable "token" {
type = string
default = env("DIGITALOCEAN_TOKEN")
}

# "timestamp" template function replacement
locals { timestamp = regex_replace(timestamp(), "[- TZ:]", "") }

# All locals variables are generated from variables that uses expressions
# that are not allowed in HCL2 variables.
# Read the documentation for locals blocks here:
# https://www.packer.io/docs/templates/hcl_templates/blocks/locals
locals {
image_name = "marketplace-snapshot-${local.timestamp}"
ssh_user = "root"
}

source "digitalocean" "caddy_image" {
api_token = var.token
image = "ubuntu-20-04-x64"
region = "nyc3"
size = "s-1vcpu-1gb"
snapshot_name = local.image_name
ssh_username = local.ssh_user
}

build {
sources = ["source.digitalocean.caddy_image"]

provisioner "shell" {
scripts = [
"scripts/00-base.sh",
"scripts/10-firewall.sh",
"scripts/20-caddy.sh"
]
}

provisioner "file" {
destination = "/etc/fail2ban/jail.local"
source = "files/fail2ban/jail.local"
}

provisioner "ansible" {
galaxy_file = "ansible/requirements.yml"
playbook_file = "ansible/ansible-playbook.yml"
user = local.ssh_user
}

provisioner "shell" {
scripts = [
"scripts/50-services.sh",
"scripts/90-cleanup.sh",
"scripts/95-prep.sh",
"scripts/99-img_check.sh"
]
}
}

0 comments on commit 7e1546d

Please sign in to comment.