Skip to content

Commit

Permalink
Merge pull request #2 from dafyddj/feat/initial
Browse files Browse the repository at this point in the history
feat: initial `copier-template` for building Vagrant boxes using `packer`
  • Loading branch information
dafyddj authored Apr 18, 2024
2 parents 1ef9fab + 0a9a04e commit c2af821
Show file tree
Hide file tree
Showing 35 changed files with 523 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "base"]
path = base
url = https://github.com/dafyddj/workflow-packer-build
1 change: 1 addition & 0 deletions base
Submodule base added at 9af312
5 changes: 5 additions & 0 deletions copier.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
_subdirectory: template
_templates_suffix: .copier-jinja

# Questions
box_name:
type: str
help: What is the name of your Vagrant box? (including namespace/org)
14 changes: 14 additions & 0 deletions template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/box/
/vms/

/iso

.kitchen/
.vagrant/
output-*/
packer_cache/

*.snapshot
*.cat.pkr.hcl

serial.box
11 changes: 11 additions & 0 deletions template/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
# - id: trailing-whitespace
# - id: end-of-file-fixer
- id: check-yaml
exclude: ^kitchen.yml$
- id: check-added-large-files
21 changes: 21 additions & 0 deletions template/bin/version
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

if [ "${DEBUG:-false}" = "true" ]; then
set -x # Run the entire script in debug mode
fi

serial="serial.box"
if [ ! -e "$serial" ] ; then
cur=1
else
read cur <$serial
fi

next=$((cur + 1))
echo $next >$serial

cur=${GITHUB_RUN_NUMBER:-$cur}

VERSION="$(date +%Y.%-m).$cur"

echo $VERSION
29 changes: 29 additions & 0 deletions template/kitchen.yml.copier-jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
driver:
name: vagrant
box_check_update: false
ssh:
insert_key: false
<% if ENV.key?('TECHNEG_VAGRANT_PROVIDER') %>
provider: <%= ENV['TECHNEG_VAGRANT_PROVIDER'] %>
<% else %>
provider: qemu
<% end %>

provisioner:
name: dummy

verifier:
name: shell

platforms:
- name: alpine_linux
driver:
<% if ENV.key?('TECHNEG_VAGRANT_BOX') %>
box: <%= ENV['TECHNEG_VAGRANT_BOX'] %>
<% else %>
box: {{ box_name }}
<% end %>

suites:
- name: default
58 changes: 58 additions & 0 deletions template/post.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
$(_module_name)_srcs := $(addprefix $(_module_path)/,$(srcs)) $(extra_srcs)
$(_module_name)_targets := $(addprefix $(_module_name)-,$(os_vers))

$(foreach os_ver,$(os_vers), \
$(eval $(_module_name)_artifact_$(os_ver) := $($(_module_name)_output)/$(artifact_pre)$(os_ver)$(artifact_ext)))
$(_module_name)_artifacts := $(foreach os_ver,$(os_vers),$($(_module_name)_artifact_$(os_ver)))

ifneq ($(_NO_RULES),T)
ifneq ($($(_module_name)_defined),T)
all: $($(_module_name)_targets)

.PHONY: $(_module_name) $($(_module_name)_targets) $(os_vers)
$(_module_name): $($(_module_name)_targets)
$(os_vers): %: $(_module_name)-%
.SECONDEXPANSION:
$($(_module_name)_targets): $(_module_name)-%: $$($(_module_name)_artifact_%)

_CLEAN := clean-$(_module_name)
_CLEAN_2 := $(addprefix $(_CLEAN)-,$(os_vers))
.PHONY: clean $(_CLEAN_1) $(_CLEAN_2)
clean: $(_CLEAN)
$(_CLEAN): %: $(addprefix %-,$(os_vers))
$(_CLEAN_2): clean-$(_module_name)-%:
$(info Cleaning $($(_module_name)_output)/$*$(artifact_ext))

$($(_module_name)_artifacts): _path := $(_module_path)
$($(_module_name)_artifacts): _pvars := $(_module_pvars)

.SECONDEXPANSION:
$($(_module_name)_output)/$(artifact_pre)%$(vdiext): $($(_module_name)_srcs) $$($($(_module_name)_depends_on))
$(info Making $@)
@$(VBOXMANAGE) controlvm $(*F) poweroff 2>/dev/null || true
@$(VBOXMANAGE) unregistervm $(*F) --delete 2>/dev/null || true
@$(PACKER) build $(PFLAGS) $(_pvars) -var "root_dir=$(_ROOT)" -var "output_dir=$(@D)" -only \*.$(*F) $(_path)

.SECONDEXPANSION:
$($(_module_name)_output)/$(artifact_pre)%$(snapext): $($(_module_name)_srcs) $$($($(_module_name)_depends_on))
$(info Making $@)
@$(VBOXMANAGE) controlvm $* poweroff 2>/dev/null || true
@$(PACKER) build $(PFLAGS) $(_pvars) -only \*.$* $(_path)
@touch $@

.SECONDEXPANSION:
$($(_module_name)_output)/$(artifact_pre)%$(boxext): $($(_module_name)_srcs) $$($($(_module_name)_depends_on))
$(info Making $@)
[ -n ${GITHUB_ACTIONS+1} ] && echo "vagrant-box=$@" >> $$GITHUB_OUTPUT || true
@$(VBOXMANAGE) controlvm $* poweroff 2>/dev/null || true
@$(PACKER) build $(PFLAGS) $(_pvars) -var "root_dir=$(_ROOT)" -var "box_dir=$(@D)" -only \*.$* $(_path)

%.cat.pkr.hcl: %.build %.provision
$(info Making $@)
@cat $^ > $@
@echo } >> $@
@$(PACKER) init $@

$(_module_name)_defined := T
endif
endif
28 changes: 28 additions & 0 deletions template/pre.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.PHONY: all
all:

_makefiles := $(filter %/Makefile,$(MAKEFILE_LIST))
_included_from := $(patsubst $(_ROOT)/%,%,$(if $(_makefiles), \
$(patsubst %/Makefile,%,$(word $(words $(_makefiles)),$(_makefiles)))))
ifeq ($(_included_from),)
_module := $(patsubst $(_ROOT)/%,%,$(CURDIR))
else
_module := $(_included_from)
endif
_module_path := $(_ROOT)/$(_module)
_module_name := $(subst /,_,$(_module))
$(_module_name)_output := $(_module_path)

vdiext = .vdi
snapext := .snapshot
boxext := .box

os_vers := $(shell cat $(_ROOT)/os_vers)

artifact_pre :=
extra_srcs :=

PACKER := packer
PFLAGS := -timestamp-ui -force

VBOXMANAGE := VBoxManage
5 changes: 5 additions & 0 deletions template/root.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
_push = $(eval _save$1 := $(MAKEFILE_LIST))
_pop = $(eval MAKEFILE_LIST := $(_save$1))
_INCLUDE = $(call _push,$1)$(eval include $(_ROOT)/$1/Makefile)$(call _pop,$1)
depends_on = $(call _INCLUDE,$1)
depends_on_no_build = $(eval _NO_RULES := T)$(call _INCLUDE,$1)$(eval _NO_RULES :=)
63 changes: 63 additions & 0 deletions template/upload/main.pkr.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
variable "arch" {
type = string
default = "x64"
}

variable "box_dir" {
type = string
default = "../box/virtualbox"
}

variable "cm" {
type = string
default = "nocm"
}

variable "cm_version" {
type = string
default = ""
}

variable "no_release" {
type = bool
default = true
}

variable "prefix" {
type = string
default = "test-"
}

variable "vagrant_cloud_org" {
type = string
default = "techneg"
}

variable "version" {
type = string
default = "0.0.1pre"
}

source "null" "upload" {
communicator = "none"
}

build {
name = "upload"

source "null.upload" {
name = "alpine318"
}

post-processors {
post-processor "artifice" {
files = [ "${var.box_dir}/${source.name}.box" ]
}

post-processor "vagrant-cloud" {
box_tag = "${var.vagrant_cloud_org}/${var.prefix}${source.name}-x64-${var.cm}"
version = "${var.version}"
no_release = var.no_release
}
}
}
10 changes: 10 additions & 0 deletions test/GenerateProject/catalogue.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ The following new files/directories were created:
----.copier-answers.yml
----.github
--------renovate.json5
----.gitignore
----.pre-commit-config.yaml
----bin
--------version
----kitchen.yml
----post.mk
----pre.mk
----root.mk
----upload
--------main.pkr.hcl
1 change: 1 addition & 0 deletions test/GenerateProject/gitignore.tt
1 change: 1 addition & 0 deletions test/GenerateProject/kitchen_yml.tt
2 changes: 1 addition & 1 deletion test/GenerateProject/options.tt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$TEXTTEST_ROOT/.. .
-d box_name=techneg/alpine318-x64-nocm $TEXTTEST_ROOT/.. .
1 change: 1 addition & 0 deletions test/GenerateProject/post_mk.tt
1 change: 1 addition & 0 deletions test/GenerateProject/pre-commit_yaml.tt
1 change: 1 addition & 0 deletions test/GenerateProject/pre_mk.tt
1 change: 1 addition & 0 deletions test/GenerateProject/root_mk.tt
1 change: 1 addition & 0 deletions test/GenerateProject/upload_main.tt
1 change: 1 addition & 0 deletions test/GenerateProject/version.tt
15 changes: 15 additions & 0 deletions test/SetBoxName/catalogue.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The following new files/directories were created:
<Test Directory>
----.copier-answers.yml
----.github
--------renovate.json5
----.gitignore
----.pre-commit-config.yaml
----bin
--------version
----kitchen.yml
----post.mk
----pre.mk
----root.mk
----upload
--------main.pkr.hcl
14 changes: 14 additions & 0 deletions test/SetBoxName/gitignore.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/box/
/vms/

/iso

.kitchen/
.vagrant/
output-*/
packer_cache/

*.snapshot
*.cat.pkr.hcl

serial.box
29 changes: 29 additions & 0 deletions test/SetBoxName/kitchen_yml.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
driver:
name: vagrant
box_check_update: false
ssh:
insert_key: false
<% if ENV.key?('TECHNEG_VAGRANT_PROVIDER') %>
provider: <%= ENV['TECHNEG_VAGRANT_PROVIDER'] %>
<% else %>
provider: qemu
<% end %>

provisioner:
name: dummy

verifier:
name: shell

platforms:
- name: alpine_linux
driver:
<% if ENV.key?('TECHNEG_VAGRANT_BOX') %>
box: <%= ENV['TECHNEG_VAGRANT_BOX'] %>
<% else %>
box: techneg/another-box
<% end %>

suites:
- name: default
1 change: 1 addition & 0 deletions test/SetBoxName/options.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-d box_name=techneg/another-box $TEXTTEST_ROOT/.. .
Loading

0 comments on commit c2af821

Please sign in to comment.