Class: Docker::Swarm::Swarm
- Inherits:
-
Object
- Object
- Docker::Swarm::Swarm
- Includes:
- Docker
- Defined in:
- lib/docker/swarm/swarm.rb
Overview
This class represents a Docker Swarm Node.
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#manager_ip ⇒ Object
readonly
Returns the value of attribute manager_ip.
-
#manager_join_token ⇒ Object
readonly
Returns the value of attribute manager_join_token.
-
#node_hash ⇒ Object
readonly
Returns the value of attribute node_hash.
-
#node_ip ⇒ Object
readonly
Returns the value of attribute node_ip.
-
#worker_join_token ⇒ Object
readonly
Returns the value of attribute worker_join_token.
Class Method Summary collapse
- .find(connection, options = {}) ⇒ Object
-
.init(opts, connection) ⇒ Object
Initialize Swarm.
- .leave(force, connection) ⇒ Object
-
.swarm(connection, options = {}) ⇒ Object
docker swarm join-token -q worker.
Instance Method Summary collapse
- #connection ⇒ Object
- #create_network(options) ⇒ Object
- #create_network_overlay(network_name) ⇒ Object
- #create_service(opts = {}) ⇒ Object
- #find_service(id) ⇒ Object
- #find_service_by_name(name) ⇒ Object
- #join(node_connection, node_ip = nil, manager_ip = nil, join_token = nil, listen_address = "0.0.0.0:2377") ⇒ Object
- #join_manager(node_connection, listen_address = "0.0.0.0:2377") ⇒ Object
- #join_worker(node_connection, listen_address = "0.0.0.0:2377") ⇒ Object
- #leave(node, force = false) ⇒ Object
- #manager_nodes ⇒ Object
- #networks ⇒ Object
-
#nodes ⇒ Object
Return all of the Nodes.
- #remove ⇒ Object
- #remove_node(worker_node) ⇒ Object
- #services ⇒ Object
- #socket_connection(node_connection) ⇒ Object
- #store_manager(manager_connection, listen_address_and_port) ⇒ Object
- #tasks ⇒ Object
- #update_data(hash) ⇒ Object
- #worker_nodes ⇒ Object
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def hash @hash end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def id @id end |
#manager_ip ⇒ Object (readonly)
Returns the value of attribute manager_ip.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def manager_ip @manager_ip end |
#manager_join_token ⇒ Object (readonly)
Returns the value of attribute manager_join_token.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def manager_join_token @manager_join_token end |
#node_hash ⇒ Object (readonly)
Returns the value of attribute node_hash.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def node_hash @node_hash end |
#node_ip ⇒ Object (readonly)
Returns the value of attribute node_ip.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def node_ip @node_ip end |
#worker_join_token ⇒ Object (readonly)
Returns the value of attribute worker_join_token.
8 9 10 |
# File 'lib/docker/swarm/swarm.rb', line 8 def worker_join_token @worker_join_token end |
Class Method Details
.find(connection, options = {}) ⇒ Object
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/docker/swarm/swarm.rb', line 329 def self.find(connection, = {}) query = {} response = connection.get('/swarm', query, expects: [200, 404, 406], full_response: true) if (response.status == 200) hash = JSON.parse(response.body) swarm = self.find_swarm_for_id(hash['ID']) if (swarm) swarm.update_data(hash) else swarm = Docker::Swarm::Swarm.new(hash, connection, ) end manager_node = swarm.nodes.find {|n| (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true) } listen_address = manager_node.hash['ManagerStatus']['Addr'] swarm.store_manager(connection, listen_address) return swarm elsif (response.status > 200) return nil else raise "Error finding swarm: HTTP-#{response.status} #{response.body}" end end |
.init(opts, connection) ⇒ Object
Initialize Swarm
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/docker/swarm/swarm.rb', line 282 def self.init(opts, connection) query = {} resp = connection.post('/swarm/init', query, :body => opts.to_json, full_response: true, expects: [200, 404, 406, 500]) if (resp.status == 200) swarm = Docker::Swarm::Swarm.swarm(opts, connection) manager_node = swarm.nodes.find {|n| (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true) } byebug listen_address = manager_node.hash['ManagerStatus']['Addr'] swarm.store_manager(connection, listen_address) return swarm else raise "Bad response: #{resp.status} #{resp.body}" end end |
.leave(force, connection) ⇒ Object
320 321 322 323 324 325 326 327 |
# File 'lib/docker/swarm/swarm.rb', line 320 def self.leave(force, connection) query = {} query['force'] = force response = connection.post('/swarm/leave', query, expects: [200, 406, 500], full_response: true) if (response.status == 500) raise "Error leaving: #{response.body} HTTP-#{response.status}" end end |
.swarm(connection, options = {}) ⇒ Object
docker swarm join-token -q worker
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/docker/swarm/swarm.rb', line 300 def self.swarm(connection, = {}) swarm = nil query = {} resp = connection.get('/swarm', query, :body => .to_json, expects: [200, 404, 406], full_response: true) if (resp.status == 406) || (resp.status == 404) return nil elsif (resp.status == 200) hash = JSON.parse(resp.body) swarm = self.find_swarm_for_id(hash['ID']) if (swarm) swarm.update_data(hash) else swarm = Docker::Swarm::Swarm.new(hash, connection, ) end else raise "Bad response: #{resp.status} #{resp.body}" end return swarm end |
Instance Method Details
#connection ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/docker/swarm/swarm.rb', line 70 def connection @node_hash.keys.each do |node_id| node_info = @node_hash[node_id] if (node_info[:hash]['ManagerStatus']) return node_info[:connection] end end return @manager_connection end |
#create_network(options) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/docker/swarm/swarm.rb', line 139 def create_network() response = connection.post('/networks/create', {}, body: .to_json, expects: [200, 201, 500], full_response: true) if (response.status <= 201) hash = JSON.parse(response.body) response = connection.get("/networks/#{hash['Id']}", {}, expects: [200, 201], full_response: true) hash = Docker::Util.parse_json(response.body) network = Docker::Swarm::Network.new(self, hash) return network else raise "Error creating network: HTTP-#{response.status} - #{response.body}" end end |
#create_network_overlay(network_name) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/docker/swarm/swarm.rb', line 152 def (network_name) subnet_16_parts = [10, 10, 0, 0] max_vxlanid = 200 # Sometimes nodes have leftover networks not on other nodes, that have subnets that can't be duplicated in # the new overlay network. nodes.each do |node| node.networks.each do |network| if (network.driver == 'overlay') if (network.hash['Options']) vxlanid = network.hash['Options']["com.docker.network.driver.overlay.vxlanid_list"] if (vxlanid) && (vxlanid.to_i > max_vxlanid) max_vxlanid = vxlanid.to_i end end end # Make sure our new network doesn't duplicate subnet of other network. if (network.hash['IPAM']) && (network.hash['IPAM']['Config']) network.hash['IPAM']['Config'].each do |subnet_config| if (subnet_config['Subnet']) subnet = subnet_config['Subnet'] subnet = subnet.split(".") if (subnet[0] == '10') && (subnet[1] == '255') else if (subnet[0].to_i == subnet_16_parts[0]) if (subnet[1].to_i >= subnet_16_parts[1]) subnet_16_parts[1] = subnet[1].to_i + 1 if (subnet_16_parts[1] >= 255) raise "Ran out of subnets" end end end end # subnet_config['Gateway'] end end end end end = { "Name" => network_name, "CheckDuplicate" => true, "Driver" => "overlay", "EnableIPv6" => false, "IPAM" => { "Driver" => "default", "Config" => [ { "Subnet" => "#{subnet_16_parts.join(".")}/16", "Gateway"=> "#{subnet_16_parts[0, 3].join('.')}.1" } ], "Options" => { } }, "Internal" => false, "Options" => { "com.docker.network.driver.overlay.vxlanid_list" => (max_vxlanid + 1).to_s }, "Labels" => { # "com.example.some-label": "some-value", # "com.example.some-other-label": "some-other-value" } } create_network() end |
#create_service(opts = {}) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/docker/swarm/swarm.rb', line 240 def create_service(opts = {}) query = {} response = self.connection.post('/services/create', query, :body => opts.to_json, expects: [201, 404, 409, 500], full_response: true) if (response.status <= 201) info = JSON.parse(response.body) service_id = info['ID'] return self.find_service(service_id) else raise "Error creating service: HTTP-#{response.status} #{response.body}" end return nil end |
#find_service(id) ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/docker/swarm/swarm.rb', line 253 def find_service(id) query = {} opts = {} response = self.connection.get("/services/#{id}", query, :body => opts.to_json) hash = JSON.parse(response) return Docker::Swarm::Service.new(self, hash) end |
#find_service_by_name(name) ⇒ Object
261 262 263 264 265 266 |
# File 'lib/docker/swarm/swarm.rb', line 261 def find_service_by_name(name) services.each do |service| return service if (service.name == name) end return nil end |
#join(node_connection, node_ip = nil, manager_ip = nil, join_token = nil, listen_address = "0.0.0.0:2377") ⇒ Object
26 27 28 29 30 31 32 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 |
# File 'lib/docker/swarm/swarm.rb', line 26 def join(node_connection, node_ip = nil, manager_ip = nil, join_token = nil, listen_address = "0.0.0.0:2377") node_ids_before = [] query = {} unless socket_connection(node_connection) node_ids_before = nodes().collect {|n| n.id} node_ip = node_connection.url.split("//").last.split(":").first manager_ip = self.connection.url.split("//").last.split(":").first end = { "ListenAddr" => "#{listen_address}", "AdvertiseAddr" => "#{node_ip}:2377", "RemoteAddrs" => ["#{manager_ip}:2377"], "JoinToken" => join_token } new_node = nil response = node_connection.post('/swarm/join', query, :body => .to_json, expects: [200, 406, 500], full_response: true) if (response.status == 200) nodes.each do |node| if (!node_ids_before.include? node.id) new_node = node @node_hash[node.id] = {hash: node.hash, connection: node_connection} end end unless socket_connection(node_connection) return new_node elsif (response.status == 406) puts "Node is already part of a swarm - maybe this swarm, maybe another swarm." return nil else raise "Error joining (#{node_connection}): HTTP-#{response.status} #{response.body}" end end |
#join_manager(node_connection, listen_address = "0.0.0.0:2377") ⇒ Object
66 67 68 |
# File 'lib/docker/swarm/swarm.rb', line 66 def join_manager(node_connection, listen_address = "0.0.0.0:2377") join(node_connection, @node_ip, @manager_ip, @manager_join_token, listen_address) end |
#join_worker(node_connection, listen_address = "0.0.0.0:2377") ⇒ Object
62 63 64 |
# File 'lib/docker/swarm/swarm.rb', line 62 def join_worker(node_connection, listen_address = "0.0.0.0:2377") join(node_connection, @node_ip, @manager_ip, @worker_join_token) end |
#leave(node, force = false) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/docker/swarm/swarm.rb', line 106 def leave(node, force = false) node_info = @node_hash[node.id] if (node_info) Docker::Swarm::Swarm.leave(force, node_info[:connection]) end end |
#manager_nodes ⇒ Object
117 118 119 |
# File 'lib/docker/swarm/swarm.rb', line 117 def manager_nodes return nodes.select { |node| node.role == :manager} || [] end |
#networks ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/docker/swarm/swarm.rb', line 125 def networks all_networks = [] response = connection.get("/networks", {}, full_response: true) if (response.status == 200) hashes = JSON.parse(response.body) hashes.each do |hash| all_networks << Docker::Swarm::Network.new(self, hash) end else raise "Error finding netw" end return all_networks end |
#nodes ⇒ Object
Return all of the Nodes.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/docker/swarm/swarm.rb', line 223 def nodes opts = {} query = {} response = self.connection.get('/nodes', query, :body => opts.to_json, expects: [200, 406], full_response: true) if (response.status == 200) hashes = JSON.parse(response.body) nodes = [] hashes.each do |node_hash| node = Docker::Swarm::Node.new(self, node_hash) nodes << node end return nodes || [] else return [] end end |
#remove ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/docker/swarm/swarm.rb', line 80 def remove services().each do |service| service.remove() end worker_nodes.each do |node| leave(node, true) end manager_nodes.each do |node| leave(node, true) end end |
#remove_node(worker_node) ⇒ Object
113 114 115 |
# File 'lib/docker/swarm/swarm.rb', line 113 def remove_node(worker_node) Swarm::Node.remove(worker_node.id, self.connection) end |
#services ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/docker/swarm/swarm.rb', line 268 def services items = [] query = {} opts = {} response = self.connection.get("/services", query, :body => opts.to_json) hashes = JSON.parse(response) hashes.each do |hash| items << Docker::Swarm::Service.new(self, hash) end return items end |
#socket_connection(node_connection) ⇒ Object
22 23 24 |
# File 'lib/docker/swarm/swarm.rb', line 22 def socket_connection(node_connection) node_connection.url.include?('unix:///') end |
#store_manager(manager_connection, listen_address_and_port) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/docker/swarm/swarm.rb', line 10 def store_manager(manager_connection, listen_address_and_port) node = nodes.find {|n| (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true) && (n.hash['ManagerStatus']['Addr'] == listen_address_and_port) } raise "Node not found for: #{listen_address}" if (!node) @node_hash[node.id] = {hash: node.hash, connection: manager_connection} end |
#tasks ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/docker/swarm/swarm.rb', line 93 def tasks items = [] query = {} opts = {} resp = self.connection.get('/tasks', query, :body => opts.to_json) hashes = JSON.parse(resp) items = [] hashes.each do |hash| items << Swarm::Task.new(self, hash) end return items end |
#update_data(hash) ⇒ Object
18 19 20 |
# File 'lib/docker/swarm/swarm.rb', line 18 def update_data(hash) @hash = hash end |
#worker_nodes ⇒ Object
121 122 123 |
# File 'lib/docker/swarm/swarm.rb', line 121 def worker_nodes return nodes.select { |node| node.role == :worker} || [] end |