Class: Fog::Hetznercloud::Compute::Servers

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

Instance Method Summary collapse

Instance Method Details

#all(filters = {}) ⇒ Object



7
8
9
10
# File 'lib/fog/hetznercloud/models/compute/servers.rb', line 7

def all(filters = {})
  servers = service.list_servers(filters).body['servers'] || []
  load(servers)
end

#bootstrap(new_attributes = {}) ⇒ Object

Note:

When booting without ssh_keys set, bootstrapping will wait until the server is read and not until you can ssh into the server Bootstrapping waits until bootstap_timeout seconds and then destroy the server. Default timeout is 120 seconds, which should be ok unless you have a lot of user-data scripts.

Sets the proc used to determine the IP Address used for ssh/scp interactions.

Examples:

service.servers.bootstrap :name => "bootstrap-server",
                          :image => service.flavors.first.id,
                          :server_type => service.server_types.all(:name => 'cx11').identity,
                          :private_key_path => "~/.ssh/fog_rsa",
                          :user_data => "./file",
                          :location => 'nbg1',
                          :ssh_keys => [ 'keyname' ],
                          :bootstap_timeout => 120


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

def bootstrap(new_attributes = {})
  require 'securerandom'

  defaults = {
    name: "hc-#{SecureRandom.hex(3)}",
    image: 'centos-7',
    server_type: 'cx11'
  }

  bootstap_timeout = new_attributes[:bootstap_timeout].nil? ? 120 : new_attributes[:bootstap_timeout]

  server = create(defaults.merge(new_attributes))

  # if ssh keys are provided wait until we can SSH the server
  # othwise just wait till the server is ready as we will never
  # be able to login
  status = false
  begin
    status = Timeout.timeout(bootstap_timeout) do
      if new_attributes[:ssh_keys].nil?
        server.wait_for { ready? }
      else
        server.wait_for { sshable?(auth_methods: %w[publickey]) }
      end
    end
  rescue Timeout::Error => e
    server.destroy
    raise e
  end

  server
end

#get(identity) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/fog/hetznercloud/models/compute/servers.rb', line 12

def get(identity)
  if (server = service.get_server(identity).body['server'])
    new(server)
  end
rescue Fog::Hetznercloud::Compute::UnknownResourceError
  nil
end