From 3bcb83e55b3a9dcc8a39c0afef9be70001bca571 Mon Sep 17 00:00:00 2001 From: afg Date: Tue, 28 Aug 2018 07:35:41 +0800 Subject: [PATCH] Allow {{ ServerHost }} in --cmdline --- pixiecore/booters.go | 8 ++++++-- pixiecore/http.go | 8 ++++++-- pixiecore/pixiecore.go | 7 ++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/pixiecore/booters.go b/pixiecore/booters.go index a83daa8..6c2ec18 100644 --- a/pixiecore/booters.go +++ b/pixiecore/booters.go @@ -30,6 +30,10 @@ import ( "time" ) +func dummyServerHost() string { + return "{{ ServerHost }}" +} + // StaticBooter boots all machines with the same Spec. // // IDs in spec should be either local file paths, or HTTP/HTTPS URLs. @@ -50,7 +54,7 @@ func StaticBooter(spec *Spec) (Booter, error) { ret.otherIDs = append(ret.otherIDs, id) return fmt.Sprintf("{{ ID \"other-%d\" }}", len(ret.otherIDs)-1) } - cmdline, err := expandCmdline(spec.Cmdline, template.FuncMap{"ID": f}) + cmdline, err := expandCmdline(spec.Cmdline, template.FuncMap{"ID": f, "ServerHost": dummyServerHost}) if err != nil { return nil, err } @@ -237,7 +241,7 @@ func (b *apibooter) BootSpec(m Machine) (*Spec, error) { } return fmt.Sprintf("{{ ID %q }}", id), nil } - ret.Cmdline, err = expandCmdline(ret.Cmdline, template.FuncMap{"URL": f}) + ret.Cmdline, err = expandCmdline(ret.Cmdline, template.FuncMap{"URL": f, "ServerHost": dummyServerHost}) if err != nil { return nil, err } diff --git a/pixiecore/http.go b/pixiecore/http.go index cfc0e98..55d2705 100644 --- a/pixiecore/http.go +++ b/pixiecore/http.go @@ -207,10 +207,14 @@ func ipxeScript(mach Machine, spec *Spec, serverHost string) ([]byte, error) { fmt.Fprintf(&b, "initrd=initrd%d ", i) } - f := func(id string) string { + idFunc := func(id string) string { return fmt.Sprintf("http://%s/_/file?name=%s", serverHost, url.QueryEscape(id)) } - cmdline, err := expandCmdline(spec.Cmdline, template.FuncMap{"ID": f}) + serverHostFunc := func() string { + return serverHost + } + cmdline, err := expandCmdline(spec.Cmdline, + template.FuncMap{"ID": idFunc, "ServerHost": serverHostFunc}) if err != nil { return nil, fmt.Errorf("expanding cmdline %q: %s", spec.Cmdline, err) } diff --git a/pixiecore/pixiecore.go b/pixiecore/pixiecore.go index a9c831f..cbbbe87 100644 --- a/pixiecore/pixiecore.go +++ b/pixiecore/pixiecore.go @@ -77,9 +77,10 @@ type Spec struct { // Optional init ramdisks for linux kernels Initrd []ID // Optional kernel commandline. This string is evaluated as a - // text/template template, in which "ID(x)" function is - // available. Invoking ID(x) returns a URL that will call - // Booter.ReadBootFile(x) when fetched. + // text/template template, in which these functions are available: + // "ID(x)": returns a URL that will call + // Booter.ReadBootFile(x) when fetched. + // "ServerHost()": returns the server address of the http server Cmdline string // Message to print on the client machine before booting. Message string