class Fog::Compute::Google::Server
Constants
- GCE_SCOPE_ALIASES
Public Instance Methods
# File lib/fog/compute/google/models/server.rb, line 486 def add_ssh_key(username, key, async = true) metadata = generate_ssh_key_metadata(username, key) data = service.set_server_metadata( identity, zone_name, metadata[:fingerprint], metadata[:items] ) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
Helper method that returns all of server’s ip addresses, both private and public.
@return [Array]
# File lib/fog/compute/google/models/server.rb, line 266 def addresses private_ip_addresses + public_ip_addresses end
Attach a disk to a running server
@param disk [Object, String] disk object or a self-link @param async [TrueClass] execute the api call asynchronously @param options [Hash] @return [Object]
# File lib/fog/compute/google/models/server.rb, line 276 def attach_disk(disk, async = true, attached_disk_options = {}) requires :identity, :zone if disk.is_a? Disk disk_obj = disk.get_attached_disk elsif disk.is_a? String disk_obj = service.disks.attached_disk_obj(disk, **attached_disk_options) end data = service.attach_disk(identity, zone_name, disk_obj) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
Destroy a server.
@param async [TrueClass] execute the command asynchronously @return [Fog::Compute::Google::Operation]
# File lib/fog/compute/google/models/server.rb, line 206 def destroy(async = true) requires :name, :zone data = service.delete_server(name, zone_name) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async operation end
Detach disk from a running instance
@param device_name [Object] @param async [TrueClass] @returns [Fog::Compute::Google::Server] server object
# File lib/fog/compute/google/models/server.rb, line 298 def detach_disk(device_name, async = true) requires :identity, :zone data = service.detach_disk(identity, zone, device_name) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
# File lib/fog/compute/google/models/server.rb, line 585 def ensure_key_comment(key, default_comment = "fog-user") parts = key.strip.split parts << default_comment if parts.size < 3 parts.join(" ") end
# File lib/fog/compute/google/models/server.rb, line 569 def generate_ssh_key_metadata(username, key) if metadata.nil? self.metadata = Hash.new end metadata[:items] = [] if metadata[:items].nil? metadata_map = Hash[metadata[:items].map { |item| [item[:key], item[:value]] }] ssh_keys = metadata_map["ssh-keys"] || metadata_map["sshKeys"] || "" ssh_keys += "\n" unless ssh_keys.empty? ssh_keys += "#{username}:#{ensure_key_comment(key, username)}" metadata_map["ssh-keys"] = ssh_keys metadata[:items] = metadata_to_item_list(metadata_map) metadata end
Return the source image of the server’s boot disk
@return [String] image self link
# File lib/fog/compute/google/models/server.rb, line 192 def image_name boot_disk = disks.first unless boot_disk.is_a?(Disk) source = boot_disk[:source] match = source.match(%r{/zones/(.*)/disks/(.*)$}) boot_disk = service.disks.get(match[2], match[1]) end boot_disk.source_image.nil? ? nil : boot_disk.source_image end
# File lib/fog/compute/google/models/server.rb, line 505 def map_scopes(scopes) return [] if scopes.nil? scopes.flat_map do |scope| if GCE_SCOPE_ALIASES.key? scope # Expand scope alias to list of related scopes GCE_SCOPE_ALIASES[scope] else [scope_url(scope)] end end end
Returns metadata items as a Hash.
@return [Hash<String, String>] items
# File lib/fog/compute/google/models/server.rb, line 312 def metadata_as_h if metadata.nil? || metadata[:items].nil? || metadata[:items].empty? return {} end Hash[metadata[:items].map { |item| [item[:key], item[:value]] }] end
Helper method that returns the first private ip address of the instance.
@return [String]
# File lib/fog/compute/google/models/server.rb, line 247 def private_ip_address private_ip_addresses.first end
Helper method that returns all of server’s private ip addresses.
@return [Array]
# File lib/fog/compute/google/models/server.rb, line 254 def private_ip_addresses addresses = [] if network_interfaces.respond_to? :map addresses = network_interfaces.map { |nic| nic[:network_ip] } end addresses end
Check if instance is provisioning. On staging vs. provisioning difference: cloud.google.com/compute/docs/instances/checking-instance-status
@return [TrueClass or FalseClass]
# File lib/fog/compute/google/models/server.rb, line 456 def provisioning? status == "PROVISIONING" end
Helper method that returns first public ip address needed for Fog::Compute::Server.ssh default behavior.
@return [String]
# File lib/fog/compute/google/models/server.rb, line 221 def public_ip_address public_ip_addresses.first end
Helper method that returns all of server’s public ip addresses.
@return [Array]
# File lib/fog/compute/google/models/server.rb, line 228 def public_ip_addresses addresses = [] if network_interfaces.respond_to? :flat_map addresses = network_interfaces.flat_map do |nic| if nic[:access_configs].respond_to? :each nic[:access_configs].select { |config| config[:name] == "External NAT" } .map { |config| config[:nat_ip] } else [] end end end addresses end
Check if instance is ready.
@return [TrueClass or FalseClass]
# File lib/fog/compute/google/models/server.rb, line 478 def ready? status == "RUNNING" end
# File lib/fog/compute/google/models/server.rb, line 320 def reboot(async = true) requires :identity, :zone data = service.reset_server(identity, zone_name) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async operation end
# File lib/fog/compute/google/models/server.rb, line 500 def reload data = service.get_server(name, zone_name).to_h merge_attributes(data) end
# File lib/fog/compute/google/models/server.rb, line 591 def reset_windows_password(user) service.reset_windows_password(:server => self, :user => user) end
# File lib/fog/compute/google/models/server.rb, line 517 def save(username: nil, public_key: nil) requires :name requires :machine_type requires :disks requires :zone generate_ssh_key_metadata(self.username, self.public_key) if self.public_key # XXX HACK This is a relic of 1.0 change that for some reason added those arguments # to `save` method. This is left in place to keep things backwards-compatible # TODO(2.0): Remove arguments from save generate_ssh_key_metadata(username, public_key) if public_key options = attributes.reject { |_, v| v.nil? } if service_accounts && service_accounts[0] service_accounts[0][:scopes] = map_scopes(service_accounts[0][:scopes]) options[:service_accounts] = service_accounts end if attributes[:external_ip] if options[:network_interfaces].nil? || options[:network_interfaces].empty? options[:network_interfaces] = [ { :network => "global/networks/#{GOOGLE_COMPUTE_DEFAULT_NETWORK}" } ] end # Add external IP as default access config if given options[:network_interfaces][0][:access_configs] = [ { :name => "External NAT", :type => "ONE_TO_ONE_NAT", :nat_ip => attributes[:external_ip] } ] end if attributes[:network_ip] options[:network_interfaces][0][:network_ip] = attributes[:network_ip] end data = service.insert_server(name, zone_name, options) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } reload end
# File lib/fog/compute/google/models/server.rb, line 353 def serial_port_output(port: 1) requires :identity, :zone service.get_server_serial_port_output(identity, zone_name, :port => port).to_h[:contents] end
# File lib/fog/compute/google/models/server.rb, line 359 def set_disk_auto_delete(auto_delete, device_name = nil, async = true) requires :identity, :zone if device_name.nil? && disks.count > 1 raise ArgumentError.new("Device name is required if multiple disks are attached") end device_name ||= disks.first[:device_name] data = service.set_server_disk_auto_delete( identity, zone_name, auto_delete, device_name ) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
# File lib/fog/compute/google/models/server.rb, line 424 def set_machine_type(new_machine_type, async = true) requires :identity, :zone raise Fog::Errors::Error.new("Instance must be stopped to change machine type") unless stopped? data = service.set_server_machine_type( identity, zone_name, new_machine_type ) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
Set an instance metadata
@param [Bool] async Perform the operation asyncronously @param [Hash] new_metadata A new metadata object
Format: {'foo' => 'bar', 'baz'=>'foo'}
@returns [Fog::Compute::Google::Server] server object
# File lib/fog/compute/google/models/server.rb, line 404 def set_metadata(new_metadata = {}, async = true) requires :identity, :zone unless new_metadata.is_a?(Hash) raise Fog::Errors::Error.new("Instance metadata should be a hash") end # If metadata is presented in {'foo' => 'bar', 'baz'=>'foo'} new_metadata_items = new_metadata.each.map { |k, v| { :key => k.to_s, :value => v.to_s } } data = service.set_server_metadata( identity, zone_name, metadata[:fingerprint], new_metadata_items ) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
# File lib/fog/compute/google/models/server.rb, line 378 def set_scheduling(async = true, on_host_maintenance: nil, automatic_restart: nil, preemptible: nil) requires :identity, :zone data = service.set_server_scheduling( identity, zone_name, :on_host_maintenance => on_host_maintenance, :automatic_restart => automatic_restart, :preemptible => preemptible ) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async reload end
Check if instance is staging. On staging vs. provisioning difference: cloud.google.com/compute/docs/instances/checking-instance-status
@return [TrueClass or FalseClass]
# File lib/fog/compute/google/models/server.rb, line 464 def staging? status == "STAGING" end
# File lib/fog/compute/google/models/server.rb, line 331 def start(async = true) requires :identity, :zone data = service.start_server(identity, zone_name) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async operation end
# File lib/fog/compute/google/models/server.rb, line 342 def stop(async = true, discard_local_ssd=false) requires :identity, :zone data = service.stop_server(identity, zone_name, discard_local_ssd) operation = Fog::Compute::Google::Operations .new(:service => service) .get(data.name, data.zone) operation.wait_for { ready? } unless async operation end
Check if instance is stopped.
@return [TrueClass or FalseClass]
# File lib/fog/compute/google/models/server.rb, line 471 def stopped? status == "TERMINATED" end
# File lib/fog/compute/google/models/server.rb, line 482 def zone_name zone.nil? ? nil : zone.split("/")[-1] end
Private Instance Methods
# File lib/fog/compute/google/models/server.rb, line 597 def metadata_to_item_list(metadata) metadata.map { |k, v| { :key => k, :value => v } } end
# File lib/fog/compute/google/models/server.rb, line 601 def scope_url(scope) if scope.start_with?("https://") scope else "https://www.googleapis.com/auth/#{scope}" end end