Parent

Files

Class/Module Index [+]

Quicksearch

Fog::Compute::AWS::Server

Attributes

architecture[RW]
instance_initiated_shutdown_behavior[RW]
password[RW]
private_key[W]
private_key_path[W]
public_key[W]
public_key_path[W]
username[W]

Public Class Methods

new(attributes={}) click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 46
def initialize(attributes={})
  self.groups     ||= ["default"] unless attributes[:subnet_id]
  self.flavor_id  ||= 't1.micro'
  self.image_id   ||= begin
    self.username = 'ubuntu'
    case attributes[:connection].instance_variable_get(:@region) # Ubuntu 10.04 LTS 64bit (EBS)
    when 'ap-northeast-1'
      'ami-5e0fa45f'
    when 'ap-southeast-1'
      'ami-f092eca2'
    when 'eu-west-1'
      'ami-3d1f2b49'
    when 'us-east-1'
      'ami-3202f25b'
    when 'us-west-1'
      'ami-f5bfefb0'
    end
  end
  super
end

Public Instance Methods

addresses() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 67
def addresses
  requires :id

  connection.addresses(:server => self)
end
console_output() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 73
def console_output
  requires :id

  connection.get_console_output(id)
end
destroy() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 79
def destroy
  requires :id

  connection.terminate_instances(id)
  true
end
flavor() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 95
def flavor
  @flavor ||= connection.flavors.all.detect {|flavor| flavor.id == flavor_id}
end
flavor=(new_flavor) click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 91
def flavor=(new_flavor)
  @flavor = new_flavor
end
flavor_id() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 87
def flavor_id
  @flavor && @flavor.id || attributes[:flavor_id]
end
key_pair() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 99
def key_pair
  requires :key_name

  connection.key_pairs.all(key_name).first
end
key_pair=(new_keypair) click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 105
def key_pair=(new_keypair)
  self.key_name = new_keypair && new_keypair.name
end
monitor=(new_monitor) click to toggle source

I tried to call it monitoring= and be smart with attributes[] but in save a merge_attribute is called after run_instance thus making an un-necessary request. Use this until finding a clever solution

# File lib/fog/compute/models/aws/server.rb, line 242
def monitor=(new_monitor)
  if identity
    case new_monitor
    when true
      response = connection.monitor_instances(identity)
    when false
      response = connection.unmonitor_instances(identity)
    else
      raise ArgumentError.new("only Boolean allowed here")
    end
  end
  self.monitoring = new_monitor
end
private_key() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 114
def private_key
  @private_key ||= private_key_path && File.read(private_key_path)
end
private_key_path() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 109
def private_key_path
  @private_key_path ||= Fog.credentials[:private_key_path]
  @private_key_path &&= File.expand_path(@private_key_path)
end
public_key() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 123
def public_key
  @public_key ||= public_key_path && File.read(public_key_path)
end
public_key_path() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 118
def public_key_path
  @public_key_path ||= Fog.credentials[:public_key_path]
  @public_key_path &&= File.expand_path(@public_key_path)
end
ready?() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 127
def ready?
  state == 'running'
end
reboot() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 131
def reboot
  requires :id
  connection.reboot_instances(id)
  true
end
save() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 137
def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
  requires :image_id

  options = {
    'BlockDeviceMapping'          => block_device_mapping,
    'ClientToken'                 => client_token,
    'InstanceInitiatedShutdownBehavior' => instance_initiated_shutdown_behavior,
    'InstanceType'                => flavor_id,
    'KernelId'                    => kernel_id,
    'KeyName'                     => key_name,
    'Monitoring.Enabled'          => monitoring,
    'Placement.AvailabilityZone'  => availability_zone,
    'Placement.GroupName'         => placement_group,
    'Placement.Tenancy'           => tenancy,
    'RamdiskId'                   => ramdisk_id,
    'SecurityGroup'               => groups,
    'SubnetId'                    => subnet_id,
    'UserData'                    => user_data
  }
  options.delete_if {|key, value| value.nil?}

  # If subnet is defined we are working on a virtual private cloud.
  # subnet & security group cannot co-exist. I wish VPC just ignored
  # the security group parameter instead, it would be much easier!
  if subnet_id
    options.delete('SecurityGroup')
  else
    options.delete('SubnetId')
  end

  data = connection.run_instances(image_id, 1, 1, options)
  merge_attributes(data.body['instancesSet'].first)
  true
end
scp(local_path, remote_path, upload_options = {}) click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 210
def scp(local_path, remote_path, upload_options = {})
  requires :public_ip_address, :username

  scp_options = {}
  scp_options[:key_data] = [private_key] if private_key
  Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end
setup(credentials = {}) click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 173
def setup(credentials = {})
  requires :identity, :public_ip_address, :username
  require 'multi_json'

  commands = [
    %{mkdir .ssh},
    %{passwd -l #{username}},
    %{echo "#{MultiJson.encode(attributes)}" >> ~/attributes.json}
  ]
  if public_key
    commands << %{echo "#{public_key}" >> ~/.ssh/authorized_keys}
  end

  # wait for aws to be ready
  Timeout::timeout(360) do
    begin
      Timeout::timeout(8) do
        Fog::SSH.new(public_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
      end
    rescue Errno::ECONNREFUSED
      sleep(2)
      retry
    rescue Net::SSH::AuthenticationFailed, Timeout::Error
      retry
    end
  end
  Fog::SSH.new(public_ip_address, username, credentials).run(commands)
end
ssh(commands) click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 202
def ssh(commands)
  requires :identity, :public_ip_address, :username

  options = {}
  options[:key_data] = [private_key] if private_key
  Fog::SSH.new(public_ip_address, username, options).run(commands)
end
start() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 218
def start
  requires :id
  connection.start_instances(id)
  true
end
stop() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 224
def stop
  requires :id
  connection.stop_instances(id)
  true
end
username() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 230
def username
  @username ||= 'root'
end
volumes() click to toggle source
# File lib/fog/compute/models/aws/server.rb, line 234
def volumes
  requires :id
  connection.volumes(:server => self)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.