2013-08-15 22:11:27 -04:00
|
|
|
package packer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2013-10-16 22:21:14 -04:00
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2013-08-15 22:11:27 -04:00
|
|
|
"strconv"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2013-12-26 19:29:21 -05:00
|
|
|
// InitTime is the UTC time when this package was initialized. It is
|
|
|
|
// used as the timestamp for all configuration templates so that they
|
|
|
|
// match for a single build.
|
|
|
|
var InitTime time.Time
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
InitTime = time.Now().UTC()
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:11:27 -04:00
|
|
|
// ConfigTemplate processes string data as a text/template with some common
|
|
|
|
// elements and functions available. Plugin creators should process as
|
|
|
|
// many fields as possible through this.
|
|
|
|
type ConfigTemplate struct {
|
|
|
|
UserVars map[string]string
|
|
|
|
|
|
|
|
root *template.Template
|
|
|
|
i int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfigTemplate creates a new configuration template processor.
|
|
|
|
func NewConfigTemplate() (*ConfigTemplate, error) {
|
|
|
|
result := &ConfigTemplate{
|
|
|
|
UserVars: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
result.root = template.New("configTemplateRoot")
|
|
|
|
result.root.Funcs(template.FuncMap{
|
2013-09-05 14:09:37 -04:00
|
|
|
"isotime": templateISOTime,
|
2013-08-15 22:11:27 -04:00
|
|
|
"timestamp": templateTimestamp,
|
|
|
|
"user": result.templateUser,
|
2013-09-05 15:19:56 -04:00
|
|
|
"uuid": templateUuid,
|
2013-08-15 22:11:27 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process processes a single string, compiling and executing the template.
|
|
|
|
func (t *ConfigTemplate) Process(s string, data interface{}) (string, error) {
|
|
|
|
tpl, err := t.root.New(t.nextTemplateName()).Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := tpl.Execute(buf, data); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the template.
|
|
|
|
func (t *ConfigTemplate) Validate(s string) error {
|
|
|
|
root, err := t.root.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = root.New("template").Parse(s)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-09-07 21:42:50 -04:00
|
|
|
// Add additional functions to the template
|
|
|
|
func (t *ConfigTemplate) Funcs(funcs template.FuncMap) {
|
|
|
|
t.root.Funcs(funcs)
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:11:27 -04:00
|
|
|
func (t *ConfigTemplate) nextTemplateName() string {
|
|
|
|
name := fmt.Sprintf("tpl%d", t.i)
|
|
|
|
t.i++
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
// User is the function exposed as "user" within the templates and
|
|
|
|
// looks up user variables.
|
|
|
|
func (t *ConfigTemplate) templateUser(n string) (string, error) {
|
|
|
|
result, ok := t.UserVars[n]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("uknown user var: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2013-09-05 20:12:15 -04:00
|
|
|
func templateISOTime() string {
|
|
|
|
return time.Now().UTC().Format(time.RFC3339)
|
|
|
|
}
|
|
|
|
|
2013-08-15 22:11:27 -04:00
|
|
|
func templateTimestamp() string {
|
2013-12-26 19:29:21 -05:00
|
|
|
return strconv.FormatInt(InitTime.Unix(), 10)
|
2013-08-15 22:11:27 -04:00
|
|
|
}
|
2013-09-05 15:19:56 -04:00
|
|
|
|
|
|
|
func templateUuid() string {
|
2013-10-16 22:27:15 -04:00
|
|
|
return uuid.TimeOrderedUUID()
|
2013-09-05 15:19:56 -04:00
|
|
|
}
|