Class: VmShepherd::AwsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vm_shepherd/aws_manager.rb

Defined Under Namespace

Classes: RetryLimitExceeded

Constant Summary collapse

AWS_REGION =
'us-east-1'
OPS_MANAGER_INSTANCE_TYPE =
'm3.medium'
RETRY_LIMIT =
60
RETRY_INTERVAL =
5
DO_NOT_TERMINATE_TAG_KEY =
'do_not_terminate'

Instance Method Summary collapse

Constructor Details

#initialize(aws_options) ⇒ AwsManager

Returns a new instance of AwsManager.



14
15
16
17
18
19
20
21
# File 'lib/vm_shepherd/aws_manager.rb', line 14

def initialize(aws_options)
  AWS.config(
    access_key_id: aws_options.fetch(:aws_access_key),
    secret_access_key: aws_options.fetch(:aws_secret_key),
    region: AWS_REGION
  )
  @aws_options = aws_options
end

Instance Method Details

#deploy(ami_file_path) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vm_shepherd/aws_manager.rb', line 23

def deploy(ami_file_path)
  image_id = File.read(ami_file_path).strip

  instance =
    retry_ignoring_error_until(AWS::EC2::Errors::InvalidIPAddress::InUse) do
      AWS.ec2.instances.create(
        image_id: image_id,
        key_name: aws_options.fetch(:ssh_key_name),
        security_group_ids: [aws_options.fetch(:security_group_id)],
        subnet: aws_options.fetch(:public_subnet_id),
        instance_type: OPS_MANAGER_INSTANCE_TYPE
      )
    end

  retry_ignoring_error_until(AWS::EC2::Errors::InvalidInstanceID::NotFound) do
    instance.status == :running
  end

  instance.associate_elastic_ip(aws_options.fetch(:elastic_ip_id))
  instance.add_tag('Name', value: aws_options.fetch(:vm_name))
end

#destroyObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vm_shepherd/aws_manager.rb', line 45

def destroy
  subnets = [
    AWS.ec2.subnets[aws_options.fetch(:public_subnet_id)],
    AWS.ec2.subnets[aws_options.fetch(:private_subnet_id)]
  ]

  subnets.each do |subnet|
    subnet.instances.each do |instance|
      instance.terminate unless instance.tags.to_h.fetch(DO_NOT_TERMINATE_TAG_KEY, false)
    end
  end
end