From ac8d06550a9439fa2dfd895091659868c5db4e6d Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Wed, 21 Aug 2013 18:48:47 -0700 Subject: [PATCH 1/2] [google|compute] Set default zone to be one not in maintence. --- lib/fog/google/models/compute/servers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/google/models/compute/servers.rb b/lib/fog/google/models/compute/servers.rb index 568814337..d8e91ce8c 100644 --- a/lib/fog/google/models/compute/servers.rb +++ b/lib/fog/google/models/compute/servers.rb @@ -49,7 +49,7 @@ module Fog :name => "fog-#{Time.now.to_i}", :image_name => "debian-7-wheezy-v20130617", :machine_type => "n1-standard-1", - :zone_name => "us-central1-a", + :zone_name => "us-central1-b", :private_key_path => File.expand_path("~/.ssh/id_rsa"), :public_key_path => File.expand_path("~/.ssh/id_rsa.pub"), :username => ENV['USER'], From 111bd6089c00294e8e1ec357081ceef3a1600b28 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Wed, 21 Aug 2013 18:51:38 -0700 Subject: [PATCH 2/2] Add support for multiple sshkeys and downed zones. --- lib/fog/google/compute.rb | 12 ++++++++++ lib/fog/google/models/compute/server.rb | 30 +++++++++++++++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/fog/google/compute.rb b/lib/fog/google/compute.rb index b5bb26d8c..1646634d5 100644 --- a/lib/fog/google/compute.rb +++ b/lib/fog/google/compute.rb @@ -112,6 +112,18 @@ module Fog @default_network = 'default' end + # TODO: Total hack, create zone and zones model. + def zones + zones = [] + self.list_zones.data[:body]["items"].each do |z| + if z["status"] == "UP" + zones.push z["name"] + end + end + + return zones + end + def build_result(api_method, parameters, body_object=nil) if body_object result = @client.execute( diff --git a/lib/fog/google/models/compute/server.rb b/lib/fog/google/models/compute/server.rb index 1b15dd4ce..768109ad4 100644 --- a/lib/fog/google/models/compute/server.rb +++ b/lib/fog/google/models/compute/server.rb @@ -67,6 +67,23 @@ module Fog end end + def add_ssh_key username, key + if self.metadata.nil? + self.metadata = Hash.new("") + end + + # You can have multiple SSH keys, seperated by newlines. + # https://developers.google.com/compute/docs/console?hl=en#sshkeys + if !self.metadata["sshKeys"].empty? + self.metadata["sshKeys"] += "\n" + end + + self.metadata["sshKeys"] += "#{username}:#{key.strip}" + + return self.metadata + end + + def reload data = service.get_server(self.name, self.zone).body self.merge_attributes(data) @@ -77,13 +94,11 @@ module Fog requires :machine_type requires :zone_name - if self.metadata.nil? - self.metadata = {} + if not service.zones.include? self.zone_name + raise ArgumentError.new "#{self.zone_name.inspect} is either down or you don't have permission to use it." end - self.metadata.merge!({ - "sshKeys" => "#{username}:#{public_key.strip}" - }) if public_key + self.add_ssh_key(self.username, self.public_key) if self.public_key options = { 'image' => image_name, @@ -94,10 +109,11 @@ module Fog 'disks' => disks, 'kernel' => kernel, 'metadata' => metadata - } - options.delete_if {|key, value| value.nil?} + }.delete_if {|key, value| value.nil?} + service.insert_server(name, zone_name, options) data = service.backoff_if_unfound {service.get_server(self.name, self.zone_name).body} + service.servers.merge_attributes(data) end