From 81a8cca451337a70fe53cb312f43e945a3fda52e Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 14 Aug 2016 22:28:39 -0700 Subject: [PATCH] pixiecore: refactor APIBooter commandline construction. This allows API servers to return URL(x) in the cmdline template, which pixiecore translates to an appropriate ID(x) template call to get the file proxied via Pixiecore. --- pixiecore/booters.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pixiecore/booters.go b/pixiecore/booters.go index eec9275..36c5805 100644 --- a/pixiecore/booters.go +++ b/pixiecore/booters.go @@ -165,7 +165,6 @@ func (b *apibooter) BootSpec(m Machine) (*Spec, error) { Cmdline interface{} `json:"cmdline"` Message string `json:"message"` }{} - if err = json.NewDecoder(body).Decode(&r); err != nil { return nil, err } @@ -198,7 +197,6 @@ func (b *apibooter) BootSpec(m Machine) (*Spec, error) { if r.Cmdline != nil { switch c := r.Cmdline.(type) { case string: - ret.Cmdline = c case map[string]interface{}: ret.Cmdline, err = b.constructCmdline(c) if err != nil { @@ -209,6 +207,15 @@ func (b *apibooter) BootSpec(m Machine) (*Spec, error) { } } + f := func(u string) (string, error) { + id, err := signURL(u, &b.key) + if err != nil { + return "", err + } + return fmt.Sprintf("{{ ID %q }}", id), nil + } + ret.Cmdline, err = expandCmdline(ret.Cmdline, template.FuncMap{"URL": f}) + return &ret, nil } @@ -290,7 +297,7 @@ func (b *apibooter) constructCmdline(m map[string]interface{}) (string, error) { case bool: ret = append(ret, k) case string: - ret = append(ret, fmt.Sprintf("%s=%s", k, v)) + ret = append(ret, fmt.Sprintf("%s=%q", k, v)) case map[string]interface{}: urlStr, ok := v["url"].(string) if !ok { @@ -300,11 +307,7 @@ func (b *apibooter) constructCmdline(m map[string]interface{}) (string, error) { if err != nil { return "", fmt.Errorf("invalid url for cmdline key %q: %s", k, err) } - encoded, err := signURL(urlStr, &b.key) - if err != nil { - return "", err - } - ret = append(ret, fmt.Sprintf("%s=%s", k, encoded)) + ret = append(ret, fmt.Sprintf("%s={{ URL %q }}", k, urlStr)) default: return "", fmt.Errorf("unsupported value kind %T for cmdline key %q", m[k], k) }