class Docker::Swarm::Node
Constants
- AVAILABILITY
Attributes
hash[R]
swarm[R]
Public Class Methods
networks_on_host(connection, swarm)
click to toggle source
# File lib/docker/swarm/node.rb, line 180 def self.networks_on_host(connection, swarm) networks = [] response = connection.get("/networks", {}, full_response: true, expects: [200]) network_hashes = JSON.parse(response.body) network_hashes.each do |network_hash| networks << Docker::Swarm::Network.new(swarm, network_hash) end return networks end
new(swarm, hash)
click to toggle source
# File lib/docker/swarm/node.rb, line 9 def initialize(swarm, hash) @hash = hash @swarm = swarm end
remove(node_id, connection)
click to toggle source
# File lib/docker/swarm/node.rb, line 172 def self.remove(node_id, connection) query = {} response = connection.delete("/nodes/#{node_id}", query, expects: [200, 406, 500], full_response: true) if (response.status != 200) raise "Error deleting node: HTTP-#{response.status} #{response.body}" end end
Public Instance Methods
activate()
click to toggle source
# File lib/docker/swarm/node.rb, line 83 def activate change_availability(:active) end
availability()
click to toggle source
# File lib/docker/swarm/node.rb, line 46 def availability return @hash['Spec']['Availability'].to_sym end
change_availability(new_availability)
click to toggle source
# File lib/docker/swarm/node.rb, line 131 def change_availability(new_availability) raise "Bad availability param: #{availability}" if (!AVAILABILITY[availability]) refresh if (self.availability != new_availability) @hash['Spec']['Availability'] = AVAILABILITY[new_availability] query = {version: @hash['Version']['Index']} response = @swarm.connection.post("/nodes/#{self.id}/update", query, :body => @hash['Spec'].to_json, expects: [200, 500], full_response: true) if (response.status != 200) raise "Error changing node availability: #{response.body} HTTP-#{response.status}" end end end
connection()
click to toggle source
# File lib/docker/swarm/node.rb, line 28 def connection if (@swarm) && (@swarm.node_hash[id()]) return @swarm.node_hash[id()][:connection] else return nil end end
drain(opts = {})
click to toggle source
# File lib/docker/swarm/node.rb, line 54 def drain(opts = {}) change_availability(:drain) if (opts[:wait_for_drain]) opts[:wait_seconds] while (running_tasks.length > 0) puts "Waiting for node (#{host_name}) to drain. Still has #{running_tasks.length} tasks running." end end end
find_network_by_id(network_id)
click to toggle source
# File lib/docker/swarm/node.rb, line 162 def find_network_by_id(network_id) networks.each do |network| if (network.id == network_id) return network end end return nil end
find_network_by_name(network_name)
click to toggle source
# File lib/docker/swarm/node.rb, line 153 def find_network_by_name(network_name) networks.each do |network| if (network.name == network_name) return network end end return nil end
host_name()
click to toggle source
# File lib/docker/swarm/node.rb, line 24 def host_name return @hash['Description']['Hostname'] end
id()
click to toggle source
# File lib/docker/swarm/node.rb, line 20 def id return @hash['ID'] end
leave(force = true)
click to toggle source
# File lib/docker/swarm/node.rb, line 125 def leave(force = true) drain(wait_for_drain: true, wait_seconds: 60) # change_availability(:active) @swarm.leave(self, force) end
networks()
click to toggle source
# File lib/docker/swarm/node.rb, line 144 def networks() if (connection) return Docker::Swarm::Node.networks_on_host(connection, @swarm) else debugger raise "No connection set for node: #{self.host_name}, ID: #{self.id}" end end
refresh()
click to toggle source
# File lib/docker/swarm/node.rb, line 14 def refresh query = {} response = @swarm.connection.get("/nodes/#{id}", query, expects: [200]) @hash = JSON.parse(response) end
remove()
click to toggle source
# File lib/docker/swarm/node.rb, line 87 def remove leave(true) refresh start_time = Time.now while (self.status != 'down') refresh raise "Node not down 60 seconds after leaving swarm: #{self.host_name}" if (Time.now.to_i - start_time.to_i > 60) end Docker::Swarm::Node.remove(self.id, @swarm.connection) end
remove_network(network)
click to toggle source
# File lib/docker/swarm/node.rb, line 104 def remove_network(network) attempts = 0 if (self.connection == nil) puts "Warning: node asked to remove network, but no connection for node: #{self.id} #{self.host_name}" else while (self.find_network_by_id(network.id) != nil) response = self.connection.delete("/networks/#{network.id}", {}, expects: [204, 404, 500], full_response: true) if (response.status == 500) puts "Warning: Deleting network (#{network.name}) from #{self.host_name} returned HTTP-#{response.status} #{response.body}" end sleep 1 attempts += 1 if (attempts > 30) raise "Failed to remove network: #{network.name} from #{self.host_name}, operation timed out. Response: HTTP#{response.status} #{response.body}" end end end end
remove_network_with_name(network_name)
click to toggle source
# File lib/docker/swarm/node.rb, line 99 def remove_network_with_name(network_name) network = find_network_by_name(network_name) self.remove_network(network) if (network) end
role()
click to toggle source
# File lib/docker/swarm/node.rb, line 36 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
running_tasks()
click to toggle source
# File lib/docker/swarm/node.rb, line 73 def running_tasks return tasks.select {|t| t.status == 'running'} end
status()
click to toggle source
# File lib/docker/swarm/node.rb, line 50 def status return @hash['Status']['State'] end
swarm_connection()
click to toggle source
# File lib/docker/swarm/node.rb, line 64 def swarm_connection node_hash = @swarm.node_hash[self.id] if (node_hash) return node_hash[:connection] end return nil end
tasks()
click to toggle source
# File lib/docker/swarm/node.rb, line 77 def tasks return @swarm.tasks.select {|t| (t.node != nil) && (t.node.id == self.id) } end