Refactor in preperation for non-Debian support
I dunno why anyone would ever use non-Debian but I guess some people do
This commit is contained in:
parent
6ccb983f10
commit
5e3a83e585
6 changed files with 386 additions and 20 deletions
80
http.go
80
http.go
|
@ -49,14 +49,7 @@ func (httphandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
case http.MethodPost:
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error reading body")
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
log.Debug(string(body))
|
||||
|
||||
printRequestBody(r)
|
||||
log.Info("VM booted")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
wg.Done()
|
||||
|
@ -68,7 +61,36 @@ func (httphandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
}
|
||||
|
||||
func buildCloudConfig(i int, name string, phoneHomeURL string) error {
|
||||
func buildCloudConfig(name string, phoneHomeURL string) error {
|
||||
userdata, err := yaml.Marshal(cloudinit.UserData{
|
||||
Packages: argPackages,
|
||||
SSHAuthorizedKeys: argSSHKeys,
|
||||
PhoneHome: cloudinit.PhoneHome{
|
||||
URL: phoneHomeURL,
|
||||
Post: []string{"pub_key_dsa", "pub_key_rsa", "pub_key_ed25519", "fqdn"},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
httpStaticContent["/user-data"] = append([]byte("#cloud-config\n"), userdata...)
|
||||
|
||||
metadata, err := yaml.Marshal(cloudinit.MetaData{
|
||||
InstanceID: name,
|
||||
LocalHostname: name,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
httpStaticContent["/meta-data"] = metadata
|
||||
|
||||
httpStaticContent["/vendor-data"] = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildCloudConfigPrefix(prefix string, name string, phoneHomeURL string) error {
|
||||
userdata, err := yaml.Marshal(cloudinit.UserData{
|
||||
Packages: argPackages,
|
||||
SSHAuthorizedKeys: argSSHKeys,
|
||||
|
@ -81,7 +103,7 @@ func buildCloudConfig(i int, name string, phoneHomeURL string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
httpStaticContent[fmt.Sprintf("/%d/user-data", i)] = append([]byte("#cloud-config\n"), userdata...)
|
||||
httpStaticContent[fmt.Sprintf("/%s/user-data", prefix)] = append([]byte("#cloud-config\n"), userdata...)
|
||||
|
||||
metadata, err := yaml.Marshal(cloudinit.MetaData{
|
||||
InstanceID: name,
|
||||
|
@ -90,9 +112,43 @@ func buildCloudConfig(i int, name string, phoneHomeURL string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
httpStaticContent[fmt.Sprintf("/%d/meta-data", i)] = metadata
|
||||
httpStaticContent[fmt.Sprintf("/%s/meta-data", prefix)] = metadata
|
||||
|
||||
httpStaticContent[fmt.Sprintf("/%d/vendor-data", i)] = nil
|
||||
httpStaticContent[fmt.Sprintf("/%s/vendor-data", prefix)] = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func printRequestBody(r *http.Request) {
|
||||
log := logrus.WithFields(logrus.Fields{
|
||||
"method": r.Method,
|
||||
"path": r.URL,
|
||||
"remote_addr": r.RemoteAddr,
|
||||
})
|
||||
|
||||
switch r.Header.Get("Content-Type") {
|
||||
case "application/x-www-form-urlencoded":
|
||||
if err := r.ParseForm(); err != nil {
|
||||
logrus.WithError(err).Error("error parsing request body")
|
||||
return
|
||||
}
|
||||
fields := logrus.Fields{}
|
||||
for k, v := range r.Form {
|
||||
if len(v) == 1 {
|
||||
fields[k] = v[0]
|
||||
} else {
|
||||
fields[k] = v
|
||||
}
|
||||
}
|
||||
log.WithFields(fields).Debug("form request body")
|
||||
default:
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error reading body")
|
||||
}
|
||||
r.Body.Close()
|
||||
|
||||
log.Debug(string(body))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue