/
filesystemlayout.go
249 lines (231 loc) · 7.14 KB
/
filesystemlayout.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package cmd
import (
"errors"
"fmt"
"log"
"net/http"
metalgo "github.com/metal-stack/metal-go"
fsmodel "github.com/metal-stack/metal-go/api/client/filesystemlayout"
"github.com/metal-stack/metal-go/api/models"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
filesystemLayoutCmd = &cobra.Command{
Use: "filesystemlayout",
Aliases: []string{"fsl"},
Short: "manage filesystemlayouts",
Long: "a filesystemlayout is a specification how the disks in a machine are partitioned, formatted and mounted.",
}
filesystemListCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "list all filesystems",
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemList(driver)
},
PreRun: bindPFlags,
}
filesystemDescribeCmd = &cobra.Command{
Use: "describe <filesystemID>",
Short: "describe a filesystem",
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemDescribe(driver, args)
},
ValidArgsFunction: filesystemLayoutListCompletionFunc,
}
filesystemApplyCmd = &cobra.Command{
Use: "apply",
Short: "create/update a filesystem",
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemApply(driver)
},
PreRun: bindPFlags,
}
filesystemDeleteCmd = &cobra.Command{
Use: "delete <filesystemID>",
Short: "delete a filesystem",
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemDelete(driver, args)
},
PreRun: bindPFlags,
ValidArgsFunction: filesystemLayoutListCompletionFunc,
}
filesystemTryCmd = &cobra.Command{
Use: "try",
Short: "try to detect a filesystem by given size and image",
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemTry(driver)
},
PreRun: bindPFlags,
}
filesystemMatchCmd = &cobra.Command{
Use: "match",
Short: "check if a machine satisfies all disk requirements of a given filesystemlayout",
RunE: func(cmd *cobra.Command, args []string) error {
return filesystemMatch(driver)
},
PreRun: bindPFlags,
}
)
func init() {
filesystemApplyCmd.Flags().StringP("file", "f", "", `filename of the create or update request in yaml format, or - for stdin.
Example:
# metalctl filesystem describe default > default.yaml
# vi default.yaml
## either via stdin
# cat default.yaml | metalctl filesystem apply -f -
## or via file
# metalctl filesystem apply -f default.yaml`)
err := filesystemApplyCmd.MarkFlagRequired("file")
if err != nil {
log.Fatal(err.Error())
}
filesystemTryCmd.Flags().StringP("size", "", "", "size to try")
filesystemTryCmd.Flags().StringP("image", "", "", "image to try")
err = filesystemTryCmd.MarkFlagRequired("size")
if err != nil {
log.Fatal(err.Error())
}
err = filesystemTryCmd.MarkFlagRequired("image")
if err != nil {
log.Fatal(err.Error())
}
err = filesystemTryCmd.RegisterFlagCompletionFunc("size", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return sizeListCompletion(driver)
})
if err != nil {
log.Fatal(err.Error())
}
err = filesystemTryCmd.RegisterFlagCompletionFunc("image", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return imageListCompletion(driver)
})
if err != nil {
log.Fatal(err.Error())
}
filesystemMatchCmd.Flags().StringP("machine", "", "", "machine id to check for match [required]")
filesystemMatchCmd.Flags().StringP("filesystemlayout", "", "", "filesystemlayout id to check against [required]")
err = filesystemMatchCmd.MarkFlagRequired("machine")
if err != nil {
log.Fatal(err.Error())
}
err = filesystemMatchCmd.MarkFlagRequired("filesystemlayout")
if err != nil {
log.Fatal(err.Error())
}
err = filesystemMatchCmd.RegisterFlagCompletionFunc("machine", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return machineListCompletion(driver)
})
if err != nil {
log.Fatal(err.Error())
}
err = filesystemMatchCmd.RegisterFlagCompletionFunc("filesystemlayout", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return filesystemLayoutListCompletion(driver)
})
if err != nil {
log.Fatal(err.Error())
}
filesystemLayoutCmd.AddCommand(filesystemListCmd)
filesystemLayoutCmd.AddCommand(filesystemDescribeCmd)
filesystemLayoutCmd.AddCommand(filesystemDeleteCmd)
filesystemLayoutCmd.AddCommand(filesystemApplyCmd)
filesystemLayoutCmd.AddCommand(filesystemTryCmd)
filesystemLayoutCmd.AddCommand(filesystemMatchCmd)
}
func filesystemList(driver *metalgo.Driver) error {
resp, err := driver.FilesystemLayoutList()
if err != nil {
return err
}
return printer.Print(resp)
}
func filesystemDescribe(driver *metalgo.Driver, args []string) error {
if len(args) < 1 {
return fmt.Errorf("no filesystem ID given")
}
filesystemID := args[0]
resp, err := driver.FilesystemLayoutGet(filesystemID)
if err != nil {
return err
}
return detailer.Detail(resp)
}
// TODO: General apply method would be useful as these are quite a lot of lines and it's getting erroneous
func filesystemApply(driver *metalgo.Driver) error {
var iars []models.V1FilesystemLayoutCreateRequest
var iar models.V1FilesystemLayoutCreateRequest
err := readFrom(viper.GetString("file"), &iar, func(data interface{}) {
doc := data.(*models.V1FilesystemLayoutCreateRequest)
iars = append(iars, *doc)
// the request needs to be renewed as otherwise the pointers in the request struct will
// always point to same last value in the multi-document loop
iar = models.V1FilesystemLayoutCreateRequest{}
})
if err != nil {
return err
}
var response []*models.V1FilesystemLayoutResponse
for _, iar := range iars {
p, err := driver.FilesystemLayoutGet(*iar.ID)
if err != nil {
var r *fsmodel.GetFilesystemLayoutDefault
if !errors.As(err, &r) {
return err
}
if r.Code() != http.StatusNotFound {
return err
}
}
if p == nil {
resp, err := driver.FilesystemLayoutCreate(iar)
if err != nil {
return err
}
response = append(response, resp)
continue
}
resp, err := driver.FilesystemLayoutUpdate(models.V1FilesystemLayoutUpdateRequest(iar))
if err != nil {
return err
}
response = append(response, resp)
}
return printer.Print(response)
}
func filesystemDelete(driver *metalgo.Driver, args []string) error {
if len(args) < 1 {
return fmt.Errorf("no filesystem ID given")
}
filesystemID := args[0]
resp, err := driver.FilesystemLayoutDelete(filesystemID)
if err != nil {
return err
}
return detailer.Detail(resp)
}
func filesystemTry(driver *metalgo.Driver) error {
size := viper.GetString("size")
image := viper.GetString("image")
try := models.V1FilesystemLayoutTryRequest{
Size: &size,
Image: &image,
}
resp, err := driver.FilesystemLayoutTry(try)
if err != nil {
return err
}
return printer.Print(resp)
}
func filesystemMatch(driver *metalgo.Driver) error {
machine := viper.GetString("machine")
fsl := viper.GetString("filesystemlayout")
match := models.V1FilesystemLayoutMatchRequest{
Machine: &machine,
Filesystemlayout: &fsl,
}
resp, err := driver.FilesystemLayoutMatch(match)
if err != nil {
return err
}
return printer.Print(resp)
}