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: ENV["AWS_ACCESS_KEY"],
    aws_secret_access_key: ENV["AWS_SECRET_KEY"],
  }
  @compute = Fog::Compute.new @aws_options
  @attrs = attrs
  @options = options
end

Instance Method Details

#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]
      return instance.public_ip_address
    else
      return 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