pi 0.1.23 → 0.1.24

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.
data/lib/cli.rb CHANGED
@@ -10,6 +10,7 @@ module PI
10
10
  autoload :Config, "#{ROOT}/cli/config"
11
11
  autoload :Runner, "#{ROOT}/cli/runner"
12
12
  autoload :ChooseHelper, "#{ROOT}/cli/choose_helper"
13
+ autoload :InteractHelper, "#{ROOT}/cli/interact_helper"
13
14
 
14
15
  module Command
15
16
  autoload :Base, "#{ROOT}/cli/commands/base"
@@ -1,50 +1,148 @@
1
+ require "set"
1
2
  module PI::Cli
2
3
  module ChooseHelper
3
4
 
5
+ YES_SET = Set.new(["y", "Y", "yes", "YES"])
6
+ NO_SET = Set.new(["n", "N", "no", "NO"])
7
+
4
8
  def choose_project
5
9
  projects = client.projects
6
10
  err "No Projects" if projects.nil? || projects.empty?
7
- useproject = nil
8
11
  projects.sort! {|a, b| a[:name] <=> b[:name] }
9
- choose do |menu|
10
- display "=============Project Name==========="
11
- menu.prompt = "Select Project: "
12
- menu.select_by = :index_or_name
13
- projects.each do |project|
14
- menu.choice("#{project[:name]}") { useproject = project }
15
- end
12
+ project_choices = Array.new
13
+ projects.each do |p|
14
+ project_choices << p[:name]
16
15
  end
16
+ useproject = ask "Select Project", :choices => project_choices, :indexed => true
17
17
  display "Selected Project: ",false
18
- display "#{useproject[:name]}".green
19
- return useproject
18
+ display "#{useproject}"
19
+ projects.each do |p|
20
+ return p if p[:name] == useproject
21
+ end
22
+ end
23
+
24
+ def choose_target
25
+ targets = client.targets
26
+ err "No Targets" if targets.nil? || targets.empty?
27
+ targets.sort! {|a, b| a[:name] <=> b[:name] }
28
+ target_choices = Array.new
29
+ targets.each do |p|
30
+ target_choices << p[:name]
31
+ end
32
+ usetarget = ask "Select Target", :choices => target_choices, :indexed => true
33
+ display "Selected Target: ",false
34
+ display "#{usetarget}"
35
+ targets.each do |p|
36
+ return p if p[:name] == usetarget
37
+ end
20
38
  end
21
39
 
22
- def choose_app(projectid)
23
- apps = client.apps(projectid)
24
- err "No Applications" if apps.nil? || apps.empty?
25
- useapp = nil
26
- apps.sort! {|a, b| a[:name] <=> b[:name] }
27
- choose do |menu|
28
- display "=============Application Name==========="
29
- menu.prompt = "Select Application: "
30
- menu.select_by = :index_or_name
31
- apps.each do |app|
32
- menu.choice("#{app[:name]}") { useapp = app }
40
+ def select_apps
41
+ choices = ["all","project","target"]
42
+ select_type = ask"Select application by", :choices => choices, :indexed => true
43
+ display "Select application by #{select_type}"
44
+ case select_type
45
+ when "all"
46
+ app = client.apps
47
+ when "project"
48
+ useproject = choose_project
49
+ app = client.apps(useproject[:name])
50
+ when "target"
51
+ usetarget = choose_target
52
+ project=nil
53
+ app = client.apps(project, usetarget[:name])
54
+ else
55
+ if select_type
56
+ err "Unknown select type [#{select_type}]"
33
57
  end
34
58
  end
59
+ end
60
+
61
+ def choose_app
62
+ apps = select_apps
63
+ err "No application!" if apps.nil? || apps.empty?
64
+ app_choices = Array.new
65
+ apps.each do |a|
66
+ app_choices << a[:name]
67
+ end
68
+ appname = ask "Select Application", :choices => app_choices, :indexed => true
35
69
  display "Selected Application: ",false
36
- display "#{useapp[:name]}".green
37
- return useapp
70
+ display "#{appname}"
71
+ apps.each do |a|
72
+ return a if a[:name] == appname
73
+ end
38
74
  end
39
75
 
40
- def check_status(appname, eventname)
76
+ def choose_app_help(appid_or_appname)
77
+ target = @options[:target]
78
+ target_not_all = true
79
+ if appid_or_appname =~ /^[1-9]\d*$/
80
+ appid = appid_or_appname
81
+ app = client.app_get_byid(appid)
82
+ return target_not_all, app
83
+ elsif appid_or_appname == nil
84
+ if target.nil?
85
+ app = choose_app
86
+ return target_not_all, app
87
+ else
88
+ err "Not enough arguments"
89
+ end
90
+ else
91
+ appname = appid_or_appname
92
+ err "Not enough arguments" if target == nil
93
+ if target != "all"
94
+ app = client.app_search(target, appname)
95
+ err "The application '#{appname}' is not found!" if app[:id] == 0
96
+ return target_not_all, app
97
+ else
98
+ apps = client.app_search_target_is_all(appname)
99
+ err "The application '#{appname}' is not found!" if apps.size == 0
100
+ target_not_all = false
101
+ return target_not_all, apps
102
+ end
103
+ end
104
+ end
105
+
106
+ def choose_app_help_target_not_all(appid_or_appname)
107
+ target = @options[:target]
108
+ if appid_or_appname =~ /^[1-9]\d*$/
109
+ appid = appid_or_appname
110
+ app = client.app_get_byid(appid)
111
+ return app
112
+ elsif appid_or_appname == nil
113
+ if target.nil?
114
+ app = choose_app
115
+ return app
116
+ else
117
+ err "Not enough arguments"
118
+ end
119
+ else
120
+ appname = appid_or_appname
121
+ err "Not enough arguments" if target == nil
122
+ app = client.app_search(target, appname)
123
+ err "The application '#{appname}' is not found!" if app[:id] == 0
124
+ return app
125
+ end
126
+ end
127
+
128
+ def check_status(target, appname, eventname)
41
129
  for i in 1..12 do
42
- result = client.app_message(appname,eventname)
130
+ result = client.app_message(target,appname,eventname)
43
131
  break result unless result[:text].empty?
44
132
  sleep(10)
45
133
  end
46
134
  return result
47
135
  end
136
+
137
+ # only [a-zA-Z0-9] for project name and app name (size <=18)
138
+ def check_name_valid?(name)
139
+ (name =~ /^\w+$/ && name.size <= 18) ? true : false
140
+ end
141
+
142
+ #not include number, ', ", \, and begin with vcap_, vmc_ for env name (size <= 18)
143
+ def check_envname_valid?(name)
144
+ (name =~ /\b(?!vcap_|vmc_)\D+\b/ && name =~ /^[^\'\"\\]+$/ && name.size <= 18) ? true : false
145
+ end
48
146
 
49
147
  end
50
148
  end
@@ -1,10 +1,9 @@
1
1
  require "rubygems"
2
- require "interact"
3
- require "set"
2
+
4
3
  module PI::Cli::Command
5
4
  class Apps < Base
6
5
  include PI::Cli::ChooseHelper
7
- include Interactive
6
+ include PI::Cli::InteractHelper
8
7
  DEFAULTS = {
9
8
  "mem" => "64M",
10
9
  "instances" => 1,
@@ -12,16 +11,18 @@ module PI::Cli::Command
12
11
  "isDebug" => false,
13
12
  "needMonitor" => false
14
13
  }
15
- YES_SET = Set.new(["y", "Y", "yes", "YES"])
16
14
 
17
- def apps(projectid=nil)
18
- unless projectid
19
- useproject = choose_project
20
- projectid = useproject[:id]
21
- end
22
- app_sum = client.app_sum(projectid)
15
+ def apps
16
+ client.check_login_status
17
+ project = @options[:project]
18
+ target = @options[:target]
19
+ project = nil if project == "all"
20
+ target = nil if target == "all"
21
+ app_sum = client.app_sum(project, target)
23
22
  total_app = Integer(app_sum[1])
24
23
  return display "No Applications" if total_app == 0
24
+ pageNum = -1
25
+ numPerPage = -1
25
26
  if total_app >= 50
26
27
  puts "Total applications: #{total_app}."
27
28
  numPerPage = nil
@@ -47,103 +48,82 @@ module PI::Cli::Command
47
48
  break
48
49
  end
49
50
  }
50
- pageNum = pageNum.to_i
51
- else
52
- pageNum = -1
53
- numPerPage = -1
54
- end
55
- queryParam = {
56
- :pageNum => pageNum,
57
- :NumPerPage => numPerPage
58
- }
59
- apps = client.apps(projectid, queryParam)
51
+ pageNum = pageNum.to_i
52
+ end
53
+ apps = client.apps(project, target, pageNum, numPerPage)
54
+ return display "No Applications" if apps.nil? || apps.empty?
60
55
  return display JSON.pretty_generate(apps) if @options[:json]
61
- return display "No Applications" if apps.nil? || apps.empty?
62
56
  apps.sort! {|a, b| a[:name] <=> b[:name] }
63
57
  apps_table = table do |t|
64
- t.headings = 'Target', 'App name', 'URL', 'Deploy Type','Version', 'Status', 'Instances', 'Running Instances'
58
+ t.headings = 'ID', 'Target', 'App name', 'URL', 'Deploy Type','Version', 'Status', 'Instances', 'Running Instances'
65
59
  apps.each do |app|
66
- t << [app[:targetName], app[:name] ,app[:url], app[:deployType], app[:tag], app[:status], app[:instances], app[:runInstances]]
60
+ t << [app[:id], app[:targetName], app[:name] ,app[:url], app[:deployType], app[:tag], app[:status], app[:instances], app[:runInstances]]
67
61
  end
68
62
  end
69
63
  display apps_table
70
64
  end
71
65
 
72
66
  def create_app(appname=nil)
73
- projectid, mem, tag, binarylist, branch, deployType, instances = nil, nil, nil, nil, nil, nil, nil
74
- loop{
67
+ client.check_login_status
68
+ binarylist, deployType, instances = nil, nil, nil
69
+ # choose the target
70
+ targets = client.targets
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
77
+ display "Selected Target: ",false
78
+ display "#{target}"
79
+ target_memory = client.target_memory(target)
80
+ target_memory = target_memory[1]
81
+ display ("Available memory: #{target_memory} M")
82
+
83
+ # choose the project
84
+ useproject = choose_project
85
+ projectname = useproject[:name]
86
+ projectid = useproject[:id]
87
+
88
+ loop{
75
89
  appname = ask "Application Name" unless appname
76
- if app_exists_byappname?(appname)
90
+ if not check_name_valid?(appname)
91
+ display "Invalid application name '#{appname}'."
92
+ appname =nil
93
+ next
94
+ else if app_exists?(target, appname)
77
95
  display "Application '#{appname}' already exists."
78
96
  appname =nil
79
- next
80
- else
97
+ next
98
+ else
81
99
  break
100
+ end
82
101
  end
83
102
  }
84
-
85
- # choose the project
86
- useproject = choose_project
87
- projectid = useproject[:id]
88
-
103
+
89
104
  # choose the tag
90
105
  if useproject[:runtime] =~ /^(ruby)/i
91
- tag = client.project_tags(useproject[:name])
92
- err "No tags!" if tag.nil? || tag.empty?
93
- if tag.count == 1
94
- tag = tag[0]
95
- display "Tag: #{tag}"
96
- deployType = "git"
97
- else
98
- choose do |menu|
99
- display "=============Tags==============="
100
- menu.prompt = "Select Tag: "
101
- menu.select_by = :index_or_name
102
- tag.each do |t|
103
- menu.choice(t) { tag = t }
104
- end
105
- end
106
- display "Selected Version: ",false
107
- display "#{tag}"
108
- deployType = "git"
109
- end
106
+ tags = client.project_tags(useproject[:name])
107
+ err "No tags!" if tags.nil? || tags.empty?
108
+ tag = ask "Select Tag", :choices => tags, :indexed => true
109
+ display "Selected Tag: ",false
110
+ display "#{tag}"
111
+ deployType = "git"
110
112
  else
111
- binarylist = client.project_binary(projectid)
113
+ binarylist = client.project_binary(projectname)
112
114
  err "No version!" if binarylist.nil? || binarylist.empty?
113
- choose do |menu|
114
- display "=============Versions============"
115
- menu.prompt = "Select Version: "
116
- menu.select_by = :index_or_name
117
- binarylist.each do |version|
118
- menu.choice("#{version[:versionName]}") { tag = version }
119
- end
115
+ binary_choices = Array.new
116
+ binarylist.each do |b|
117
+ binary_choices << b[:versionName]
120
118
  end
119
+ tag = ask "Select Version", :choices => binary_choices, :indexed => true
121
120
  display "Selected Version: ",false
122
- display "#{tag[:versionName]}"
123
- tag = tag[:versionName]
121
+ display "#{tag}"
124
122
  deployType = "binary"
125
- end
126
-
127
- # choose the target
128
- targets = client.targets
129
- err "No Targets!" if targets.nil? || targets.empty?
130
- usetarget = nil
131
- choose do |menu|
132
- display "=============Targets============"
133
- menu.prompt = "Select Target: "
134
- menu.select_by = :index_or_name
135
- targets.each do |target|
136
- menu.choice("#{target[:name]}-#{target[:targetUrl]}") { usetarget = target }
137
- end
138
- end
139
- display "Selected Target: ",false
140
- display "#{usetarget[:name]}-#{usetarget[:targetUrl]}"
141
- target_memory = client.target_memory(usetarget[:name])
142
- target_memory = target_memory[1]
143
- display ("Available memory: #{target_memory} M")
123
+ end
144
124
  # URL combination
145
125
  user = client.user_info
146
- url = "#{appname}.#{user[:userName]}.#{usetarget[:name]}.samsungpaas.com"
126
+ url = "#{appname}.#{user[:userName]}.#{target}.samsungpaas.com"
147
127
  display "URL: #{url}"
148
128
  # choose the mem
149
129
  mem = ask "Memory reservation", :default => DEFAULTS["mem"], :choices => ["64M", "128M", "256M", "512M", "1G", "2G"]
@@ -161,48 +141,25 @@ module PI::Cli::Command
161
141
  break
162
142
  end
163
143
  }
164
-
144
+ isDebug = false
165
145
  isDebug = ask "Need debug?", :default => DEFAULTS["isDebug"] if deployType == "binary"
166
146
  needRestart = ask "Need restart?", :default => DEFAULTS["needRestart"]
167
147
  needMonitor = ask "Need monitor?", :default => DEFAULTS["needMonitor"]
168
- if not isDebug.nil?
169
- isDebug = (isDebug == false ? "no" : "yes")
170
- else
171
- isDebug = "no"
172
- end
173
- if not needRestart.nil?
174
- needRestart = (needRestart == false ? "no" : "yes")
175
- else
176
- needRestart = "no"
177
- end
178
- if not needMonitor.nil?
179
- needMonitor = (needMonitor == false ? "no" : "yes")
180
- else
181
- needMonitor = "no"
182
- end
148
+ isDebug = (isDebug == false ? "no" : "yes")
149
+ needRestart = (needRestart == false ? "no" : "yes")
150
+ needMonitor = (needMonitor == false ? "no" : "yes")
183
151
  # choose the branch
184
- branch = client.project_branchs(useproject[:name])
185
- err "No branchs!" if branch.nil? || branch.empty?
186
- if branch.count == 1
187
- branch = branch[0]
188
- display "Branch: #{branch}"
189
- else
190
- choose do |menu|
191
- display "=============Branchs============"
192
- menu.prompt = "Select Branch: "
193
- menu.default = "master"
194
- menu.select_by = :index_or_name
195
- branch.each do |b|
196
- menu.choice(b) { branch = b }
197
- end
198
- end
199
- display "Selected Branch: ",false
200
- display "#{branch}"
201
- end
152
+ branchs = client.project_branchs(useproject[:name])
153
+ err "No branchs!" if branchs.nil? || branchs.empty?
154
+ branch = ask "Select Branch", :choices => branchs, :indexed => true, :default => "master"
155
+ display "Selected Branch: ",false
156
+ display "#{branch}"
202
157
 
203
158
  manifest = {
159
+ :targetName => target,
204
160
  :name => "#{appname}",
205
- :targetName => "#{usetarget[:name]}",
161
+ :projectName => "#{projectname}",
162
+ :projectId => "#{projectid}",
206
163
  :branch => branch,
207
164
  :url => url,
208
165
  :memory => mem_quota,
@@ -224,8 +181,8 @@ module PI::Cli::Command
224
181
  end
225
182
  end
226
183
 
227
- client.create_app(projectid, manifest)
228
- result = check_status(appname, "create")
184
+ client.create_app(manifest)
185
+ result = check_status(target, appname, "create")
229
186
  Thread.kill(t)
230
187
  if result[:code] == 200
231
188
  if not result[:text].empty?
@@ -238,14 +195,10 @@ module PI::Cli::Command
238
195
  end
239
196
  end
240
197
 
241
- def delete_app(appid=nil)
242
- unless appid
243
- useproject = choose_project
244
- projectid = useproject[:id]
245
- useapp = choose_app(projectid)
246
- appid = useapp[:id]
247
- end
248
- err "The application is not found! App id :#{appid}" unless app_exists_byappid?(appid)
198
+ def delete_app(appid_or_appname=nil)
199
+ client.check_login_status
200
+ app = choose_app_help_target_not_all(appid_or_appname)
201
+
249
202
  display "Deleting application: ",false
250
203
 
251
204
  t = Thread.new do
@@ -256,79 +209,62 @@ module PI::Cli::Command
256
209
  end
257
210
  end
258
211
 
259
- client.delete_app(appid)
212
+ client.delete_app(app[:id])
260
213
  Thread.kill(t)
261
214
  display "OK".green
262
215
  end
263
216
 
264
- def start_app(appid=nil)
265
- unless appid
266
- useproject = choose_project
267
- projectid = useproject[:id]
268
- useapp = choose_app(projectid)
269
- appid = useapp[:id]
270
- end
271
- app = client.app_get_byid(appid)
272
- err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
273
- display "Starting application: ",false
274
-
275
- t = Thread.new do
276
- loop do
277
- display '.', false
278
- sleep (1)
279
- break unless t.alive?
280
- end
281
- end
282
-
283
- client.start_app(appid)
284
- result = check_status(app[:name], "start")
285
- Thread.kill(t)
286
- if result[:code] == 200
287
- display "OK".green
217
+ def start_app(appid_or_appname=nil)
218
+ client.check_login_status
219
+ target_not_all, app = choose_app_help(appid_or_appname)
220
+ if target_not_all
221
+ return display "The application '#{app[:name]}'(target:#{app[:targetName]}) has already started.".yellow if app[:status] == "STARTED"
222
+ do_start_app(app)
288
223
  else
289
- err result[:text]
224
+ app.each do |a|
225
+ next display "The application '#{a[:name]}'(target:#{a[:targetName]}) has already started.".yellow if a[:status] == "STARTED"
226
+ do_start_app(a)
227
+ end
290
228
  end
291
229
  end
292
230
 
293
- def stop_app(appid=nil)
294
- unless appid
295
- useproject = choose_project
296
- projectid = useproject[:id]
297
- useapp = choose_app(projectid)
298
- appid = useapp[:id]
231
+ def stop_app(appid_or_appname=nil)
232
+ client.check_login_status
233
+ target_not_all, app = choose_app_help(appid_or_appname)
234
+ if target_not_all
235
+ return display "The application '#{app[:name]}'(target:#{app[:targetName]}) has already stopped.".yellow if app[:status] == "STOPPED"
236
+ do_stop_app(app)
237
+ else
238
+ app.each do |a|
239
+ next display "The application '#{a[:name]}'(target:#{a[:targetName]}) has already stopped.".yellow if a[:status] == "STOPPED"
240
+ do_stop_app(a)
241
+ end
299
242
  end
300
- err "The application is not found! App id :#{appid}" unless app_exists_byappid?(appid)
301
- display "Stoping application: ",false
302
- client.stop_app(appid)
303
- display "OK".green
304
243
  end
305
244
 
306
- def restart_app(appid=nil)
307
- unless appid
308
- useproject = choose_project
309
- projectid = useproject[:id]
310
- useapp = choose_app(projectid)
311
- appid = useapp[:id]
245
+ def restart_app(appid_or_appname=nil)
246
+ client.check_login_status
247
+ target_not_all, app = choose_app_help(appid_or_appname)
248
+ if target_not_all
249
+ do_stop_app(app) if app[:status] == "STARTED"
250
+ do_start_app(app)
251
+ else
252
+ app.each do |a|
253
+ do_stop_app(a) if a[:status] == "STARTED"
254
+ do_start_app(a)
255
+ end
312
256
  end
313
- app = client.app_get_byid(appid)
314
- err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
315
- stop_app(appid)
316
- start_app(appid)
317
257
  end
318
258
 
319
- def scale_app(appid=nil)
320
- unless appid
321
- useproject = choose_project
322
- projectid = useproject[:id]
323
- useapp = choose_app(projectid)
324
- appid = useapp[:id]
325
- end
326
- app = client.app_get_byid(appid)
327
- err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
328
- instance = nil
259
+ def scale_app(appid_or_appname=nil)
260
+ client.check_login_status
261
+ instance = @options[:instance]
262
+ app = choose_app_help_target_not_all(appid_or_appname)
329
263
  loop{
330
- instance = ask "How many instances?", :default => app[:instances]
331
- instance = instance.to_s
264
+ unless instance
265
+ instance = ask "How many instances?", :default => app[:instances]
266
+ instance = instance.to_s
267
+ end
332
268
  if not instance =~ /^[1-9]\d*$/
333
269
  display "Invalid Number!"
334
270
  instance =nil
@@ -337,6 +273,7 @@ module PI::Cli::Command
337
273
  break
338
274
  end
339
275
  }
276
+ err "Don't need scale application" if instance == app[:instances].to_s
340
277
  display "Scaling application: ",false
341
278
 
342
279
  t = Thread.new do
@@ -347,59 +284,39 @@ module PI::Cli::Command
347
284
  end
348
285
  end
349
286
 
350
- client.scale_app(appid,instance)
351
- result = check_status(app[:name], "scale")
287
+ client.scale_app(app[:id],instance)
288
+ result = check_status(app[:targetName],app[:name], "scale")
352
289
  Thread.kill(t)
353
- if result[:code] == 200
354
- display "OK".green
290
+ if result[:code] == 200
291
+ if not result[:text].empty?
292
+ display "OK".green
293
+ else
294
+ display "Please try again"
295
+ end
355
296
  else
356
297
  err result[:text]
357
298
  end
358
299
  end
359
300
 
360
- def update_app(appid=nil)
361
- unless appid
362
- useproject = choose_project
363
- projectid = useproject[:id]
364
- projectname = useproject[:name]
365
- useapp = choose_app(projectid)
366
- appid = useapp[:id]
367
- end
368
- app = client.app_get_byid(appid)
369
- err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
370
- if app[:deployType] == "git"
371
- unless useproject
372
- projects = client.projects
373
- projects.each do |p|
374
- projectname = p[:name] if p[:id] == app[:projectId]
375
- end
376
- end
377
- tag = client.project_tags(projectname)
378
- err "No tags!" if tag.nil? || tag.empty?
379
- choose do |menu|
380
- display "=============Tags==============="
381
- menu.prompt = "Select Tag: "
382
- menu.select_by = :index_or_name
383
- tag.each do |t|
384
- menu.choice(t) { tag = t }
385
- end
386
- end
301
+ def update_app(appid_or_appname=nil)
302
+ client.check_login_status
303
+ app = choose_app_help_target_not_all(appid_or_appname)
304
+ if app[:deployType] == "git"
305
+ tags = client.project_tags(app[:projectName])
306
+ err "No tags!" if tags.nil? || tags.empty?
307
+ tag = ask "Select Tag", :choices => tags, :indexed => true
308
+ err "Don't need update application!" if tag == app[:tag]
387
309
  display "Selected tag: ",false
388
310
  display "#{tag}"
389
- else
390
- tag = client.project_binary(app[:projectId])
391
- err "No version!" if tag.nil? || tag.empty?
392
- choose do |menu|
393
- display "=============Versions============"
394
- menu.prompt = "Select Version: "
395
- menu.select_by = :index_or_name
396
- tag.each do |version|
397
- menu.choice("#{version[:versionName]}") { tag = version }
398
- end
311
+ else
312
+ tags = client.project_binary(app[:projectName])
313
+ err "No version!" if tags.nil? || tags.empty?
314
+ tag_choices = Array.new
315
+ tags.each do |t|
316
+ tag_choices << t[:versionName]
399
317
  end
400
- display "Selected Version: ",false
401
- display "#{tag[:versionName]}"
402
- tag = tag[:versionName]
318
+ tag = ask "Select Version", :choices => tag_choices, :indexed => true
319
+ err "Don't need update application!" if tag == app[:tag]
403
320
  end
404
321
  display "Updating application: ",false
405
322
 
@@ -411,26 +328,24 @@ module PI::Cli::Command
411
328
  end
412
329
  end
413
330
 
414
- client.update_app(appid,tag)
415
- result = check_status(app[:name], "rollback")
331
+ client.update_app(app[:id],tag)
332
+ result = check_status(app[:targetName], app[:name], "rollback")
416
333
  Thread.kill(t)
417
- if result[:code] == 200
418
- display "OK".green
334
+ if result[:code] == 200
335
+ if not result[:text].empty?
336
+ display "OK".green
337
+ else
338
+ display "Please try again"
339
+ end
419
340
  else
420
341
  err result[:text]
421
- end
342
+ end
422
343
  end
423
344
 
424
- def status(appname=nil)
425
- unless appname
426
- useproject = choose_project
427
- projectid = useproject[:id]
428
- useapp = choose_app(projectid)
429
- appname = useapp[:name]
430
- end
431
- app = client.app_get_byname(appname)
432
- err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false )
433
- status = client.status(appname)
345
+ def status(appid_or_appname=nil)
346
+ client.check_login_status
347
+ app = choose_app_help_target_not_all(appid_or_appname)
348
+ status = client.status(app[:id])
434
349
  return display JSON.pretty_generate(status) if @options[:json]
435
350
  return display "No Status!" if status.nil? || status.empty?
436
351
  status_table = table do |t|
@@ -442,16 +357,10 @@ module PI::Cli::Command
442
357
  display status_table
443
358
  end
444
359
 
445
- def app_env(appname=nil)
446
- unless appname
447
- useproject = choose_project
448
- projectid = useproject[:id]
449
- useapp = choose_app(projectid)
450
- appname = useapp[:name]
451
- end
452
- app = client.app_get_byname(appname)
453
- err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)
454
- env = client.app_env(appname)
360
+ def app_env(appid_or_appname=nil)
361
+ client.check_login_status
362
+ app = choose_app_help_target_not_all(appid_or_appname)
363
+ env = client.app_env(app[:id])
455
364
  return display JSON.pretty_generate(env) if @options[:json]
456
365
  return display "No Environments!" if env.nil? || env.empty?
457
366
  env.sort! {|a, b| a[:name] <=> b[:name] }
@@ -464,70 +373,75 @@ module PI::Cli::Command
464
373
  display env_table
465
374
  end
466
375
 
467
- def create_env(appname=nil)
468
- unless appname
469
- useproject = choose_project
470
- projectid = useproject[:id]
471
- useapp = choose_app(projectid)
472
- appname = useapp[:name]
473
- end
474
- app = client.app_get_byname(appname)
475
- err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)
376
+ def create_env(appid_or_appname=nil)
377
+ client.check_login_status
378
+ app = choose_app_help_target_not_all(appid_or_appname)
379
+
476
380
  name = nil
477
381
  loop{
478
382
  name = ask "Environment Name" unless name
479
- if env_exists_byappname?(app[:name],name)
480
- display "Environment '#{name}' already exists."
383
+ if not check_envname_valid?(name)
384
+ display "Invalid environment name '#{name}'."
481
385
  name =nil
482
386
  next
387
+ else if env_exists?(app[:id],name)
388
+ display "Environment '#{name}' already exists."
389
+ name =nil
390
+ next
483
391
  else
484
392
  break
485
393
  end
394
+ end
486
395
  }
487
396
  value = ask "Environment Value"
488
397
  manifest = {
489
398
  :name => name,
490
399
  :value => value
491
400
  }
492
- client.create_env(appname, manifest)
401
+ client.create_env(app[:id], manifest)
493
402
  display "OK".green
494
403
  end
495
404
 
496
- def delete_env(appname=nil)
497
- unless appname
498
- useproject = choose_project
499
- projectid = useproject[:id]
500
- useapp = choose_app(projectid)
501
- appname = useapp[:name]
502
- end
503
- app = client.app_get_byname(appname)
504
- err "The application is not found! App name :#{appname}" unless (appname == app[:name] ? true : false)
505
- name = ask "Environment Name"
506
- err "No Environment '#{name}'!" unless env_exists_byappname?(app[:name],name)
507
- value = nil
508
- env = client.app_env(app[:name])
509
- env.each do |e|
510
- value = e[:value] if name == e[:name]
405
+ def delete_env(appid_or_appname=nil)
406
+ 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]
414
+ 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
+
422
+ delete_envs = Array.new
423
+ envs.each do |e|
424
+ name.each do |n|
425
+ delete_envs.push("#{n}=#{e[:value]}") if n == e[:name]
426
+ end
511
427
  end
512
- manifest = "#{name}=#{value}".to_a
513
- client.delete_env(appname,manifest)
514
- display "OK".green
428
+
429
+ delete_envs.each do |env|
430
+ display "Deleting environment '#{env}': ",false
431
+ client.delete_env(app[:id], env)
432
+ display "OK".green
433
+ end
515
434
  end
516
435
 
517
- def app_log(appid=nil)
518
- unless appid
519
- useproject = choose_project
520
- projectid = useproject[:id]
521
- useapp = choose_app(projectid)
522
- appid = useapp[:id]
523
- end
524
- app = client.app_get_byid(appid)
525
- err "The application is not found! App id :#{appid}" unless (appid.to_f == app[:id] ? true : false)
436
+ def app_log(appid_or_appname=nil)
437
+ client.check_login_status
438
+ app = choose_app_help_target_not_all(appid_or_appname)
439
+
526
440
  max_index = app[:instances]-1
527
441
  instanceindex = ask "Select instance index(from 0 to #{max_index})", :default => 0
528
442
  filepath = "logs/"
529
443
  logs = client.app_log(app[:id],filepath,instanceindex)
530
- logs = logs[1].to_a
444
+ logs = logs[1].split("\n")
531
445
  file = ask "Select logs, indexed list?", :choices => logs, :indexed => true
532
446
  display "Selected logs: ",false
533
447
  file = file.split(' ')
@@ -541,19 +455,43 @@ module PI::Cli::Command
541
455
 
542
456
  private
543
457
 
544
- def app_exists_byappname?(appname=nil)
545
- app = client.app_get_byname(appname)
546
- appname == app[:name] ? true : false
458
+ def app_exists?(target, appname)
459
+ app = client.app_search(target,appname)
460
+ app[:id] != 0 ? true : false
547
461
  end
548
462
 
549
- def app_exists_byappid?(appid=nil)
550
- app = client.app_get_byid(appid)
551
- appid.to_f == app[:id] ? true : false
463
+ def env_exists?(appid, envname)
464
+ env = client.env_verify(appid, envname)
465
+ "true" == env[1] ? false : true
552
466
  end
553
467
 
554
- def env_exists_byappname?(appname,name)
555
- env = client.env_verify(appname,name)
556
- "true" == env[1] ? false : true
468
+ def do_start_app(app)
469
+ display "Starting application '#{app[:name]}'(target:#{app[:targetName]}): ",false
470
+ t = Thread.new do
471
+ loop do
472
+ display '.', false
473
+ sleep (1)
474
+ break unless t.alive?
475
+ end
476
+ end
477
+ client.start_app(app[:id])
478
+ result = check_status(app[:targetName], app[:name], "start")
479
+ Thread.kill(t)
480
+ if result[:code] == 200
481
+ if not result[:text].empty?
482
+ display "OK".green
483
+ else
484
+ display "Please try again"
485
+ end
486
+ else
487
+ err result[:text]
488
+ end
489
+ end
490
+
491
+ def do_stop_app(app)
492
+ display "Stoping application '#{app[:name]}'(target:#{app[:targetName]}): ",false
493
+ client.stop_app(app[:id])
494
+ display "OK".green
557
495
  end
558
496
 
559
497
  def mem_choice_to_quota(mem_choice)