Class: Dployr::Compute::GCE

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

Instance Method Summary collapse

Methods included from Common

wait_ssh

Constructor Details

#initialize(options, attrs) ⇒ GCE

Returns a new instance of GCE.



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

def initialize(options, attrs)
  @gce_options = {
    provider: 'Google',
    google_project: ENV["GOOGLE_PROJECT_ID"],
    google_client_email: ENV["GOOGLE_CLIENT_EMAIL"],
    google_key_location: ENV["GOOGLE_KEY_LOCATION"],
  }
  @compute = Fog::Compute.new @gce_options
  @attrs = attrs
  @options = options
end

Instance Method Details

#destroyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dployr/compute/gce.rb', line 37

def destroy()
  instance = get_instance(["RUNNING", "STOPPED"])
  if instance
    puts "Destroying instance #{@attrs["name"]}...".yellow
    instance.destroy(async = false)
    puts "Destroying disk #{@attrs["name"]}...".yellow
    disk = @compute.disks.get(@attrs["name"])
    disk.destroy
    # Bug in fog. It return "persistent-disk-0" instead of boot disk name (usually the same name of the machine)
    # instance.disks.each do |disk|
    #   gdisk = @compute.disks.get(disk["deviceName"])
    #   gdisk.destroy
    # end
    return   
  end
  raise "Instance #{@attrs["name"]} not found"
end

#get_infoObject



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

def get_info()
  get_instance(["RUNNING", "STOPPED"])
end

#get_ipObject



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

def get_ip()
  instance = get_instance(["PROVISIONING", "STAGING", "RUNNING"])
  if instance
    if @options[:public_ip]
      return instance.public_ip_address
    else
      return instance.private_ip_address
    end
  end
end

#haltObject



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dployr/compute/gce.rb', line 55

def halt()
  instance = get_instance(["RUNNING"])
  if instance
    instance.disks.each do |disk|
      if disk["autoDelete"] == true
        raise "Cannot halt instance with autoDelete disks"
      end
    end
    return instance.destroy
  end
  raise "Instance #{@attrs["name"]} not found"
end

#startObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dployr/compute/gce.rb', line 68

def start()
    external_ip()
    server = get_instance(["STOPPED"])
    if server
      puts "Starting stopped instance for #{@attrs["name"]} in #{@options[:region]}...".yellow
      server.start
    else
      puts "Creating boot disk...".yellow
      disks = create_disk()
      if defined? @attrs["autodelete_disk"]
        autodelete_disk  = @attrs["autodelete_disk"]
      else
        autodelete_disk = false
      end
      
      puts "Creating new instance for #{@attrs["name"]} in #{@options[:region]}...".yellow
      
      start_options = {
        name: @attrs["name"],
        zone_name: @options[:region],
        machine_type: @attrs["instance_type"],
        network: @attrs["network"],
        disks: [disks.get_as_boot_disk(true, autodelete_disk)],
        external_ip: @attrs["public_ip"]
      }
      puts start_options.to_yaml
      server = @compute.servers.create(start_options)
    end
    print "Wait for instance to get online".yellow
    server.wait_for { print ".".yellow; ready? }
    print "\n"
    wait_ssh(@attrs, server, @options[:public_ip])
end