2018-03-08 18:22:28 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2019-12-14 06:32:38 -05:00
|
|
|
// This is used in the BasicPlaceholderData() func in the packer/provisioner.go
|
|
|
|
// To force users to access generated data via the "generated" func.
|
|
|
|
const PlaceholderMsg = "To set this dynamically in the Packer template, " +
|
2019-12-17 14:20:57 -05:00
|
|
|
"you must use the `build` function"
|
2019-12-14 06:32:38 -05:00
|
|
|
|
2018-03-08 18:22:28 -05:00
|
|
|
// Used to set variables which we need to access later in the build, where
|
|
|
|
// state bag and config information won't work
|
2018-04-16 14:51:04 -04:00
|
|
|
func sharedStateFilename(suffix string, buildName string) string {
|
2018-03-08 18:22:28 -05:00
|
|
|
uuid := os.Getenv("PACKER_RUN_UUID")
|
2018-04-16 14:51:04 -04:00
|
|
|
return filepath.Join(os.TempDir(), fmt.Sprintf("packer-%s-%s-%s", uuid, suffix, buildName))
|
2018-03-08 18:22:28 -05:00
|
|
|
}
|
|
|
|
|
2018-04-16 14:51:04 -04:00
|
|
|
func SetSharedState(key string, value string, buildName string) error {
|
|
|
|
return ioutil.WriteFile(sharedStateFilename(key, buildName), []byte(value), 0600)
|
2018-03-08 18:22:28 -05:00
|
|
|
}
|
|
|
|
|
2018-04-16 14:51:04 -04:00
|
|
|
func RetrieveSharedState(key string, buildName string) (string, error) {
|
|
|
|
value, err := ioutil.ReadFile(sharedStateFilename(key, buildName))
|
2018-03-08 18:22:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(value), nil
|
|
|
|
}
|
|
|
|
|
2018-04-16 14:51:04 -04:00
|
|
|
func RemoveSharedStateFile(key string, buildName string) {
|
|
|
|
os.Remove(sharedStateFilename(key, buildName))
|
2018-03-08 18:22:28 -05:00
|
|
|
}
|