pve-go/userdata.go
2020-06-01 12:18:20 +02:00

91 lines
2.5 KiB
Go

// Copyright (C) 2020 Marius Schellenberger
package pve
import (
"encoding/base64"
"gopkg.in/yaml.v2"
"net/url"
)
func parseUserData(s string) (u *UserData, err error) {
s, err = url.QueryUnescape(s)
if err != nil {
return
}
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return
}
return UnmarshalUserData(b)
}
func UnmarshalUserData(b []byte) (u *UserData, err error) {
u = new(UserData)
err = yaml.Unmarshal(b, u)
return
}
func MarshalUserData(u *UserData) ([]byte, error) {
return yaml.Marshal(u)
}
type UserData struct {
Hostname string `yaml:"hostname"`
ManageEtcHosts bool `yaml:"manage_etc_hosts,omitempty"`
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys,omitempty"`
PackageUpdate bool `yaml:"package_update,omitempty"`
PackageUpgrade bool `yaml:"package_upgrade,omitempty"`
APT APTConfig `yaml:"apt,omitempty"`
NTP NTP `yaml:"ntp,omitempty"`
Users []User `yaml:"users,omitempty"`
Packages [][]string `yaml:"packages,omitempty"`
Runcmd []string `yaml:"runcmd,omitempty"`
FinalMessage string `yaml:"final_message,omitempty"`
WriteFiles []File `yaml:"write_files,omitempty"`
}
func (u *UserData) String() (s string, err error) {
b, err := MarshalUserData(u)
if err != nil {
return
}
s = base64.StdEncoding.EncodeToString(b)
return
}
type NTP struct {
Servers []string `yaml:"servers,omitempty"`
Enabled *bool `yaml:"enabled,omitempty"`
}
type User struct {
Name string `yaml:"name"`
Gecos *string `yaml:"gecos,omitempty"`
Groups *string `yaml:"groups,omitempty"`
HomeDir *string `yaml:"homedir,omitempty"`
Inactive *bool `yaml:"inactive,omitempty"`
Shell *string `yaml:"shell,omitempty"`
Passwd *string `yaml:"passwd,omitempty"`
PrimaryGroup *string `yaml:"primary_group,omitempty"`
LockPassword *bool `yaml:"lock_passwd,omitempty"`
Sudo *string `yaml:"sudo,omitempty"`
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys,omitempty"`
}
type File struct {
Content string `yaml:"content"`
Path string `yaml:"path"`
Owner string `yaml:"owner,omitempty"`
Permissions string `yaml:"permissions,omitempty"`
}
type APTConfig struct {
Sources map[string]APTSource
}
type APTSource struct {
Arches string `yaml:"arches,omitempty"`
Source string `yaml:"source"`
Keyserver string `yaml:"keyserver"`
Keyid string `yaml:"keyid"`
}