Class: Fog::Compute::AWS::Servers

Inherits:
Fog::Collection show all
Defined in:
lib/fog/compute/models/aws/servers.rb

Instance Attribute Summary

Attributes inherited from Fog::Collection

#connection

Instance Method Summary collapse

Methods inherited from Fog::Collection

#clear, #create, #inspect, #load, model, #model, #new, #reload, #table, #to_json

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

#initialize(attributes) ⇒ Servers

Creates a new server

AWS.servers.new

Returns

Returns the details of the new server

>> AWS.servers.new

<Fog::AWS::Compute::Server
  id=nil,
  ami_launch_index=nil,
  availability_zone=nil,
  block_device_mapping=nil,
  client_token=nil,
  dns_name=nil,
  groups=["default"],
  flavor_id="m1.small",
  image_id=nil,
  ip_address=nil,
  kernel_id=nil,
  key_name=nil,
  created_at=nil,
  monitoring=nil,
  product_codes=nil,
  private_dns_name=nil,
  private_ip_address=nil,
  ramdisk_id=nil,
  reason=nil,
  root_device_name=nil,
  root_device_type=nil,
  state=nil,
  state_reason=nil,
  subnet_id=nil,
  tags=nil,
  user_data=nil
>


53
54
55
56
# File 'lib/fog/compute/models/aws/servers.rb', line 53

def initialize(attributes)
  self.filters ||= {}
  super
end

Instance Method Details

#all(filters = self.filters) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fog/compute/models/aws/servers.rb', line 58

def all(filters = self.filters)
  unless filters.is_a?(Hash)
    Formatador.display_line("[yellow][WARN] all with #{filters.class} param is deprecated, use all('instance-id' => []) instead[/] [light_black](#{caller.first})[/]")
    filters = {'instance-id' => [*filters]}
  end
  self.filters = filters
  data = connection.describe_instances(filters).body
  load(
    data['reservationSet'].map do |reservation|
      reservation['instancesSet'].map do |instance|
        instance.merge(:groups => reservation['groupSet'])
      end
    end.flatten
  )
end

#bootstrap(new_attributes = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/compute/models/aws/servers.rb', line 74

def bootstrap(new_attributes = {})
  server = connection.servers.new(new_attributes)

  unless new_attributes[:key_name]
    # first or create fog_#{credential} keypair
    name = Fog.respond_to?(:credential) && Fog.credential || :default
    unless server.key_pair = connection.key_pairs.get("fog_#{name}")
      server.key_pair = connection.key_pairs.create(
        :name => "fog_#{name}",
        :public_key => server.public_key
      )
    end
  end

  # make sure port 22 is open in the first security group
  security_group = connection.security_groups.get(server.groups.first)
  authorized = security_group.ip_permissions.detect do |ip_permission|
    ip_permission['ipRanges'].first && ip_permission['ipRanges'].first['cidrIp'] == '0.0.0.0/0' &&
    ip_permission['fromPort'] == 22 &&
    ip_permission['ipProtocol'] == 'tcp' &&
    ip_permission['toPort'] == 22
  end
  unless authorized
    security_group.authorize_port_range(22..22)
  end

  server.save
  server.wait_for { ready? }
  server.setup(:key_data => [server.private_key])
  server
end

#get(server_id) ⇒ Object

Used to retreive a server

server_id is required to get the associated server information.

You can run the following command to get the details: AWS.servers.get(“i-5c973972”)

Returns

>> AWS.servers.get(“i-5c973972”)

<Fog::AWS::Compute::Server
  id="i-5c973972",
  ami_launch_index=0,
  availability_zone="us-east-1b",
  block_device_mapping=[],
  client_token=nil,
  dns_name="ec2-25-2-474-44.compute-1.amazonaws.com",
  groups=["default"],
  flavor_id="m1.small",
  image_id="test",
  ip_address="25.2.474.44",
  kernel_id="aki-4e1e1da7",
  key_name=nil,
  created_at=Mon Nov 29 18:09:34 -0500 2010,
  monitoring=false,
  product_codes=[],
  private_dns_name="ip-19-76-384-60.ec2.internal",
  private_ip_address="19.76.384.60",
  ramdisk_id="ari-0b3fff5c",
  reason=nil,
  root_device_name=nil,
  root_device_type="instance-store",
  state="running",
  state_reason={},
  subnet_id=nil,
  tags={},
  user_data=nil
>


146
147
148
149
150
151
152
# File 'lib/fog/compute/models/aws/servers.rb', line 146

def get(server_id)
  if server_id
    self.class.new(:connection => connection).all('instance-id' => server_id).first
  end
rescue Fog::Errors::NotFound
  nil
end