/
template.go
175 lines (156 loc) · 4.93 KB
/
template.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
/*
* Copyright 2018-2023, CS Systemes d'Information, http://csgroup.eu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package commands
import (
"os"
"strings"
"time"
"github.com/schollz/progressbar/v3"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"github.com/CS-SI/SafeScale/v22/lib/client"
clitools "github.com/CS-SI/SafeScale/v22/lib/utils/cli"
"github.com/CS-SI/SafeScale/v22/lib/utils/fail"
"github.com/CS-SI/SafeScale/v22/lib/utils/strprocess"
)
var templateCmdName = "template"
// TemplateCommand command
var TemplateCommand = cli.Command{
Name: "template",
Usage: "template COMMAND",
Subcommands: cli.Commands{
templateList,
templateMatch,
templateInspect,
},
}
var templateList = cli.Command{
Name: "list",
Aliases: []string{"ls"},
Usage: "List available templates",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all",
Usage: "Lists all available templates (ignoring any filter set in tenant file)",
},
cli.BoolFlag{
Name: "scanned-only, S",
Usage: "Display only templates with scanned information",
},
},
Action: func(c *cli.Context) (ferr error) {
defer fail.OnPanic(&ferr)
logrus.Tracef("SafeScale command: %s %s with args '%s'", templateCmdName, c.Command.Name, c.Args())
if beta := os.Getenv("SAFESCALE_BETA"); beta != "" {
description := "Listing templates"
pb := progressbar.NewOptions(-1, progressbar.OptionFullWidth(), progressbar.OptionClearOnFinish(), progressbar.OptionSetDescription(description))
go func() {
for {
if pb.IsFinished() {
return
}
err := pb.Add(1)
if err != nil {
return
}
time.Sleep(100 * time.Millisecond)
}
}()
defer func() {
_ = pb.Finish()
}()
}
templates, err := ClientSession.Template.List(c.Bool("all"), c.Bool("scanned-only"), 0)
if err != nil {
err = fail.FromGRPCStatus(err)
return clitools.FailureResponse(clitools.ExitOnRPC(strprocess.Capitalize(client.DecorateTimeoutError(err, "list of templates", false).Error())))
}
return clitools.SuccessResponse(templates.GetTemplates())
},
}
var templateMatch = cli.Command{
Name: "match",
Usage: "List templates that match the SIZING",
ArgsUsage: "SIZING",
Action: func(c *cli.Context) (ferr error) {
defer fail.OnPanic(&ferr)
logrus.Tracef("SafeScale command: %s %s with args '%s'", templateCmdName, c.Command.Name, c.Args())
var sizing []string
sizing = append(sizing, c.Args().First())
sizing = append(sizing, c.Args().Tail()...)
sizingAsString := strings.Join(sizing, ",")
if beta := os.Getenv("SAFESCALE_BETA"); beta != "" {
description := "Filtering templates"
pb := progressbar.NewOptions(-1, progressbar.OptionFullWidth(), progressbar.OptionClearOnFinish(), progressbar.OptionSetDescription(description))
go func() {
for {
if pb.IsFinished() {
return
}
err := pb.Add(1)
if err != nil {
return
}
time.Sleep(100 * time.Millisecond)
}
}()
defer func() {
_ = pb.Finish()
}()
}
templates, err := ClientSession.Template.Match(sizingAsString, 0)
if err != nil {
err = fail.FromGRPCStatus(err)
return clitools.FailureResponse(clitools.ExitOnRPC(strprocess.Capitalize(client.DecorateTimeoutError(err, "list of templates", false).Error())))
}
return clitools.SuccessResponse(templates.GetTemplates())
},
}
var templateInspect = cli.Command{
Name: "inspect",
Aliases: []string{"show"},
Usage: "Display available template information",
ArgsUsage: "NAME",
Action: func(c *cli.Context) (ferr error) {
defer fail.OnPanic(&ferr)
logrus.Tracef("SafeScale command: %s %s with args '%s'", templateCmdName, c.Command.Name, c.Args())
if beta := os.Getenv("SAFESCALE_BETA"); beta != "" {
description := "Inspecting templates"
pb := progressbar.NewOptions(-1, progressbar.OptionFullWidth(), progressbar.OptionClearOnFinish(), progressbar.OptionSetDescription(description))
go func() {
for {
if pb.IsFinished() {
return
}
err := pb.Add(1)
if err != nil {
return
}
time.Sleep(100 * time.Millisecond)
}
}()
defer func() {
_ = pb.Finish()
}()
}
template, err := ClientSession.Template.Inspect(c.Args().First(), 0)
if err != nil {
err = fail.FromGRPCStatus(err)
return clitools.FailureResponse(clitools.ExitOnRPC(strprocess.Capitalize(client.DecorateTimeoutError(err, "list of template information", false).Error())))
}
return clitools.SuccessResponse(template)
},
}