Module: NexClient::Commands::Apps

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

Constant Summary collapse

SCALING_DIRECTIONS =
[:up,:down]
APPS_TITLE =
"App Details".colorize(:green)
APPS_HEADERS =
['id','name','status','image','ssl','storage','preferred region','nodes','owner'].map(&:upcase)
VARS_TITLE =
"Environment Variables".colorize(:blue)
VARS_HEADERS =
['key','value'].map(&:upcase)
SCM_TITLE =
"Source Control Management".colorize(:yellow)
SCM_HEADERS =
['provider','repo','branch','last commit'].map(&:upcase)

Constants included from Helpers

Helpers::LOG_COLORS

Class Method Summary collapse

Methods included from Helpers

display_logs, display_record_errors, error, success

Class Method Details

.create(args, opts) ⇒ Object



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

def self.create(args,opts)
  image_info = args.first.split(':')
  attrs = { image: image_info[0], image_tag: image_info[1] }

  # Meta attributes
  attrs[:ssl_enabled] = !opts.no_ssl if opts.no_ssl.present?
  attrs[:persistent_storage] = opts.storage if opts.storage.present?

  # Env variables via command line
  if opts.env.present?
    attrs[:vars] ||= {}
    opts.env.each do |e|
      key,val = e.split('=',2)
      attrs[:vars][key] = val
    end
  end

  # Env variables from file
  if opts.env_file.present?
    f = File.read(opts.env_file)
    attrs[:vars] ||= {}
    f.split('\n').each do |e|
      key,val = e.split('=',2)
      attrs[:vars][key] = val
    end
  end

  # Create the app
  e = NexClient::App.new(attrs)

  # Add owner if specified
  if opts.owner.present?
    o = NexClient::Organization.find(handle:opts.owner).first
    unless o
      error("Error! Could not find organization: #{opts.owner}")
      return false
    end
    e.relationships.attributes = { owner: { data: { type: 'organizations', id: o.id } } }
  end

  # Save the resource
  e.save

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

  # Display app
  self.display_apps(NexClient::App.includes(:owner).find(e.id).first)
end

.destroy(args, opts) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/nex_client/commands/apps.rb', line 131

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

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

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

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

.display_apps(list) ⇒ Object



266
267
268
269
270
271
272
273
274
# File 'lib/nex_client/commands/apps.rb', line 266

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

.display_scm(list) ⇒ Object



286
287
288
289
290
291
292
293
294
# File 'lib/nex_client/commands/apps.rb', line 286

def self.display_scm(list)
  table = Terminal::Table.new title: SCM_TITLE, headings: SCM_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row([e['provider'],e['repo'],e['branch'],e['last_commit'] || '- nothing pushed yet -'])
    end
  end
  puts table
  puts "\n"
end

.display_vars(list) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/nex_client/commands/apps.rb', line 276

def self.display_vars(list)
  table = Terminal::Table.new title: VARS_TITLE, headings: VARS_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

.format_node_count(record) ⇒ Object



317
318
319
320
# File 'lib/nex_client/commands/apps.rb', line 317

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

.format_owner(record) ⇒ Object



312
313
314
315
# File 'lib/nex_client/commands/apps.rb', line 312

def self.format_owner(record)
  return '-' unless o = record.owner
  "#{o.type[0]}:#{o.handle}"
end

.format_record(record) ⇒ Object



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/nex_client/commands/apps.rb', line 296

def self.format_record(record)
  owner = self.format_owner(record)
  node_count = self.format_node_count(record)
  [
    record.id,
    record.name,
    record.status,
    record.image,
    record.ssl_enabled,
    record.persistent_storage,
    record.preferred_region,
    node_count,
    owner
  ]
end

.info(args, opts) ⇒ Object



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/apps.rb', line 40

def self.info(args,opts)
  name = args.first
  e = NexClient::App.includes(:addons,:owner).find(name: name).first

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

  # Display app details
  self.display_apps(e)

  # Display all vars
  self.display_vars(e.all_vars)

  # Display all vars
  self.display_scm(e.scm)

  # Display all addons
  Addons.display_addons(e.addons.select { |a| a.status == 'active'})
end

.list(args, opts) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nex_client/commands/apps.rb', line 18

def self.list(args,opts)
  filters = {}
  filters[:status] = [opts.status || 'active']
  filters[:ssl_enabled] = opts.ssl if opts.ssl.present?
  filters[:persistent_storage] = opts.storage if opts.storage.present?
  filters[:'owner.handle'] = opts.owner || '@self'

  # All option
  filters = {} if opts.all

  # Display
  list = NexClient::App.includes(:owner).where(filters).order('status')
  self.display_apps(list)

  # Loop through results
  while (list.pages.links||{})['next']
    ask("Press enter for next page")
    list = list.pages.next
    self.display_apps(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/apps.rb', line 63

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

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

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

.manage_scm(args, opts) ⇒ Object



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

def self.manage_scm(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

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

  # Link SCM
  if opts.link
    provider,repo,branch = opts.link.split(':')
    attrs = { provider: provider, config: { repo: repo, branch: branch } }

    # Update and reload the resource
    e.link_scm({data: { attributes: attrs } })
    e = NexClient::App.find(e.id).first
  end

  # Unlink SCM
  if !opts.link && opts.unlink
    # Update and reload the resource
    e.unlink_scm
    e = NexClient::App.find(e.id).first
  end

  # Display all vars
  self.display_scm(e.scm)
end

.manage_vars(args, opts) ⇒ Object



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

def self.manage_vars(args,opts)
  name = args.first
  e = NexClient::App.find(name: name).first

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

  # Add/Delete vars
  if opts.add.present? || opts.delete.present?
    vars = (e.vars || {}).dup

    # Add vars
    (opts.add || []).each do |v|
      key,val = v.split('=',2)
      vars[key] = val
    end

    # Delete vars
    (opts.delete || []).each { |k| vars.delete(k) }

    # Update and reload the resource
    e.update_attributes(vars: vars)
    e = NexClient::App.find(e.id).first
  end

  # Display all vars
  self.display_vars(e.all_vars)
end

.restart(args, opts) ⇒ Object



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

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

  # Display error
  unless e
    error("Error! Could not find app: #{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 app: #{name}...")
end

.scale(direction, args, opts) ⇒ Object



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

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

  # Display error
  unless e
    error("Error! Could not find app: #{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