diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cfdfbeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +terraform/*/.terraform/ +terraform/*/.terraform.lock.hcl +terraform/*/terraform.tfstate* diff --git a/Makefile b/Makefile index 69600ae..cda63ca 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,31 @@ -all: iso_checksum alpine-qemu.pkr.hcl +VM_STATE_RUNNING ?= true + +all: build_all alpine-qemu + +build_all: iso_checksum alpine-qemu.pkr.hcl alpine-qemu.pkr.hcl: - @echo "==> Validating $@ ..." @packer validate $@ - @echo "==> Building VM box for $@ ..." @packer build -on-error=abort $@ -format: +alpine-qemu: + @terraform -chdir=./terraform/alpine-qemu init -upgrade -migrate-state + @terraform -chdir=./terraform/alpine-qemu validate + @terraform -chdir=./terraform/alpine-qemu apply -var="running=$(VM_STATE_RUNNING)" + +format-packer: @packer fmt -diff -recursive . +format-terraform: + @terraform fmt -diff -recursive ./terraform/ + clean: @find ./artifacts/* -prune -exec rm -rfv {} \; +destroy: + @find ./terraform/* -prune -exec terraform -chdir={} apply -destroy \; + iso_checksum: @find . -name *.pkr.hcl -type f -exec sh update_iso_checksum.sh {} \; -.PHONY: all alpine-qemu.pkr.hcl format clean iso_checksum +.PHONY: all build_all alpine-qemu.pkr.hcl alpine-qemu format-packer format-terraform clean destroy iso_checksum diff --git a/README.md b/README.md index 7fe0398..8b015a8 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,15 @@ This repository holds Packer's VM templates I use. Most (if not all) of them run ## VM boxes -- **[alpine-qemu.pkr.hcl](./alpine-qemu.pkr.hcl)**: libvirt-compatible VM on local machine running Alpine edge. +- [alpine-qemu.pkr.hcl](./alpine-qemu.pkr.hcl): libvirt-compatible VM on local machine running Alpine edge. + +```bash +# Build the VM box +make alpine-qemu.pkr.hcl + +# Import the built box to libvirt volume and launch it +VM_STATE_RUNNING=true terraform -chdir=terraform/alpine-qemu apply +``` ## License diff --git a/alpine-qemu.pkr.hcl b/alpine-qemu.pkr.hcl index d70b0c6..5b97959 100644 --- a/alpine-qemu.pkr.hcl +++ b/alpine-qemu.pkr.hcl @@ -82,7 +82,7 @@ source "qemu" "alpine-qemu" { ssh_password = var.ssh_password ssh_username = var.ssh_username ssh_wait_timeout = "30m" - vm_name = "packer-alpine-edge-qemu-x86_64" + vm_name = "packer-alpine-edge-qemu-x86_64.qcow2" } build { diff --git a/terraform/alpine-qemu/main.tf b/terraform/alpine-qemu/main.tf new file mode 100644 index 0000000..90bea84 --- /dev/null +++ b/terraform/alpine-qemu/main.tf @@ -0,0 +1,67 @@ +terraform { + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = "0.6.14" + } + } +} + +variable "vm_name" { + type = string + default = "packer-alpine-edge-qemu-x86_64" + description = "Name of the VM to import" +} + +variable "running" { + type = bool + nullable = false + default = true + description = "Dynamically start/stop the VM" +} + +variable "cpus" { + type = number + nullable = false + default = 2 + description = "Number of CPU cores to use for the VM" +} + +variable "ram" { + type = string + nullable = false + default = "2048" + description = "The amount of memory assigned to the VM" +} + +provider "libvirt" { + uri = "qemu:///system" +} + +resource "libvirt_volume" "alpine-qemu-image" { + name = "${var.vm_name}.qcow2" + source = "../../artifacts/qemu/${var.vm_name}.qcow2" + pool = "default" +} + +resource "libvirt_domain" "alpine-qemu" { + name = var.vm_name + vcpu = var.cpus + memory = var.ram + running = var.running + qemu_agent = true + autostart = false + + disk { + volume_id = libvirt_volume.alpine-qemu-image.id + } + + network_interface { + network_name = "default" + wait_for_lease = true + } +} + +output "ips" { + value = libvirt_domain.alpine-qemu.network_interface.0.addresses +} diff --git a/update_iso_checksum.sh b/update_iso_checksum.sh index e66008c..08eee9b 100644 --- a/update_iso_checksum.sh +++ b/update_iso_checksum.sh @@ -17,4 +17,4 @@ iso_url=$(hcl2json "$filename" | jq -r '.variable.iso_url[].default') printf "Checking sha512sum of \033[1;33m%s\033[0m ...\n" "$iso_url" sha512_sum=$(wget -qO- "${iso_url}".sha512 | awk '{print $1}') -sed -i -E "s|(default = \"sha512:).*\"|\\1$sha512_sum\"|g" "$filename" && printf "ISO checksum updated in \033[1;32m%s\033[0m.\n" "$filename" +sed -i -E "s|(default += \"sha512:).*\"|\\1$sha512_sum\"|g" "$filename" && printf "ISO checksum updated in \033[1;32m%s\033[0m.\n" "$filename"