Class: PI::Cli::Command::Apps
- Includes:
- PI::Cli::ChooseHelper, InteractHelper
- Defined in:
- lib/cli/commands/apps.rb
Constant Summary collapse
- DEFAULTS =
{ "mem" => "64M", "instances" => 1, "action_for_change" => "notify", "graceful" => true, "lazy" => true, "isDebug" => false, "needMonitor" => false, "probe" => false }
Constants included from InteractHelper
InteractHelper::ESCAPES, InteractHelper::EVENTS
Constants included from PI::Cli::ChooseHelper
PI::Cli::ChooseHelper::NO_SET, PI::Cli::ChooseHelper::YES_SET
Instance Method Summary collapse
- #app_env(appid_or_appname = nil) ⇒ Object
- #app_log(appid_or_appname = nil) ⇒ Object
- #apps ⇒ Object
- #create_app(appname = nil) ⇒ Object
- #create_env(appid_or_appname = nil) ⇒ Object
- #delete_app(appid_or_appname = nil) ⇒ Object
- #delete_env(appid_or_appname = nil) ⇒ Object
- #restart_app(appid_or_appname = nil) ⇒ Object
- #scale_app(appid_or_appname = nil) ⇒ Object
- #start_app(appid_or_appname = nil) ⇒ Object
- #status(appid_or_appname = nil) ⇒ Object
- #stop_app(appid_or_appname = nil) ⇒ Object
- #update_app(appid_or_appname = nil) ⇒ Object
Methods included from InteractHelper
#asks, #read_char, #read_event, #read_line
Methods included from PI::Cli::ChooseHelper
#check_envname_valid?, #check_envvalue_valid?, #check_name_valid?, #check_status, #check_version_name_valid?, #choose_app, #choose_app_help, #choose_app_help_target_not_all, #choose_dnsid, #choose_project, #choose_serviceid, #choose_target, #get_graceful, #select_apps, #total_opts
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(appid_or_appname = nil) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/cli/commands/apps.rb', line 416 def app_env(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) env = client.app_env(appid) 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_or_appname = nil) ⇒ Object
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/cli/commands/apps.rb', line 520 def app_log(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|instanceid|file)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status instanceindex = @options[:instanceid] file = @options[:file] app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) max_index = app[:instances]-1 instanceindex = ask "Select instance index(from 0 to #{max_index})", :default => 0 unless instanceindex unless file filepath = "logs/" logs = client.app_log(appid,filepath,instanceindex) logs = logs[1].split("\n") file = ask "Select logs, indexed list?", :choices => logs, :indexed => true display "Selected logs: ",false file = file.split(' ') file = file[0] display "#{file}" end filepath = "logs/#{file}" logs = client.app_log(appid,filepath,instanceindex) display "\n" display logs[1] end |
#apps ⇒ Object
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 71 72 |
# File 'lib/cli/commands/apps.rb', line 18 def apps all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(projectid|target)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status projectid = @options[:projectid] target = @options[:target] projectid = 0 if projectid == "all" || projectid == nil target = nil if target == "all" app_sum = client.app_sum(projectid, target) total_app = Integer(app_sum[1]) return display "No Applications" if total_app == 0 pageNum = -1 numPerPage = -1 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}".red 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}".red pageNum =nil next else break end } pageNum = pageNum.to_i end apps = client.apps(projectid, target, pageNum, numPerPage) return display "No Applications" if apps.nil? || apps.empty? return display JSON.pretty_generate(apps) if @options[:json] apps.sort! {|a, b| a[:name] <=> b[:name] } apps_table = table do |t| t.headings = 'ID', 'Target', 'App name', 'URL', 'Deploy Type','Version', 'Status', 'Instances', 'Running Instances' apps.each do |app| t << [app[:id], app[:targetName], app[:name] ,app[:url], app[:deployType], app[:tag].empty? ? "Latest" : app[:tag], app[:status], app[:instances], app[:runInstances]] end end display apps_table end |
#create_app(appname = nil) ⇒ Object
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 |
# File 'lib/cli/commands/apps.rb', line 74 def create_app(appname=nil) all_opts = total_opts all_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status binarylist, deployType, instances = nil, nil, nil if appname if not check_name_valid?(appname) err "Invalid application name '#{appname}'." end end # choose the target targets = client.targets err "No Targets!" if targets.nil? || targets.empty? target = ask "Select Target", :choices => targets.collect { |t| t[:name] }, :indexed => true display "Selected Target: ",false display "#{target}" target_memory = client.target_memory(target) loop{ appname = ask "Application Name" unless appname if not check_name_valid?(appname) display "Invalid application name '#{appname}'.".red appname =nil next else if app_exists?(target, appname) display "Application '#{appname}' already exists.".red appname =nil next else break end end } # choose the project useproject = choose_project projectname = useproject[:name] projectid = useproject[:id] # choose the tag if useproject[:runtime] =~ /^(ruby)/i deployType = "git" choices = ["By Tag Name","By Commit ID","Latest Revision"] select_type = ask"Upgrade Type", :choices => choices, :indexed => true case select_type when "By Tag Name" = client.(projectid) err "No tags!" if .nil? || .empty? tag = ask "Select Tag", :choices => , :indexed => true display "Selected Tag: ",false display "#{tag}" display "Branch : Master" when "By Commit ID" commits = client.project_commits_bybranch(projectid) tag = ask "Select Commit ID", :choices => commits.collect { |c| c[:commit] }, :indexed => true when "Latest Revision" tag = "" else err "Invalid Input" end else deployType = "binary" binarylist = client.project_binary(projectid) err "No version!" if binarylist.nil? || binarylist.empty? tag = ask "Select Version", :choices => binarylist.collect { |b| b[:versionName] }, :indexed => true display "Selected Version: ",false display "#{tag}" end # URL combination user = client.user_info url = "#{appname}.#{user[:userName]}.#{target}.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) target_memory = Integer(target_memory[1]) max_instance = (target_memory.to_f/mem.to_i).floor display ("Available memory: #{target_memory} M, maximum instances: #{max_instance}") loop{ instances = ask "How many instances(from 1 to #{max_instance})?", :default => DEFAULTS["instances"] if not instances.to_s =~ /^[1-9]\d*$/ display "Invalid Number!".red instances =nil next else if instances < 1 || instances > max_instance display "Invalid Number! Please input number from 1 to #{max_instance}".red instances =nil next else break end end } isDebug = false isDebug = ask "Need debug?", :default => DEFAULTS["isDebug"] if deployType == "binary" isDebug = (isDebug == false ? "no" : "yes") choices = ["restart", "notify"] action_for_change = ask"Reaction when service changed", :default => DEFAULTS["action_for_change"], :choices => choices, :indexed => true probe = false probe = ask "Need JMX monitoring?", :default => DEFAULTS["probe"] if deployType == "binary" probe = (probe == false ? "no" : "yes") # needMonitor = ask "Need monitor?", :default => DEFAULTS["needMonitor"] # needMonitor = (needMonitor == false ? "no" : "yes") manifest = { :targetName => target, :name => "#{appname}", :projectName => "#{projectname}", :projectId => "#{projectid}", :branch => "master", :url => url, :memory => mem_quota, :instances => instances, :deployType => deployType, :tag => tag, :isDebug => isDebug, :action_for_change => action_for_change, :needMonitor => "no", :probe => probe } display "Creating application \"#{appname}\": ",false t = Thread.new do loop do display '.', false sleep (1) break unless t.alive? end end tmp = client.create_app(manifest) uuid = tmp[1] result = check_status(uuid) Thread.kill(t) end |
#create_env(appid_or_appname = nil) ⇒ Object
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/cli/commands/apps.rb', line 438 def create_env(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|name|value)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status name = @options[:name] value = @options[:value] app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) loop{ name = ask "Environment Name" unless name if not check_envname_valid?(name) name =nil next else if env_exists?(appid,name) display "Environment '#{name}' already exists.".red name =nil next else break end end } loop{ value = ask "Environment Value" unless value if not check_envvalue_valid?(value) display "Invalid environment value '#{value}'.".red value =nil next else break end } manifest = { :name => name, :value => value } client.create_env(appid, manifest) display "OK".green end |
#delete_app(appid_or_appname = nil) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/cli/commands/apps.rb', line 217 def delete_app(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : 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(appid_or_appname = nil) ⇒ Object
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/cli/commands/apps.rb', line 482 def delete_env(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|name)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status name = @options[:name] app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) envs = client.app_env(appid) err "No environments available to delete" if envs.nil? || envs.empty? if name name = name.split(",") else name = asks "Environment Name", :choices => envs.collect { |e| e[:name] }, :indexed => true display "Selected Environment Name: ",false name.each do |m| display "#{m} ",false end display "\n" end delete_envs = Array.new envs.each do |e| name.each do |n| delete_envs.push("#{n}=#{e[:value]}") if n == e[:name] end end err " The environment is not exist!" if delete_envs.nil? || delete_envs.empty? delete_envs.each do |env| display "Deleting environment '#{env}': ",false client.delete_env(appid, env) display "OK".green end end |
#restart_app(appid_or_appname = nil) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/cli/commands/apps.rb', line 282 def restart_app(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|graceful)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status target_not_all, app, appid = choose_app_help(appid_or_appname) if target_not_all appid = (appid == nil ? app[:id] : appid) do_restart_app(app, appid) else app.each do |a| do_restart_app(a, a[:id]) end end end |
#scale_app(appid_or_appname = nil) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 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 |
# File 'lib/cli/commands/apps.rb', line 300 def scale_app(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|instance)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status instance = @options[:instance] app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) target_memory = client.target_memory(app[:targetName]) target_memory = Integer(target_memory[1]) max_instance = (target_memory.to_f/app[:memory].to_i).floor display ("Available memory: #{target_memory} M, maximum instances: #{max_instance}") loop{ instance = ask "Current instance: #{app[:instances]}, Instance number" unless instance if not instance.to_s =~ /^[1-9]\d*$/ display "Invalid Number!".red instance =nil next else if instance.to_i < 1 || instance.to_i > max_instance display "Invalid Number! Please input number from 1 to #{max_instance}".red instance =nil next else break end end } err "Don't need scale application" if instance.to_s == app[:instances].to_s # graceful = get_graceful(app) display "Scaling application: ",false t = Thread.new do loop do display '.', false sleep (1) break unless t.alive? end end tmp = client.scale_app(appid, instance) uuid = tmp[1] result = check_status(uuid) Thread.kill(t) end |
#start_app(appid_or_appname = nil) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/cli/commands/apps.rb', line 242 def start_app(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status target_not_all, app, appid = choose_app_help(appid_or_appname) if target_not_all appid = (appid == nil ? app[:id] : appid) return display "The application '#{app[:name]}'(target:#{app[:targetName]}) has already started.".yellow if app[:status] == "STARTED" do_start_app(app, appid) else app.each do |a| next display "The application '#{a[:name]}'(target:#{a[:targetName]}) has already started.".yellow if a[:status] == "STARTED" do_start_app(a, a[:id]) end end end |
#status(appid_or_appname = nil) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/cli/commands/apps.rb', line 395 def status(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) status = client.status(appid) 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_or_appname = nil) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/cli/commands/apps.rb', line 262 def stop_app(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|graceful)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status target_not_all, app, appid = choose_app_help(appid_or_appname) if target_not_all appid = (appid == nil ? app[:id] : appid) # return display "The application '#{app[:name]}'(target:#{app[:targetName]}) has already stopped.".yellow if app[:status] == "STOPPED" do_stop_app(app, appid) else app.each do |a| next display "The application '#{a[:name]}'(target:#{a[:targetName]}) has already stopped.".yellow if a[:status] == "STOPPED" do_stop_app(a, a[:id]) end end end |
#update_app(appid_or_appname = nil) ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 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 |
# File 'lib/cli/commands/apps.rb', line 347 def update_app(appid_or_appname=nil) all_opts = total_opts all_opts_except_valid_opts = all_opts.delete_if{ |key,value| key.to_s =~ /(target|ver|graceful)/i} all_opts_except_valid_opts.each_value do |value| err "Invalid options" if value != nil end client.check_login_status tag = @options[:ver] app, appid = choose_app_help_target_not_all(appid_or_appname) appid = (appid == nil ? app[:id] : appid) if app[:deployType] == "git" = client.(app[:projectId]) err "No tags!" if .nil? || .empty? if tag err "Invalid tag" unless .include?(tag) else tag = ask "Select Tag", :choices => , :indexed => true end err "Don't need update application!" if tag == app[:tag] else = client.project_binary(app[:projectId]) err "No version!" if .nil? || .empty? if tag = .collect { |b| b[:versionName] } err "Invalid version" unless .include?(tag) else tag = ask "Select Version", :choices => .collect { |b| b[:versionName] }, :indexed => true end err "Don't need update application!" if tag == app[:tag] end graceful = get_graceful(app) # lazy = get_lazy(app) display "Updating application: ",false t = Thread.new do loop do display '.', false sleep (1) break unless t.alive? end end tmp = client.update_app(appid,tag,graceful) uuid = tmp[1] result = check_status(uuid) Thread.kill(t) end |