Class: Docker::Swarm::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/swarm/node.rb

Overview

This class represents a Docker Swarm Node.

Constant Summary collapse

AVAILABILITY =
{
  active: "active",
  drain:  "drain"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, connection) ⇒ Node

Returns a new instance of Node.


10
11
12
13
14
# File 'lib/docker/swarm/node.rb', line 10

def initialize(hash, connection)
  @hash = hash
  @connection = connection
  hash['Description']['Hostname']
end

Instance Attribute Details

#hashObject (readonly)

include Docker::Base


4
5
6
# File 'lib/docker/swarm/node.rb', line 4

def hash
  @hash
end

Class Method Details

.all(opts = {}, conn = Docker.connection) ⇒ Object

Return all of the Nodes.


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/docker/swarm/node.rb', line 58

def self.all(opts = {}, conn = Docker.connection)
  raise "opts needs to be hash" if (opts.class != Hash)
  query = {}
  resp = conn.get('/nodes', query, :body => opts.to_json)
  hashes = JSON.parse(resp)
  nodes = []
  hashes.each do |node_hash|
    nodes << Docker::Swarm::Node.new(node_hash, conn)
  end
  return nodes
end

Instance Method Details

#activateObject


46
47
48
# File 'lib/docker/swarm/node.rb', line 46

def activate
  change_availability(:active)
end

#availabilityObject


34
35
36
# File 'lib/docker/swarm/node.rb', line 34

def availability
  return @hash['Spec']['Availability'].to_sym
end

#change_availability(availability) ⇒ Object


50
51
52
53
54
55
# File 'lib/docker/swarm/node.rb', line 50

def change_availability(availability)
  raise "Bad availability param: #{availability}" if (!AVAILABILITY[availability])
  @hash['Spec']['Availability'] = AVAILABILITY[availability]
  query = {version: @hash['Version']['Index']}
  response = @connection.post("/nodes/#{self.id}/update", query, :body => @hash['Spec'].to_json)
end

#drainObject


42
43
44
# File 'lib/docker/swarm/node.rb', line 42

def drain
  change_availability(:drain)
end

#host_nameObject


20
21
22
# File 'lib/docker/swarm/node.rb', line 20

def host_name
  return @hash['Description']['Hostname']
end

#idObject


16
17
18
# File 'lib/docker/swarm/node.rb', line 16

def id 
  return @hash['ID']
end

#roleObject


24
25
26
27
28
29
30
31
32
# File 'lib/docker/swarm/node.rb', line 24

def role
  if (@hash['Spec']['Role'] == "worker")
    return :worker
  elsif (@hash['Spec']['Role'] == "manager")
    return :manager
  else
    raise "Couldn't determine machine role from spec: #{@hash['Spec']}"
  end
end

#statusObject


38
39
40
# File 'lib/docker/swarm/node.rb', line 38

def status
  return @hash['Status']['State']
end