Class: Fog::Compute::Google::Servers

Inherits:
Fog::Collection
  • Object
show all
Defined in:
lib/fog/compute/google/models/servers.rb

Instance Method Summary collapse

Instance Method Details

#all(zone: nil, filter: nil, max_results: nil, order_by: nil, page_token: nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fog/compute/google/models/servers.rb', line 7

def all(zone: nil, filter: nil, max_results: nil,
        order_by: nil, page_token: nil)
  opts = {
    :filter => filter,
    :max_results => max_results,
    :order_by => order_by,
    :page_token => page_token
  }

  if zone
    data = service.list_servers(zone, opts).to_h[:items] || []
  else
    data = []
    service.list_aggregated_servers(opts).items.each_value do |scoped_lst|
      if scoped_lst && scoped_lst.instances
        data.concat(scoped_lst.instances.map(&:to_h))
      end
    end
  end
  load(data)
end

#bootstrap(public_key_path: nil, **opts) ⇒ Object


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
# File 'lib/fog/compute/google/models/servers.rb', line 44

def bootstrap(public_key_path: nil, **opts)
  user = ENV["USER"]
  public_key = get_public_key(public_key_path)

  name = "fog-#{Time.now.to_i}"
  zone_name = "us-central1-f"

  disks = opts[:disks]

  if disks.nil? || disks.empty?
    # create the persistent boot disk
    source_img = service.images.get_from_family("debian-8")
    disk_defaults = {
      :name => name,
      :size_gb => 10,
      :zone_name => zone_name,
      :source_image => source_img.self_link
    }
    disk = service.disks.create(disk_defaults.merge(opts))
    disk.wait_for { disk.ready? }

    disks = [disk]
  end

  data = opts.merge(
    :name => name,
    :zone => zone_name,
    :disks => disks
  )
  data[:machine_type] = "n1-standard-1" unless data[:machine_type]

  server = new(data)
  server.save(:username => user, :public_key => public_key)
  # TODO: sshable? was removed, needs to be fixed for tests
  # server.wait_for { sshable? }
  server
end

#get(identity, zone = nil) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/compute/google/models/servers.rb', line 29

def get(identity, zone = nil)
  response = nil
  if zone
    response = service.get_server(identity, zone).to_h
  else
    server = all(:filter => "name eq .*#{identity}").first
    response = server.attributes if server
  end
  return nil if response.nil?
  new(response)
rescue ::Google::Apis::ClientError => e
  raise e unless e.status_code == 404
  nil
end