vmc-ng/lib/vmc/cli/app.rb in vmc-0.4.0.beta.13 vs vmc-ng/lib/vmc/cli/app.rb in vmc-0.4.0.beta.14

- old
+ new

@@ -38,14 +38,22 @@ input :name, :desc => "Filter by name regexp" input :runtime, :desc => "Filter by runtime regexp" input :framework, :desc => "Filter by framework regexp" input :url, :desc => "Filter by url regexp" def apps(input) - apps = - with_progress("Getting applications") do - client.apps - end + if v2? + space = client.current_space + apps = + with_progress("Getting applications in #{c(space.name, :name)}") do + space.apps + end + else + apps = + with_progress("Getting applications") do + client.apps + end + end if apps.empty? and !quiet? puts "" puts "No applications." return @@ -105,39 +113,56 @@ def push(input) path = File.expand_path(input[:path] || ".") name = input[:name] if input[:name] - detector = Detector.new(client, path) - frameworks = detector.all_frameworks - detected, default = detector.frameworks + exists = + if v2? + client.current_space.apps.find { |a| a.name == name } + elsif client.app(name).exists? + client.app(name) + end - app = client.app(name) - - if app.exists? - upload_app(app, path) - invoke :restart, :name => app.name if input[:restart] + if exists + upload_app(exists, path) + invoke :restart, :name => exists.name if input[:restart] return end + detector = Detector.new(client, path) + frameworks = detector.all_frameworks + detected, default = detector.frameworks + + app = client.app + app.name = name + app.space = client.current_space if v2? app.total_instances = input[:instances] + framework_names = frameworks.collect(&:name).sort if detected.empty? - framework = input[:framework, frameworks.keys.sort, nil] + framework_name = input[:framework, framework_names, nil] else - framework = input[:framework, detected.keys.sort + ["other"], default] - if framework == "other" + detected_names = detected.collect(&:name).sort + framework_name = + input[:framework, detected_names + ["other"], default] + + if framework_name == "other" input.forget(:framework) - framework = input[:framework, frameworks.keys.sort, nil] + framework_name = input[:framework, framework_names, nil] end end - framework_runtimes = - frameworks[framework]["runtimes"].collect { |k| k["name"] } + framework = frameworks.find { |f| f.name == framework_name } - runtime = input[:runtime, framework_runtimes.sort] + runtimes = v2? ? client.runtimes : framework.runtimes + runtime_name = input[:runtime, runtimes.collect(&:name).sort] + runtime = runtimes.find { |r| r.name == runtime_name } + fail "Invalid framework '#{input[:framework]}'" unless framework + + fail "Invalid runtime '#{input[:runtime]}'" unless runtime + app.framework = framework app.runtime = runtime if framework == "standalone" app.command = input[:command] @@ -153,11 +178,11 @@ end app.memory = megabytes(input[:memory, framework, runtime]) bindings = [] - if input[:create_services] && !force? + if !v2? && input[:create_services] && !force? services = client.system_services while true vendor = ask "What kind?", :choices => services.keys.sort meta = services[vendor] @@ -186,11 +211,11 @@ break unless ask "Create another service?", :default => false end end - if input[:bind_services] && !force? + if !v2? && input[:bind_services] && !force? services = client.services.collect(&:name) while true choices = services - bindings break if choices.empty? @@ -774,11 +799,11 @@ status = app_status(a) puts "#{c(a.name, :name)}: #{status}" - puts " platform: #{b(a.framework)} on #{b(a.runtime)}" + puts " platform: #{b(a.framework.name)} on #{b(a.runtime.name)}" print " usage: #{b(human_size(a.memory * 1024 * 1024, 0))}" print " #{c(IS_UTF8 ? "\xc3\x97" : "x", :dim)} #{b(a.total_instances)}" print " instance#{a.total_instances == 1 ? "" : "s"}" puts "" @@ -791,10 +816,14 @@ puts " services: #{a.services.collect { |s| b(s) }.join(", ")}" end end def upload_app(app, path) + if v2? + fail "V2 API currently does not support uploading or starting apps." + end + with_progress("Uploading #{c(app.name, :name)}") do app.upload(path) end end @@ -919,8 +948,52 @@ }.each do |s| with_progress("Deleting service #{c(s, :name)}") do client.service(s).delete! end end + end + + def usage(used, limit) + "#{b(human_size(used))} of #{b(human_size(limit, 0))}" + end + + def percentage(num, low = 50, mid = 70) + color = + if num <= low + :good + elsif num <= mid + :warning + else + :bad + end + + c(format("%.1f\%", num), color) + end + + def megabytes(str) + if str =~ /T$/i + str.to_i * 1024 * 1024 + elsif str =~ /G$/i + str.to_i * 1024 + elsif str =~ /M$/i + str.to_i + elsif str =~ /K$/i + str.to_i / 1024 + else # assume megabytes + str.to_i + end + end + + def human_size(num, precision = 1) + sizes = ["G", "M", "K"] + sizes.each.with_index do |suf, i| + pow = sizes.size - i + unit = 1024 ** pow + if num >= unit + return format("%.#{precision}f%s", num / unit, suf) + end + end + + format("%.#{precision}fB", num) end end end