Class: RancherMetadata::API

Inherits:
Object
  • Object
show all
Defined in:
lib/rancher-metadata/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ API

Returns a new instance of API.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rancher-metadata/api.rb', line 14

def initialize(config)
  defaults = {
    :api_url => ["http://rancher-metadata/2015-12-19"],
    :max_attempts => 3
  }

  if config.has_key?(:api_url)
    config[:api_url] = [ config[:api_url] ] unless config[:api_url].is_a?(Array)

    raise("Must pass one or more API endpoints") unless config[:api_url].size > 0
  end

  @config = defaults.merge(config)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/rancher-metadata/api.rb', line 12

def config
  @config
end

Instance Method Details

#api_get(query) ⇒ Object



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
# File 'lib/rancher-metadata/api.rb', line 33

def api_get(query)
  success = true
  attempts = 1
  data = nil

  self.config[:max_attempts].times.each do |i|
    self.config[:api_url].each do |api_url|
      begin
        uri = URI.parse("#{api_url}#{query}")
        req = Net::HTTP::Get.new(uri.path, {'Content-Type' => 'application/json', 'Accept' => 'application/json'})
        resp = Net::HTTP.new(uri.host, uri.port).request(req)
        begin
          data = JSON.parse(resp.body)
        rescue JSON::ParserError
          data = resp.body
        end

        success = true

        break
      rescue
        STDERR.puts("Failed to query Rancher Metadata API on #{api_url} - Caught exception (#{$!})")
      end
    end

    i += 1
  end

  raise("Failed to query Rancher Metadata API (#{attempts} out of #{self.config[:max_attempts]} failed)") unless success

  is_error?(data) ? nil : data
end

#get_container(container_name = nil) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rancher-metadata/api.rb', line 168

def get_container(container_name = nil)
  container = nil

  if container_name
    container = self.api_get("/containers/#{container_name}")
  else
    container = self.api_get("/self/container")
  end

  container['create_index'] = container['create_index'].to_i if (container.has_key?('create_index') and container['create_index'].is_a?(String))
  container['service_index'] = container['service_index'].to_i if (container.has_key?('service_index') and container['service_index'].is_a?(String))

  container
end

#get_container_create_index(container_name = nil) ⇒ Object



187
188
189
190
# File 'lib/rancher-metadata/api.rb', line 187

def get_container_create_index(container_name = nil)
 i = self.get_container_field("create_index", container_name)
 i ? i.to_i : nil
end

#get_container_field(field, container_name = nil) ⇒ Object



183
184
185
# File 'lib/rancher-metadata/api.rb', line 183

def get_container_field(field, container_name = nil)
  container_name ? self.api_get("/containers/#{container_name}/#{field}") : self.api_get("/self/container/#{field}")
end

#get_container_host_uuid(container_name = nil) ⇒ Object



227
228
229
# File 'lib/rancher-metadata/api.rb', line 227

def get_container_host_uuid(container_name = nil)
  self.get_container_field("host_uuid", container_name)
end

#get_container_hostname(container_name = nil) ⇒ Object



218
219
220
# File 'lib/rancher-metadata/api.rb', line 218

def get_container_hostname(container_name = nil)
  self.get_container_field("hostname", container_name)
end

#get_container_ip(container_name = nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rancher-metadata/api.rb', line 192

def get_container_ip(container_name = nil)
  if container_name
  # are we running within the rancher managed network?
    # FIXME: https://github.com/rancher/rancher/issues/2750
    if self.is_network_managed?
      self.api_get("/self/container/primary_ip")
    else
      self.get_host_ip
    end
  else
    self.api_get("/containers/#{container_name}/primary_ip")
  end
end

#get_container_name(container_name = nil) ⇒ Object



206
207
208
# File 'lib/rancher-metadata/api.rb', line 206

def get_container_name(container_name = nil)
  self.get_container_field("name", container_name)
end

#get_container_service_index(container_name = nil) ⇒ Object



222
223
224
225
# File 'lib/rancher-metadata/api.rb', line 222

def get_container_service_index(container_name = nil)
 i = self.get_container_field("service_index", container_name)
 i ? i.to_i : nil
end

#get_container_service_name(container_name = nil) ⇒ Object



210
211
212
# File 'lib/rancher-metadata/api.rb', line 210

def get_container_service_name(container_name = nil)
  self.get_container_field("service_name", container_name)
end

#get_container_stack_name(container_name = nil) ⇒ Object



214
215
216
# File 'lib/rancher-metadata/api.rb', line 214

def get_container_stack_name(container_name = nil)
  self.get_container_field("stack_name", container_name)
end

#get_containersObject



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rancher-metadata/api.rb', line 155

def get_containers
  containers = []

  self.api_get("/containers").each do |container|
    container['create_index'] = container['create_index'].to_i if (container.has_key?('create_index') and container['create_index'].is_a?(String))
    container['service_index'] = container['service_index'].to_i if (container.has_key?('service_index') and container['service_index'].is_a?(String))

    containers << container
  end

  containers
end

#get_host(host_name = nil) ⇒ Object



235
236
237
# File 'lib/rancher-metadata/api.rb', line 235

def get_host(host_name = nil)
  host_name ? self.api_get("/hosts/#{host_name}") : self.api_get("/self/host")
end

#get_host_field(field, host_name = nil) ⇒ Object



239
240
241
# File 'lib/rancher-metadata/api.rb', line 239

def get_host_field(field, host_name = nil)
  host_name ? self.api_get("/hosts/#{host_name}/#{field}") : self.api_get("/self/host/#{field}")
end

#get_host_ip(host_name = nil) ⇒ Object



243
244
245
# File 'lib/rancher-metadata/api.rb', line 243

def get_host_ip(host_name = nil)
  self.get_host_field("agent_ip", host_name)
end

#get_host_name(host_name = nil) ⇒ Object



251
252
253
# File 'lib/rancher-metadata/api.rb', line 251

def get_host_name(host_name = nil)
  self.get_host_field("name", host_name)
end

#get_host_uuid(host_name = nil) ⇒ Object



247
248
249
# File 'lib/rancher-metadata/api.rb', line 247

def get_host_uuid(host_name = nil)
  self.get_host_field("uuid", host_name)
end

#get_service(service = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rancher-metadata/api.rb', line 70

def get_service(service = {})
  if service.empty?
    return self.api_get("/self/service")
  else
    raise("Missing rancher service name") unless service.has_key?(:service_name)

    unless service.has_key?(:stack_name)
      return self.api_get("/self/stack/services/#{service[:service_name]}")
    else
      return self.api_get("/stacks/#{service[:stack_name]}/services/#{service[:service_name]}")
    end
  end
end

#get_service_containers(service = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rancher-metadata/api.rb', line 102

def get_service_containers(service = {})
  containers = {}

  self.get_service_field("containers", service).each do |container|
    container['create_index'] = container['create_index'].to_i if (container.has_key?('create_index') and container['create_index'].is_a?(String))
    container['service_index'] = container['service_index'].to_i if (container.has_key?('service_index') and container['service_index'].is_a?(String))

    containers[container['name']] = container
  end

  containers
end

#get_service_field(field, service = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rancher-metadata/api.rb', line 84

def get_service_field(field, service = {})
  if service.empty?
    return self.api_get("/self/service/#{field}")
  else
    raise("Must provide service name") unless service.has_key?(:service_name)

    unless service.has_key?(:stack_name)
      return self.api_get("/self/stack/services/#{service[:service_name]}/#{field}")
    else
      return self.api_get("/stacks/#{service[:stack_name]}/services/#{service[:service_name]}/#{field}")
    end
  end
end


119
120
121
# File 'lib/rancher-metadata/api.rb', line 119

def get_service_links(service = {})
  self.get_service_field("links", service)
end

#get_service_metadata(service = {}) ⇒ Object



115
116
117
# File 'lib/rancher-metadata/api.rb', line 115

def (service = {})
  self.get_service_field("metadata", service)
end

#get_service_scale_size(service = {}) ⇒ Object



98
99
100
# File 'lib/rancher-metadata/api.rb', line 98

def get_service_scale_size(service = {})
  self.get_service_field("scale", service).to_i
end

#get_servicesObject



66
67
68
# File 'lib/rancher-metadata/api.rb', line 66

def get_services
  self.api_get("/services")
end

#get_stack(stack_name = nil) ⇒ Object



147
148
149
# File 'lib/rancher-metadata/api.rb', line 147

def get_stack(stack_name = nil)
  stack_name ? self.api_get("/stacks/#{stack_name}") : self.api_get("/self/stack")
end

#get_stack_services(stack_name = nil) ⇒ Object



151
152
153
# File 'lib/rancher-metadata/api.rb', line 151

def get_stack_services(stack_name = nil)
  stack_name ? self.api_get("/stacks/#{stack_name}/services") : self.api_get("/self/stack/services")
end

#get_stacksObject



143
144
145
# File 'lib/rancher-metadata/api.rb', line 143

def get_stacks
  self.api_get("/stacks")
end

#is_error?(data) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rancher-metadata/api.rb', line 29

def is_error?(data)
  ( data.is_a?(Hash) and data.has_key?('code') and data['code'] == 404 ) ? true : false
end

#is_network_managed?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/rancher-metadata/api.rb', line 231

def is_network_managed?
  self.get_container_create_index ? true : false
end

#wait_service_containers(service = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rancher-metadata/api.rb', line 123

def wait_service_containers(service = {})
  scale = self.get_service_scale_size(service)

  old = []
  loop do
    containers = self.get_service_containers(service)
    new = containers.keys()

    (new - old).each do |name|
      yield(name, containers[name])
    end

    old = new

    break if new.size >= scale

    sleep(0.5)
  end
end