save users some bash scripting by exposing IP and port separately as well as together

This commit is contained in:
Megan Marsh 2018-12-05 16:39:46 -08:00
parent 963982a6a0
commit e69391f28b
5 changed files with 56 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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{

View File

@ -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

View File

@ -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 {