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

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

Instance Method Summary collapse

Instance Method Details

#add_ssh_key(username, key) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/fog/google/models/compute/server.rb', line 189

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

#addressesObject



107
108
109
# File 'lib/fog/google/models/compute/server.rb', line 107

def addresses
  [private_ip_address, public_ip_address]
end

#attach_disk(disk, options = {}) ⇒ Object



111
112
113
114
115
116
# File 'lib/fog/google/models/compute/server.rb', line 111

def attach_disk(disk, options = {})
  requires :identity, :zone

  data = service.attach_disk(identity, zone_name, disk, options)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#destroy(async = true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/fog/google/models/compute/server.rb', line 65

def destroy(async=true)
  requires :name, :zone

  data = service.delete_server(name, zone_name)
  operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
  unless async
    operation.wait_for { ready? }
  end
  operation
end

#detach_disk(device_name) ⇒ Object



118
119
120
121
122
123
# File 'lib/fog/google/models/compute/server.rb', line 118

def detach_disk(device_name)
  requires :identity, :zone

  data = service.detach_disk(identity, zone, device_name)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#flavor_idObject



57
58
59
# File 'lib/fog/google/models/compute/server.rb', line 57

def flavor_id
  machine_type
end

#flavor_id=(flavor_id) ⇒ Object



61
62
63
# File 'lib/fog/google/models/compute/server.rb', line 61

def flavor_id=(flavor_id)
  machine_type=flavor_id
end

#imageObject

not used since v1



77
78
79
80
# File 'lib/fog/google/models/compute/server.rb', line 77

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



38
39
40
41
42
43
44
45
46
# File 'lib/fog/google/models/compute/server.rb', line 38

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



34
35
36
# File 'lib/fog/google/models/compute/server.rb', line 34

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

#kernelObject



52
53
54
55
# File 'lib/fog/google/models/compute/server.rb', line 52

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

#kernel=(args) ⇒ Object



48
49
50
# File 'lib/fog/google/models/compute/server.rb', line 48

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

#private_ip_addressObject



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

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fog/google/models/compute/server.rb', line 82

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)


181
182
183
# File 'lib/fog/google/models/compute/server.rb', line 181

def ready?
  self.state == RUNNING
end

#rebootObject



125
126
127
128
129
130
# File 'lib/fog/google/models/compute/server.rb', line 125

def reboot
  requires :identity, :zone

  data = service.reset_server(identity, zone_name)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#reloadObject



209
210
211
212
# File 'lib/fog/google/models/compute/server.rb', line 209

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

#saveObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fog/google/models/compute/server.rb', line 214

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,
      'auto_restart' => auto_restart,
      'on_host_maintenance' => on_host_maintenance,
      'can_ip_forward' => can_ip_forward
  }.delete_if {|key, value| value.nil?}

  if service_accounts
    options['serviceAccounts'] = [{
      "kind" => "compute#serviceAccount",
      "email" => "default",
      "scopes" => service_accounts.map {
        |w| w.start_with?("https://") ? w : "https://www.googleapis.com/auth/#{w}"
      }
    }]
  end

  data = service.insert_server(name, zone_name, options)
  operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
  operation.wait_for { !pending? }
  reload
end

#serial_port_outputObject



146
147
148
149
150
151
# File 'lib/fog/google/models/compute/server.rb', line 146

def serial_port_output
  requires :identity, :zone

  data = service.get_server_serial_port_output(identity, zone_name)
  data.body['contents']
end

#set_disk_auto_delete(auto_delete, device_name) ⇒ Object



153
154
155
156
157
158
# File 'lib/fog/google/models/compute/server.rb', line 153

def set_disk_auto_delete(auto_delete, device_name)
  requires :identity, :zone

  data = service.set_server_disk_auto_delete(identity, zone_name, auto_delete, device_name)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#set_metadata(metadata = {}) ⇒ Object



167
168
169
170
171
172
# File 'lib/fog/google/models/compute/server.rb', line 167

def ( = {})
  requires :identity, :zone

  data = service.(identity, zone_name, self.['fingerprint'], )
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#set_scheduling(on_host_maintenance, automatic_restart) ⇒ Object



160
161
162
163
164
165
# File 'lib/fog/google/models/compute/server.rb', line 160

def set_scheduling(on_host_maintenance, automatic_restart)
  requires :identity, :zone

  data = service.set_server_scheduling(identity, zone_name, on_host_maintenance, automatic_restart)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#set_tags(tags = []) ⇒ Object



174
175
176
177
178
179
# File 'lib/fog/google/models/compute/server.rb', line 174

def set_tags(tags = [])
  requires :identity, :zone

  data = service.set_tags(identity, zone_name, self.tags['fingerprint'], tags)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#startObject



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

def start
  requires :identity, :zone

  data = service.start_server(identity, zone_name)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#stopObject



139
140
141
142
143
144
# File 'lib/fog/google/models/compute/server.rb', line 139

def stop
  requires :identity, :zone

  data = service.stop_server(identity, zone_name)
  Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
end

#zone_nameObject



185
186
187
# File 'lib/fog/google/models/compute/server.rb', line 185

def zone_name
  zone.nil? ? nil : zone.split('/')[-1]
end