From be7403e3673f9f9060ba0ac44fde1e4242a097b5 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Sun, 4 Jul 2021 04:24:47 +0000 Subject: [PATCH] Misc Packer improvements --- packer/config.json | 44 -------------------------- packer/config.pkr.hcl | 69 +++++++++++++++++++++++++++++++++++++++++ packer/provision.bash | 7 ++++- packer/riju.service | 3 ++ tools/packer-build.bash | 9 +++++- 5 files changed, 86 insertions(+), 46 deletions(-) delete mode 100644 packer/config.json create mode 100644 packer/config.pkr.hcl diff --git a/packer/config.json b/packer/config.json deleted file mode 100644 index e4f6f8e..0000000 --- a/packer/config.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "variables": { - "admin_password": "{{env `ADMIN_PASSWORD`}}" - }, - "builders": [ - { - "type": "amazon-ebs", - "source_ami_filter": { - "filters": { - "virtualization-type": "hvm", - "root-device-type": "ebs", - "name": "ubuntu/images/hvm-ssd/ubuntu-*-21.04-amd64-server-*" - }, - "owners": ["099720109477"], - "most_recent": true - }, - "instance_type": "t3.micro", - "ssh_username": "ubuntu", - "ami_name": "riju-{{timestamp}}" - } - ], - "provisioners": [ - { - "type": "file", - "source": "riju-init-volume", - "destination": "/tmp/riju-init-volume" - }, - { - "type": "file", - "source": "../supervisor/out/riju-supervisor", - "destination": "/tmp/riju-supervisor" - }, - { - "type": "file", - "source": "riju.service", - "destination": "/tmp/riju.service" - }, - { - "type": "shell", - "script": "provision.bash", - "environment_vars": ["ADMIN_PASSWORD={{user `admin_password`}}"] - } - ] -} diff --git a/packer/config.pkr.hcl b/packer/config.pkr.hcl new file mode 100644 index 0000000..49d213e --- /dev/null +++ b/packer/config.pkr.hcl @@ -0,0 +1,69 @@ +variable "admin_password" { + type = string + default = "${env("ADMIN_PASSWORD")}" +} + +variable "aws_region" { + type = string + default = "${env("AWS_REGION")}" +} + +variable "s3_bucket" { + type = string + default = "${env("S3_BUCKET")}" +} + +variable "supervisor_access_token" { + type = string + default = "${env("SUPERVISOR_ACCESS_TOKEN")}" +} + +data "amazon-ami" "ubuntu" { + filters = { + name = "ubuntu/images/hvm-ssd/ubuntu-*-21.04-amd64-server-*" + root-device-type = "ebs" + virtualization-type = "hvm" + } + most_recent = true + owners = ["099720109477"] +} + +locals { + timestamp = regex_replace(timestamp(), "[- TZ:]", "") +} + +source "amazon-ebs" "ubuntu" { + ami_name = "riju-${local.timestamp}" + instance_type = "t3.micro" + source_ami = "${data.amazon-ami.ubuntu.id}" + ssh_username = "ubuntu" +} + +build { + sources = ["source.amazon-ebs.ubuntu"] + + provisioner "file" { + destination = "/tmp/riju-init-volume" + source = "riju-init-volume" + } + + provisioner "file" { + destination = "/tmp/riju-supervisor" + source = "../supervisor/out/riju-supervisor" + } + + provisioner "file" { + destination = "/tmp/riju.service" + source = "riju.service" + } + + provisioner "shell" { + environment_vars = [ + "ADMIN_PASSWORD=${var.admin_password}", + "AWS_REGION=${var.aws_region}", + "S3_BUCKET=${var.s3_bucket}", + "SUPERVISOR_ACCESS_TOKEN=${var.supervisor_access_token}", + ] + script = "provision.bash" + } +} diff --git a/packer/provision.bash b/packer/provision.bash index f2f0f73..046b79e 100644 --- a/packer/provision.bash +++ b/packer/provision.bash @@ -3,6 +3,8 @@ set -euo pipefail : ${ADMIN_PASSWORD} +: ${S3_BUCKET} +: ${SUPERVISOR_ACCESS_TOKEN} mkdir /tmp/riju-work pushd /tmp/riju-work @@ -21,7 +23,7 @@ ubuntu_name="$(lsb_release -cs)" sudo tee -a /etc/apt/sources.list.d/custom.list >/dev/null <&2 "no default AWS region specified, and AWS_REGION unset" + exit 1 +fi + cd packer -packer build config.json +packer build config.pkr.hcl