class RHC::Commands::Cartridge

Public Instance Methods

add(cart_type) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 49
def add(cart_type)
  cart = check_cartridges(cart_type, :from => not_standalone_cartridges).first

  say "Adding #{cart.name} to application '#{options.app}' ... "

  say format_usage_message(cart) if cart.usage_rate?

  rest_app = rest_client.find_application(options.namespace, options.app, :include => :cartridges)
  rest_cartridge = rest_app.add_cartridge(cart.name)

  success "Success"

  paragraph{ display_cart(rest_cartridge) }

  results{ rest_cartridge.messages.each { |msg| success msg } }

  0
end
list() click to toggle source
# File lib/rhc/commands/cartridge.rb, line 14
def list
  carts = rest_client.cartridges.sort_by{ |c| "#{c.type == 'standalone' && 1}_#{c.tags.include?('experimental') ? 1 : 0}_#{(c.display_name || c.name).downcase}" }

  list = if options.verbose
    carts.map do |c|
      name = c.display_name != c.name && "#{color(c.display_name, :cyan)} [#{c.name}]" || c.name
      tags = c.tags - RHC::Rest::Cartridge::HIDDEN_TAGS
      [
        underline("#{name} (#{c.only_in_new? ? 'web' : 'addon'})"),
        c.description,
        tags.present? ? "\nTagged with: #{tags.sort.join(', ')}" : nil,
        c.usage_rate? ? "\n#{format_usage_message(c)}" : nil,
      ].compact << "\n"
    end.flatten
  else
    table(carts.collect do |c|
      [c.usage_rate? ? "#{c.name} (*)" : c.name,
       c.display_name,
       c.only_in_new? ? 'web' : 'addon']
    end)
  end

  say list.join("\n")
  paragraph{ say "Note: Web cartridges can only be added to new applications." }
  paragraph{ say "(*) denotes a cartridge with additional usage costs." } if carts.any? { |c| c.usage_rate? }

  0
end
reload(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 154
def reload(cartridge)
  cartridge_action(cartridge, :reload){ |_, c| results{ say "#{c.name} reloaded" } }
  0
end
remove(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 89
def remove(cartridge)
  rest_app = rest_client.find_application(options.namespace, options.app, :include => :cartridges)
  rest_cartridge = check_cartridges(cartridge, :from => rest_app.cartridges).first

  confirm_action "Removing a cartridge is a destructive operation that may result in loss of data associated with the cartridge.\n\nAre you sure you wish to remove #{rest_cartridge.name} from '#{rest_app.name}'?"

  say "Removing #{rest_cartridge.name} from '#{rest_app.name}' ... "
  rest_cartridge.destroy
  success "removed"

  0
end
restart(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 130
def restart(cartridge)
  cartridge_action(cartridge, :restart){ |_, c| results{ say "#{c.name} restarted" } }
  0
end
scale(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 166
def scale(cartridge)
  raise RHC::MissingScalingValueException unless options.min || options.max

  rest_app = rest_client.find_application(options.namespace, options.app, :include => :cartridges)
  rest_cartridge = check_cartridges(cartridge, :from => rest_app.cartridges).first

  raise RHC::CartridgeNotScalableException unless rest_cartridge.scalable?

  cart = rest_cartridge.set_scales({
    :scales_from => options.min,
    :scales_to   => options.max
  })

  results do
    paragraph{ display_cart(cart) }
    success "Success: Scaling values updated"
  end

  0
end
show(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 73
def show(cartridge)
  rest_app = rest_client.find_application(options.namespace, options.app, :include => :cartridges)
  rest_cartridge = check_cartridges(cartridge, :from => rest_app.cartridges).first

  display_cart(rest_cartridge)

  0
end
start(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 108
def start(cartridge)
  cartridge_action(cartridge, :start){ |_, c| results{ say "#{c.name} started" } }
  0
end
status(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 141
def status(cartridge)
  rest_app = rest_client.find_application(options.namespace, options.app, :include => :cartridges)
  rest_cartridge = check_cartridges(cartridge, :from => rest_app.cartridges).first
  results { rest_cartridge.status.each{ |msg| say msg['message'] } }
  0
end
stop(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 119
def stop(cartridge)
  cartridge_action(cartridge, :stop){ |_, c| results{ say "#{c.name} stopped" } }
  0
end
storage(cartridge) click to toggle source
# File lib/rhc/commands/cartridge.rb, line 197
def storage(cartridge)
  cartridges = Array(cartridge)
  rest_app = rest_client.find_application(options.namespace, options.app, :include => :cartridges)

  # Pull the desired action
  #
  actions = options.__hash__.keys & [:show, :add, :remove, :set]

  # Ensure that only zero or one action was selected
  raise RHC::AdditionalStorageArgumentsException if actions.length > 1

  operation = actions.first || :show
  amount = options.__hash__[operation]

  # Perform a storage change action if requested
  if operation == :show
    results do
      if cartridges.length == 0
        display_cart_storage_list rest_app.cartridges
      else
        check_cartridges(cartridge, :from => rest_app.cartridges).each do |cart|
          display_cart_storage_info cart, cart.display_name
        end
      end
    end
  else
    raise RHC::MultipleCartridgesException,
      'Exactly one cartridge must be specified for this operation' if cartridges.length != 1

    rest_cartridge = check_cartridges(cartridge, :from => rest_app.cartridges).first
    amount = amount.match(%r^(\d+)(GB)?$/)
    raise RHC::AdditionalStorageValueException if amount.nil?

    # If the amount is specified, find the regex match and convert to a number
    amount = amount[1].to_i
    total_amount = rest_cartridge.additional_gear_storage

    if operation == :add
      total_amount += amount
    elsif operation == :remove
      if amount > total_amount && !options.force
        raise RHC::AdditionalStorageRemoveException
      else
        total_amount = [total_amount - amount, 0].max
      end
    else
      total_amount = amount
    end

    cart = rest_cartridge.set_storage(:additional_gear_storage => total_amount)
    results do
      say "Success: additional storage space set to #{total_amount}GB\n"
      display_cart_storage_info cart
    end
  end

  0
end