From 9dc8490404112f94a9456558133165be270009a3 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 24 Apr 2022 14:44:43 +0100 Subject: [PATCH] refactor: rewrite packer config in Jsonnet This will allow us to write the configuration for multiple images without the need to duplicate most of the source. --- .gitignore | 2 + build/.gitkeep | 0 images/lxd/image.pkr.json | 67 ------------------------------ images/lxd/variables.auto.pkr.json | 13 ------ jsonnet/lib/provisioners.libsonnet | 57 +++++++++++++++++++++++++ main.jsonnet | 56 +++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 80 deletions(-) create mode 100644 .gitignore create mode 100644 build/.gitkeep delete mode 100644 images/lxd/image.pkr.json delete mode 100644 images/lxd/variables.auto.pkr.json create mode 100644 jsonnet/lib/provisioners.libsonnet create mode 100644 main.jsonnet diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b540b79 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/* +!build/.gitkeep diff --git a/build/.gitkeep b/build/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/images/lxd/image.pkr.json b/images/lxd/image.pkr.json deleted file mode 100644 index 40bc25f..0000000 --- a/images/lxd/image.pkr.json +++ /dev/null @@ -1,67 +0,0 @@ -{ - "source": { - "lxd": { - "flow_infra": { - "container_name": "${var.lxd_container_name}", - "image": "${var.lxd_base_image}", - "publish_properties": { - "description": "LXD image for Flow Infra Dev" - }, - "virtual_machine": false - } - } - }, - - "build": { - "source": { - "lxd.flow_infra": { - "name": "flow-infra", - "output_image": "${var.lxd_output_image_name}" - } - }, - "provisioner": { - "shell": { - "inline": ["apk add bash"] - } - }, - "provisioner": { - "shell": { - "environment_vars": [ - "FLOW_USERNAME=${var.flow_username}", - "FLOW_GID=${var.flow_gid}", - "FLOW_UID=${var.flow_uid}", - "ROOT_SETUP_DIRECTORY=${var.root_setup_directory}" - ], - "script": "${path.root}/../../provisioners/shell/setup.sh" - } - }, - "provisioner": { - "file": { - "source": "${path.root}/../../files/compose/docker-compose.yaml", - "destination": "${var.root_setup_directory}/template/compose/" - }, - "file": { - "source": "${path.root}/../../files/scripts/bootstrap.sh", - "destination": "${var.root_setup_directory}/bootstrap.sh" - }, - "file": { - "sources": [ - "${path.root}/../../files/traefik/Dockerfile", - "${path.root}/../../files/traefik/dynamic_dashboard.yaml", - "${path.root}/../../files/traefik/entrypoint.sh", - "${path.root}/../../files/traefik/traefik.yaml" - ], - "destination": "${var.root_setup_directory}/template/traefik/" - }, - "file": { - "sources": [ - "${path.root}/../../files/gitea/app.ini", - "${path.root}/../../files/gitea/Dockerfile", - "${path.root}/../../files/gitea/dynamic_git.yaml", - "${path.root}/../../files/gitea/entrypoint.sh" - ], - "destination": "${var.root_setup_directory}/template/gitea/" - } - } - } -} diff --git a/images/lxd/variables.auto.pkr.json b/images/lxd/variables.auto.pkr.json deleted file mode 100644 index da0dc3b..0000000 --- a/images/lxd/variables.auto.pkr.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "variables": { - "lxd_base_image": "images:alpine/3.15", - "lxd_container_name": "flow-infra-lxd-packer-builder", - "lxd_output_image_name": "flow-infra", - - "flow_username": "flow", - "flow_gid": 22379, - "flow_uid": 22379, - - "root_setup_directory": "/etc/flow/setup" - } -} diff --git a/jsonnet/lib/provisioners.libsonnet b/jsonnet/lib/provisioners.libsonnet new file mode 100644 index 0000000..d0a3037 --- /dev/null +++ b/jsonnet/lib/provisioners.libsonnet @@ -0,0 +1,57 @@ +{ + installBash: { + shell: { + inline: ['apk add bash'], + }, + }, + + uploadDockerCompose(root_setup_directory): { + file: { + source: '${path.root}/../files/compose/docker-compose.yaml', + destination: std.format('%s/template/compose/', root_setup_directory), + }, + }, + + uploadBootstrapScript(root_setup_directory): { + file: { + source: '${path.root}/../files/scripts/bootstrap.sh', + destination: std.format('%s/bootstrap.sh', root_setup_directory), + }, + }, + + uploadTraefikTemplates(root_setup_directory): { + file: { + sources: [ + '${path.root}/../files/traefik/Dockerfile', + '${path.root}/../files/traefik/dynamic_dashboard.yaml', + '${path.root}/../files/traefik/entrypoint.sh', + '${path.root}/../files/traefik/traefik.yaml', + ], + destination: std.format('%s/template/traefik/', root_setup_directory), + }, + }, + + uploadGiteaTemplates(root_setup_directory): { + file: { + sources: [ + '${path.root}/../files/gitea/app.ini', + '${path.root}/../files/gitea/Dockerfile', + '${path.root}/../files/gitea/dynamic_git.yaml', + '${path.root}/../files/gitea/entrypoint.sh', + ], + destination: std.format('%s/template/gitea/', root_setup_directory), + }, + }, + + runSetup(username, gid, uid, root_setup_directory): { + shell: { + environment_vars: [ + std.format('FLOW_USERNAME=%s', username), + std.format('FLOW_GID=%d', gid), + std.format('FLOW_UID=%d', uid), + std.format('ROOT_SETUP_DIRECTORY=%s', root_setup_directory), + ], + script: '${path.root}/../provisioners/shell/setup.sh', + }, + }, +} diff --git a/main.jsonnet b/main.jsonnet new file mode 100644 index 0000000..a31398f --- /dev/null +++ b/main.jsonnet @@ -0,0 +1,56 @@ +local provisioners = import 'jsonnet/lib/provisioners.libsonnet'; +local type = std.extVar('type'); + +{ + local images = { + lxd: { + local lxd_base_image = 'images:alpine/3.15', + local lxd_container_name = 'flow-infra-lxd-packer-builder', + local lxd_image_name = 'flow-infra-local', + source: { + lxd: { + flow_infra: { + container_name: lxd_container_name, + image: lxd_base_image, + publish_properties: { + description: 'LXD image for Flow Infrastructure Development', + }, + virtual_machine: false, + }, + }, + }, + + build_source_ref: { + 'lxd.flow_infra': { + name: lxd_image_name, + output_image: lxd_image_name, + }, + }, + }, + + linode: { + source: {}, + build_source_ref: {}, + }, + }, + + source: images[type].source, + + build: { + local flow_username = 'flow', + local flow_gid = 22379, + local flow_uid = 22379, + local root_setup_directory = '/etc/flow/setup', + + source: images[type].build_source_ref, + + provisioner: [ + provisioners.installBash, + provisioners.runSetup(flow_username, flow_gid, flow_uid, root_setup_directory), + provisioners.uploadDockerCompose(root_setup_directory), + provisioners.uploadBootstrapScript(root_setup_directory), + provisioners.uploadTraefikTemplates(root_setup_directory), + provisioners.uploadGiteaTemplates(root_setup_directory), + ], + }, +}