vmc-ng/lib/vmc/cli/app.rb in vmc-0.4.0.beta.36 vs vmc-ng/lib/vmc/cli/app.rb in vmc-0.4.0.beta.37

- old
+ new

@@ -3,12 +3,10 @@ require "vmc/cli" require "vmc/detect" module VMC class App < CLI - MEM_CHOICES = ["64M", "128M", "256M", "512M"] - # TODO: don't hardcode; bring in from remote MEM_DEFAULTS_FRAMEWORK = { "rails3" => "256M", "spring" => "512M", "grails" => "512M", @@ -41,11 +39,11 @@ :from_given => by_name("space") 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) + def apps if space = input[:space] || client.current_space apps = with_progress("Getting applications in #{c(space.name, :name)}") do # depth of 2 for service binding instance names space.apps(2) @@ -77,11 +75,11 @@ desc "Show app information" group :apps input :app, :argument => :required, :from_given => by_name("app"), :desc => "App to show" - def app(input) + def app display_app(input[:app]) end desc "Push an application, syncing changes if it exists" group :apps, :manage @@ -93,11 +91,12 @@ input(:url, :desc => "URL bound to app") { |default| ask("URL", :default => default) } input(:memory, :desc => "Memory limit") { |framework, runtime| ask("Memory Limit", - :choices => MEM_CHOICES, + :choices => memory_choices, + :allow_other => true, :default => MEM_DEFAULTS_RUNTIME[runtime] || MEM_DEFAULTS_FRAMEWORK[framework] || "64M") } @@ -142,11 +141,11 @@ } input(:bind_services, :type => :boolean, :desc => "Interactively bind services?") { ask "Bind other services to application?", :default => false } - def push(input) + def push path = File.expand_path(input[:path]) name = input[:name] if exists = client.app_by_name(name) @@ -248,11 +247,11 @@ input :apps, :argument => :splat, :singular => :app, :desc => "Applications to start", :from_given => by_name("app") input :debug_mode, :aliases => "-d", :desc => "Debug mode to start in" - def start(input) + def start apps = input[:apps] fail "No applications given." if apps.empty? spaced(apps) do |app| app = filter(:start_app, app) @@ -285,11 +284,11 @@ desc "Stop an application" group :apps, :manage input :apps, :argument => :splat, :singular => :app, :desc => "Applications to start", :from_given => by_name("app") - def stop(input) + def stop apps = input[:apps] fail "No applications given." if apps.empty? spaced(apps) do |app| with_progress("Stopping #{c(app.name, :name)}") do |s| @@ -310,11 +309,11 @@ input :apps, :argument => :splat, :singular => :app, :desc => "Applications to start", :from_given => by_name("app") input :debug_mode, :aliases => "-d", :desc => "Debug mode to start in" - def restart(input) + def restart invoke :stop, :apps => input[:apps] invoke :start, :apps => input[:apps], :debug_mode => input[:debug_mode] end @@ -335,11 +334,11 @@ } input :orphaned, :aliases => "-o", :type => :boolean, :desc => "Delete orphaned instances" input :all, :default => false, :desc => "Delete all applications" - def delete(input) + def delete apps = client.apps if input[:all] return unless input[:really, "ALL APPS", :bad] @@ -373,11 +372,11 @@ desc "List an app's instances" group :apps, :info, :hidden => true input :apps, :argument => :splat, :singular => :app, :desc => "Applications to start", :from_given => by_name("app") - def instances(input) + def instances no_v2 apps = input[:apps] fail "No applications given." if apps.empty? @@ -406,16 +405,17 @@ input(:instances, :type => :numeric, :desc => "Number of instances to run") { |default| ask("Instances", :default => default) } input(:memory, :desc => "Memory limit") { |default| - ask("Memory Limit", :choices => MEM_CHOICES, + ask("Memory Limit", :choices => memory_choices(default), + :allow_other => true, :default => human_size(default * 1024 * 1024, 0)) } input :restart, :type => :boolean, :default => true, :desc => "Restart app after updating?" - def scale(input) + def scale app = input[:app] instances = input.given(:instances) memory = input.given(:memory) @@ -450,11 +450,11 @@ :from_given => by_name("app") input :instance, :type => :numeric, :default => 0, :desc => "Instance of application to get the logs of" input :all, :default => false, :desc => "Get logs for every instance" - def logs(input) + def logs no_v2 app = input[:app] instances = @@ -500,11 +500,11 @@ input :app, :argument => true, :desc => "Application to inspect the files of", :from_given => by_name("app") input :path, :argument => true, :default => "/", :desc => "Path of file to read" - def file(input) + def file no_v2 app = input[:app] file = @@ -522,11 +522,11 @@ input :app, :argument => true, :desc => "Application to inspect the files of", :from_given => by_name("app") input :path, :argument => true, :default => "/", :desc => "Path of directory to list" - def files(input) + def files no_v2 app = input[:app] files = @@ -544,11 +544,11 @@ desc "Get application health" group :apps, :info, :hidden => true input :apps, :argument => :splat, :singular => :app, :desc => "Applications to start", :from_given => by_name("app") - def health(input) + def health apps = input[:apps] fail "No applications given." if apps.empty? health = with_progress("Getting health status") do @@ -567,11 +567,11 @@ desc "Display application instance status" group :apps, :info, :hidden => true input :app, :argument => true, :desc => "Application to get the stats for", :from_given => by_name("app") - def stats(input) + def stats no_v2 app = input[:app] stats = @@ -605,11 +605,11 @@ input :app, :argument => true, :desc => "Application to add the URL to", :from_given => by_name("app") input :url, :argument => true, :desc => "URL to route" - def map(input) + def map no_v2 app = input[:app] simple = input[:url].sub(/^https?:\/\/(.*)\/?/i, '\1') @@ -627,11 +627,11 @@ :desc => "Application to remove the URL from", :from_given => by_name("app") input(:url, :argument => true, :desc => "URL to unmap") { |choices| ask("Which URL?", :choices => choices) } - def unmap(input) + def unmap no_v2 app = input[:app] url = input[:url, app.urls] @@ -653,11 +653,11 @@ desc "Show all environment variables set for an app" group :apps, :info, :hidden => true input :app, :argument => true, :desc => "Application to inspect the environment of", :from_given => by_name("app") - def env(input) + def env app = input[:app] vars = with_progress("Getting env for #{c(app.name, :name)}") do |s| app.env @@ -682,11 +682,11 @@ :desc => "Environment variable name" input :value, :argument => :optional, :desc => "Environment variable value" input :restart, :type => :boolean, :default => true, :desc => "Restart app after updating?" - def set_env(input) + def set_env app = input[:app] name = input[:name] if value = input[:value] name = input[:name] @@ -716,11 +716,11 @@ :from_given => by_name("app") input :name, :argument => true, :desc => "Environment variable name" input :restart, :type => :boolean, :default => true, :desc => "Restart app after updating?" - def unset_env(input) + def unset_env app = input[:app] name = input[:name] with_progress("Updating #{c(app.name, :name)}") do app.env.delete(name) @@ -733,11 +733,11 @@ end desc "DEPRECATED. Use 'push' instead." input :app, :argument => :optional - def update(input) + def update fail "The 'update' command is no longer needed; use 'push' instead." end private @@ -974,8 +974,24 @@ format("%.#{precision}fB", num) end def target_base client.target.sub(/^https?:\/\/([^\.]+\.)?(.+)\/?/, '\2') + end + + def memory_choices(exclude = 0) + info = client.info + used = info[:usage][:memory] + limit = info[:limits][:memory] + available = limit - used + exclude + + mem = 64 + choices = [] + until mem > available + choices << human_size(mem * 1024 * 1024, precision = 0) + mem *= 2 + end + + choices end end end