Class: PI::Cli::Command::Apps

Inherits:
Base show all
Includes:
Interactive, PI::Cli::ChooseHelper
Defined in:
lib/cli/commands/apps.rb

Constant Summary collapse

DEFAULTS =
{
  "mem"         => "64M",
  "instances"   => 1,
  "needRestart" => false,
  "isDebug"     => false,
  "needMonitor" => false
}
YES_SET =
Set.new(["y", "Y", "yes", "YES"])

Instance Method Summary collapse

Methods included from PI::Cli::ChooseHelper

#check_status, #choose_app, #choose_project

Methods inherited from Base

#auth_token, #client, #initialize, #target_url

Constructor Details

This class inherits a constructor from PI::Cli::Command::Base

Instance Method Details

#app_env(appname = nil) ⇒ Object



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/cli/commands/apps.rb', line 445

def app_env(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  app  = client.app_get_byname(appname)
  err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)      
  env = client.app_env(appname)
  return display JSON.pretty_generate(env) if @options[:json]
  return display "No Environments!" if env.nil? || env.empty?
  env.sort! {|a, b| a[:name] <=> b[:name] }
  env_table = table do |t|
    t.headings = 'Name', 'Value'
    env.each do |e|
      t << [e[:name], e[:value]]
    end
  end
  display env_table
end

#app_log(appid = nil) ⇒ Object



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/cli/commands/apps.rb', line 517

def app_log(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end
  app  = client.app_get_byid(appid)
  err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)      
  max_index = app[:instances]-1
  instanceindex = ask "Select instance index(from 0 to #{max_index})", :default => 0
  filepath = "logs/"
  logs = client.app_log(app[:id],filepath,instanceindex)
  logs = logs[1].to_a
  file = ask "Select logs, indexed list?", :choices => logs, :indexed => true
  display "Selected logs: ",false
  file = file.split(' ')
  file = file[0]
  display "#{file}" 
  filepath = "logs/#{file}" 
  logs = client.app_log(app[:id],filepath,instanceindex)
  display "\n"
  display logs[1]
end

#apps(projectid = nil) ⇒ Object



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

def apps(projectid=nil)
  unless projectid
    useproject = choose_project
    projectid = useproject[:id]
  end      
  app_sum = client.app_sum(projectid)
  total_app = Integer(app_sum[1])
  return display "No Applications" if total_app == 0
  if total_app >= 50
    puts "Total applications: #{total_app}."  
    numPerPage = nil     
    loop{
    numPerPage = ask "Number per page"
    if not numPerPage =~ /^[1-9]\d*$/
      display "Invalid Number! Please input number from 1 to #{total_app}" 
      numPerPage =nil
      next
    else
      break
    end
   } 
   numPerPage = numPerPage.to_i
   maxpage = (total_app.to_f/numPerPage).ceil
   loop{
    pageNum = ask "Page number"
    if not pageNum =~ /^[1-9]\d*$/
      display "Invalid Number! Please input number from 1 to #{maxpage}" 
      pageNum =nil
      next
    else
      break
    end
   } 
   pageNum = pageNum.to_i
  else
    pageNum = -1
    numPerPage = -1
  end     
  queryParam = {
    :pageNum => pageNum,
    :NumPerPage => numPerPage
    }   
  apps = client.apps(projectid, queryParam)         
  return display JSON.pretty_generate(apps) if @options[:json]
  return display "No Applications" if apps.nil? || apps.empty?
  apps.sort! {|a, b| a[:name] <=> b[:name] }
  apps_table = table do |t|
    t.headings = 'Target', 'App name', 'URL', 'Deploy Type','Version', 'Status', 'Instances', 'Running Instances'
    apps.each do |app|
      t << [app[:targetName], app[:name] ,app[:url], app[:deployType], app[:tag], app[:status], app[:instances], app[:runInstances]]
    end
  end
  display apps_table
end

#create_app(appname = nil) ⇒ Object



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

def create_app(appname=nil)
  projectid, mem, tag, binarylist, branch, deployType, instances = nil, nil, nil, nil, nil, nil, nil
  loop{        
    appname = ask "Application Name" unless appname
    if app_exists_byappname?(appname)
      display "Application '#{appname}' already exists." 
      appname =nil 
      next
    else
      break
    end
  }  

  # choose the project
  useproject = choose_project
  projectid = useproject[:id]

  # choose the tag
  if useproject[:runtime] =~ /^(ruby)/i
    tag = client.project_tags(useproject[:name])
    err "No tags!" if tag.nil? || tag.empty?
    if tag.count == 1
      tag = tag[0]
      display "Tag: #{tag}"
      deployType = "git"
    else
      choose do |menu|
        display "=============Tags==============="
        menu.prompt = "Select Tag: "
        menu.select_by = :index_or_name
        tag.each do |t|
          menu.choice(t) { tag = t }
        end
      end
      display "Selected Version: ",false
      display "#{tag}"
      deployType = "git"
    end
  else
    binarylist = client.project_binary(projectid)
    err "No version!" if binarylist.nil? || binarylist.empty?
    choose do |menu|
      display "=============Versions============"
      menu.prompt = "Select Version: "
      menu.select_by = :index_or_name
      binarylist.each do |version|
        menu.choice("#{version[:versionName]}") { tag = version }
      end
    end
    display "Selected Version: ",false
    display "#{tag[:versionName]}"
    tag = tag[:versionName]
    deployType = "binary"
  end

  # choose the target
  targets = client.targets  
  err "No Targets!" if targets.nil? || targets.empty?
  usetarget = nil
  choose do |menu|
     display "=============Targets============"
     menu.prompt = "Select Target: "
     menu.select_by = :index_or_name
     targets.each do |target|
       menu.choice("#{target[:name]}-#{target[:targetUrl]}") { usetarget = target }
     end
  end
  display "Selected Target: ",false
  display "#{usetarget[:name]}-#{usetarget[:targetUrl]}"
  target_memory = client.target_memory(usetarget[:name])
  target_memory = target_memory[1]
  display ("Available memory: #{target_memory} M")
  # URL combination
  user = client.
  url = "#{appname}.#{user[:userName]}.#{usetarget[:name]}.samsungpaas.com"
  display "URL: #{url}"  
  # choose the mem
  mem = ask "Memory reservation", :default => DEFAULTS["mem"], :choices => ["64M", "128M", "256M", "512M", "1G", "2G"]
  # Set to MB number
  mem_quota = mem_choice_to_quota(mem)
  
  loop{
    instances = ask "How many instances?", :default => DEFAULTS["instances"]
    instances = instances.to_s
    if not instances =~ /^[1-9]\d*$/
      display "Invalid Number!" 
      instances =nil 
      next
    else
      break
    end
    }  

  isDebug = ask "Need debug?", :default => DEFAULTS["isDebug"] if deployType == "binary"
  needRestart = ask "Need restart?", :default => DEFAULTS["needRestart"]
  needMonitor = ask "Need monitor?", :default => DEFAULTS["needMonitor"]
  if not isDebug.nil?
    isDebug = (isDebug == false ? "no" : "yes")
  else
    isDebug = "no"
  end
  if not needRestart.nil?
    needRestart = (needRestart == false ? "no" : "yes")
  else
    needRestart = "no"
  end
  if not needMonitor.nil?
    needMonitor = (needMonitor == false ? "no" : "yes")
  else
    needMonitor = "no"
  end
  # choose the branch
  branch = client.project_branchs(useproject[:name])
  err "No branchs!" if branch.nil? || branch.empty?
  if branch.count == 1
    branch = branch[0]
    display "Branch: #{branch}"
  else
    choose do |menu|
      display "=============Branchs============"
      menu.prompt = "Select Branch: "
      menu.default = "master"
      menu.select_by = :index_or_name
      branch.each do |b|
        menu.choice(b) { branch = b }
      end
    end
    display "Selected Branch: ",false
    display "#{branch}"
  end
          
  manifest = {
    :name        => "#{appname}",
    :targetName  => "#{usetarget[:name]}",
    :branch      => branch,
    :url         => url,
    :memory      => mem_quota,
    :instances   => instances,
    :deployType  => deployType,
    :tag         => tag,
    :isDebug     => isDebug,
    :needRestart => needRestart,
    :needMonitor => needMonitor
    }
 
  display "Creating application \"#{appname}\": ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.create_app(projectid, manifest)
  result = check_status(appname, "create")
  Thread.kill(t)
  if result[:code] == 200 
    if not result[:text].empty?
      display "OK".green
    else
      display "Please try again"
    end            
  else
    err result[:text]   
  end
end

#create_env(appname = nil) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/cli/commands/apps.rb', line 467

def create_env(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  app  = client.app_get_byname(appname)
  err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)      
  name = nil
  loop{        
    name = ask "Environment Name" unless name
    if env_exists_byappname?(app[:name],name)
      display "Environment '#{name}' already exists." 
      name =nil 
      next
    else
      break
    end
  }  
  value = ask "Environment Value"
  manifest = {
    :name => name,
    :value => value
  }
  client.create_env(appname, manifest)
  display "OK".green
end

#delete_app(appid = nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/cli/commands/apps.rb', line 241

def delete_app(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end           
  err "The application is not found! App id :#{appid}" unless app_exists_byappid?(appid)      
  display "Deleting application: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.delete_app(appid)
  Thread.kill(t)
  display "OK".green
end

#delete_env(appname = nil) ⇒ Object



496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/cli/commands/apps.rb', line 496

def delete_env(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  app  = client.app_get_byname(appname)
  err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)      
  name = ask "Environment Name"
  err "No Environment '#{name}'!" unless env_exists_byappname?(app[:name],name)     
  value = nil
  env = client.app_env(app[:name])
  env.each do |e|
    value = e[:value] if name == e[:name]
  end
  manifest = "#{name}=#{value}".to_a
  client.delete_env(appname,manifest)
  display "OK".green
end

#restart_app(appid = nil) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/cli/commands/apps.rb', line 306

def restart_app(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end
  app  = client.app_get_byid(appid)
  err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
  stop_app(appid)
  start_app(appid)
end

#scale_app(appid = nil) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/cli/commands/apps.rb', line 319

def scale_app(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end
  app  = client.app_get_byid(appid)
  err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
  instance = nil
  loop{
    instance = ask "How many instances?", :default => app[:instances]
    instance = instance.to_s
    if not instance =~ /^[1-9]\d*$/
      display "Invalid Number!" 
      instance =nil 
      next
    else
      break
    end
    } 
  display "Scaling application: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.scale_app(appid,instance)
  result = check_status(app[:name], "scale")
  Thread.kill(t)
  if result[:code] == 200
    display "OK".green   
  else
    err result[:text]   
  end     
end

#start_app(appid = nil) ⇒ Object



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

def start_app(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end
  app  = client.app_get_byid(appid)
  err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
  display "Starting application: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.start_app(appid)
  result = check_status(app[:name], "start")
  Thread.kill(t)
  if result[:code] == 200
    display "OK".green   
  else
    err result[:text]   
  end
end

#status(appname = nil) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/cli/commands/apps.rb', line 424

def status(appname=nil)
  unless appname         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appname = useapp[:name]
  end
  app  = client.app_get_byname(appname)
  err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false )      
  status = client.status(appname)
  return display JSON.pretty_generate(status) if @options[:json]
  return display "No Status!" if status.nil? || status.empty?
  status_table = table do |t|
    t.headings = 'Instance ID', 'Status', 'CPU(%)', 'Memory(M)', 'Disk(M)', 'RequestCount', 'ErrorCount'
    status.each do |s|
      t << [s[:instancesId][0,6], s[:status], s[:cpu], s[:memory], s[:disk], s[:requestCount], s[:errorCount]]
    end
  end
  display status_table
end

#stop_app(appid = nil) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/cli/commands/apps.rb', line 293

def stop_app(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end
  err "The application is not found! App id :#{appid}" unless app_exists_byappid?(appid)
  display "Stoping application: ",false
  client.stop_app(appid)
  display "OK".green
end

#update_app(appid = nil) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/cli/commands/apps.rb', line 360

def update_app(appid=nil)
  unless appid         
    useproject = choose_project
    projectid = useproject[:id]
    projectname = useproject[:name]
    useapp = choose_app(projectid)
    appid = useapp[:id]
  end   
  app  = client.app_get_byid(appid)
  err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
  if app[:deployType] == "git"      
    unless useproject
      projects = client.projects
      projects.each do |p|
      projectname = p[:name] if p[:id] == app[:projectId]
    end
    end
    tag = client.project_tags(projectname)
    err "No tags!" if tag.nil? || tag.empty?
    choose do |menu|
      display "=============Tags==============="
      menu.prompt = "Select Tag: "
      menu.select_by = :index_or_name
      tag.each do |t|
        menu.choice(t) { tag = t }
      end
    end
    display "Selected tag: ",false
    display "#{tag}"
  else
    tag = client.project_binary(app[:projectId])
    err "No version!" if tag.nil? || tag.empty?
    choose do |menu|
      display "=============Versions============"
      menu.prompt = "Select Version: "
      menu.select_by = :index_or_name
      tag.each do |version|
        menu.choice("#{version[:versionName]}") { tag = version }
      end
    end
    display "Selected Version: ",false
    display "#{tag[:versionName]}"   
    tag = tag[:versionName]   
  end
  display "Updating application: ",false
  
  t = Thread.new do     
  loop do
    display '.', false 
    sleep (1)
    break unless t.alive?
  end
  end
  
  client.update_app(appid,tag) 
  result = check_status(app[:name], "rollback")
  Thread.kill(t)
  if result[:code] == 200
    display "OK".green   
  else
    err result[:text]   
  end    
end