class RHC::Commands::App

Public Instance Methods

create(name, cartridge, additional_cartridges) click to toggle source
# File lib/rhc/commands/app.rb, line 26
def create(name, cartridge, additional_cartridges)
  options.default          :dns => true,
    :git => true

  warnings = []
  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 options.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 options.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}"
          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 options.enable_jenkins and jenkins_rest_app
    }
  end

  0
end
delete(app) click to toggle source
# File lib/rhc/commands/app.rb, line 156
def delete(app)
  rest_domain = rest_client.find_domain(options.namespace)
  rest_app = rest_domain.find_application(app)
  do_delete = true

  do_delete = agree "Are you sure you wish to delete the '#{rest_app.name}' application? (yes/no)" unless options.confirm

  if do_delete
    paragraph { say "Deleting application '#{rest_app.name}'" }
    rest_app.destroy
    results { say "Application '#{rest_app.name}' successfully deleted" }
  end
  0
end
force_stop(app) click to toggle source
# File lib/rhc/commands/app.rb, line 197
def force_stop(app)
  app_action app, :stop, true

  results { say "#{app} force stopped" }
  0
end
git_clone(app) click to toggle source

TODO: Implement default values for arguments once ffranz has added context arguments argument :directory, "The name of a new directory to clone into", [], :default => nil

# File lib/rhc/commands/app.rb, line 140
def git_clone(app)
  rest_domain = rest_client.find_domain(options.namespace)
  rest_app = rest_domain.find_application(app)
  run_git_clone(rest_app)
  0
end
reload(app) click to toggle source
# File lib/rhc/commands/app.rb, line 219
def reload(app)
  app_action app, :reload

  results { say "#{app} config reloaded" }
  0
end
restart(app) click to toggle source
# File lib/rhc/commands/app.rb, line 208
def restart(app)
  app_action app, :restart

  results { say "#{app} restarted" }
  0
end
show(app) click to toggle source
# File lib/rhc/commands/app.rb, line 242
def show(app)
  rest_domain = rest_client.find_domain(options.namespace)
  rest_app = rest_domain.find_application(app)
  unless options.state
    display_app(rest_app,rest_app.cartridges,rest_app.scalable_carts.first)
  else
    results do
      rest_app.gear_groups.each do |gg|
        say "Geargroup #{gg.cartridges.collect { |c| c['name'] }.join('+')} is #{gg.gears.first['state']}"
      end
    end
  end
  0
end
start(app) click to toggle source
# File lib/rhc/commands/app.rb, line 175
def start(app)
  app_action app, :start

  results { say "#{app} started" }
  0
end
status(app) click to toggle source
# File lib/rhc/commands/app.rb, line 262
def status(app)
  # TODO: add a way to deprecate this and alias to show --apache
  options.state = true
  show(app)
end
stop(app) click to toggle source
# File lib/rhc/commands/app.rb, line 186
def stop(app)
  app_action app, :stop

  results { say "#{app} stopped" }
  0
end
tidy(app) click to toggle source
# File lib/rhc/commands/app.rb, line 230
def tidy(app)
  app_action app, :tidy

  results { say "#{app} cleaned up" }
  0
end