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: (attrs["google_project_id"] or ENV["GOOGLE_PROJECT_ID"]),
    google_client_email: (attrs["google_client_email"] or ENV["GOOGLE_CLIENT_EMAIL"]),
    google_key_location: (attrs["google_key_location"] or ENV["GOOGLE_KEY_LOCATION"]),
  }
  @compute = Fog::Compute.new @gce_options
  @attrs = attrs
  @options = options
end

Instance Method Details

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



104
105
106
107
108
109
110
111
112
# File 'lib/dployr/compute/gce.rb', line 104

def create_network(network_name, network_range, firewalls, routes)
  create_subnet(network_name, network_range)

  if firewalls.respond_to?("each")
    firewalls.each do |key, value|
      add_firewall(key , network_name, value["address"], value["protocol"], value["port"])
    end
  end
end

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dployr/compute/gce.rb', line 114

def delete_network(network_name, network_range, firewalls, routes)
  if exists("network", network_name)
    delete_routes(routes)

    if firewalls.respond_to?("each")
      firewalls.each do |key, value|
        delete_firewall(key)
      end
    end

    delete_subnet(network_name)
  else
    puts "\tNetwork #{network_name} not found. Nothing to delete!"
  end
end

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

#haltObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# 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
    instance.destroy
  else
    raise "Instance #{@attrs["name"]} not found"
  end
end

#startObject



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
101
102
# File 'lib/dployr/compute/gce.rb', line 69

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