Class: OpsManager::Appliance::AWS

Inherits:
Base
  • Object
show all
Defined in:
lib/ops_manager/appliance/aws.rb

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from OpsManager::Appliance::Base

Instance Method Details

#deploy_vmObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ops_manager/appliance/aws.rb', line 8

def deploy_vm
  image_id = ::YAML.load_file(ami_mapping_file)[config[:opts][:region]]

  server = connection.servers.create(
    block_device_mapping: [{
      'DeviceName'     => '/dev/xvda',
      'Ebs.VolumeSize' => config[:opts][:disk_size_in_gb],
    }],
    key_name: config[:opts][:ssh_keypair_name],
    flavor_id: config[:opts][:instance_type],
    subnet_id: config[:opts][:subnet_id],
    image_id: image_id,
    private_ip_address: config[:ip],
    security_group_ids: security_group_ids,
    availability_zone: config[:opts][:availability_zone],
    iam_instance_profile_name: config[:opts][:instance_profile_name],
    tags: {
      'Name' => vm_name,
    }
  )
  server.wait_for { ready? }
  return server
end

#stop_current_vm(name) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ops_manager/appliance/aws.rb', line 32

def stop_current_vm(name)
  server = connection.servers.all("private-ip-address" => config[:ip], "tag:Name" => name).first
  if ! server
    fail "VM not found matching IP '#{config[:ip]}', named '#{name}'"
  end
  server.stop
  server.wait_for { server.state == "stopped" }

  # Create ami of stopped server
  response = connection.create_image(server.id, "#{name}-backup", "Backup of #{name}")
  image = connection.images.get( response.data[:body]['imageId'])
  image.wait_for(timeout=36000) { image.state == "available" }
  if image.state != "available"
    fail "Error creating backup AMI, bailing out before destroying the VM"
  end

  puts "Saved #{name} to AMI #{image.id} (#{name}-backup) for safe-keeping"

  server.destroy
  if !Fog.mocking?
    server.wait_for { server.state == 'terminated' }
  else
    # Fog's mock doesn't support transitioning state from terminating -> terminated
    # so we have to hack this here
    server.wait_for { server.state == 'terminating' }
  end
end