Class: Command::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/command/base.rb
Overview
rubocop:disable Metrics/ClassLength
Direct Known Subclasses
ApplyTemplate, BuildImage, CleanupOldImages, CleanupStaleApps, Config, CopyImageFromUpstream, Delete, DeployImage, Env, Exists, Info, LatestImage, Logs, Maintenance, MaintenanceOff, MaintenanceOn, MaintenanceSetPage, NoCommand, Open, PromoteAppFromUpstream, Ps, PsRestart, PsStart, PsStop, Run, RunCleanup, RunDetached, SetupApp, Test, Version
Constant Summary
collapse
- USAGE =
Used to call the command (‘cpl NAME`) NAME = “” Displayed when running `cpl help` or `cpl help NAME` (defaults to `NAME`)
""
- REQUIRES_ARGS =
Throws error if ‘true` and no arguments are passed to the command or if `false` and arguments are passed to the command
false
- DEFAULT_ARGS =
Default arguments if none are passed to the command
[].freeze
- OPTIONS =
Options for the command (use option methods below)
[].freeze
- EXAMPLES =
Displayed when running ‘cpl help` DESCRIPTION = “” Displayed when running `cpl help NAME` LONG_DESCRIPTION = “” Displayed along with `LONG_DESCRIPTION` when running `cpl help NAME`
""
- HIDE =
If ‘true`, hides the command from `cpl help`
false
- NO_IMAGE_AVAILABLE =
"NO_IMAGE_AVAILABLE"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
29
30
31
|
# File 'lib/command/base.rb', line 29
def initialize(config)
@config = config
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
5
6
7
|
# File 'lib/command/base.rb', line 5
def config
@config
end
|
Class Method Details
.all_commands ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/command/base.rb', line 33
def self.all_commands
Dir["#{__dir__}/*.rb"].each_with_object({}) do |file, result|
filename = File.basename(file, ".rb")
classname = File.read(file).match(/^\s+class (\w+) < Base($| .*$)/)&.captures&.first
result[filename.to_sym] = Object.const_get("::Command::#{classname}") if classname
end
end
|
.all_options ⇒ Object
179
180
181
|
# File 'lib/command/base.rb', line 179
def self.all_options
methods.grep(/_option$/).map { |method| send(method.to_s) }
end
|
.all_options_by_key_name ⇒ Object
183
184
185
186
187
188
|
# File 'lib/command/base.rb', line 183
def self.all_options_by_key_name
all_options.each_with_object({}) do |option, result|
option[:params][:aliases]&.each { |current_alias| result[current_alias.to_s] = option }
result["--#{option[:name]}"] = option
end
end
|
.app_option(required: false) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/command/base.rb', line 54
def self.app_option(required: false)
{
name: :app,
params: {
aliases: ["-a"],
banner: "APP_NAME",
desc: "Application name",
type: :string,
required: required
}
}
end
|
.commit_option(required: false) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/command/base.rb', line 93
def self.commit_option(required: false)
{
name: :commit,
params: {
aliases: ["-c"],
banner: "COMMIT_HASH",
desc: "Commit hash",
type: :string,
required: required
}
}
end
|
.image_option(required: false) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/command/base.rb', line 80
def self.image_option(required: false)
{
name: :image,
params: {
aliases: ["-i"],
banner: "IMAGE_NAME",
desc: "Image name",
type: :string,
required: required
}
}
end
|
.org_option(required: false) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/command/base.rb', line 41
def self.org_option(required: false)
{
name: :org,
params: {
aliases: ["-o"],
banner: "ORG_NAME",
desc: "Organization name",
type: :string,
required: required
}
}
end
|
.skip_confirm_option(required: false) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/command/base.rb', line 119
def self.skip_confirm_option(required: false)
{
name: :yes,
params: {
aliases: ["-y"],
banner: "SKIP_CONFIRM",
desc: "Skip confirmation",
type: :boolean,
required: required
}
}
end
|
.terminal_size_option(required: false) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/command/base.rb', line 156
def self.terminal_size_option(required: false)
{
name: :terminal_size,
params: {
banner: "ROWS,COLS",
desc: "Override remote terminal size (e.g. `--terminal-size 10,20`)",
type: :string,
required: required
}
}
end
|
.upstream_token_option(required: false) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/command/base.rb', line 106
def self.upstream_token_option(required: false)
{
name: :upstream_token,
params: {
aliases: ["-t"],
banner: "UPSTREAM_TOKEN",
desc: "Upstream token",
type: :string,
required: required
}
}
end
|
.use_local_token_option(required: false) ⇒ Object
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/command/base.rb', line 145
def self.use_local_token_option(required: false)
{
name: :use_local_token,
params: {
desc: "Override remote CPLN_TOKEN with local token",
type: :boolean,
required: required
}
}
end
|
.version_option(required: false) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/command/base.rb', line 132
def self.version_option(required: false)
{
name: :version,
params: {
aliases: ["-v"],
banner: "VERSION",
desc: "Displays the current version of the CLI",
type: :boolean,
required: required
}
}
end
|
.wait_option(title = "", required: false) ⇒ Object
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/command/base.rb', line 168
def self.wait_option(title = "", required: false)
{
name: :wait,
params: {
desc: "Waits for #{title}",
type: :boolean,
required: required
}
}
end
|
.workload_option(required: false) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/command/base.rb', line 67
def self.workload_option(required: false)
{
name: :workload,
params: {
aliases: ["-w"],
banner: "WORKLOAD_NAME",
desc: "Workload name",
type: :string,
required: required
}
}
end
|
Instance Method Details
#args_join(args) ⇒ Object
NOTE: use simplified variant atm, as shelljoin do different escaping TODO: most probably need better logic for escaping various quotes
242
243
244
|
# File 'lib/command/base.rb', line 242
def args_join(args)
args.join(" ")
end
|
#cp ⇒ Object
291
292
293
|
# File 'lib/command/base.rb', line 291
def cp
@cp ||= Controlplane.new(config)
end
|
#ensure_workload_deleted(workload) ⇒ Object
202
203
204
205
206
|
# File 'lib/command/base.rb', line 202
def ensure_workload_deleted(workload)
step("Deleting workload") do
cp.delete_workload(workload)
end
end
|
#latest_image(app = config.app, org = config.org) ⇒ Object
220
221
222
223
224
225
226
227
|
# File 'lib/command/base.rb', line 220
def latest_image(app = config.app, org = config.org)
@latest_image ||= {}
@latest_image[app] ||=
begin
items = cp.image_query(app, org)["items"]
latest_image_from(items, app_name: app)
end
end
|
#latest_image_from(items, app_name: config.app, name_only: true) ⇒ Object
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/command/base.rb', line 208
def latest_image_from(items, app_name: config.app, name_only: true)
matching_items = items.filter { |item| item["name"].start_with?("#{app_name}:") }
if matching_items.empty?
name_only ? "#{app_name}:#{NO_IMAGE_AVAILABLE}" : nil
else
latest_item = matching_items.max_by { |item| (item["name"]) }
name_only ? latest_item["name"] : latest_item
end
end
|
#latest_image_next(app = config.app, org = config.org) ⇒ Object
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/command/base.rb', line 229
def latest_image_next(app = config.app, org = config.org)
@latest_image_next ||= {}
@latest_image_next[app] ||= begin
latest_image_name = latest_image(app, org)
image = latest_image_name.split(":").first
image += ":#{extract_image_number(latest_image_name) + 1}"
image += "_#{config.options[:commit]}" if config.options[:commit]
image
end
end
|
295
296
297
|
# File 'lib/command/base.rb', line 295
def perform(cmd)
system(cmd) || exit(false)
end
|
#progress ⇒ Object
246
247
248
|
# File 'lib/command/base.rb', line 246
def progress
$stderr
end
|
#step(message, abort_on_error: true, retry_on_failure: false) ⇒ Object
rubocop:disable Metrics/MethodLength
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/command/base.rb', line 268
def step(message, abort_on_error: true, retry_on_failure: false)
progress.print("#{message}...")
Shell.use_tmp_stderr do
success = false
begin
if retry_on_failure
until (success = yield)
progress.print(".")
sleep 1
end
else
success = yield
end
rescue RuntimeError => e
step_error(e, abort_on_error: abort_on_error)
end
step_finish(success)
end
end
|
#step_error(error, abort_on_error: true) ⇒ Object
250
251
252
253
254
255
256
257
258
|
# File 'lib/command/base.rb', line 250
def step_error(error, abort_on_error: true)
message = error.message
if abort_on_error
progress.puts(" #{Shell.color('failed!', :red)}\n\n")
Shell.abort(message)
else
Shell.write_to_tmp_stderr(message)
end
end
|
#step_finish(success) ⇒ Object
260
261
262
263
264
265
266
|
# File 'lib/command/base.rb', line 260
def step_finish(success)
if success
progress.puts(" #{Shell.color('done!', :green)}")
else
progress.puts(" #{Shell.color('failed!', :red)}\n\n#{Shell.read_from_tmp_stderr}\n\n")
end
end
|
#wait_for_replica(workload, location) ⇒ Object
196
197
198
199
200
|
# File 'lib/command/base.rb', line 196
def wait_for_replica(workload, location)
step("Waiting for replica", retry_on_failure: true) do
cp.workload_get_replicas_safely(workload, location: location)&.dig("items", 0)
end
end
|
#wait_for_workload(workload) ⇒ Object
190
191
192
193
194
|
# File 'lib/command/base.rb', line 190
def wait_for_workload(workload)
step("Waiting for workload", retry_on_failure: true) do
cp.fetch_workload(workload)
end
end
|