1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
|
require "fog/compute/models/server"
module Fog
module Compute
class Google
class Server < Fog::Compute::Server
identity :name
# @return [Boolean]
attribute :can_ip_forward, :aliases => "canIpForward"
# @return [String]
attribute :cpu_platform, :aliases => "cpuPlatform"
# @return [String]
attribute :creation_timestamp, :aliases => "creationTimestamp"
# @return [Boolean]
attribute :deletion_protection, :aliases => "deletionProtection"
# @return [String]
attribute :description
# New disks may include :initialize_params before save.
#
# @example Minimal disks pre-creation:
# [
# {
# :initialize_params => {
# :source_image => "projects/debian-cloud/global/images/family/debian-9"
# }
# }
# ]
#
# @example disks post-creation:
# [
# {
# :auto_delete => false,
# :boot => true,
# :device_name => "persistent-disk-0",
# :index => 0,
# :interface => "SCSI",
# :kind => "compute#attachedDisk",
# :licenses => ["https://www.googleapis.com/compute/v1/..."],
# :mode => "READ_WRITE",
# :source => "https://www.googleapis.com/compute/v1/.../mydisk",
# :type => "PERSISTENT"
# }
# ]
# @return [Array<Hash>]
attribute :disks
# @example Enable the display device
# {
# :enable_display => true
# }
# @return [Hash<String,Boolean>]
attribute :display_device, :aliases => "displayDevice"
# @example Guest accelerators
# [
# {
# :accelerator_count => 1,
# :accelerator_type => "...my/accelerator/type"
# }
# ]
# @return [Array<Hash>]
attribute :guest_accelerators, :aliases => "guestAccelerators"
# @return [Fixnum]
attribute :id
# @return [String]
attribute :kind
# @return [String]
attribute :label_fingerprint, :aliases => "labelFingerprint"
# @return [Hash<String,String>]
attribute :labels
# @return [String]
attribute :machine_type, :aliases => "machineType"
# If set initially before save, the expected format
# is the API format as shown below.
#
# If you want to pass in a Hash, see {#set_metadata}.
# If you want to access the metadata items as a Hash, see
# {#metadata_as_h}.
#
# @example Metadata in API format
#
# {
# :fingerprint => "...",
# :items => [
# { :key => "foo", :value => "bar" },
# ]
# }
# @return [Hash]
attribute :metadata
# @return [String]
attribute :min_cpu_platform, :aliases => "minCpuPlatform"
# @example Network interfaces
# [
# {
# :kind => "compute#networkInterface",
# :name => "nic0",
# :network => "https://www.googleapis.com/compute/v1/.../my-network/"
# :network_ip => "0.0.0.0",
# :subnetwork => "https://www.googleapis.com/compute/v1/.../my-subnetwork"
# }
# ],
# @return [Array<Hash>]
attribute :network_interfaces, :aliases => "networkInterfaces"
# @example Scheduling object
# {
# :automatic_restart => true,
# :on_host_maintenance => "MIGRATE",
# :preemptible=>false
# }
# @return [Hash]
attribute :scheduling
# @return [String]
attribute :self_link, :aliases => "selfLink"
# @example Service accounts in API format
# [
# {
# :email => "my-service-account@developer.gserviceaccount.com",
# :scopes => [],
# }
# ]
# @return [Array<Hash>]
attribute :service_accounts, :aliases => "serviceAccounts"
# @return [Boolean]
attribute :start_restricted, :aliases => "startRestricted"
# @return [String]
attribute :status, :aliases => "status"
# @return [String]
attribute :status_message, :aliases => "statusMessage"
# @example Tags in API format
# @return [Hash]
attribute :tags
# @return [String]
attribute :zone, :aliases => :zone_name
GCE_SCOPE_ALIASES = {
"default" => %w(
https://www.googleapis.com/auth/cloud.useraccounts.readonly
https://www.googleapis.com/auth/devstorage.read_only
https://www.googleapis.com/auth/logging.write
https://www.googleapis.com/auth/monitoring.write
https://www.googleapis.com/auth/pubsub
https://www.googleapis.com/auth/service.management.readonly
https://www.googleapis.com/auth/servicecontrol
https://www.googleapis.com/auth/trace.append
),
"bigquery" => ["https://www.googleapis.com/auth/bigquery"],
"cloud-platform" => ["https://www.googleapis.com/auth/cloud-platform"],
"compute-ro" => ["https://www.googleapis.com/auth/compute.readonly"],
"compute-rw" => ["https://www.googleapis.com/auth/compute"],
"datastore" => ["https://www.googleapis.com/auth/datastore"],
"logging-write" => ["https://www.googleapis.com/auth/logging.write"],
"monitoring" => ["https://www.googleapis.com/auth/monitoring"],
"monitoring-write" => ["https://www.googleapis.com/auth/monitoring.write"],
"service-control" => ["https://www.googleapis.com/auth/servicecontrol"],
"service-management" => ["https://www.googleapis.com/auth/service.management.readonly"],
"sql" => ["https://www.googleapis.com/auth/sqlservice"],
"sql-admin" => ["https://www.googleapis.com/auth/sqlservice.admin"],
"storage-full" => ["https://www.googleapis.com/auth/devstorage.full_control"],
"storage-ro" => ["https://www.googleapis.com/auth/devstorage.read_only"],
"storage-rw" => ["https://www.googleapis.com/auth/devstorage.read_write"],
"taskqueue" => ["https://www.googleapis.com/auth/taskqueue"],
"useraccounts-ro" => ["https://www.googleapis.com/auth/cloud.useraccounts.readonly"],
"useraccounts-rw" => ["https://www.googleapis.com/auth/cloud.useraccounts"],
"userinfo-email" => ["https://www.googleapis.com/auth/userinfo.email"]
}.freeze
# Return the source image of the server's boot disk
#
# @return [String] image self link
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
# Destroy a server.
#
# @param async [TrueClass] execute the command asynchronously
# @return [Fog::Compute::Google::Operation]
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
# Helper method that returns first public ip address needed for
# Fog::Compute::Server.ssh default behavior.
#
# @return [String]
def public_ip_address
public_ip_addresses.first
end
# Helper method that returns all of server's public ip addresses.
#
# @return [Array]
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
# Helper method that returns the first private ip address of the
# instance.
#
# @return [String]
def private_ip_address
private_ip_addresses.first
end
# Helper method that returns all of server's private ip addresses.
#
# @return [Array]
def private_ip_addresses
addresses = []
if network_interfaces.respond_to? :map
addresses = network_interfaces.map { |nic| nic[:network_ip] }
end
addresses
end
# Helper method that returns all of server's ip addresses,
# both private and public.
#
# @return [Array]
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]
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
# Detach disk from a running instance
#
# @param device_name [Object]
# @param async [TrueClass]
# @returns [Fog::Compute::Google::Server] server object
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
# Returns metadata items as a Hash.
#
# @return [Hash<String, String>] items
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
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
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
def stop(async = true)
requires :identity, :zone
data = service.stop_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
def serial_port_output(port: 1)
requires :identity, :zone
service.get_server_serial_port_output(identity, zone_name, :port => port).to_h[:contents]
end
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
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 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
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
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
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
# Check if instance is provisioning. On staging vs. provisioning difference:
# https://cloud.google.com/compute/docs/instances/checking-instance-status
#
# @return [TrueClass or FalseClass]
def provisioning?
status == "PROVISIONING"
end
# Check if instance is staging. On staging vs. provisioning difference:
# https://cloud.google.com/compute/docs/instances/checking-instance-status
#
# @return [TrueClass or FalseClass]
def staging?
status == "STAGING"
end
# Check if instance is stopped.
#
# @return [TrueClass or FalseClass]
def stopped?
status == "TERMINATED"
end
# Check if instance is ready.
#
# @return [TrueClass or FalseClass]
def ready?
status == "RUNNING"
end
def zone_name
zone.nil? ? nil : zone.split("/")[-1]
end
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
def reload
data = service.get_server(name, zone_name).to_h
merge_attributes(data)
end
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
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
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
def ensure_key_comment(key, default_comment = "fog-user")
parts = key.strip.split
parts << default_comment if parts.size < 3
parts.join(" ")
end
def reset_windows_password(user)
service.reset_windows_password(:server => self, :user => user)
end
private
def metadata_to_item_list(metadata)
metadata.map { |k, v| { :key => k, :value => v } }
end
def scope_url(scope)
if scope.start_with?("https://")
scope
else
"https://www.googleapis.com/auth/#{scope}"
end
end
end
end
end
end
|