2010-10-04 17:02:08 -04:00
|
|
|
require 'fog/core/collection'
|
2011-08-24 21:37:00 -04:00
|
|
|
require 'fog/aws/models/compute/server'
|
2010-03-16 18:46:21 -04:00
|
|
|
|
2009-09-16 02:11:06 -04:00
|
|
|
module Fog
|
2011-06-16 19:28:54 -04:00
|
|
|
module Compute
|
|
|
|
class AWS
|
2009-09-16 02:11:06 -04:00
|
|
|
|
2010-01-08 14:29:07 -05:00
|
|
|
class Servers < Fog::Collection
|
2009-09-16 02:11:06 -04:00
|
|
|
|
2010-10-04 18:46:12 -04:00
|
|
|
attribute :filters
|
|
|
|
|
2011-06-16 19:28:54 -04:00
|
|
|
model Fog::Compute::AWS::Server
|
2009-10-30 02:35:28 -04:00
|
|
|
|
2010-11-29 18:44:44 -05:00
|
|
|
# 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
|
|
|
|
# >
|
|
|
|
#
|
2011-09-23 17:53:01 -04:00
|
|
|
|
2009-10-09 02:01:09 -04:00
|
|
|
def initialize(attributes)
|
2010-11-19 16:45:45 -05:00
|
|
|
self.filters ||= {}
|
2009-10-09 02:01:09 -04:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-11-19 16:45:45 -05:00
|
|
|
def all(filters = self.filters)
|
2010-10-11 16:45:26 -04:00
|
|
|
unless filters.is_a?(Hash)
|
2011-10-19 15:49:34 -04:00
|
|
|
Fog::Logger.deprecation("all with #{filters.class} param is deprecated, use all('instance-id' => []) instead [light_black](#{caller.first})[/]")
|
2010-10-11 16:45:26 -04:00
|
|
|
filters = {'instance-id' => [*filters]}
|
|
|
|
end
|
2010-11-19 16:45:45 -05:00
|
|
|
self.filters = filters
|
2010-10-04 18:46:12 -04:00
|
|
|
data = connection.describe_instances(filters).body
|
2010-03-06 23:02:10 -05:00
|
|
|
load(
|
|
|
|
data['reservationSet'].map do |reservation|
|
|
|
|
reservation['instancesSet'].map do |instance|
|
2012-05-18 01:33:19 -04:00
|
|
|
instance.merge(:groups => reservation['groupSet'], :security_group_ids => reservation['groupIds'])
|
2010-03-06 23:02:10 -05:00
|
|
|
end
|
2010-03-08 20:35:34 -05:00
|
|
|
end.flatten
|
2010-03-06 23:02:10 -05:00
|
|
|
)
|
2009-09-16 02:11:06 -04:00
|
|
|
end
|
|
|
|
|
2010-09-15 20:31:45 -04:00
|
|
|
def bootstrap(new_attributes = {})
|
2010-09-23 20:39:25 -04:00
|
|
|
server = connection.servers.new(new_attributes)
|
|
|
|
|
2010-10-20 17:32:30 -04:00
|
|
|
unless new_attributes[:key_name]
|
|
|
|
# first or create fog_#{credential} keypair
|
2010-09-24 17:36:59 -04:00
|
|
|
name = Fog.respond_to?(:credential) && Fog.credential || :default
|
2010-10-20 17:32:30 -04:00
|
|
|
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
|
2010-09-22 15:06:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# make sure port 22 is open in the first security group
|
|
|
|
security_group = connection.security_groups.get(server.groups.first)
|
2010-10-13 16:20:18 -04:00
|
|
|
authorized = security_group.ip_permissions.detect do |ip_permission|
|
2010-09-22 15:06:25 -04:00
|
|
|
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
|
2010-09-15 20:31:45 -04:00
|
|
|
end
|
2010-10-13 16:20:18 -04:00
|
|
|
unless authorized
|
2010-09-22 15:06:25 -04:00
|
|
|
security_group.authorize_port_range(22..22)
|
|
|
|
end
|
|
|
|
|
2010-09-23 20:39:25 -04:00
|
|
|
server.save
|
2010-12-16 15:05:15 -05:00
|
|
|
server.wait_for { ready? }
|
2010-09-23 20:39:25 -04:00
|
|
|
server.setup(:key_data => [server.private_key])
|
2010-09-15 20:31:45 -04:00
|
|
|
server
|
|
|
|
end
|
|
|
|
|
2012-03-07 13:52:07 -05:00
|
|
|
# Used to retrieve a server
|
2010-11-29 18:44:44 -05:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# >
|
|
|
|
#
|
2012-03-07 13:52:07 -05:00
|
|
|
|
2010-01-08 14:29:07 -05:00
|
|
|
def get(server_id)
|
|
|
|
if server_id
|
2010-10-04 18:46:12 -04:00
|
|
|
self.class.new(:connection => connection).all('instance-id' => server_id).first
|
2009-10-22 23:46:15 -04:00
|
|
|
end
|
2010-05-26 20:27:17 -04:00
|
|
|
rescue Fog::Errors::NotFound
|
2009-09-25 12:01:51 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2009-09-16 02:11:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|