/
firmware.go
171 lines (146 loc) · 5.96 KB
/
firmware.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
package cmd
import (
"fmt"
metalgo "github.com/metal-stack/metal-go"
"github.com/metal-stack/metalctl/cmd/output"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type firmwareTask int
const (
upload firmwareTask = iota
remove
)
func newFirmwareCmd(c *config) *cobra.Command {
firmwareCmd := &cobra.Command{
Use: "firmware",
Short: "manage firmwares",
Long: "list, upload and remove firmwares.",
}
firmwareListCmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "list firmwares",
Long: "lists all available firmwares matching the given criteria.",
RunE: func(cmd *cobra.Command, args []string) error {
return c.firmwareList()
},
PreRun: bindPFlags,
}
firmwareUploadCmd := &cobra.Command{
Use: "upload",
Short: "upload a firmware",
}
firmwareUploadBiosCmd := &cobra.Command{
Use: "bios <file>",
Short: "upload a BIOS firmware",
Long: "the given BIOS firmware file will be uploaded and tagged as given revision.",
RunE: func(cmd *cobra.Command, args []string) error {
return c.firmwareUploadBios(args)
},
PreRun: bindPFlags,
}
firmwareUploadBmcCmd := &cobra.Command{
Use: "bmc <file>",
Short: "upload a BMC firmware",
Long: "the given BMC firmware file will be uploaded and tagged as given revision.",
RunE: func(cmd *cobra.Command, args []string) error {
return c.firmwareUploadBmc(args)
},
PreRun: bindPFlags,
}
firmwareRemoveCmd := &cobra.Command{
Use: "remove",
Aliases: []string{"rm", "delete", "del"},
Short: "remove a firmware",
Long: "removes the specified firmware.",
RunE: func(cmd *cobra.Command, args []string) error {
return c.firmwareRemove(args)
},
PreRun: bindPFlags,
}
firmwareListCmd.Flags().StringP("kind", "", "", "the firmware kind [bmc|bios]")
firmwareListCmd.Flags().StringP("vendor", "", "", "the vendor")
firmwareListCmd.Flags().StringP("board", "", "", "the board type")
firmwareListCmd.Flags().StringP("machineid", "", "", "the machine id (ignores vendor and board flags)")
must(firmwareListCmd.RegisterFlagCompletionFunc("kind", c.comp.FirmwareKindCompletion))
must(firmwareListCmd.RegisterFlagCompletionFunc("vendor", c.comp.FirmwareVendorCompletion))
must(firmwareListCmd.RegisterFlagCompletionFunc("board", c.comp.FirmwareBoardCompletion))
firmwareCmd.AddCommand(firmwareListCmd)
firmwareUploadBiosCmd.Flags().StringP("vendor", "", "", "the vendor (required)")
firmwareUploadBiosCmd.Flags().StringP("board", "", "", "the board type (required)")
firmwareUploadBiosCmd.Flags().StringP("revision", "", "", "the BIOS firmware revision (required)")
must(firmwareUploadBiosCmd.MarkFlagRequired("vendor"))
must(firmwareUploadBiosCmd.MarkFlagRequired("board"))
must(firmwareUploadBiosCmd.MarkFlagRequired("revision"))
must(firmwareUploadBiosCmd.RegisterFlagCompletionFunc("vendor", c.comp.FirmwareVendorCompletion))
must(firmwareUploadBiosCmd.RegisterFlagCompletionFunc("board", c.comp.FirmwareBoardCompletion))
firmwareUploadCmd.AddCommand(firmwareUploadBiosCmd)
firmwareUploadBmcCmd.Flags().StringP("vendor", "", "", "the vendor (required)")
firmwareUploadBmcCmd.Flags().StringP("board", "", "", "the board type (required)")
firmwareUploadBmcCmd.Flags().StringP("revision", "", "", "the BMC firmware revision (required)")
must(firmwareUploadBmcCmd.MarkFlagRequired("vendor"))
must(firmwareUploadBmcCmd.MarkFlagRequired("board"))
must(firmwareUploadBmcCmd.MarkFlagRequired("revision"))
must(firmwareUploadBmcCmd.RegisterFlagCompletionFunc("vendor", c.comp.FirmwareVendorCompletion))
must(firmwareUploadBmcCmd.RegisterFlagCompletionFunc("board", c.comp.FirmwareBoardCompletion))
firmwareUploadCmd.AddCommand(firmwareUploadBmcCmd)
firmwareCmd.AddCommand(firmwareUploadCmd)
firmwareRemoveCmd.Flags().StringP("kind", "", "", "the firmware kind [bmc|bios] (required)")
firmwareRemoveCmd.Flags().StringP("vendor", "", "", "the vendor (required)")
firmwareRemoveCmd.Flags().StringP("board", "", "", "the board type (required)")
firmwareRemoveCmd.Flags().StringP("revision", "", "", "the firmware revision (required)")
must(firmwareRemoveCmd.MarkFlagRequired("kind"))
must(firmwareRemoveCmd.MarkFlagRequired("vendor"))
must(firmwareRemoveCmd.MarkFlagRequired("board"))
must(firmwareRemoveCmd.MarkFlagRequired("revision"))
must(firmwareRemoveCmd.RegisterFlagCompletionFunc("kind", c.comp.FirmwareKindCompletion))
must(firmwareRemoveCmd.RegisterFlagCompletionFunc("vendor", c.comp.FirmwareVendorCompletion))
must(firmwareRemoveCmd.RegisterFlagCompletionFunc("board", c.comp.FirmwareBoardCompletion))
must(firmwareRemoveCmd.RegisterFlagCompletionFunc("revision", c.comp.FirmwareRevisionCompletion))
firmwareCmd.AddCommand(firmwareRemoveCmd)
return firmwareCmd
}
func (c *config) firmwareList() error {
var err error
var resp *metalgo.FirmwaresResponse
kind := metalgo.FirmwareKind(viper.GetString("kind"))
id := viper.GetString("machineid")
switch id {
case "":
vendor := viper.GetString("vendor")
board := viper.GetString("board")
resp, err = c.driver.ListFirmwares(kind, vendor, board)
default:
resp, err = c.driver.MachineListFirmwares(kind, id)
}
if err != nil {
return err
}
return output.New().Print(resp.Firmwares)
}
func (c *config) firmwareUploadBios(args []string) error {
return c.manageFirmware(upload, metalgo.Bios, args)
}
func (c *config) firmwareUploadBmc(args []string) error {
return c.manageFirmware(upload, metalgo.Bmc, args)
}
func (c *config) firmwareRemove(args []string) error {
return c.manageFirmware(remove, metalgo.Bios, args)
}
func (c *config) manageFirmware(task firmwareTask, kind metalgo.FirmwareKind, args []string) error {
revision := viper.GetString("revision")
vendor := viper.GetString("vendor")
board := viper.GetString("board")
var err error
switch task {
case upload:
if len(args) < 1 {
return fmt.Errorf("no firmware file given")
}
_, err = c.driver.UploadFirmware(kind, vendor, board, revision, args[0])
case remove:
_, err = c.driver.RemoveFirmware(kind, vendor, board, revision)
}
return err
}