# File lib/rhc/commands/app.rb, line 27
    def create(name, cartridges)
      cartridge = check_cartridges(cartridges).first

      options.default \
        :dns => true,
        :git => true

      header "Creating application '#{name}'"
      paragraph do
        table({"Namespace:" => options.namespace,
               "Cartridge:" => cartridge,
               "Gear Size:" => options.gear_size || "default",
               "Scaling:" => options.scaling ? "yes" : "no",
              }
             ).each { |s| say "  #{s}" }
      end

      raise RHC::DomainNotFoundException.new("No domains found. Please create a domain with 'rhc domain create <namespace>' before creating applications.") if rest_client.domains.empty?

      rest_domain = rest_client.find_domain(options.namespace)

      # check to make sure the right options are set for enabling jenkins
      jenkins_rest_app = check_jenkins(name, rest_domain) if enable_jenkins?

      # create the main app
      rest_app = create_app(name, cartridge, rest_domain,
                            options.gear_size, options.scaling)

      # create a jenkins app if not available
      # don't error out if there are issues, setup warnings instead
      begin
        jenkins_rest_app = setup_jenkins_app(rest_domain) if enable_jenkins? and jenkins_rest_app.nil?
      rescue Exception => e
        add_issue("Jenkins failed to install - #{e}",
                  "Installing jenkins and jenkins-client",
                  "rhc app create jenkins",
                  "rhc cartridge add jenkins-client -a #{rest_app.name}")
      end

      if jenkins_rest_app
        success, attempts, exit_code, exit_message = false, 1, 157, nil
        while (!success && exit_code == 157 && attempts < MAX_RETRIES)
          begin
            setup_jenkins_client(rest_app)
            success = true
          rescue RHC::Rest::ServerErrorException => e
            if (e.code == 157)
              # error downloading Jenkins /jnlpJars/jenkins-cli.jar
              attempts += 1
              debug "Jenkins server could not be contacted, sleep and then retry: attempt #{attempts}\n    #{e.message}"
              Kernel.sleep(10)
            end
            exit_code = e.code
            exit_message = e.message
          rescue Exception => e
            # timeout and other exceptions
            exit_code = 1
            exit_message = e.message
          end
        end
        add_issue("Jenkins client failed to install - #{exit_message}",
                  "Install the jenkins client",
                  "rhc cartridge add jenkins-client -a #{rest_app.name}") if !success
      end

      if options.dns
        say "Your application's domain name is being propagated worldwide (this might take a minute)..."
        unless dns_propagated? rest_app.host
          add_issue("We were unable to lookup your hostname (#{rest_app.host}) in a reasonable amount of time and can not clone your application.",
                    "Clone your git repo",
                    "rhc app git-clone #{rest_app.name}")

          output_issues(rest_app)
          return 0
        end

        if options.git
          begin
            run_git_clone(rest_app)
          rescue RHC::GitException => e
            warn "#{e}"
            unless RHC::Helpers.windows? and windows_nslookup_bug?(rest_app)
              add_issue("We were unable to clone your application's git repo - #{e}",
                        "Clone your git repo",
                        "rhc app git-clone #{rest_app.name}")
            end
          end
        end
      end

      display_app(rest_app, rest_app.cartridges, rest_app.scalable_carts.first)

      if issues?
        output_issues(rest_app)
      else
        results { 
          rest_app.messages.each { |msg| say msg } 
          jenkins_rest_app.messages.each { |msg| say msg } if enable_jenkins? and jenkins_rest_app
        }
      end

      0
    end