Class: Dployr::Compute::AWS

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/dployr/compute/aws.rb

Instance Method Summary collapse

Methods included from Common

wait_ssh

Constructor Details

#initialize(options, attrs) ⇒ AWS

Returns a new instance of AWS.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dployr/compute/aws.rb', line 10

def initialize(options, attrs)
  @aws_options = {
    region: options[:region][0..-2],
    provider: 'AWS',
    aws_access_key_id: (attrs["aws_access_key"] or ENV["AWS_ACCESS_KEY"]),
    aws_secret_access_key: (attrs["aws_secret_key"] or ENV["AWS_SECRET_KEY"]),
  }
  @compute = Fog::Compute.new @aws_options
  @attrs = attrs
  @options = options
end

Instance Method Details

#create_network(network_name, network_range, firewalls, routes) ⇒ Object



81
82
83
84
85
86
# File 'lib/dployr/compute/aws.rb', line 81

def create_network(network_name, network_range, firewalls, routes)
  # Routes not used in AWS
  create_vpc(network_range)                # VPC + INTERNET GATEWAY
  create_subnet(network_range)             # SUBNET + ROUTE TABLE
  configure_security_group(firewalls)      # SECURITY GROUP
end

#delete_network(network_name, network_range, firewalls, routes) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dployr/compute/aws.rb', line 88

def delete_network(network_name, network_range, firewalls, routes)
  # Network_name, firewalls and routes not used. We only need "vpc_id"
  if exist_vpc network_range
    vpcId = get_vpcId(network_range)

    delete_route_tables(vpcId)
    delete_subnets(vpcId)
    delete_network_acls(vpcId)
    delete_internet_gateways(vpcId)
    delete_security_groups(vpcId)
    delete_vpc(vpcId)
  else
    puts "\tNetwork #{network_range} not found. Nothing to delete!"
  end
end

#destroyObject



37
38
39
40
41
42
43
44
# File 'lib/dployr/compute/aws.rb', line 37

def destroy
  instance = get_instance ["running", "stopped", "stopping"]
  if instance
    instance.destroy
  else
    raise "Instance #{@attrs["name"]} not found"
  end
end

#get_infoObject



33
34
35
# File 'lib/dployr/compute/aws.rb', line 33

def get_info
  get_instance ["running", "stopped", "stopping"]
end

#get_ipObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/dployr/compute/aws.rb', line 22

def get_ip
  instance = get_instance ["running"] # TODO: add starting states
  if instance
    if @options[:public_ip]
      instance.public_ip_address
    else
      instance.private_ip_address
    end
  end
end

#haltObject



46
47
48
49
50
51
52
53
# File 'lib/dployr/compute/aws.rb', line 46

def halt
  instance = get_instance ["running"]
  if instance
    instance.stop
  else
    raise "Instance #{@attrs["name"]} not found"
  end
end

#startObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dployr/compute/aws.rb', line 55

def start
  server = get_instance ["stopped", "stopping"]
  if server
    puts "Starting stopped instance for #{@attrs["name"]} in #{@options[:region]}...".yellow
    server.start
  else
    puts "Creating new instance for #{@attrs["name"]} in #{@options[:region]}...".yellow
    options = {
      availability_zone: @options[:region],
      flavor_id: @attrs["instance_type"],
      image_id: @attrs["ami"],
      key_name: @attrs["keypair"],
      subnet_id: @attrs["subnet_id"],
      security_group_ids: @attrs["security_groups"],
      tags: { Name: @attrs["name"] }
    }
    puts options.to_yaml
    server = @compute.servers.create options
  end
  print "Wait for instance to get online".yellow
  server.wait_for { print ".".yellow; ready? }
  print "\n"
  elastic_ip server
  wait_ssh @attrs, server, @options[:public_ip]
end