docker-swarm-sdk 1.2.6 → 1.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '0788875137185f4cd9dc756a4f5472c88b6c095c'
4
- data.tar.gz: b8cf6d7b1ea314c346e27481abc87de47839ba74
3
+ metadata.gz: 80ce05c3772a87ef6d5ac0535192fa4d4fc9eaab
4
+ data.tar.gz: fb89cd6b07981d2f8d949bfcc8dc2e1be051528b
5
5
  SHA512:
6
- metadata.gz: 7f036700557339985b766a4f75fff68ed0133efaf89329d1dddaf4f7c13e5739a08cfba2e57e8f9b91b225047c2733cbb111c8029a8f330f0980190b83263abb
7
- data.tar.gz: 7e618fffef5ed3b5b966274732524e8da14a423dc7ac1670976f5fb63e93f6875b25b7a59c898d39b6713e26151873618962b0c7cf6a6d65c7d5f150613fa94e
6
+ metadata.gz: 47f1523152677724924eda382cb0451bfa83a347845d612654ec00ea22e33836344087de47bac60ea43f23d787c0a9b88a13790fc3ae0c720434713256eba62e
7
+ data.tar.gz: fc1c34a42182692424913556f99ff0d7cca3328eede7b201ed731daed9fc3f1d3e589cba7ad6976993a0a2adec0b61bef01f708528e368e31606739c24b5a75f
data/README.md CHANGED
@@ -19,6 +19,9 @@ Sample Usage
19
19
  # Make a connection to the Swarm manager's API. (Assumes port 2375 exposed for API)
20
20
  master_connection = Docker::Swarm::Connection.new('http://10.20.30.1:2375')
21
21
 
22
+ # If swarm on the swarm master and using socket:
23
+ master_connection = Docker::Swarm::Connection.new('unix:///var/run/docker.sock')
24
+
22
25
  # Manager node intializes swarm
23
26
  swarm_init_options = { "ListenAddr" => "0.0.0.0:2377" }
24
27
  swarm = Docker::Swarm::Swarm.init(swarm_init_options, master_connection)
@@ -90,7 +93,7 @@ tasks = swarm.tasks()
90
93
 
91
94
  # Scale up or down the number of replicas on a service
92
95
  service.scale(20)
93
-
96
+
94
97
  # Worker leaves the swarm - no forcing
95
98
  swarm.leave(worker_node, node)
96
99
 
@@ -1,14 +1,14 @@
1
1
  require 'docker-api'
2
+ require 'active_support'
2
3
 
3
4
  class Docker::Swarm::Service
4
- #include Docker::Base
5
5
  attr_reader :hash
6
6
 
7
7
  def initialize(swarm, hash)
8
8
  @swarm = swarm
9
9
  @hash = hash
10
10
  end
11
-
11
+
12
12
  def name()
13
13
  @hash['Spec']['Name']
14
14
  end
@@ -16,13 +16,13 @@ class Docker::Swarm::Service
16
16
  def id()
17
17
  return @hash['ID']
18
18
  end
19
-
19
+
20
20
  def reload()
21
21
  s = @swarm.find_service(id())
22
22
  @hash = s.hash
23
23
  return self
24
24
  end
25
-
25
+
26
26
  def network_ids
27
27
  network_ids = []
28
28
  if (@hash['Endpoint']['VirtualIPs'])
@@ -32,16 +32,23 @@ class Docker::Swarm::Service
32
32
  end
33
33
  return network_ids
34
34
  end
35
-
35
+
36
36
  def remove(opts = {})
37
37
  query = {}
38
38
  @swarm.connection.delete("/services/#{self.id}", query, :body => opts.to_json)
39
39
  end
40
-
41
- def update(opts)
40
+
41
+ def update(options = {})
42
+ specs = @hash['Spec'].deep_merge(options)
42
43
  query = {}
43
44
  version = @hash['Version']['Index']
44
- response = @swarm.connection.post("/services/#{self.id}/update?version=#{version}", query, :body => opts.to_json)
45
+ response = @swarm.connection.post("/services/#{self.id}/update?version=#{version}", query, :body => specs.to_json)
46
+ end
47
+
48
+ def restart
49
+ options = {}
50
+ options['TaskTemplate'] = {'ForceUpdate' => 1}
51
+ update(options)
45
52
  end
46
53
 
47
54
  def scale(count)
@@ -49,6 +56,10 @@ class Docker::Swarm::Service
49
56
  self.update(@hash['Spec'])
50
57
  end
51
58
 
59
+ def replicas
60
+ @hash['Spec']['Mode']['Replicated']['Replicas']
61
+ end
62
+
52
63
  def self.DEFAULT_OPTIONS
53
64
  default_service_create_options = {
54
65
  "Name" => "<<Required>>",
@@ -73,7 +84,7 @@ class Docker::Swarm::Service
73
84
  },
74
85
  "Reservations" => {
75
86
  # "NanoCPUs" => ?
76
- # MemoryBytes =>
87
+ # MemoryBytes =>
77
88
  }
78
89
  },
79
90
  "RestartPolicy" => {
@@ -107,6 +118,4 @@ class Docker::Swarm::Service
107
118
  }
108
119
  return default_service_create_options
109
120
  end
110
-
111
-
112
- end
121
+ end
@@ -8,8 +8,8 @@ class Docker::Swarm::Swarm
8
8
  attr_reader :node_ip, :manager_ip, :worker_join_token, :manager_join_token, :id, :hash, :node_hash
9
9
 
10
10
  def store_manager(manager_connection, listen_address_and_port)
11
- node = nodes.find {|n|
12
- (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true) && (n.hash['ManagerStatus']['Addr'] == listen_address_and_port)
11
+ node = nodes.find {|n|
12
+ (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true) && (n.hash['ManagerStatus']['Addr'] == listen_address_and_port)
13
13
  }
14
14
  raise "Node not found for: #{listen_address}" if (!node)
15
15
  @node_hash[node.id] = {hash: node.hash, connection: manager_connection}
@@ -18,7 +18,7 @@ class Docker::Swarm::Swarm
18
18
  def update_data(hash)
19
19
  @hash = hash
20
20
  end
21
-
21
+
22
22
  def socket_connection(node_connection)
23
23
  node_connection.url.include?('unix:///')
24
24
  end
@@ -32,7 +32,7 @@ class Docker::Swarm::Swarm
32
32
  node_ip = node_connection.url.split("//").last.split(":").first
33
33
  manager_ip = self.connection.url.split("//").last.split(":").first
34
34
  end
35
-
35
+
36
36
  join_options = {
37
37
  "ListenAddr" => "#{listen_address}",
38
38
  "AdvertiseAddr" => "#{node_ip}:2377",
@@ -62,11 +62,11 @@ class Docker::Swarm::Swarm
62
62
  def join_worker(node_connection, listen_address = "0.0.0.0:2377")
63
63
  join(node_connection, @node_ip, @manager_ip, @worker_join_token)
64
64
  end
65
-
65
+
66
66
  def join_manager(node_connection, listen_address = "0.0.0.0:2377")
67
67
  join(node_connection, @node_ip, @manager_ip, @manager_join_token, listen_address)
68
68
  end
69
-
69
+
70
70
  def connection
71
71
  @node_hash.keys.each do |node_id|
72
72
  node_info = @node_hash[node_id]
@@ -81,7 +81,7 @@ class Docker::Swarm::Swarm
81
81
  services().each do |service|
82
82
  service.remove()
83
83
  end
84
-
84
+
85
85
  worker_nodes.each do |node|
86
86
  leave(node, true)
87
87
  end
@@ -89,7 +89,7 @@ class Docker::Swarm::Swarm
89
89
  leave(node, true)
90
90
  end
91
91
  end
92
-
92
+
93
93
  def tasks
94
94
  items = []
95
95
  query = {}
@@ -109,11 +109,11 @@ class Docker::Swarm::Swarm
109
109
  Docker::Swarm::Swarm.leave(force, node_info[:connection])
110
110
  end
111
111
  end
112
-
112
+
113
113
  def remove_node(worker_node)
114
114
  Swarm::Node.remove(worker_node.id, self.connection)
115
115
  end
116
-
116
+
117
117
  def manager_nodes
118
118
  return nodes.select { |node| node.role == :manager} || []
119
119
  end
@@ -121,7 +121,7 @@ class Docker::Swarm::Swarm
121
121
  def worker_nodes
122
122
  return nodes.select { |node| node.role == :worker} || []
123
123
  end
124
-
124
+
125
125
  def networks
126
126
  all_networks = []
127
127
  response = connection.get("/networks", {}, full_response: true)
@@ -135,7 +135,7 @@ class Docker::Swarm::Swarm
135
135
  end
136
136
  return all_networks
137
137
  end
138
-
138
+
139
139
  def create_network(options)
140
140
  response = connection.post('/networks/create', {}, body: options.to_json, expects: [200, 201, 500], full_response: true)
141
141
  if (response.status <= 201)
@@ -148,11 +148,11 @@ class Docker::Swarm::Swarm
148
148
  raise "Error creating network: HTTP-#{response.status} - #{response.body}"
149
149
  end
150
150
  end
151
-
151
+
152
152
  def create_network_overlay(network_name)
153
153
  subnet_16_parts = [10, 10, 0, 0]
154
154
  max_vxlanid = 200
155
-
155
+
156
156
  # Sometimes nodes have leftover networks not on other nodes, that have subnets that can't be duplicated in
157
157
  # the new overlay network.
158
158
  nodes.each do |node|
@@ -165,7 +165,7 @@ class Docker::Swarm::Swarm
165
165
  end
166
166
  end
167
167
  end
168
-
168
+
169
169
  # Make sure our new network doesn't duplicate subnet of other network.
170
170
  if (network.hash['IPAM']) && (network.hash['IPAM']['Config'])
171
171
  network.hash['IPAM']['Config'].each do |subnet_config|
@@ -189,7 +189,7 @@ class Docker::Swarm::Swarm
189
189
  end
190
190
  end
191
191
  end
192
-
192
+
193
193
 
194
194
  options = {
195
195
  "Name" => network_name,
@@ -218,7 +218,7 @@ class Docker::Swarm::Swarm
218
218
  }
219
219
  create_network(options)
220
220
  end
221
-
221
+
222
222
  # Return all of the Nodes.
223
223
  def nodes
224
224
  opts = {}
@@ -249,7 +249,7 @@ class Docker::Swarm::Swarm
249
249
  end
250
250
  return nil
251
251
  end
252
-
252
+
253
253
  def find_service(id)
254
254
  query = {}
255
255
  opts = {}
@@ -257,14 +257,14 @@ class Docker::Swarm::Swarm
257
257
  hash = JSON.parse(response)
258
258
  return Docker::Swarm::Service.new(self, hash)
259
259
  end
260
-
260
+
261
261
  def find_service_by_name(name)
262
262
  services.each do |service|
263
263
  return service if (service.name == name)
264
264
  end
265
265
  return nil
266
266
  end
267
-
267
+
268
268
  def services
269
269
  items = []
270
270
  query = {}
@@ -276,17 +276,18 @@ class Docker::Swarm::Swarm
276
276
  end
277
277
  return items
278
278
  end
279
-
280
-
279
+
280
+
281
281
  # Initialize Swarm
282
282
  def self.init(opts, connection)
283
283
  query = {}
284
284
  resp = connection.post('/swarm/init', query, :body => opts.to_json, full_response: true, expects: [200, 404, 406, 500])
285
285
  if (resp.status == 200)
286
286
  swarm = Docker::Swarm::Swarm.swarm(opts, connection)
287
- manager_node = swarm.nodes.find {|n|
288
- (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true)
287
+ manager_node = swarm.nodes.find {|n|
288
+ (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true)
289
289
  }
290
+ byebug
290
291
  listen_address = manager_node.hash['ManagerStatus']['Addr']
291
292
  swarm.store_manager(connection, listen_address)
292
293
  return swarm
@@ -296,14 +297,14 @@ class Docker::Swarm::Swarm
296
297
  end
297
298
 
298
299
  # docker swarm join-token -q worker
299
- def self.swarm(options, connection)
300
+ def self.swarm(connection, options = {})
300
301
  query = {}
301
302
  resp = connection.get('/swarm', query, :body => options.to_json, expects: [200, 404, 406], full_response: true)
302
303
  if (resp.status == 406) || (resp.status == 404)
303
304
  return nil
304
305
  elsif (resp.status == 200)
305
306
  hash = JSON.parse(resp.body)
306
- swarm = self.find_swarm_for_id(hash['ID'])
307
+ swarm = self.find_swarm_for_id(hash['ID'])
307
308
  if (swarm)
308
309
  swarm.update_data(hash)
309
310
  else
@@ -313,7 +314,7 @@ class Docker::Swarm::Swarm
313
314
  raise "Bad response: #{resp.status} #{resp.body}"
314
315
  end
315
316
  end
316
-
317
+
317
318
  def self.leave(force, connection)
318
319
  query = {}
319
320
  query['force'] = force
@@ -322,39 +323,39 @@ class Docker::Swarm::Swarm
322
323
  raise "Error leaving: #{response.body} HTTP-#{response.status}"
323
324
  end
324
325
  end
325
-
326
+
326
327
  def self.find(connection, options = {})
327
328
  query = {}
328
329
  response = connection.get('/swarm', query, expects: [200, 404, 406], full_response: true)
329
330
  if (response.status == 200)
330
331
  hash = JSON.parse(response.body)
331
- swarm = self.find_swarm_for_id(hash['ID'])
332
+ swarm = self.find_swarm_for_id(hash['ID'])
332
333
  if (swarm)
333
334
  swarm.update_data(hash)
334
335
  else
335
336
  swarm = Docker::Swarm::Swarm.new(hash, connection, options)
336
337
  end
337
- manager_node = swarm.nodes.find {|n|
338
- (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true)
338
+ manager_node = swarm.nodes.find {|n|
339
+ (n.hash['ManagerStatus']) && (n.hash['ManagerStatus']['Leader'] == true)
339
340
  }
340
341
  listen_address = manager_node.hash['ManagerStatus']['Addr']
341
342
  swarm.store_manager(connection, listen_address)
342
343
  return swarm
343
344
  elsif (response.status > 200)
344
345
  return nil
345
- else
346
+ else
346
347
  raise "Error finding swarm: HTTP-#{response.status} #{response.body}"
347
348
  end
348
349
  end
349
-
350
-
350
+
351
+
351
352
  private
352
353
  @@swarms = {}
353
-
354
+
354
355
  def self.find_swarm_for_id(swarm_id)
355
356
  return @@swarms[swarm_id]
356
357
  end
357
-
358
+
358
359
  def initialize(hash, manager_connection = nil, options = {})
359
360
  @hash = hash
360
361
  @id = hash['ID']
@@ -366,8 +367,8 @@ class Docker::Swarm::Swarm
366
367
  @manager_connection = manager_connection
367
368
  @@swarms[@id] = self
368
369
  end
369
-
370
-
371
-
370
+
371
+
372
+
372
373
 
373
374
  end
@@ -7,19 +7,19 @@ class Docker::Swarm::Task
7
7
  @hash = hash
8
8
  @swarm = swarm
9
9
  end
10
-
11
- def id
10
+
11
+ def id
12
12
  return @hash['ID']
13
13
  end
14
-
14
+
15
15
  def image
16
16
  return @hash['Spec']['ContainerSpec']['Image']
17
17
  end
18
-
18
+
19
19
  def service_id
20
20
  @hash['ServiceID']
21
21
  end
22
-
22
+
23
23
  def service
24
24
  return @swarm.services.find { |service|
25
25
  self.service_id == service.id
@@ -29,19 +29,27 @@ class Docker::Swarm::Task
29
29
  def node_id
30
30
  @hash['NodeID']
31
31
  end
32
-
32
+
33
33
  def node
34
34
  return @swarm.nodes.find {|n| n.id == self.node_id}
35
35
  end
36
-
36
+
37
37
  def created_at
38
38
  return DateTime.parse(@hash.first['CreatedAt'])
39
39
  end
40
-
40
+
41
41
  def status
42
42
  @hash['Status']['State'].to_sym
43
43
  end
44
44
 
45
+ def status_timestamp
46
+ return DateTime.parse(@hash['Status']['Timestamp'])
47
+ end
48
+
49
+ def status_message
50
+ @hash['Status']['Message']
51
+ end
52
+
45
53
  def networks
46
54
  all_networks = @swarm.networks
47
55
  nets = []
@@ -1,7 +1,7 @@
1
1
  module Docker
2
2
  module Swarm
3
- # The version of the docker-api gem.
4
- VERSION = '1.2.6'
3
+ # The version of this gem.
4
+ VERSION = '1.2.7'
5
5
 
6
6
  # The version of the compatible Docker remote API.
7
7
  API_VERSION = '1.24'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker-swarm-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.6
4
+ version: 1.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-30 00:00:00.000000000 Z
11
+ date: 2017-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '6.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '6.0'
55
83
  - !ruby/object:Gem::Dependency
56
84
  name: rake
57
85
  requirement: !ruby/object:Gem::Requirement
@@ -177,5 +205,5 @@ rubyforge_project:
177
205
  rubygems_version: 2.6.12
178
206
  signing_key:
179
207
  specification_version: 4
180
- summary: Ruby SDK for Docker Swarm API
208
+ summary: Ruby API for Docker Swarm
181
209
  test_files: []