class RightApiHelper::Provisioner

This is the main class to use to create a server on the RightScale platform. Use the {#provision} method to create and launch the server.

The other methods are for checking server state and gathering information once the server is operational.

Constants

BAD_STATES_UP
RETRY_DELAY

Public Class Methods

new(right_api_client) click to toggle source
Calls superclass method RightApiHelper::Base::new
# File lib/right_api_helper/provisioner.rb, line 71
def initialize(right_api_client)
  super(right_api_client)
  @api_shim = RightApiHelper::API15.new(right_api_client)
end

Public Instance Methods

connection_url() click to toggle source
# File lib/right_api_helper/provisioner.rb, line 76
def connection_url
  raise RightScaleError, "No server provisioned. No connection URL available." unless @server
  unless @data_request_url
    user_data = @server.current_instance.show(:view => "full").user_data
    @data_request_url = @api_shim.data_request_url(user_data)
    @log.debug "Data Request URL: #{@data_request_url}"
  end
  @data_request_url
end
provision(server_name = "default", server_template = nil, cloud_name = "ec2", deployment_name = "default", inputs = nil, multi_cloud_image_name = nil, ssh_key_uuid = nil, security_groups = nil) click to toggle source

Provision a server using RightScale

@param server_name [String] the name to give the server that will be

created.

@param server_template [String] the name or ID of the ServerTemplate to

create the server from.

@param cloud_name [String] name of cloud to provision on. @param deployment_name [String] name of deployment to add the server to.

This will be created if it does not exist.

@param inputs [Array] An array of {Input} objects. @param ssh_key_id [String] The resource_uuid of an ssh key from the

RightScale dashboard. Only required on EC2 and Eucalyptus.

@param secgroup_id [Array] An array of security group IDs to place the

server in.

@raise {RightApiProvisionException} if anything

goes wrong
# File lib/right_api_helper/provisioner.rb, line 103
def provision(server_name = "default",
              server_template = nil,
              cloud_name = "ec2",
              deployment_name = "default",
              inputs = nil,
              multi_cloud_image_name = nil,
              ssh_key_uuid = nil,
              security_groups = nil)

  # fail if the requested cloud is not registered with RightScale account
  @cloud = @api_shim.find_cloud_by_name(cloud_name)
  unless @cloud
    clouds = @client.list_clouds.inject("") { |str, c| str == "" ? c.name : "#{str}, #{c.name}" }
    raise RightScaleError, "ERROR: cannot find a cloud named: '#{cloud_name}'. " +
          "Please check the spelling of the 'cloud_name' parameter in " +
          "your Vagrant file and verify the cloud is registered with " +
          "your RightScale account? Supported clouds: #{clouds}"
  end

  # Verify ssh key uuid, if required by cloud
  if @api_shim.requires_ssh_keys?(@cloud)
    @ssh_key = @api_shim.find_ssh_key_by_uuid_or_first(@cloud, ssh_key_uuid)
    raise RightScaleError, "ERROR: cannot find an ssh_key named: #{ssh_key_uuid}" unless @ssh_key
  end

  # Verify security group, if required by cloud
  if @api_shim.requires_security_groups?(@cloud)
    @sec_groups = []
    if !security_groups.nil? && !security_groups.is_a?(Array)
      raise RightScaleError, "security_groups must be an array.  You passed a #{security_groups.class}"
    end
    security_groups ||= ["default"]
    security_groups.each do |name|
      group = @api_shim.find_security_group_by_name(@cloud, name)
      group = @api_shim.find_security_group_by_id(@cloud, name) unless group
      raise RightScaleError, "ERROR: cannot find an security group : #{name}" unless group
      @sec_groups << group
    end
  end

  # check for existing deployment and server in RightScale account
  @deployment = @api_shim.find_deployment_by_name(deployment_name)
  @log.info "Deployment '#{deployment_name}' #{@deployment ? "found." : "not found."}"
  @server = @api_shim.find_server_by_name(server_name) if @deployment
  @log.info "Server '#{server_name}' #{@server ? "found." : "not found."}"

  if @server
    # verify existing server is on the cloud we are requesting, if not fail.
    actual_cloud_name = @api_shim.server_cloud_name(@server)
    raise RightScaleError, "ERROR: the server is in the '#{actual_cloud_name}' cloud, " +
          "and not in the requested '#{cloud_name}' cloud.\n" +
          "Please delete the server or pick and new server name." if cloud_name != actual_cloud_name
  end

  unless @deployment && @server
    # we need to create a server, can we find the servertemplate?
    begin
      @servertemplate = @api_shim.find_servertemplate(server_template)
    rescue RightScaleError
      raise RightScaleError, "ERROR: cannot find ServerTemplate '#{server_template}'. Did you import it?\n" +
            "Visit http://bit.ly/VnOiA7 for more info.\n\n"
    end

    # We need to find the to be used in the server if the MCI name is given
    begin
      @mci =
        if multi_cloud_image_name.nil? || multi_cloud_image_name.empty?
          nil
        else
          @client.find_mci_by_name(multi_cloud_image_name)
        end
    rescue Exception => e
      raise RightScaleError, "ERROR: Cannot find the mci '#{multi_cloud_image_name}'. Please make sure" +
        " that you have the MCI under the server template selected." +
        " Exception: #{e.inspect}"
    end
  end

  # create deployment and server as needed
  unless @deployment
    @deployment = @api_shim.create_deployment(deployment_name)
    @log.info "Created deployment."
  end

  unless @server
    @server = @api_shim.create_server(@deployment, @servertemplate, @mci, @cloud, server_name, @ssh_key, @sec_groups)
    @log.info "Created server."
  end

  unless @api_shim.is_provisioned?(@server)

    # setup any inputs
    begin
      @api_shim.set_server_inputs(@server, inputs) if inputs && ! inputs.empty?
    rescue Exception => e
      raise RightScaleError, "Problem setting inputs. \n #{e.message}\n\n"
    end

    # launch server
    @log.info "Launching server..."
    @server = @api_shim.launch_server(@server, inputs)
    @api_shim.set_bad_states(BAD_STATES_UP)
    @api_shim.server_wait_for_state(@server, "booting", 30)
  end

end
server_info() click to toggle source
# File lib/right_api_helper/provisioner.rb, line 219
def server_info
  info = @api_shim.server_info(@server)
  while info.private_ip_addresses.empty?
    @log.info "Waiting for cloud to provide IP address..."
    sleep 30
    info = @api_shim.server_info(@server)
  end
  info
end
server_ready?() click to toggle source
# File lib/right_api_helper/provisioner.rb, line 210
def server_ready?
  @api_shim.server_ready?(@server)
end
wait_for_operational() click to toggle source
# File lib/right_api_helper/provisioner.rb, line 214
def wait_for_operational
  @api_shim.set_bad_states(BAD_STATES_UP)
  @api_shim.server_wait_for_state(@server, "operational", 30)
end