Module: NexClient::Commands::Addons

Extended by:
Helpers
Defined in:
lib/nex_client/commands/addons.rb

Constant Summary collapse

SCALING_DIRECTIONS =
[:up,:down]
ADDONS_TITLE =
"Addons".colorize(:red)
ADDONS_HEADERS =
['name','status','service','size','nodes','app'].map(&:upcase)
OPTS_TITLE =
"Options".colorize(:blue)
OPTS_HEADERS =
['key','value'].map(&:upcase)

Constants included from Helpers

Helpers::LOG_COLORS, Helpers::VARS_HEADERS, Helpers::VARS_TITLE

Class Method Summary collapse

Methods included from Helpers

display_events, display_logs, display_raw_vars, display_record_errors, display_vars, display_yaml_vars, error, events, format_cmd, hash_from_file, perform_ssh_cmd, success, vars_from_file, vars_from_plain_file, vars_from_yaml_file, with_cert_identity

Class Method Details

.create(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/addons.rb', line 93

def self.create(args,opts)
  svc_name,app_name = args
  app = NexClient::App.find(name: app_name).first

  # Display error
  unless app
    error("Error! Could not find app: #{app_name}")
    return false
  end

  # Attributes
  attrs = {}
  attrs[:service] = svc_name
  attrs[:container_size] = opts.size if opts.size.present?

  # Additional options (log drain, region balancing)
  attrs[:opts] = self.extract_config_options(opts)

  addon = NexClient::Addon.new(attrs)
  addon.relationships.attributes = { app: { data: { type: 'apps', id: app.id } } }
  addon.save

  # Display errors if any
  if addon.errors.any?
    display_record_errors(addon)
    return false
  end

  # Display app
  self.display_addons(NexClient::Addon.includes(:app).find(addon.id).first)
end

.destroy(args, opts) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/nex_client/commands/addons.rb', line 150

def self.destroy(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the name of this addon to confirm: ")
  unless answer == e.name
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed addon: #{name}")
end

.display_addons(list) ⇒ Object



222
223
224
225
226
227
228
229
230
# File 'lib/nex_client/commands/addons.rb', line 222

def self.display_addons(list)
  table = Terminal::Table.new title: ADDONS_TITLE, headings: ADDONS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end

.display_id(e) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/nex_client/commands/addons.rb', line 232

def self.display_id(e)
  table = Terminal::Table.new do |t|
    t.add_row(['ADDON ID',e.id])
  end
  puts table
  puts "\n"
end

.display_options(list) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/nex_client/commands/addons.rb', line 240

def self.display_options(list)
  table = Terminal::Table.new title: OPTS_TITLE, headings: OPTS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      e.each { |k,v| t.add_row([k,v]) }
    end
  end
  puts table
  puts "\n"
end

.extract_config_options(cli_opts) ⇒ Object



273
274
275
276
277
278
279
280
281
# File 'lib/nex_client/commands/addons.rb', line 273

def self.extract_config_options(cli_opts)
  hash = {}
  %w(http_log_drain region_balancing).each do |opt_name|
    if cli_opts.method_missing(opt_name.to_sym).present?
      hash[opt_name.to_s] = cli_opts.method_missing(opt_name.to_sym)
    end
  end
  return hash
end

.format_app(record) ⇒ Object



263
264
265
266
# File 'lib/nex_client/commands/addons.rb', line 263

def self.format_app(record)
  return '-' unless a = record.app
  a.name
end

.format_node_count(record) ⇒ Object



268
269
270
271
# File 'lib/nex_client/commands/addons.rb', line 268

def self.format_node_count(record)
  return '-' unless record.node_count && record.max_node_count
  "#{record.node_count}/#{record.max_node_count}"
end

.format_record(record) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/nex_client/commands/addons.rb', line 250

def self.format_record(record)
  app = self.format_app(record)
  node_count = self.format_node_count(record)
  [
    record.name,
    record.status,
    record.service,
    record.container_size,
    node_count,
    app
  ]
end

.info(args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/addons.rb', line 37

def self.info(args,opts)
  name = args.first
  e = NexClient::Addon.includes(:app).find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Display app id
  self.display_id(e)

  # Display addon details
  self.display_addons(e)

  # Display all vars
  self.display_vars(e.all_vars,!opts.full_vars)

  # Display options
  self.display_options(e.opts) if e.respond_to?(:opts)

  # Display App
  Apps.display_apps(e.app)
end

.list(args, opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/nex_client/commands/addons.rb', line 14

def self.list(args,opts)
  filters = {}
  filters[:status] = opts.status || ['active','restarting']
  filters[:service] = opts.service if opts.service.present?

  # All option
  filters = {} if opts.all

  # Scope to specific app
  filters[:'app.name'] = args.first if args.first.present?

  # Create table
  list = NexClient::Addon.includes(:app).where(filters).order('status')
  self.display_addons(list)

  # Loop through results
  while (list.pages.links||{})['next']
    return true if ask("Press enter for next page ('q' to quit)") =~ /q/
    list = list.pages.next
    self.display_addons(list)
  end
end

.logs(args, opts) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nex_client/commands/addons.rb', line 63

def self.logs(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Retrieve logs and display them
  logs = e.logs(tail: opts.tail).first
  self.display_logs(logs.log_ret)
end

.restart(args, opts) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/nex_client/commands/addons.rb', line 200

def self.restart(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Perform
  e.restart

  # Display errors if any
  if e.errors.any?
    display_record_errors(e)
    return false
  end

  success("Initiated phased restart for addon: #{name}...")
end

.scale(direction, args, opts) ⇒ Object



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
# File 'lib/nex_client/commands/addons.rb', line 171

def self.scale(direction,args,opts)
  return false unless SCALING_DIRECTIONS.include?(direction.to_sym)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Scaling attributes
  attrs = {}
  count = opts.count || 1
  attrs[:count] = count
  attrs[:preferred_region] = opts.region if opts.region.present?

  # Perform
  e.send("scale_#{direction}",{data: { attributes: attrs } })

  # Display errors if any
  if e.errors.any?
    display_record_errors(e)
    return false
  end

  success("Successfully requested to scale #{name} #{direction} by #{count} #{'node'.pluralize(count)}...")
end

.ssh(args, opts) ⇒ Object

SSH to the addon



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nex_client/commands/addons.rb', line 79

def self.ssh(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Perform command
  perform_ssh_cmd(e.ssh_cmd_template)
end

.update(args, opts) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nex_client/commands/addons.rb', line 125

def self.update(args,opts)
  name = args.first
  e = NexClient::Addon.find(name: name).first

  # Display error
  unless e
    error("Error! Could not find addon: #{name}")
    return false
  end

  # Attributes
  attrs = {}
  attrs[:container_size] = opts.size if opts.size.present?

  # Additional options (log drain, region balancing)
  # Hash#dup is required for the resource to detect changes
  attrs[:opts] = (e.opts || {}).dup.merge(self.extract_config_options(opts))

  # Update
  e.update_attributes(attrs)

  # Display app
  self.display_addons(NexClient::Addon.includes(:app).find(e.id).first)
end