require "vmc/detect" module VMC::App module Create attr_accessor :input attr_writer :path def get_inputs inputs = {} inputs[:name] = name = input[:name] inputs[:infra] = infra = determine_infra if infras_enabled? inputs[:url] = input[:url, name, infra] inputs[:uris] = [inputs[:url]] inputs[:total_instances] = input[:instances] inputs[:space] = client.current_space if client.current_space inputs[:production] = !!(input[:plan] =~ /^p/i) if v2? inputs[:framework] = framework = determine_framework inputs[:command] = input[:command] if can_have_custom_start_command?(framework) inputs[:runtime] = determine_runtime(framework) inputs[:buildpack] = input[:buildpack] if v2? human_mb = human_mb(detector.suggested_memory(framework) || 64) inputs[:memory] = megabytes(input[:memory, human_mb]) inputs end def ask_url(name, infra) urlbase = !infra.nil? ? "#{infra.base}" : "no-infra.af.cm" ask("Enter URL?", :default => "#{name}.#{urlbase}") end def determine_infra return input[:infra] if input.has?(:infra) input[:infra, detector.all_infras, nil, nil] end def determine_framework return input[:framework] if input.has?(:framework) if (detected_framework = detector.detect_framework) input[:framework, [detected_framework], detected_framework, :other] else input[:framework, detector.all_frameworks, nil, nil] end end def determine_runtime(framework) return input[:runtime] if input.has?(:runtime) detected_runtimes = if framework.name == "standalone" detector.detect_runtimes else detector.runtimes(framework) end default_runtime = detected_runtimes.size == 1 ? detected_runtimes.first : nil if detected_runtimes.empty? input[:runtime, detector.all_runtimes, nil, nil] else input[:runtime, detected_runtimes, default_runtime, :other] end end def create_app(inputs) app = client.app inputs.each { |key, value| app.send(:"#{key}=", value) unless value.nil? } app = filter(:create_app, app) with_progress("Creating #{c(app.name, :name)}") do wrap_message_format_errors do app.create! end end app end def map_route(app) line unless quiet? host = input[:host, app.name] if v2? domain = input[:domain, app] mapped_url = false until domain == "none" || !domain || mapped_url begin host = "" if host == "none" invoke :map, :app => app, :host => host, :domain => domain mapped_url = true rescue CFoundry::RouteHostTaken, CFoundry::UriAlreadyTaken => e raise if force? line c(e.description, :bad) line input.forget(:host) if v2? input.forget(:domain) host = input[:host, app.name] if v2? domain = input[:domain, app] # version bumps on v1 even though mapping fails app.invalidate! unless v2? end end end def create_services(app) return unless input[:create_services] while true invoke :create_service, { :app => app }, :plan => :interact break unless ask("Create another service?", :default => false) end end def bind_services(app) return unless input[:bind_services] while true invoke :bind_service, :app => app break if (all_instances - app.services).empty? break unless ask("Bind another service?", :default => false) end end def start_app(app) invoke :start, :app => app if input[:start] end private def can_have_custom_start_command?(framework) %w(standalone buildpack).include?(framework.name) end def all_instances @all_instances ||= client.service_instances end def detector @detector ||= VMC::Detector.new(client, @path) end def target_base client.target.sub(/^https?:\/\/([^\.]+\.)?(.+)\/?/, '\2') end end end