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.
This commit is contained in:
Dan Anglin 2022-04-24 14:44:43 +01:00
parent d7d0e8c4e9
commit 9dc8490404
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
6 changed files with 115 additions and 80 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
build/*
!build/.gitkeep

0
build/.gitkeep Normal file
View file

View file

@ -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/"
}
}
}
}

View file

@ -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"
}
}

View file

@ -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',
},
},
}

56
main.jsonnet Normal file
View file

@ -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),
],
},
}