class Fog::Compute::Google::Server

Constants

GCE_SCOPE_ALIASES

Public Instance Methods

add_ssh_key(username, key, async = true) click to toggle source
# 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
addresses() click to toggle source

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_disk(disk, async = true, attached_disk_options = {}) click to toggle source

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(async = true) click to toggle source

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(device_name, async = true) click to toggle source

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
ensure_key_comment(key, default_comment = "fog-user") click to toggle source
# 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
generate_ssh_key_metadata(username, key) click to toggle source
# 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
image_name() click to toggle source

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
map_scopes(scopes) click to toggle source
# 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
metadata_as_h() click to toggle source

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
private_ip_address() click to toggle source

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
private_ip_addresses() click to toggle source

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
provisioning?() click to toggle source

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
public_ip_address() click to toggle source

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
public_ip_addresses() click to toggle source

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
ready?() click to toggle source

Check if instance is ready.

@return [TrueClass or FalseClass]

# File lib/fog/compute/google/models/server.rb, line 478
def ready?
  status == "RUNNING"
end
reboot(async = true) click to toggle source
# 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
reload() click to toggle source
# 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
reset_windows_password(user) click to toggle source
# File lib/fog/compute/google/models/server.rb, line 591
def reset_windows_password(user)
  service.reset_windows_password(:server => self, :user => user)
end
save(username: nil, public_key: nil) click to toggle source
# 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
serial_port_output(port: 1) click to toggle source
# 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
set_disk_auto_delete(auto_delete, device_name = nil, async = true) click to toggle source
# 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
set_machine_type(new_machine_type, async = true) click to toggle source
# 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_metadata(new_metadata = {}, async = true) click to toggle source

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
set_scheduling(async = true, on_host_maintenance: nil, automatic_restart: nil, preemptible: nil) click to toggle source
# 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
set_tags(new_tags = [], async = true) click to toggle source
# File lib/fog/compute/google/models/server.rb, line 439
def set_tags(new_tags = [], async = true)
  requires :identity, :zone

  data = service.set_server_tags(
    identity, zone_name, tags[:fingerprint], new_tags
  )
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, data.zone)
  operation.wait_for { ready? } unless async
  reload
end
staging?() click to toggle source

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
start(async = true) click to toggle source
# 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
stop(async = true, discard_local_ssd=false) click to toggle source
# 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
stopped?() click to toggle source

Check if instance is stopped.

@return [TrueClass or FalseClass]

# File lib/fog/compute/google/models/server.rb, line 471
def stopped?
  status == "TERMINATED"
end
zone_name() click to toggle source
# File lib/fog/compute/google/models/server.rb, line 482
def zone_name
  zone.nil? ? nil : zone.split("/")[-1]
end

Private Instance Methods

metadata_to_item_list(metadata) click to toggle source
# 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
scope_url(scope) click to toggle source
# 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