This follows #8232 which added the code to generate the code required to parse HCL files for each packer component. All old config files of packer will keep on working the same. Packer takes one argument. When a directory is passed, all files in the folder with a name ending with “.pkr.hcl” or “.pkr.json” will be parsed using the HCL2 format. When a file ending with “.pkr.hcl” or “.pkr.json” is passed it will be parsed using the HCL2 format. For every other case; the old packer style will be used. ## 1. the hcl2template pkg can create a packer.Build from a set of HCL (v2) files I had to make the packer.coreBuild (which is our one and only packer.Build ) a public struct with public fields ## 2. Components interfaces get a new ConfigSpec Method to read a file from an HCL file. This is a breaking change for packer plugins. a packer component can be a: builder/provisioner/post-processor each component interface now gets a `ConfigSpec() hcldec.ObjectSpec` which allows packer to tell what is the layout of the hcl2 config meant to configure that specific component. This ObjectSpec is sent through the wire (RPC) and a cty.Value is now sent through the already existing configuration entrypoints: Provisioner.Prepare(raws ...interface{}) error Builder.Prepare(raws ...interface{}) ([]string, error) PostProcessor.Configure(raws ...interface{}) error close #1768 Example hcl files: ```hcl // file amazon-ebs-kms-key/run.pkr.hcl build { sources = [ "source.amazon-ebs.first", ] provisioner "shell" { inline = [ "sleep 5" ] } post-processor "shell-local" { inline = [ "sleep 5" ] } } // amazon-ebs-kms-key/source.pkr.hcl source "amazon-ebs" "first" { ami_name = "hcl2-test" region = "us-east-1" instance_type = "t2.micro" kms_key_id = "c729958f-c6ba-44cd-ab39-35ab68ce0a6c" encrypt_boot = true source_ami_filter { filters { virtualization-type = "hvm" name = "amzn-ami-hvm-????.??.?.????????-x86_64-gp2" root-device-type = "ebs" } most_recent = true owners = ["amazon"] } launch_block_device_mappings { device_name = "/dev/xvda" volume_size = 20 volume_type = "gp2" delete_on_termination = "true" } launch_block_device_mappings { device_name = "/dev/xvdf" volume_size = 500 volume_type = "gp2" delete_on_termination = true encrypted = true } ami_regions = ["eu-central-1"] run_tags { Name = "packer-solr-something" stack-name = "DevOps Tools" } communicator = "ssh" ssh_pty = true ssh_username = "ec2-user" associate_public_ip_address = true } ```
128 lines
2.9 KiB
Go
128 lines
2.9 KiB
Go
package packer
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// ConfigFile returns the default path to the configuration file. On
|
|
// Unix-like systems this is the ".packerconfig" file in the home directory.
|
|
// On Windows, this is the "packer.config" file in the application data
|
|
// directory.
|
|
func ConfigFile() (string, error) {
|
|
return configFile()
|
|
}
|
|
|
|
// ConfigDir returns the configuration directory for Packer.
|
|
func ConfigDir() (string, error) {
|
|
return configDir()
|
|
}
|
|
|
|
func homeDir() (string, error) {
|
|
// Prefer $HOME over user.Current due to glibc bug: golang.org/issue/13470
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
return home, nil
|
|
}
|
|
|
|
if home := os.Getenv("APPDATA"); home != "" {
|
|
return home, nil
|
|
}
|
|
|
|
// Fall back to the passwd database if not found which follows
|
|
// the same semantics as bourne shell
|
|
u, err := user.Current()
|
|
|
|
// Get homedir from specified username
|
|
// if it is set and different than what we have
|
|
if username := os.Getenv("USER"); username != "" && err == nil && u.Username != username {
|
|
u, err = user.Lookup(username)
|
|
}
|
|
|
|
// Fail if we were unable to read the record
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return u.HomeDir, nil
|
|
}
|
|
|
|
func configFile() (string, error) {
|
|
var dir string
|
|
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
|
|
log.Printf("Detected config directory from env var: %s", cd)
|
|
dir = cd
|
|
} else {
|
|
homedir, err := homeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
dir = homedir
|
|
}
|
|
return filepath.Join(dir, defaultConfigFile), nil
|
|
}
|
|
|
|
func configDir() (string, error) {
|
|
var dir string
|
|
if cd := os.Getenv("PACKER_CONFIG_DIR"); cd != "" {
|
|
log.Printf("Detected config directory from env var: %s", cd)
|
|
dir = cd
|
|
} else {
|
|
homedir, err := homeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
dir = homedir
|
|
}
|
|
|
|
return filepath.Join(dir, defaultConfigDir), nil
|
|
}
|
|
|
|
// Given a path, check to see if it's using ~ to reference a user directory.
|
|
// If so, then replace that component with the requested user directory.
|
|
// In "~/", "~" gets replaced by current user's home dir.
|
|
// In "~root/", "~user" gets replaced by root's home dir.
|
|
// ~ has to be the first character of path for ExpandUser change it.
|
|
func ExpandUser(path string) (string, error) {
|
|
var (
|
|
u *user.User
|
|
err error
|
|
)
|
|
|
|
// refuse to do anything with a zero-length path
|
|
if len(path) == 0 {
|
|
return path, nil
|
|
}
|
|
|
|
// If no expansion was specified, then refuse that too
|
|
if path[0] != '~' {
|
|
return path, nil
|
|
}
|
|
|
|
// Grab everything up to the first filepath.Separator
|
|
idx := strings.IndexAny(path, `/\`)
|
|
if idx == -1 {
|
|
idx = len(path)
|
|
}
|
|
|
|
// Now we should be able to extract the username
|
|
username := path[:idx]
|
|
|
|
// Check if the current user was requested
|
|
if username == "~" {
|
|
u, err = user.Current()
|
|
} else {
|
|
u, err = user.Lookup(username[1:])
|
|
}
|
|
|
|
// If we couldn't figure that out, then fail here
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Now we can replace the path with u.HomeDir
|
|
return filepath.Join(u.HomeDir, path[idx:]), nil
|
|
}
|