1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/google/models/compute/servers.rb

89 lines
2.4 KiB
Ruby
Raw Normal View History

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
def get(identity, zone=nil)
2013-06-20 22:00:01 -04:00
response = nil
if zone.nil?
service.list_zones.body['items'].each do |zone|
begin
response = service.get_server(identity, zone['name'])
break if response.status == 200
rescue Fog::Errors::Error
end
end
else
2013-06-20 21:57:17 -04:00
response = service.get_server(identity, zone)
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
rescue Fog::Errors::NotFound
2012-10-22 20:15:33 -04:00
nil
end
def bootstrap(new_attributes = {})
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
defaults = {
:name => name,
:disks => disks,
2013-03-12 18:46:30 -04:00
:machine_type => "n1-standard-1",
: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: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
server
end
2012-10-22 20:15:33 -04:00
end
end
end
end