pi 0.1.24 → 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -76,14 +76,16 @@ module PI::Cli
76
76
  def choose_app_help(appid_or_appname)
77
77
  target = @options[:target]
78
78
  target_not_all = true
79
+ appid = nil
79
80
  if appid_or_appname =~ /^[1-9]\d*$/
80
81
  appid = appid_or_appname
81
82
  app = client.app_get_byid(appid)
82
- return target_not_all, app
83
+ err"The application is not found!" if app[:id] == 0
84
+ return target_not_all, app, appid
83
85
  elsif appid_or_appname == nil
84
86
  if target.nil?
85
87
  app = choose_app
86
- return target_not_all, app
88
+ return target_not_all, app, appid
87
89
  else
88
90
  err "Not enough arguments"
89
91
  end
@@ -93,26 +95,28 @@ module PI::Cli
93
95
  if target != "all"
94
96
  app = client.app_search(target, appname)
95
97
  err "The application '#{appname}' is not found!" if app[:id] == 0
96
- return target_not_all, app
98
+ return target_not_all, app, appid
97
99
  else
98
100
  apps = client.app_search_target_is_all(appname)
99
101
  err "The application '#{appname}' is not found!" if apps.size == 0
100
102
  target_not_all = false
101
- return target_not_all, apps
103
+ return target_not_all, apps, appid
102
104
  end
103
105
  end
104
106
  end
105
107
 
106
108
  def choose_app_help_target_not_all(appid_or_appname)
107
- target = @options[:target]
109
+ target = @options[:target]
110
+ appid = nil
108
111
  if appid_or_appname =~ /^[1-9]\d*$/
109
112
  appid = appid_or_appname
110
113
  app = client.app_get_byid(appid)
111
- return app
114
+ err"The application is not found!" if app[:id] == 0
115
+ return app, appid
112
116
  elsif appid_or_appname == nil
113
117
  if target.nil?
114
118
  app = choose_app
115
- return app
119
+ return app, appid
116
120
  else
117
121
  err "Not enough arguments"
118
122
  end
@@ -121,10 +125,40 @@ module PI::Cli
121
125
  err "Not enough arguments" if target == nil
122
126
  app = client.app_search(target, appname)
123
127
  err "The application '#{appname}' is not found!" if app[:id] == 0
124
- return app
128
+ return app, appid
125
129
  end
126
130
  end
127
131
 
132
+ def choose_serviceid
133
+ services = client.services
134
+ err "No services" if services.nil? || services.empty?
135
+ choices = Array.new
136
+ services.each do |s|
137
+ choices.push(s[:name])
138
+ end
139
+ servicename = ask"Select service", :choices => choices, :indexed => true
140
+ serviceid = nil
141
+ services.each do |s|
142
+ serviceid = s[:id] if s[:name] == servicename
143
+ end
144
+ return serviceid
145
+ end
146
+
147
+ def choose_dnsid
148
+ dns = client.dns
149
+ err "No DNS" if dns.nil? || dns.empty?
150
+ choices = Array.new
151
+ dns.each do |d|
152
+ choices.push(d[:name])
153
+ end
154
+ dnsname = ask"Select DNS", :choices => choices, :indexed => true
155
+ dnsid = nil
156
+ dns.each do |d|
157
+ dnsid = d[:id] if dnsname == d[:name]
158
+ end
159
+ return dnsid
160
+ end
161
+
128
162
  def check_status(target, appname, eventname)
129
163
  for i in 1..12 do
130
164
  result = client.app_message(target,appname,eventname)
@@ -134,14 +168,19 @@ module PI::Cli
134
168
  return result
135
169
  end
136
170
 
137
- # only [a-zA-Z0-9] for project name and app name (size <=18)
171
+ # only [a-zA-Z0-9] for project name and app name (size <=18)
138
172
  def check_name_valid?(name)
139
173
  (name =~ /^\w+$/ && name.size <= 18) ? true : false
140
174
  end
141
175
 
142
- #not include number, ', ", \, and begin with vcap_, vmc_ for env name (size <= 18)
176
+ #not include number, and cannot be started with vcap_, vmc_ for env name (size <= 18)
143
177
  def check_envname_valid?(name)
144
- (name =~ /\b(?!vcap_|vmc_)\D+\b/ && name =~ /^[^\'\"\\]+$/ && name.size <= 18) ? true : false
178
+ (name =~ /\b(?!vcap_|vmc_|VCAP_|VMC_)\D+\b/ && name.size <= 18) ? true : false
179
+ end
180
+
181
+ #not include number, ', ", \, and begin with vcap_, vmc_ for env name (size <= 18)
182
+ def check_envvalue_valid?(value)
183
+ (value =~ /^[^\'\"\\]+$/ && value.size <= 18) ? true : false
145
184
  end
146
185
 
147
186
  end
@@ -5,11 +5,11 @@ module PI::Cli::Command
5
5
  include PI::Cli::ChooseHelper
6
6
  include PI::Cli::InteractHelper
7
7
  DEFAULTS = {
8
- "mem" => "64M",
9
- "instances" => 1,
10
- "needRestart" => false,
11
- "isDebug" => false,
12
- "needMonitor" => false
8
+ "mem" => "64M",
9
+ "instances" => 1,
10
+ "action_for_change" => "notify",
11
+ "isDebug" => false,
12
+ "needMonitor" => false
13
13
  }
14
14
 
15
15
  def apps
@@ -69,11 +69,7 @@ module PI::Cli::Command
69
69
  # choose the target
70
70
  targets = client.targets
71
71
  err "No Targets!" if targets.nil? || targets.empty?
72
- target_choices = Array.new
73
- targets.each do |t|
74
- target_choices << t[:name]
75
- end
76
- target = ask "Select Target", :choices => target_choices, :indexed => true
72
+ target = ask "Select Target", :choices => targets.collect { |t| t[:name] }, :indexed => true
77
73
  display "Selected Target: ",false
78
74
  display "#{target}"
79
75
  target_memory = client.target_memory(target)
@@ -112,11 +108,7 @@ module PI::Cli::Command
112
108
  else
113
109
  binarylist = client.project_binary(projectname)
114
110
  err "No version!" if binarylist.nil? || binarylist.empty?
115
- binary_choices = Array.new
116
- binarylist.each do |b|
117
- binary_choices << b[:versionName]
118
- end
119
- tag = ask "Select Version", :choices => binary_choices, :indexed => true
111
+ tag = ask "Select Version", :choices => binarylist.collect { |b| b[:versionName] }, :indexed => true
120
112
  display "Selected Version: ",false
121
113
  display "#{tag}"
122
114
  deployType = "binary"
@@ -143,10 +135,10 @@ module PI::Cli::Command
143
135
  }
144
136
  isDebug = false
145
137
  isDebug = ask "Need debug?", :default => DEFAULTS["isDebug"] if deployType == "binary"
146
- needRestart = ask "Need restart?", :default => DEFAULTS["needRestart"]
138
+ choices = ["restart", "notify"]
139
+ action_for_change = ask"Reaction when service changed", :default => DEFAULTS["action_for_change"], :choices => choices, :indexed => true
147
140
  needMonitor = ask "Need monitor?", :default => DEFAULTS["needMonitor"]
148
141
  isDebug = (isDebug == false ? "no" : "yes")
149
- needRestart = (needRestart == false ? "no" : "yes")
150
142
  needMonitor = (needMonitor == false ? "no" : "yes")
151
143
  # choose the branch
152
144
  branchs = client.project_branchs(useproject[:name])
@@ -156,19 +148,19 @@ module PI::Cli::Command
156
148
  display "#{branch}"
157
149
 
158
150
  manifest = {
159
- :targetName => target,
160
- :name => "#{appname}",
161
- :projectName => "#{projectname}",
162
- :projectId => "#{projectid}",
163
- :branch => branch,
164
- :url => url,
165
- :memory => mem_quota,
166
- :instances => instances,
167
- :deployType => deployType,
168
- :tag => tag,
169
- :isDebug => isDebug,
170
- :needRestart => needRestart,
171
- :needMonitor => needMonitor
151
+ :targetName => target,
152
+ :name => "#{appname}",
153
+ :projectName => "#{projectname}",
154
+ :projectId => "#{projectid}",
155
+ :branch => branch,
156
+ :url => url,
157
+ :memory => mem_quota,
158
+ :instances => instances,
159
+ :deployType => deployType,
160
+ :tag => tag,
161
+ :isDebug => isDebug,
162
+ :action_for_change => action_for_change,
163
+ :needMonitor => needMonitor
172
164
  }
173
165
 
174
166
  display "Creating application \"#{appname}\": ",false
@@ -197,7 +189,8 @@ module PI::Cli::Command
197
189
 
198
190
  def delete_app(appid_or_appname=nil)
199
191
  client.check_login_status
200
- app = choose_app_help_target_not_all(appid_or_appname)
192
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
193
+ appid = (appid == nil ? app[:id] : appid)
201
194
 
202
195
  display "Deleting application: ",false
203
196
 
@@ -209,49 +202,52 @@ module PI::Cli::Command
209
202
  end
210
203
  end
211
204
 
212
- client.delete_app(app[:id])
205
+ client.delete_app(appid)
213
206
  Thread.kill(t)
214
207
  display "OK".green
215
208
  end
216
209
 
217
210
  def start_app(appid_or_appname=nil)
218
211
  client.check_login_status
219
- target_not_all, app = choose_app_help(appid_or_appname)
212
+ target_not_all, app, appid = choose_app_help(appid_or_appname)
213
+ appid = (appid == nil ? app[:id] : appid)
220
214
  if target_not_all
221
215
  return display "The application '#{app[:name]}'(target:#{app[:targetName]}) has already started.".yellow if app[:status] == "STARTED"
222
- do_start_app(app)
216
+ do_start_app(app, appid)
223
217
  else
224
218
  app.each do |a|
225
219
  next display "The application '#{a[:name]}'(target:#{a[:targetName]}) has already started.".yellow if a[:status] == "STARTED"
226
- do_start_app(a)
220
+ do_start_app(a, a[:id])
227
221
  end
228
222
  end
229
223
  end
230
224
 
231
225
  def stop_app(appid_or_appname=nil)
232
226
  client.check_login_status
233
- target_not_all, app = choose_app_help(appid_or_appname)
227
+ target_not_all, app, appid = choose_app_help(appid_or_appname)
228
+ appid = (appid == nil ? app[:id] : appid)
234
229
  if target_not_all
235
230
  return display "The application '#{app[:name]}'(target:#{app[:targetName]}) has already stopped.".yellow if app[:status] == "STOPPED"
236
- do_stop_app(app)
231
+ do_stop_app(app, appid)
237
232
  else
238
233
  app.each do |a|
239
234
  next display "The application '#{a[:name]}'(target:#{a[:targetName]}) has already stopped.".yellow if a[:status] == "STOPPED"
240
- do_stop_app(a)
235
+ do_stop_app(a, a[:id])
241
236
  end
242
237
  end
243
238
  end
244
239
 
245
240
  def restart_app(appid_or_appname=nil)
246
241
  client.check_login_status
247
- target_not_all, app = choose_app_help(appid_or_appname)
242
+ target_not_all, app, appid = choose_app_help(appid_or_appname)
243
+ appid = (appid == nil ? app[:id] : appid)
248
244
  if target_not_all
249
- do_stop_app(app) if app[:status] == "STARTED"
250
- do_start_app(app)
245
+ do_stop_app(app, appid) if app[:status] == "STARTED"
246
+ do_start_app(app, appid)
251
247
  else
252
248
  app.each do |a|
253
- do_stop_app(a) if a[:status] == "STARTED"
254
- do_start_app(a)
249
+ do_stop_app(a, a[:id]) if a[:status] == "STARTED"
250
+ do_start_app(a, a[:id])
255
251
  end
256
252
  end
257
253
  end
@@ -259,7 +255,8 @@ module PI::Cli::Command
259
255
  def scale_app(appid_or_appname=nil)
260
256
  client.check_login_status
261
257
  instance = @options[:instance]
262
- app = choose_app_help_target_not_all(appid_or_appname)
258
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
259
+ appid = (appid == nil ? app[:id] : appid)
263
260
  loop{
264
261
  unless instance
265
262
  instance = ask "How many instances?", :default => app[:instances]
@@ -284,7 +281,7 @@ module PI::Cli::Command
284
281
  end
285
282
  end
286
283
 
287
- client.scale_app(app[:id],instance)
284
+ client.scale_app(appid,instance)
288
285
  result = check_status(app[:targetName],app[:name], "scale")
289
286
  Thread.kill(t)
290
287
  if result[:code] == 200
@@ -300,22 +297,20 @@ module PI::Cli::Command
300
297
 
301
298
  def update_app(appid_or_appname=nil)
302
299
  client.check_login_status
303
- app = choose_app_help_target_not_all(appid_or_appname)
300
+ tag = @options[:ver]
301
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
302
+ appid = (appid == nil ? app[:id] : appid)
304
303
  if app[:deployType] == "git"
305
304
  tags = client.project_tags(app[:projectName])
306
305
  err "No tags!" if tags.nil? || tags.empty?
307
- tag = ask "Select Tag", :choices => tags, :indexed => true
306
+ tag = ask "Select Tag", :choices => tags, :indexed => true unless tag
308
307
  err "Don't need update application!" if tag == app[:tag]
309
308
  display "Selected tag: ",false
310
309
  display "#{tag}"
311
310
  else
312
311
  tags = client.project_binary(app[:projectName])
313
312
  err "No version!" if tags.nil? || tags.empty?
314
- tag_choices = Array.new
315
- tags.each do |t|
316
- tag_choices << t[:versionName]
317
- end
318
- tag = ask "Select Version", :choices => tag_choices, :indexed => true
313
+ tag = ask "Select Version", :choices => tags.collect { |b| b[:versionName] }, :indexed => true unless tag
319
314
  err "Don't need update application!" if tag == app[:tag]
320
315
  end
321
316
  display "Updating application: ",false
@@ -328,7 +323,7 @@ module PI::Cli::Command
328
323
  end
329
324
  end
330
325
 
331
- client.update_app(app[:id],tag)
326
+ client.update_app(appid,tag)
332
327
  result = check_status(app[:targetName], app[:name], "rollback")
333
328
  Thread.kill(t)
334
329
  if result[:code] == 200
@@ -344,8 +339,9 @@ module PI::Cli::Command
344
339
 
345
340
  def status(appid_or_appname=nil)
346
341
  client.check_login_status
347
- app = choose_app_help_target_not_all(appid_or_appname)
348
- status = client.status(app[:id])
342
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
343
+ appid = (appid == nil ? app[:id] : appid)
344
+ status = client.status(appid)
349
345
  return display JSON.pretty_generate(status) if @options[:json]
350
346
  return display "No Status!" if status.nil? || status.empty?
351
347
  status_table = table do |t|
@@ -359,8 +355,9 @@ module PI::Cli::Command
359
355
 
360
356
  def app_env(appid_or_appname=nil)
361
357
  client.check_login_status
362
- app = choose_app_help_target_not_all(appid_or_appname)
363
- env = client.app_env(app[:id])
358
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
359
+ appid = (appid == nil ? app[:id] : appid)
360
+ env = client.app_env(appid)
364
361
  return display JSON.pretty_generate(env) if @options[:json]
365
362
  return display "No Environments!" if env.nil? || env.empty?
366
363
  env.sort! {|a, b| a[:name] <=> b[:name] }
@@ -375,16 +372,18 @@ module PI::Cli::Command
375
372
 
376
373
  def create_env(appid_or_appname=nil)
377
374
  client.check_login_status
378
- app = choose_app_help_target_not_all(appid_or_appname)
375
+ name = @options[:name]
376
+ value = @options[:value]
377
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
378
+ appid = (appid == nil ? app[:id] : appid)
379
379
 
380
- name = nil
381
380
  loop{
382
381
  name = ask "Environment Name" unless name
383
382
  if not check_envname_valid?(name)
384
383
  display "Invalid environment name '#{name}'."
385
384
  name =nil
386
385
  next
387
- else if env_exists?(app[:id],name)
386
+ else if env_exists?(appid,name)
388
387
  display "Environment '#{name}' already exists."
389
388
  name =nil
390
389
  next
@@ -393,54 +392,66 @@ module PI::Cli::Command
393
392
  end
394
393
  end
395
394
  }
396
- value = ask "Environment Value"
395
+ loop{
396
+ value = ask "Environment Value" unless value
397
+ if not check_envvalue_valid?(value)
398
+ display "Invalid environment value '#{value}'."
399
+ value =nil
400
+ next
401
+ else
402
+ break
403
+ end
404
+ }
397
405
  manifest = {
398
406
  :name => name,
399
407
  :value => value
400
408
  }
401
- client.create_env(app[:id], manifest)
409
+ client.create_env(appid, manifest)
402
410
  display "OK".green
403
411
  end
404
412
 
405
413
  def delete_env(appid_or_appname=nil)
406
414
  client.check_login_status
407
- app = choose_app_help_target_not_all(appid_or_appname)
408
-
409
- envs = client.app_env(app[:id])
410
- err "No Environments!" if envs.nil? || envs.empty?
411
- choices = Array.new
412
- envs.each do |s|
413
- choices << s[:name]
415
+ name = @options[:name]
416
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
417
+ appid = (appid == nil ? app[:id] : appid)
418
+ envs = client.app_env(appid)
419
+ err "No environments available to delete" if envs.nil? || envs.empty?
420
+ if name
421
+ name = name.split(",")
422
+ else
423
+ name = asks "Environment Name", :choices => envs.collect { |e| e[:name] }, :indexed => true
424
+ display "Selected Environment Name: ",false
425
+ name.each do |m|
426
+ display "#{m} ",false
427
+ end
428
+ display "\n"
414
429
  end
415
- name = asks "Environment Name", :choices => choices, :indexed => true
416
- display "Selected Environment Name: ",false
417
- name.each do |m|
418
- display "#{m} ",false
419
- end
420
- display "\n"
421
-
430
+
422
431
  delete_envs = Array.new
423
432
  envs.each do |e|
424
433
  name.each do |n|
425
434
  delete_envs.push("#{n}=#{e[:value]}") if n == e[:name]
426
435
  end
427
- end
436
+ end
437
+ err " The environment is not exist!" if delete_envs.nil? || delete_envs.empty?
428
438
 
429
439
  delete_envs.each do |env|
430
440
  display "Deleting environment '#{env}': ",false
431
- client.delete_env(app[:id], env)
441
+ client.delete_env(appid, env)
432
442
  display "OK".green
433
443
  end
434
444
  end
435
445
 
436
446
  def app_log(appid_or_appname=nil)
437
447
  client.check_login_status
438
- app = choose_app_help_target_not_all(appid_or_appname)
439
-
448
+ app, appid = choose_app_help_target_not_all(appid_or_appname)
449
+ appid = (appid == nil ? app[:id] : appid)
450
+
440
451
  max_index = app[:instances]-1
441
452
  instanceindex = ask "Select instance index(from 0 to #{max_index})", :default => 0
442
453
  filepath = "logs/"
443
- logs = client.app_log(app[:id],filepath,instanceindex)
454
+ logs = client.app_log(appid,filepath,instanceindex)
444
455
  logs = logs[1].split("\n")
445
456
  file = ask "Select logs, indexed list?", :choices => logs, :indexed => true
446
457
  display "Selected logs: ",false
@@ -448,7 +459,7 @@ module PI::Cli::Command
448
459
  file = file[0]
449
460
  display "#{file}"
450
461
  filepath = "logs/#{file}"
451
- logs = client.app_log(app[:id],filepath,instanceindex)
462
+ logs = client.app_log(appid,filepath,instanceindex)
452
463
  display "\n"
453
464
  display logs[1]
454
465
  end
@@ -465,7 +476,7 @@ module PI::Cli::Command
465
476
  "true" == env[1] ? false : true
466
477
  end
467
478
 
468
- def do_start_app(app)
479
+ def do_start_app(app, appid)
469
480
  display "Starting application '#{app[:name]}'(target:#{app[:targetName]}): ",false
470
481
  t = Thread.new do
471
482
  loop do
@@ -474,7 +485,7 @@ module PI::Cli::Command
474
485
  break unless t.alive?
475
486
  end
476
487
  end
477
- client.start_app(app[:id])
488
+ client.start_app(appid)
478
489
  result = check_status(app[:targetName], app[:name], "start")
479
490
  Thread.kill(t)
480
491
  if result[:code] == 200
@@ -488,9 +499,9 @@ module PI::Cli::Command
488
499
  end
489
500
  end
490
501
 
491
- def do_stop_app(app)
502
+ def do_stop_app(app, appid)
492
503
  display "Stoping application '#{app[:name]}'(target:#{app[:targetName]}): ",false
493
- client.stop_app(app[:id])
504
+ client.stop_app(appid)
494
505
  display "OK".green
495
506
  end
496
507