Add Terraform plan for the first box

This commit is contained in:
Hoang Nguyen 2022-08-29 11:09:30 +07:00
parent 80badf8182
commit e2f3d03b1d
No known key found for this signature in database
GPG Key ID: 813CF484F4993419
6 changed files with 99 additions and 8 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
terraform/*/.terraform/
terraform/*/.terraform.lock.hcl
terraform/*/terraform.tfstate*

View File

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

View File

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

View File

@ -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 {

View File

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

View File

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