From 58a65600a585d89694725c5a84ddc880f05c5f64 Mon Sep 17 00:00:00 2001 From: Gregory Shulov Date: Mon, 29 Jan 2024 22:19:41 +0200 Subject: [PATCH] Refactor centos7 packer template (#198) Refactor CentOS 7 Packer templates. Relates to #197. --- centos7/Makefile | 21 ++------ centos7/README.md | 11 +++- centos7/centos7.pkr.hcl | 50 ++++++++++++++++++- .../{centos7.ks.in => centos7.ks.pkrtpl.hcl} | 2 +- 4 files changed, 63 insertions(+), 21 deletions(-) rename centos7/http/{centos7.ks.in => centos7.ks.pkrtpl.hcl} (97%) diff --git a/centos7/Makefile b/centos7/Makefile index 436a1e31..976646c9 100644 --- a/centos7/Makefile +++ b/centos7/Makefile @@ -5,17 +5,7 @@ include ../scripts/check.mk PACKER ?= packer PACKER_LOG ?= 0 -ifneq (${KS_MIRROR},) -KS_OS_REPOS := --url="${KS_MIRROR}/os/x86_64" -KS_UPDATES_REPOS := --baseurl="${KS_MIRROR}/updates/x86_64" -KS_EXTRA_REPOS := --baseurl="${KS_MIRROR}/extras/x86_64" -else -KS_OS_REPOS := --mirrorlist="http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os" -KS_UPDATES_REPOS := --mirrorlist="http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates" -KS_EXTRA_REPOS := --mirrorlist="http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras" -endif - -export PACKER_LOG KS_PROXY KS_OS_REPOS KS_UPDATES_REPOS KS_EXTRA_REPOS +export PACKER_LOG .PHONY: all clean check-deps @@ -23,13 +13,8 @@ all: centos7.tar.gz $(eval $(call check_packages_deps)) -centos7.tar.gz: clean check-deps http/centos7.ks +centos7.tar.gz: clean check-deps ${PACKER} init centos7.pkr.hcl && ${PACKER} build centos7.pkr.hcl -http/centos7.ks: http/centos7.ks.in - envsubst '$${KS_PROXY} $${KS_OS_REPOS} $${KS_UPDATES_REPOS} $${KS_EXTRA_REPOS}' < $< | tee $@ - clean: - ${RM} -rf output-centos7 centos7.tar.gz http/centos7.ks - -.INTERMEDIATE: http/centos7.ks + ${RM} -rf output-centos7 centos7.tar.gz diff --git a/centos7/README.md b/centos7/README.md index 12caa45f..dbb02a0e 100644 --- a/centos7/README.md +++ b/centos7/README.md @@ -31,7 +31,16 @@ To use a proxy during the installation define the `KS_PROXY` variable in the environment, as bellow: ```shell -export KS_PROXY="--proxy=\"${HTTP_PROXY}\"" +export KS_PROXY="\"${HTTP_PROXY}\"" +``` + +# Building the image using a kickstart mirror + +To tell Packer to use a specific mirror set the `KS_MIRROR` environment variable +poiniting to the mirror URL. + +```shell +export KS_MIRROR="https://archive.kernel.org/centos-vault/7.9.2009" ``` ## Building an image diff --git a/centos7/centos7.pkr.hcl b/centos7/centos7.pkr.hcl index ef372439..7ae9d919 100644 --- a/centos7/centos7.pkr.hcl +++ b/centos7/centos7.pkr.hcl @@ -24,18 +24,66 @@ variable "centos7_sha256sum_url" { default = "https://mirrors.edge.kernel.org/centos/7/isos/x86_64/sha256sum.txt" } +# use can use "--url" to specify the exact url for os repo +# for ex. "--url='https://archive.kernel.org/centos-vault/7.9.2009/os/x86_64'" +variable "ks_os_repos" { + type = string + default = "--mirrorlist='http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os'" +} + +# Use --baseurl to specify the exact url for updates repo +# for ex. "--baseurl='https://archive.kernel.org/centos-vault/7.9.2009/updates/x86_64'" +variable "ks_updates_repos" { + type = string + default = "--mirrorlist='http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates'" +} + +# Use --baseurl to specify the exact url for extras repo +# for ex. "--baseurl='https://archive.kernel.org/centos-vault/7.9.2009/extras/x86_64'" +variable "ks_extras_repos" { + type = string + default = "--mirrorlist='http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras'" +} + +variable ks_proxy { + type = string + default = "${env("KS_PROXY")}" +} + +variable ks_mirror { + type = string + default = "${env("KS_MIRROR")}" +} + +locals { + ks_proxy = var.ks_proxy != "" ? "--proxy=${var.ks_proxy}" : "" + ks_os_repos = var.ks_mirror != "" ? "--url=${var.ks_mirror}/os/x86_64" : var.ks_os_repos + ks_updates_repos = var.ks_mirror != "" ? "--baseurl=${var.ks_mirror}/updates/x86_64" : var.ks_updates_repos + ks_extras_repos = var.ks_mirror != "" ? "--baseurl=${var.ks_mirror}/extras/x86_64" : var.ks_extras_repos +} + source "qemu" "centos7" { boot_command = [" ", "inst.ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/centos7.ks ", "console=ttyS0 inst.cmdline", ""] boot_wait = "3s" communicator = "none" disk_size = "4G" headless = true - http_directory = "http" iso_checksum = "file:${var.centos7_sha256sum_url}" iso_url = var.centos7_iso_url memory = 2048 qemuargs = [["-serial", "stdio"]] shutdown_timeout = "1h" + http_content = { + "/centos7.ks" = templatefile("${path.root}/http/centos7.ks.pkrtpl.hcl", + { + KS_PROXY = local.ks_proxy, + KS_OS_REPOS = local.ks_os_repos, + KS_UPDATES_REPOS = local.ks_updates_repos, + KS_EXTRAS_REPOS = local.ks_extras_repos + } + ) + } + } build { diff --git a/centos7/http/centos7.ks.in b/centos7/http/centos7.ks.pkrtpl.hcl similarity index 97% rename from centos7/http/centos7.ks.in rename to centos7/http/centos7.ks.pkrtpl.hcl index 47083a36..d6631f0f 100644 --- a/centos7/http/centos7.ks.in +++ b/centos7/http/centos7.ks.pkrtpl.hcl @@ -13,7 +13,7 @@ bootloader --location=mbr --driveorder="vda" --timeout=1 rootpw --plaintext password repo --name="Updates" ${KS_UPDATES_REPOS} ${KS_PROXY} -repo --name="Extras" ${KS_EXTRA_REPOS} ${KS_PROXY} +repo --name="Extras" ${KS_EXTRAS_REPOS} ${KS_PROXY} zerombr clearpart --all --initlabel