forked from rancher/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatalog.go
311 lines (256 loc) · 6.59 KB
/
catalog.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package cmd
import (
"strings"
"time"
"github.com/pkg/errors"
managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
addCatalogDescription = `
Add a new catalog to the Rancher server
Example:
# Add a catalog
$ rancher catalog add foo https://my.catalog
# Add a catalog and specify the branch to use
$ rancher catalog add --branch awesomebranch foo https://my.catalog
# Add a catalog and specify the helm version to use. Specify 'v2' for helm 2 and 'v3' for helm 3
$ rancher catalog add --helm-version v3 foo https://my.catalog
`
refreshCatalogDescription = `
Refresh a catalog on the Rancher server
Example:
# Refresh a catalog
$ rancher catalog refresh foo
# Refresh multiple catalogs
$ rancher catalog refresh foo bar baz
# Refresh all catalogs
$ rancher catalog refresh --all
# Refresh is asynchronous unless you specify '--wait'
$ rancher catalog refresh --all --wait --wait-timeout=60
# Default wait timeout is 60 seconds, set to 0 to remove the timeout
$ rancher catalog refresh --all --wait --wait-timeout=0
`
)
type CatalogData struct {
ID string
Catalog managementClient.Catalog
}
func CatalogCommand() cli.Command {
catalogLsFlags := []cli.Flag{
formatFlag,
quietFlag,
cli.BoolFlag{
Name: "verbose,v",
Usage: "Include the catalog's state",
},
}
return cli.Command{
Name: "catalog",
Usage: "Operations with catalogs",
Action: defaultAction(catalogLs),
Flags: catalogLsFlags,
Subcommands: []cli.Command{
{
Name: "ls",
Usage: "List catalogs",
Description: "\nList all catalogs in the current Rancher server",
ArgsUsage: "None",
Action: catalogLs,
Flags: catalogLsFlags,
},
{
Name: "add",
Usage: "Add a catalog",
Description: addCatalogDescription,
ArgsUsage: "[NAME, URL]",
Action: catalogAdd,
Flags: []cli.Flag{
cli.StringFlag{
Name: "branch",
Usage: "Branch from the url to use",
Value: "master",
},
cli.StringFlag{
Name: "helm-version",
Usage: "Version of helm the app(s) in your catalog will use for deployment. Use 'v2' for helm 2 or 'v3' for helm 3",
Value: "v2",
},
},
},
{
Name: "delete",
Usage: "Delete a catalog",
Description: "\nDelete a catalog from the Rancher server",
ArgsUsage: "[CATALOG_NAME/CATALOG_ID]",
Action: catalogDelete,
},
{
Name: "refresh",
Usage: "Refresh catalog templates",
Description: refreshCatalogDescription,
ArgsUsage: "[CATALOG_NAME/CATALOG_ID]...",
Action: catalogRefresh,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all",
Usage: "Refresh all catalogs",
},
cli.BoolFlag{
Name: "wait,w",
Usage: "Wait for catalog(s) to become active",
},
cli.IntFlag{
Name: "wait-timeout",
Usage: "Wait timeout duration in seconds",
Value: 60,
},
},
},
},
}
}
func catalogLs(ctx *cli.Context) error {
c, err := GetClient(ctx)
if err != nil {
return err
}
collection, err := c.ManagementClient.Catalog.List(defaultListOpts(ctx))
if err != nil {
return err
}
fields := [][]string{
{"ID", "ID"},
{"NAME", "Catalog.Name"},
{"URL", "Catalog.URL"},
{"BRANCH", "Catalog.Branch"},
{"KIND", "Catalog.Kind"},
{"HELMVERSION", "Catalog.HelmVersion"},
}
if ctx.Bool("verbose") {
fields = append(fields, []string{"STATE", "Catalog.State"})
}
writer := NewTableWriter(fields, ctx)
defer writer.Close()
for _, item := range collection.Data {
writer.Write(&CatalogData{
ID: item.ID,
Catalog: item,
})
}
return writer.Err()
}
func catalogAdd(ctx *cli.Context) error {
if len(ctx.Args()) < 2 {
return cli.ShowSubcommandHelp(ctx)
}
c, err := GetClient(ctx)
if err != nil {
return err
}
catalog := &managementClient.Catalog{
Branch: ctx.String("branch"),
Name: ctx.Args().First(),
Kind: "helm",
URL: ctx.Args().Get(1),
HelmVersion: strings.ToLower(ctx.String("helm-version")),
}
_, err = c.ManagementClient.Catalog.Create(catalog)
if err != nil {
return err
}
return nil
}
func catalogDelete(ctx *cli.Context) error {
if len(ctx.Args()) < 1 {
return cli.ShowSubcommandHelp(ctx)
}
c, err := GetClient(ctx)
if err != nil {
return err
}
for _, arg := range ctx.Args() {
resource, err := Lookup(c, arg, "catalog")
if err != nil {
return err
}
catalog, err := c.ManagementClient.Catalog.ByID(resource.ID)
if err != nil {
return err
}
err = c.ManagementClient.Catalog.Delete(catalog)
if err != nil {
return err
}
}
return nil
}
func catalogRefresh(ctx *cli.Context) error {
if len(ctx.Args()) < 1 && !ctx.Bool("all") {
return cli.ShowSubcommandHelp(ctx)
}
c, err := GetClient(ctx)
if err != nil {
return err
}
var catalogs []managementClient.Catalog
if ctx.Bool("all") {
opts := baseListOpts()
collection, err := c.ManagementClient.Catalog.List(opts)
if err != nil {
return err
}
// save the catalogs in case we need to wait for them to become active
catalogs = collection.Data
_, err = c.ManagementClient.Catalog.CollectionActionRefresh(collection)
if err != nil {
return err
}
} else {
for _, arg := range ctx.Args() {
resource, err := Lookup(c, arg, "catalog")
if err != nil {
return err
}
catalog, err := c.ManagementClient.Catalog.ByID(resource.ID)
if err != nil {
return err
}
// collect the refreshing catalogs in case we need to wait for them later
catalogs = append(catalogs, *catalog)
_, err = c.ManagementClient.Catalog.ActionRefresh(catalog)
if err != nil {
return err
}
}
}
if ctx.Bool("wait") {
timeout := time.Duration(ctx.Int("wait-timeout")) * time.Second
start := time.Now()
logrus.Debugf("catalog: waiting for catalogs to become active (timeout=%v)", timeout)
for _, catalog := range catalogs {
logrus.Debugf("catalog: waiting for %s to become active", catalog.Name)
resource, err := Lookup(c, catalog.Name, "catalog")
if err != nil {
return err
}
catalog, err := c.ManagementClient.Catalog.ByID(resource.ID)
if err != nil {
return err
}
for catalog.State != "active" {
time.Sleep(time.Second)
catalog, err = c.ManagementClient.Catalog.ByID(resource.ID)
if err != nil {
return err
}
if timeout > 0 && time.Since(start) > timeout {
return errors.New("catalog: timed out waiting for refresh")
}
}
}
logrus.Debugf("catalog: waited for %v", time.Since(start))
}
return nil
}