Skip to content

Commit

Permalink
add initial support for user provided ignition drop-ins
Browse files Browse the repository at this point in the history
Signed-off-by: António Meireles <[email protected]>
  • Loading branch information
AntonioMeireles committed Nov 14, 2016
1 parent 2b39d3c commit 11de135
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 121 deletions.
21 changes: 19 additions & 2 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,16 @@ func vmBootstrap(args *viper.Viper) (vm *server.VMInfo, err error) {
vm.Ethernet =
append(vm.Ethernet, server.NetworkInterface{Type: server.Raw})

err = vm.ValidateCloudConfig(args.GetString("cloud_config"))
fuzeCfgs := viperStringSliceBugWorkaround(
args.GetStringSlice("ignition-fuze-config"))

if args.GetString("cloud-config") != "" && len(fuzeCfgs) != 0 {
err = fmt.Errorf("you can either use cloud-config or " +
"ignition-fuze-config")
return
}
err = vm.ValidateUserProvidedConfigs(
args.GetString("cloud-config"), fuzeCfgs)
if err != nil {
return
}
Expand All @@ -209,8 +218,11 @@ func runFlagsDefaults(setFlag *pflag.FlagSet) {
setFlag.IntP("memory", "m", 1024,
"VM's RAM, in MB, per instance (1024 < memory < 8192)")
setFlag.IntP("cpus", "N", 1, "VM number of virtual CPUs")
setFlag.StringP("cloud_config", "L", "",
setFlag.StringP("cloud-config", "L", "",
"cloud-config file location (either an URL or a local path)")
setFlag.StringSliceP("ignition-fuze-config", "I", nil,
"ignition fuze drop-ins file(s) location "+
"(either an URL or a local path)")
setFlag.StringP("sshkey", "k", "", "VM's default ssh key")
setFlag.StringP("root", "r", "", "append a (persistent) root volume to VM")
setFlag.BoolP("format-root", "F", false,
Expand All @@ -228,6 +240,11 @@ func runFlagsDefaults(setFlag *pflag.FlagSet) {
// available but hidden...
setFlag.StringP("tap", "t", "", "append tap interface to VM")
setFlag.MarkHidden("tap")
setFlag.SetNormalizeFunc(wordSepNormalizeFunc)
}

func wordSepNormalizeFunc(f *pflag.FlagSet, name string) pflag.NormalizedName {
return pflag.NormalizedName(strings.Replace(name, "_", "-", -1))
}

func init() {
Expand Down
8 changes: 4 additions & 4 deletions components/common/assets/assets_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ passwd:
users:
- name: core
ssh_authorized_keys:
{{ range $element := .SSHAuthorizedKeys }}
{{ range $element := .SSHAuthorizedKeys -}}
- {{$element}}
{{end}}
{{end}}

{{- if .UserProvidedFuzeConfigs -}}
ignition:
config:
append:
{{ range $a,$b := .UserProvidedFuzeConfigs -}}
- source: {{$.CorectldEndpoint}}/ignition/append/{{$a}}
{{end}}
{{ end }}

storage:
{{ if .SetupRoot }}
disks:
Expand Down
65 changes: 51 additions & 14 deletions components/server/httpservices.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package server
import (
"bytes"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"text/template"

Expand All @@ -35,15 +35,18 @@ import (
var httpServices = mux.NewRouter()

type corectlTmpl struct {
SetupRoot, PersistentRoot, SharedHomedir bool
CorectlVersion, CorectldEndpoint string
NetworkdGateway, NetworkdDns, Hostname string
SSHAuthorizedKeys []string
NFShomedirPath, NFShomedirPathEscaped string
SetupRoot, PersistentRoot, SharedHomedir bool
CorectlVersion, CorectldEndpoint string
NetworkdGateway, NetworkdDns, Hostname string
NFShomedirPath, NFShomedirPathEscaped string
SSHAuthorizedKeys, UserProvidedFuzeConfigs []string
}

func httpServiceSetup() {
httpServices.HandleFunc("/{uuid}/ignition", httpInstanceIgnitionConfig)
httpServices.HandleFunc("/{uuid}/ignition/append/{id}",
httpInstanceUserProvidedIgnitionConfigs)
httpServices.HandleFunc("/{uuid}/ignition/default/config",
httpInstanceDefaultIgnitionConfig)
httpServices.HandleFunc("/{uuid}/cloud-config", httpInstanceCloudConfig)
httpServices.HandleFunc("/{uuid}/ping", httpInstanceCallback)
httpServices.HandleFunc("/{uuid}/NotIsolated",
Expand Down Expand Up @@ -88,13 +91,13 @@ func acceptableRequest(r *http.Request, w http.ResponseWriter) bool {
func httpInstanceCloudConfig(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
vm := Daemon.Active[mux.Vars(r)["uuid"]]
if vm.CloudConfig == "" || vm.CClocation != Local {
if vm.CloudConfig.Location == "" {
httpError(w, http.StatusPreconditionFailed)
} else if vm.cloudConfigContents == nil {
} else if vm.CloudConfig.Contents == nil {
httpError(w, http.StatusInternalServerError)
} else {
vars := strings.NewReplacer("__vm.Name__", vm.Name)
w.Write([]byte(vars.Replace(string(vm.cloudConfigContents))))
w.Write([]byte(vars.Replace(string(vm.CloudConfig.Contents))))
}
}
}
Expand All @@ -108,7 +111,37 @@ func isPortOpen(t string, target string) bool {
return false
}

func httpInstanceIgnitionConfig(w http.ResponseWriter, r *http.Request) {
func httpInstanceUserProvidedIgnitionConfigs(w http.ResponseWriter,
r *http.Request) {
if acceptableRequest(r, w) {
vm := Daemon.Active[mux.Vars(r)["uuid"]]
ign, err := strconv.Atoi(mux.Vars(r)["id"])
if err != nil {
httpError(w, http.StatusPreconditionFailed)
}
if len(vm.IgnitionFuzeConfigs) == 0 {
httpError(w, http.StatusPreconditionFailed)
} else if len(vm.IgnitionFuzeConfigs) < ign+1 {
httpError(w, http.StatusPreconditionFailed)
} else {
if cfgIn, err := config.ParseAsV2_0_0(
vm.IgnitionFuzeConfigs[ign].Contents); err != nil {
log.Err("%v", err.Error())
httpError(w, http.StatusInternalServerError)
} else if i, err :=
json.MarshalIndent(&cfgIn, "", " "); err != nil {
log.Err("%v", err.Error())
httpError(w, http.StatusInternalServerError)
} else {
w.Write([]byte(append(i, '\n')))
if !isLoopback(remoteIP(r.RemoteAddr)) {
Daemon.DNSServer.addRecord(vm.Name, remoteIP(r.RemoteAddr))
}
}
}
}
}
func httpInstanceDefaultIgnitionConfig(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
var (
rendered bytes.Buffer
Expand All @@ -122,16 +155,18 @@ func httpInstanceIgnitionConfig(w http.ResponseWriter, r *http.Request) {
session.Caller.Network.Address,
LocalDomainName,
vm.Name,
[]string{vm.InternalSSHkey},
session.Caller.HomeDir,
unit.UnitNamePathEscape(session.Caller.HomeDir),
[]string{vm.InternalSSHkey},
[]string{},
}
)
if vm.SSHkey != "" {
setup.SSHAuthorizedKeys = append(setup.SSHAuthorizedKeys, vm.SSHkey)
}
if vm.CloudConfig != "" && vm.CClocation == Local {
vm.cloudConfigContents, _ = ioutil.ReadFile(vm.CloudConfig)
for _, fz := range vm.IgnitionFuzeConfigs {
setup.UserProvidedFuzeConfigs =
append(setup.UserProvidedFuzeConfigs, fz.Location)
}
t, _ := template.New("").Parse(string(coreos.CoreOSIgnitionTmpl))
if err := t.Execute(&rendered, setup); err != nil {
Expand All @@ -141,8 +176,10 @@ func httpInstanceIgnitionConfig(w http.ResponseWriter, r *http.Request) {

log.Info(rendered.String())
if cfgIn, err := config.ParseAsV2_0_0(rendered.Bytes()); err != nil {
log.Err("%v", err.Error())
httpError(w, http.StatusInternalServerError)
} else if i, err := json.MarshalIndent(&cfgIn, "", " "); err != nil {
log.Err("%v", err.Error())
httpError(w, http.StatusInternalServerError)
} else {
w.Write([]byte(append(i, '\n')))
Expand Down
Loading

0 comments on commit 11de135

Please sign in to comment.