Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upallow to serve http files containing variable(s) #9485
Comments
|
A proposal for this would be to add a new http_files = {
"some_file" = "contents"
"preseed.cfg" = <<EOF
d-i passwd/user-fullname string ${var.user_name}
d-i passwd/user-uid string 1000
d-i passwd/user-password password ${var.user_password}
d-i passwd/user-password-again password ${var.user_password}
d-i passwd/username string ${var.user_name}
EOF
# this could also be an ssh public key
}This would start an http server that simply serves these string. I'd keep it simple by not adding support for subfolders which seems like a 'nice-to-have' but I think it would be unnecessary. For implementation: I'd change these structs to have that new Lines 11 to 37 in d35f342 packer/common/step_http_server.go Lines 23 to 30 in d35f342 One simple solution would be to serve them using a map[string]string:
Note that the map implementation makes the subfolder serving easy as we would just match the whole string. Another solution ( which I like a bit less ) would be to create these files in a temporary folder and serve that folder. |
|
Would it be simpler to use the golang template package (https://golang.org/pkg/text/template/) and serve up files with a particular extension? |
|
@jhawk28 I'm not sure to understand, |
|
@azr here is how I think it could work:
|
|
Ah I see, thanks for you suggestion, I like my suggestion a little bit better because it is a bit less magic and therefore easier to document & comprehend and if you were in HCL2 mode it would be super easy to put the template definition in a file on it's own, for example: # preseed.cfg.pkr.hcl
locals {
"preseed.cfg" = <<EOF
d-i passwd/user-fullname string ${var.user_name}
d-i passwd/user-uid string 1000
d-i passwd/user-password password ${var.user_password}
d-i passwd/user-password-again password ${var.user_password}
d-i passwd/username string ${var.user_name}
EOF
} |
|
to each his own. I prefer the ability to encapsulate the file outside of the packer json/hcl file. |
|
Why not save the templates off as their own files and have a map[string]string var named |
|
I don't think you need to render the templates as files, you can just serve them up in the HTTP handler. We could just identify the templates in a list. If it allowed wildcards, it would reduce the config to allow the user to define something like *.pkrtemplate. Example: {
"http_templates": ["preseed.cfg", "*.pkrtemplates"],
"http_dir": "http"
} |
|
In HCL2 we could take the templatefile function from Terraform; this would allow to load template files in other places too, making the solution less specific and more general: #preseed.cfg.tpl
d-i passwd/user-fullname string ${user_name}
d-i passwd/user-uid string 1000
d-i passwd/user-password password ${user_password}
d-i passwd/user-password-again password ${user_password}
d-i passwd/username string ${user_name}# source.pkr.hcl
source "..." "..." {
http_files = {
"preseed.cfg" = templatefile("preseed.cfg.tpl", { user_name = var.user_name , user_password = var.user_password ),
}
}etc. |
This has precedent: #3961 #1673 #2867 #4530 but was never exactly solved.
I would be nice if the contents served in HTTP by packer for the
boot_commandcould be passed from a variable.Why ?
Very often in packer buildfiles I see the boot command using local files, for example:
Very often a
preseed.cfgfile for packer will contain the following settings:d-i passwd/user-fullname string vagrant d-i passwd/user-uid string 1000 d-i passwd/user-password password vagrant d-i passwd/user-password-again password vagrant d-i passwd/username string vagrantThis results in a lot of images actually having
vagrant/vagrantas login. This is okay in most situations as a provisioning step will change this setting later on but making these actual variables could make the process a bit safer by may be passing ssh keys and faster by setting these credentials correctly initially.