tylert / packer-build

Packer Automated VM Image and Vagrant Box Builds

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Templating of external files is not yet complete in conversion from YAML to HCL

stefangweichinger opened this issue · comments

@tylert at first thanks for providing this great collection of build-scripts!
I appreciate your work and try to "fork" my own builds from this repo.

Unfortunately things seem not to work and I wonder if it's my fault or not.

I don't use the tool vagrant itself, so I run packer "manually" within packer-build/source/debian/11_bullseye.

I edited some variables to fit my local environment:

 git diff base.pkr.hcl
diff --git a/source/debian/11_bullseye/base.pkr.hcl b/source/debian/11_bullseye/base.pkr.hcl
index e03063c9..c2df116b 100644
--- a/source/debian/11_bullseye/base.pkr.hcl
+++ b/source/debian/11_bullseye/base.pkr.hcl
@@ -15,7 +15,7 @@ packer {
 
 variable "apt_cache_url" {
   type    = string
-  default = "http://myserver:3142"
+  default = "http://ivy.loc.oops.co.at:3142"
 }
 
 variable "boot_wait" {
@@ -111,7 +111,8 @@ variable "iso_path_external" {
 
 variable "iso_path_internal" {
   type    = string
-  default = "http://myserver:8080/debian"
+  #default = "http://myserver:8080/debian"
+  default = "/mnt/platz/isos/debian"
 }
 
 variable "keep_registered" {
@@ -156,7 +157,8 @@ variable "packer_cache_dir" {
 
 variable "preseed_file" {
   type    = string
-  default = "template/debian/11_bullseye/base.preseed"
+  #default = "template/debian/11_bullseye/base.preseed"
+  default = "base.preseed"
 }
 
 variable "qemu_binary" {
@@ -196,7 +198,7 @@ variable "ssh_file_transfer_method" {
 
 variable "ssh_fullname" {
   type    = string
-  default = "Ghost Writer"
+  default = "vagrant"
 }
 
 variable "ssh_handshake_attempts" {
@@ -211,7 +213,7 @@ variable "ssh_keep_alive_interval" {
 
 variable "ssh_password" {
   type    = string
-  default = "1ma63b0rk3d"
+  default = "vagrant"
 }
 
 variable "ssh_port" {
@@ -231,7 +233,7 @@ variable "ssh_timeout" {
 
 variable "ssh_username" {
   type    = string
-  default = "ghost"
+  default = "vagrant"
 }

I only run the vbox-build now:

PACKER_LOG=1 PACKER_LOG_PATH=packer.log packer build -only=virtualbox-iso.vbox base.pkr.hcl

The VM gets created and started, but always fails at setting the keyboard somehow.
I fiddled with settings in the preseed file already, but set them back again.

Do you have positive results with this build currently? Might there be any changes in packer or debian maybe?

First, I am glad to see others benefitting from my "toy project". Second, Vagrant is meant to be an output, not a build-time dependency (although, I have considered using a Vagrant box to build a Vagrant box for some delightful VM-in-VM inception). Third, you don't need to modify the templates and can instead pass your desired variable overrides in using https://www.packer.io/guides/hcl/variables#assigning-variables --most things I could foresee wanting to configure are already broken out as variables.

Lastly, if you're running into problems, I will have to apologize if I managed to break everything as I am still in the middle of refactoring all the external file templating as per https://www.hashicorp.com/blog/using-template-files-with-hashicorp-packer. Try going back to commit 9190c8f and see if you can get your things working there first. Next, I would have to guess what's in your overridden preseed file--it's possible you have neglected to either template the values or fill in your desired keyboard layout there.

I am still hopeful I can find time to finish inlining all the preseed and vagrantfile templates into the main HCL2 templates with constructs like these:

http_content = {
  "/preseed.cfg" = <<<EOF
d-i apt-setup/universe boolean true
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev linux-source dkms nfs-common linux-headers-$(uname -r) perl cifs-utils software-properties-common rsync ifupdown consul
...
EOF

Also, I haven't had much time to work on this stuff recently and I confess I haven't tested things much of late (and I kinda didn't mean for completing this refactor to be delayed so long). If you do end up finding I've done something really silly, I would be happy to accept your pull-requests to correct the problem in this repo. Given that I have "broken the build" for everyone using this repo, I might go back and make a "legacy" branch for the 9190c8f commit as this should be the most recent point before I started the painful and necessary YAML -> JSON -> HCL2 permanent conversion process.

I am not currently aware of any bugs in Debian or Packer which might explain why just the keyboard stuff isn't working.

Feel free to create a gist in your GH account containing your preseed file and template copies and we can try to take a look at it together.

@tylert thanks for your quick and detailed reply. I will test the mentioned commit asap.
My preseed is the same as yours right now, I reset it to get a working starting point (and failed as mentioned).
Sure, I want to adjust the language/locale/keyboard for my german/austrian environment later.

Can you confirm that the latest commit also fails to build for you?
I run packer-1.7.8 on Fedora 35, just as reference.

Will try your commit now, I only reply right now thinking of the different timezones we are in (keep up the conversation, otherwise it might always take a day or so ;-) ).

The mentioned commit 9190c8f contains YAML files ... as far as I see the current packer does not read those?

I think your usage of variables in the preseed-file is the reason for the failing build.
The variables aren't interpreted as intended.

I look at this to solve this. With a hard-coded preseed file things work for me.

Yes, the older commit provides a script to convert YAML 1.2 -> JSON and template the preseed files automatically--HCL2 finally now has feature-parity with the JSON support and (when finished) reduces the complexity of this repo immensely by removing the dependencies on that external templating script. The info you need to work with the YAML versions of those templates is shown here https://github.com/tylert/packer-build/tree/9190c8f1b4ebfee08b137932db829219af10aac8#using-packer-templates .

Have a look at https://learn.hashicorp.com/tutorials/packer/hcl2-upgrade if you wish to use this same YAML 1.2 -> JSON -> HCL2 trick as I used AFTER commit 9190c8f.

Yes, I was pretty sure the templating was the issue you were running into which is why I linked you to https://www.hashicorp.com/blog/using-template-files-with-hashicorp-packer in my earlier comment.

I hope this means you are unblocked and can easily override your keyboard/locale/language settings now. I would like wrap conditionals around lines such as https://github.com/tylert/packer-build/blob/master/source/debian/11_bullseye/base-uefi.preseed#L5 to make it easier to support more than a single language at a time.

I will leave your issue open here and use it to nag me to come back here to fix the templating before the end of the year (hopefully). 😄

I made progress and rewrote my hcl-script to include all 4 releases of debian in one nice file.

So far I don't have a preseed template in place, that's the next phase. I might share my work as soon as it is worth showing ;-)

See https://github.com/stefangweichinger/packer-builds/blob/main/debian-improved.pkr.hcl for my work in progress.
So far I have the templating disabled, but it worked already in general.

I added the qemu builder now, this isn't 100% yet, but the virtualbox-iso stuff works fine.

I separated the variables into an extra file and use "dynamic sources" and that loop construct to define all 4 debian-releases in one file. Feel free to comment and suggest ...

Yes, I took some of your content and modified it, if you want me to put that in a properly forked repo, tell me and I will rm that temporary draft repo.

  1. I chose not to try to never combine templates for different releases of Debian or Ubuntu in a single file. For other distros, I have found that there are often changes in things like preseed contents or in package names from release to release that can obviate doing this "cleanly and easily" so I just decided to not bother doing this.
  2. Also, if you don't combine multiple distro releases in a single file, dropping support for a distro release is as simple as deleting a complete directory and adding support for a new release is often as simple as copying the previous release and changing a few variable defaults.
  3. If you wanted to invest a bit of time to try to get the qemu support and templating stuff working fully, I would be most happy to work with you to get your pull-request merged into this (my) repo.
  4. I would prefer to inline 100% of the provisioner scripts and post-processor stuff and cloud-init and preseed stuff all into the Packer template rather than have any external files. Ideally, the only external files would be variable overrides (for doing things like settings locale/language/keyboard/timezone stuff and for choosing a different username and password). The templates should really have all the variables and builders and everything all in a single file.
  5. My ideal layout for each OS + distro combination would be: a "base" image, a "base with UEFI support" image, a "base with LUKS encryption" image and a "base with LUKS encryption and UEFI support" image.
  6. Sorry for the delay in replying but this is a very busy time of year for me and there are a lot of projects and things currently going on so do not be too discouraged if I take a few days to get back to you.

@tylert I am also quite busy, and don't find time for my fork right now. We'll keep in touch here ...

  1. with my approach it should be "simple" to define packages per release. For example, buster installs "a,b,c", bullseye installs "b,c,d".
  2. I see pros and cons for each ... will decide that later. For now I "only" do Debian, so it might be manageable for me.
  3. I think I have qemu already. Testing templating right now.
  4. let's see how my configs will look like. Template file has to be separate anyway, just as an example.
  5. so far I don't do extra images. Trying to get the basic "loop" to work while also understanding things.

See my progress at https://github.com/stefangweichinger/packer-builds

That "loop" of releases works now, personally I like that compact way of configuration.
I decided to separate the variables into variables.pkr.hcl as recommended by the upstream docs.

Templating the preseed file: not yet fully OK. I seem to have the variable-names wrong. Suggestions welcome.

I don't need to define separate locales, for example, I just want to learn how to use that feature.

commented

When you get to templating fixing :) here is what I did to have working preseed with your config:

  http_content = {
    "/preseed.cfg" = templatefile(var.preseed_file, {var=var})
  }

And removed http_directory.

Thanks for this discussion, it helped me to orient myself much faster.

Next best thing to an actual pull-request is a bit of help (and gentle nagging) to get me to finish the templating stuff. Thanks, @kepi . 31a24eb pushed.

Unfortunately, I'm on an aarch64 (M1) machine at the moment and can't test the vbox builder and I haven't done a qemu build on this machine with Packer yet so I'm a little hesitant. If you felt like you wanted to give this a test run, that would be most apprecated.

I suspect this issue to be closed now.