Class: Fog::Compute::Google::Server

Inherits:
Server show all
Defined in:
lib/fog/google/models/compute/server.rb

Instance Attribute Summary

Attributes inherited from Server

#private_key, #private_key_path, #public_key, #public_key_path, #ssh_options, #ssh_port, #username

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Server

#scp, #scp_download, #ssh, #ssh_ip_address, #ssh_ip_address=, #sshable?

Methods inherited from Model

#initialize, #inspect, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Instance Method Details

#add_ssh_key(username, key) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/fog/google/models/compute/server.rb', line 110

def add_ssh_key username, key
  if self..nil?
    self. = Hash.new("")
  end

  # You can have multiple SSH keys, seperated by newlines.
  # https://developers.google.com/compute/docs/console?hl=en#sshkeys
  if !self.["sshKeys"]
    self.["sshKeys"] = ""
  end

  if !self.["sshKeys"].empty?
    self.["sshKeys"] += "\n"
  end

  self.["sshKeys"] += "#{username}:#{key.strip}"

  return self.
end

#destroyObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/fog/google/models/compute/server.rb', line 54

def destroy
  requires :name, :zone
  operation = service.delete_server(name, zone)
  # wait until "RUNNING" or "DONE" to ensure the operation doesn't fail, raises exception on error
  Fog.wait_for do
    operation = service.get_zone_operation(zone_name, operation.body["name"])
    operation.body["status"] != "PENDING"
  end
  operation
end

#flavor_idObject



46
47
48
# File 'lib/fog/google/models/compute/server.rb', line 46

def flavor_id
  machine_type
end

#flavor_id=(flavor_id) ⇒ Object



50
51
52
# File 'lib/fog/google/models/compute/server.rb', line 50

def flavor_id=(flavor_id)
  machine_type=flavor_id
end

#imageObject

not used since v1



66
67
68
69
# File 'lib/fog/google/models/compute/server.rb', line 66

def image
  Fog::Logger.deprecation("Server.image is deprecated, get source_image from boot disk")
  service.get_image(self.image_name.split('/')[-1])
end

#image_nameObject



28
29
30
31
32
33
34
35
36
# File 'lib/fog/google/models/compute/server.rb', line 28

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

#image_name=(args) ⇒ Object



24
25
26
# File 'lib/fog/google/models/compute/server.rb', line 24

def image_name=(args)
  Fog::Logger.deprecation("image_name= is no longer used [light_black](#{caller.first})[/]")
end

#kernelObject



41
42
43
44
# File 'lib/fog/google/models/compute/server.rb', line 41

def kernel
  Fog::Logger.deprecation("kernel is no longer used [light_black](#{caller.first})[/]")
  nil
end

#kernel=(args) ⇒ Object



38
39
40
# File 'lib/fog/google/models/compute/server.rb', line 38

def kernel=(args)
  Fog::Logger.deprecation("kernel= is no longer used [light_black](#{caller.first})[/]")
end

#private_ip_addressObject



88
89
90
91
92
93
94
# File 'lib/fog/google/models/compute/server.rb', line 88

def private_ip_address
  ip = nil
  if self.network_interfaces.respond_to? :first
    ip = self.network_interfaces.first['networkIP']
  end
  ip
end

#public_ip_addressObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fog/google/models/compute/server.rb', line 71

def public_ip_address
  ip = nil
  if self.network_interfaces.respond_to? :each
    self.network_interfaces.each do |netif|
      if netif["accessConfigs"].respond_to? :each
        netif["accessConfigs"].each do |access_config|
          if access_config["name"] == "External NAT"
            ip = access_config['natIP']
          end
        end
      end
    end
  end

  ip
end

#ready?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/fog/google/models/compute/server.rb', line 96

def ready?
  self.state == RUNNING
end

#reloadObject



131
132
133
134
# File 'lib/fog/google/models/compute/server.rb', line 131

def reload
  data = service.get_server(self.name, self.zone).body
  self.merge_attributes(data)
end

#saveObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fog/google/models/compute/server.rb', line 136

def save
  requires :name
  requires :machine_type
  requires :zone_name
  requires :disks

  if not service.zones.find{ |zone| zone.name == self.zone_name }
    raise ArgumentError.new "#{self.zone_name.inspect} is either down or you don't have permission to use it."
  end

  self.add_ssh_key(self.username, self.public_key) if self.public_key

  options = {
      'machineType' => machine_type,
      'networkInterfaces' => network_interfaces,
      'network' => network,
      'externalIp' => external_ip,
      'disks' => disks,
      'metadata' => ,
      'serviceAccounts' => service_accounts,
      'tags' => tags
  }.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

#zoneObject



100
101
102
103
104
105
106
107
108
# File 'lib/fog/google/models/compute/server.rb', line 100

def zone
  if self.zone_name.is_a? String
    service.get_zone(self.zone_name.split('/')[-1]).body["name"]
  elsif zone_name.is_a? Excon::Response
    service.get_zone(zone_name.body["name"]).body["name"]
  else
    self.zone_name
  end
end