Class: Awsborn::Ec2

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
AwsConstants
Defined in:
lib/awsborn/ec2.rb

Defined Under Namespace

Classes: KeyPair

Constant Summary collapse

IP4_REGEXP =
/^(\d{1,3}\.){3}\d{1,3}$/

Constants included from AwsConstants

AwsConstants::AVAILABILITY_ZONES, AwsConstants::INSTANCE_TYPES, AwsConstants::INSTANCE_TYPES_32_BIT, AwsConstants::INSTANCE_TYPES_64_BIT, AwsConstants::REGIONS, AwsConstants::SYMBOL_CONSTANT_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AwsConstants

#aws_constant, #aws_instance_type_to_symbol, #aws_zone_to_symbol, #endpoint_for_zone_and_service, #symbol_to_aws_instance_type, #symbol_to_aws_zone, #zone_to_aws_region

Constructor Details

#initialize(zone) ⇒ Ec2

Returns a new instance of Ec2.



24
25
26
# File 'lib/awsborn/ec2.rb', line 24

def initialize (zone)
  @endpoint = endpoint_for_zone_and_service(zone, :ec2)
end

Instance Attribute Details

#instance_idObject

Returns the value of attribute instance_id.



7
8
9
# File 'lib/awsborn/ec2.rb', line 7

def instance_id
  @instance_id
end

Instance Method Details

#associate_address(address) ⇒ Object



52
53
54
55
56
57
# File 'lib/awsborn/ec2.rb', line 52

def associate_address (address)
  unless address.match(IP4_REGEXP)
    address = Resolv.getaddress address
  end
  connection.associate_address(instance_id, address)
end

#attach_volume(volume, device) ⇒ Object



74
75
76
# File 'lib/awsborn/ec2.rb', line 74

def attach_volume (volume, device)
  connection.attach_volume(volume, instance_id, device)
end

#connectionObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/awsborn/ec2.rb', line 9

def connection
  unless @connection
    env_ec2_url = ENV['EC2_URL']
    begin
      ENV['EC2_URL'] = @endpoint
      @connection = ::RightAws::Ec2.new(Awsborn.access_key_id,
                                        Awsborn.secret_access_key,
                                        :logger => Awsborn.logger)
    ensure
      ENV['EC2_URL'] = env_ec2_url
    end
  end
  @connection
end

#create_security_group_if_missing(group_name, description = "Created by Awsborn") ⇒ Object



92
93
94
95
96
# File 'lib/awsborn/ec2.rb', line 92

def create_security_group_if_missing (group_name, description = "Created by Awsborn")
  unless connection.describe_security_groups.detect { |group| group[:aws_group_name] == group_name }
    connection.create_security_group(group_name, description)
  end
end

#delete_key_pair(key_pair) ⇒ Object



41
42
43
# File 'lib/awsborn/ec2.rb', line 41

def delete_key_pair (key_pair)
  connection.delete_key_pair(key_pair.name)
end

#describe_instanceObject



59
60
61
# File 'lib/awsborn/ec2.rb', line 59

def describe_instance
  connection.describe_instances(instance_id).first
end

#generate_key_pairObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/awsborn/ec2.rb', line 30

def generate_key_pair
  time = Time.now
  key_name = "temp_key_#{time.to_i}_#{time.usec}"
  key_data = connection.create_key_pair(key_name)
  file_path = File.join(ENV['TMP_DIR'] || '/tmp', "#{key_name}.pem")
  File.open file_path, "w", 0600 do |f|
    f.write key_data[:aws_material]
  end
  KeyPair.new key_name, file_path
end

#get_console_outputObject



69
70
71
72
# File 'lib/awsborn/ec2.rb', line 69

def get_console_output
  output = connection.get_console_output(instance_id)
  output[:aws_output]
end

#instance_id_for_volume(volume_id) ⇒ Object



45
46
47
48
# File 'lib/awsborn/ec2.rb', line 45

def instance_id_for_volume (volume_id)
  response = connection.describe_volumes(volume_id).first
  response[:aws_status] == 'in-use' ? response[:aws_instance_id] : nil
end

#launch_instance(*args) ⇒ Object



63
64
65
66
67
# File 'lib/awsborn/ec2.rb', line 63

def launch_instance (*args)
  response = connection.launch_instances(*args).first
  self.instance_id = response[:aws_instance_id]
  response
end

#monitorObject



82
83
84
85
# File 'lib/awsborn/ec2.rb', line 82

def monitor
  logger.debug "Activating monitoring for #{instance_id}"
  connection.monitor_instances(instance_id)
end

#monitoring?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/awsborn/ec2.rb', line 78

def monitoring?
  %w[enabled pending].include?(describe_instance[:monitoring_state])
end

#set_instance_name(name) ⇒ Object



98
99
100
101
# File 'lib/awsborn/ec2.rb', line 98

def set_instance_name (name)
  raise "Instance hasn't been launched yet" unless instance_id
  connection.create_tags(instance_id, {"Name" => name})
end

#unmonitorObject



87
88
89
90
# File 'lib/awsborn/ec2.rb', line 87

def unmonitor
  logger.debug "Deactivating monitoring for #{instance_id}"
  connection.unmonitor_instances(instance_id)
end