From e69391f28bb79e00cbdd3854a08b6061d0808c5b Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 5 Dec 2018 16:39:46 -0800 Subject: [PATCH] save users some bash scripting by exposing IP and port separately as well as together --- common/shell-local/run.go | 12 ++++++++++-- common/step_http_server.go | 16 ++++++++++++++++ provisioner/powershell/provisioner.go | 9 +++++++++ provisioner/shell/provisioner.go | 12 +++++++++++- provisioner/windows-shell/provisioner.go | 10 ++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/common/shell-local/run.go b/common/shell-local/run.go index 0b87d3e71..6d3ecbb76 100644 --- a/common/shell-local/run.go +++ b/common/shell-local/run.go @@ -178,10 +178,18 @@ func createFlattenedEnvVars(config *Config) (string, error) { envVars["PACKER_BUILD_NAME"] = fmt.Sprintf("%s", config.PackerBuildName) envVars["PACKER_BUILDER_TYPE"] = fmt.Sprintf("%s", config.PackerBuilderType) - // expose PACKER_HTTP_ADDR + // expose ip address variables httpAddr := common.GetHTTPAddr() if httpAddr != "" { - envVars["PACKER_HTTP_ADDR"] = fmt.Sprintf("%s", httpAddr) + envVars["PACKER_HTTP_ADDR"] = httpAddr + } + httpIP := common.GetHTTPIP() + if httpIP != "" { + envVars["PACKER_HTTP_IP"] = httpIP + } + httpPort := common.GetHTTPPort() + if httpPort != "" { + envVars["PACKER_HTTP_PORT"] = httpPort } // interpolate environment variables diff --git a/common/step_http_server.go b/common/step_http_server.go index 394042961..8e9e58983 100644 --- a/common/step_http_server.go +++ b/common/step_http_server.go @@ -96,6 +96,22 @@ func GetHTTPAddr() string { return fmt.Sprintf("%s:%s", ip, port) } +func GetHTTPPort() string { + port, err := common.RetrieveSharedState("port", "") + if err != nil { + return "" + } + return fmt.Sprintf("%s", port) +} + +func GetHTTPIP() string { + ip, err := common.RetrieveSharedState("ip", "") + if err != nil { + return "" + } + return fmt.Sprintf("%s", ip) +} + func (s *StepHTTPServer) Cleanup(multistep.StateBag) { if s.l != nil { // Close the listener so that the HTTP server stops diff --git a/provisioner/powershell/provisioner.go b/provisioner/powershell/provisioner.go index ed35bdd01..3888b3d26 100644 --- a/provisioner/powershell/provisioner.go +++ b/provisioner/powershell/provisioner.go @@ -380,10 +380,19 @@ func (p *Provisioner) createFlattenedEnvVars(elevated bool) (flattened string) { envVars["PACKER_BUILD_NAME"] = p.config.PackerBuildName envVars["PACKER_BUILDER_TYPE"] = p.config.PackerBuilderType + // expose ip address variables httpAddr := common.GetHTTPAddr() if httpAddr != "" { envVars["PACKER_HTTP_ADDR"] = httpAddr } + httpIP := common.GetHTTPIP() + if httpIP != "" { + envVars["PACKER_HTTP_IP"] = httpIP + } + httpPort := common.GetHTTPPort() + if httpPort != "" { + envVars["PACKER_HTTP_PORT"] = httpPort + } // interpolate environment variables p.config.ctx.Data = &EnvVarsTemplate{ diff --git a/provisioner/shell/provisioner.go b/provisioner/shell/provisioner.go index 50977939f..9c401ae8e 100644 --- a/provisioner/shell/provisioner.go +++ b/provisioner/shell/provisioner.go @@ -463,9 +463,19 @@ func (p *Provisioner) escapeEnvVars() ([]string, map[string]string) { // Always available Packer provided env vars envVars["PACKER_BUILD_NAME"] = fmt.Sprintf("%s", p.config.PackerBuildName) envVars["PACKER_BUILDER_TYPE"] = fmt.Sprintf("%s", p.config.PackerBuilderType) + + // expose ip address variables httpAddr := common.GetHTTPAddr() if httpAddr != "" { - envVars["PACKER_HTTP_ADDR"] = fmt.Sprintf("%s", httpAddr) + envVars["PACKER_HTTP_ADDR"] = httpAddr + } + httpIP := common.GetHTTPIP() + if httpIP != "" { + envVars["PACKER_HTTP_IP"] = httpIP + } + httpPort := common.GetHTTPPort() + if httpPort != "" { + envVars["PACKER_HTTP_PORT"] = httpPort } // Split vars into key/value components diff --git a/provisioner/windows-shell/provisioner.go b/provisioner/windows-shell/provisioner.go index f9b105121..98b3b17e9 100644 --- a/provisioner/windows-shell/provisioner.go +++ b/provisioner/windows-shell/provisioner.go @@ -289,10 +289,20 @@ func (p *Provisioner) createFlattenedEnvVars() (flattened string) { // Always available Packer provided env vars envVars["PACKER_BUILD_NAME"] = p.config.PackerBuildName envVars["PACKER_BUILDER_TYPE"] = p.config.PackerBuilderType + + // expose ip address variables httpAddr := common.GetHTTPAddr() if httpAddr != "" { envVars["PACKER_HTTP_ADDR"] = httpAddr } + httpIP := common.GetHTTPIP() + if httpIP != "" { + envVars["PACKER_HTTP_IP"] = httpIP + } + httpPort := common.GetHTTPPort() + if httpPort != "" { + envVars["PACKER_HTTP_PORT"] = httpPort + } // Split vars into key/value components for _, envVar := range p.config.Vars {