-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
189 lines (165 loc) · 4.54 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/dvob/vu/internal/cloudinit"
"github.com/spf13/cobra"
)
type cloudInitOptions struct {
config *cloudinit.Config
name string
user string
sshPubKey string
sshPubKeyFile string
passwordHash string
networkOptions cloudinit.NetworkConfigOptions
profiles []string
dirs []string
}
func getAbsProfileDirs(profiles []string) ([]string, error) {
userHome, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("failed to resolve profile dirs: %w", err)
}
profileBaseDir := filepath.Join(userHome, ".config", "vu", "profiles")
dirs := []string{}
for _, profile := range profiles {
dirs = append(dirs, filepath.Join(profileBaseDir, profile))
}
return dirs, nil
}
func (o *cloudInitOptions) complete() error {
if o.user == "" {
localUser, err := user.Current()
if err != nil {
return err
}
o.user = localUser.Username
}
if o.sshPubKey == "" {
if o.sshPubKeyFile == "" {
userHome, err := os.UserHomeDir()
if err != nil {
return err
}
o.sshPubKeyFile = filepath.Join(userHome, ".ssh", "id_rsa.pub")
}
pubKey, err := os.ReadFile(o.sshPubKeyFile)
if err != nil {
return err
}
o.sshPubKey = string(pubKey)
}
// load config from profiles
profileDirs, err := getAbsProfileDirs(o.profiles)
if err != nil {
return err
}
o.config, err = cloudinit.ConfigFromDir(profileDirs...)
if err != nil {
return err
}
// load config from directories
dirConfig, err := cloudinit.ConfigFromDir(o.dirs...)
if err != nil {
return err
}
err = o.config.Merge(dirConfig)
if err != nil {
return err
}
// construct network config
networkConfig, err := cloudinit.NewNetworkConfig(o.networkOptions)
if err != nil {
return err
}
if networkConfig != nil {
o.config.NetworkConfig = networkConfig
}
// set general defaults
if o.config.UserData == nil {
o.config.UserData = &cloudinit.UserData{}
}
if o.config.MetaData == nil {
o.config.MetaData = &cloudinit.MetaData{}
}
c := cloudinit.NewDefaultConfig(o.name, o.user, o.sshPubKey)
return o.config.Merge(c)
}
func (o *cloudInitOptions) bindFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&o.user, "user", "", "user name to create in the during startup")
cmd.Flags().StringVar(&o.sshPubKey, "ssh-pub-key", "", "ssh public key to use")
cmd.Flags().StringVar(&o.passwordHash, "password-hash", "", "Password hash to login without SSH over console. The hash can be generated with openssl passwd.")
cmd.Flags().StringVar(&o.networkOptions.Address, "ip", "", "configure static IPv4 address insted of using DHCP. address has to be specified in CIDR notation.")
cmd.Flags().StringVar(&o.networkOptions.Gateway, "gateway", "", "the default IPv4 gateway. if no gateway is configured the lowest IP")
cmd.Flags().StringSliceVar(&o.networkOptions.Nameserver, "dns", []string{}, "configure the dns server address. if no address is configured the default gateway is used.")
cmd.Flags().StringSliceVar(&o.profiles, "profile", []string{}, "base profile which want to use for our configuration")
cmd.Flags().StringSliceVar(&o.dirs, "dir", []string{}, "use configuration from directory as base configuration")
}
func newConfigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "helps to debug cloud init configuration",
}
cmd.AddCommand(
newConfigShowCmd(),
newConfigWriteCmd(),
)
return cmd
}
func newConfigShowCmd() *cobra.Command {
o := &cloudInitOptions{}
cmd := &cobra.Command{
Use: "show <name>",
Short: "shows cloud init configuration",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
o.name = args[0]
err := o.complete()
if err != nil {
return err
}
output, err := o.config.String()
if err != nil {
return err
}
fmt.Println(output)
return nil
},
}
o.bindFlags(cmd)
return cmd
}
func newConfigWriteCmd() *cobra.Command {
var (
o = &cloudInitOptions{}
target string
iso bool
)
cmd := &cobra.Command{
Use: "write NAME TARGET",
Short: "writes cloud init config to directory or iso file",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
o.name = args[0]
target = args[1]
err := o.complete()
if err != nil {
return err
}
if iso {
isoData, err := o.config.ISO()
if err != nil {
return err
}
return os.WriteFile(target, isoData, 0o640)
}
return o.config.ToDir(target)
},
}
cmd.Flags().BoolVar(&iso, "iso", false, "write cloud init config to iso file")
o.bindFlags(cmd)
return cmd
}