2012-10-22 20:15:33 -04:00
|
|
|
require 'fog/core/collection'
|
2012-12-04 15:01:36 -05:00
|
|
|
require 'fog/google/models/compute/server'
|
2012-10-22 20:15:33 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
2012-12-04 15:05:11 -05:00
|
|
|
class Google
|
2012-10-22 20:15:33 -04:00
|
|
|
|
|
|
|
class Servers < Fog::Collection
|
|
|
|
|
2012-12-04 15:05:11 -05:00
|
|
|
model Fog::Compute::Google::Server
|
2012-10-22 20:15:33 -04:00
|
|
|
|
2013-07-31 09:17:34 -04:00
|
|
|
def all(filters={})
|
|
|
|
if filters['zone'].nil?
|
2013-04-29 20:25:54 -04:00
|
|
|
data = []
|
|
|
|
service.list_zones.body['items'].each do |zone|
|
|
|
|
data += service.list_servers(zone['name']).body["items"] || []
|
|
|
|
end
|
|
|
|
else
|
2013-07-31 09:17:34 -04:00
|
|
|
data = service.list_servers(filters['zone']).body["items"] || []
|
2013-04-29 20:25:54 -04:00
|
|
|
end
|
2012-10-22 20:15:33 -04:00
|
|
|
load(data)
|
|
|
|
end
|
|
|
|
|
2013-04-29 19:55:38 -04:00
|
|
|
def get(identity, zone=nil)
|
2013-06-20 22:00:01 -04:00
|
|
|
response = nil
|
2013-04-29 19:55:38 -04:00
|
|
|
if zone.nil?
|
|
|
|
service.list_zones.body['items'].each do |zone|
|
2013-08-04 12:27:32 -04:00
|
|
|
begin
|
|
|
|
response = service.get_server(identity, zone['name'])
|
|
|
|
break if response.status == 200
|
|
|
|
rescue Fog::Errors::Error
|
|
|
|
end
|
2013-04-29 19:55:38 -04:00
|
|
|
end
|
|
|
|
else
|
2013-06-20 21:57:17 -04:00
|
|
|
response = service.get_server(identity, zone)
|
2013-04-29 19:55:38 -04:00
|
|
|
end
|
2013-04-29 20:09:51 -04:00
|
|
|
|
2013-06-20 22:00:01 -04:00
|
|
|
if response.nil? or response.status != 200
|
2013-04-29 20:09:51 -04:00
|
|
|
nil
|
|
|
|
else
|
2013-06-20 21:57:17 -04:00
|
|
|
new(response.body)
|
2013-04-29 20:09:51 -04:00
|
|
|
end
|
2013-12-24 03:55:53 -05:00
|
|
|
rescue Fog::Errors::NotFound
|
2012-10-22 20:15:33 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2013-03-12 18:06:19 -04:00
|
|
|
def bootstrap(new_attributes = {})
|
2013-12-18 07:11:25 -05:00
|
|
|
name = "fog-#{Time.now.to_i}"
|
|
|
|
zone = "us-central1-b"
|
|
|
|
|
|
|
|
disks = new_attributes[:disks]
|
|
|
|
|
|
|
|
if disks.nil? or disks.empty?
|
|
|
|
# create the persistent boot disk
|
|
|
|
disk_defaults = {
|
|
|
|
:name => name,
|
|
|
|
:size_gb => 10,
|
|
|
|
:zone_name => zone,
|
|
|
|
:source_image => "debian-7-wheezy-v20131120",
|
|
|
|
}
|
|
|
|
|
|
|
|
# backwards compatibility to pre-v1
|
|
|
|
new_attributes[:source_image] = new_attributes[:image_name] if new_attributes[:image_name]
|
|
|
|
|
|
|
|
disk = service.disks.create(disk_defaults.merge(new_attributes))
|
|
|
|
disk.wait_for { disk.ready? }
|
|
|
|
disks = [disk]
|
|
|
|
end
|
|
|
|
|
2013-03-12 18:06:19 -04:00
|
|
|
defaults = {
|
2013-12-18 07:11:25 -05:00
|
|
|
:name => name,
|
|
|
|
:disks => disks,
|
2013-03-12 18:46:30 -04:00
|
|
|
:machine_type => "n1-standard-1",
|
2013-12-18 07:11:25 -05:00
|
|
|
:zone_name => zone,
|
2013-04-30 20:19:57 -04:00
|
|
|
:private_key_path => File.expand_path("~/.ssh/id_rsa"),
|
|
|
|
:public_key_path => File.expand_path("~/.ssh/id_rsa.pub"),
|
2013-06-26 20:57:57 -04:00
|
|
|
:username => ENV['USER'],
|
2013-03-12 18:06:19 -04:00
|
|
|
}
|
2013-11-22 11:14:46 -05:00
|
|
|
|
2013-03-12 18:55:04 -04:00
|
|
|
server = create(defaults.merge(new_attributes))
|
2013-06-27 03:30:22 -04:00
|
|
|
server.wait_for { sshable? }
|
2013-06-26 20:55:57 -04:00
|
|
|
|
2013-03-12 18:06:19 -04:00
|
|
|
server
|
|
|
|
end
|
2012-10-22 20:15:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|