containers_manager 0.1.4 → 0.1.5

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: 073b9e64935b5f85fa04ab0228b7091343b6670f
4
- data.tar.gz: 4bfef9d4c8358fc06ba293bfcf3594251ca7f3d0
3
+ metadata.gz: b259eb3e333b8880c703caa1d8c65b16d48f78d9
4
+ data.tar.gz: 782fbcc00dcd57da85b5a065b9af2837a1841f1b
5
5
  SHA512:
6
- metadata.gz: 60fea90782846e496751274e5a371aaef3d2e0c36e6fb7e52c388508e8bc52d215adcd8b07c14630706236106916e95c4dc2f932c5f6eb95dae00b887cc305b3
7
- data.tar.gz: 21a52b9191155ceb4e2ab288827241141368f448983c3a922da150a044b65a97a12d5db48a6b187648464bd983d258f4a78e57ff3ba386ac36fb85aafd1d31bf
6
+ metadata.gz: 1c2c123ce217dac7d09990786248eb8ae00aceda88d704a55198cf484f5fd4adc217cbc01986d858c405c3cee1dfc57714a8cfa74c1d431deb7066e1c2755d7c
7
+ data.tar.gz: f96f4b941c000e46c94bada707db16e00527b8cc797e09dfabd07936ebf3990ef0843d82d0e80d73fac59c013e963549f5e9370a930c6e7e13cf9d76e243d9a5
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ tags
@@ -1,7 +1,22 @@
1
1
  module ContainersManager
2
2
  class Application
3
+ CONFIG_FILE = '/etc/dmanage/config.yml'
4
+
3
5
  def self.exec
4
- $stdout.puts("Hello world")
6
+ load_configuration
7
+ tasks.each(&:exec)
8
+ end
9
+
10
+ private
11
+
12
+ def self.load_configuration
13
+ Configuration.load(CONFIG_FILE)
14
+ end
15
+
16
+ def self.tasks
17
+ [
18
+ DeregisterTask
19
+ ]
5
20
  end
6
21
  end
7
22
  end
@@ -6,7 +6,22 @@ module ContainersManager
6
6
 
7
7
  desc 'exec', 'Execute jobs to manage containers'
8
8
  def exec
9
- Application.exec
9
+ exception_rescue do
10
+ Application.exec
11
+ end
12
+ end
13
+
14
+ desc 'check', 'Output the configuration'
15
+ def exec
16
+ exception_rescue do
17
+ Application.check
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def exception_rescue
24
+ yield
10
25
  rescue Exception => e
11
26
  $stdout.puts(e.message)
12
27
  end
@@ -0,0 +1,20 @@
1
+ require 'yaml'
2
+
3
+ module ContainersManager
4
+ class Configuration
5
+ def self.load(file_path)
6
+ raise "#{file_path} cannot be found" unless File.exists?(file_path)
7
+
8
+ @file_path = file_path
9
+ @data = YAML.load_file(file_path)
10
+ end
11
+
12
+ private
13
+
14
+ def self.method_missing(method_name, *args, &block)
15
+ value = @data[method_name.to_s]
16
+ raise "Attribute '#{method_name}' cannot be found in the #{@file_path}" unless value
17
+ value
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+ module ContainersManager
2
+ class Consul
3
+ def initialize(configuration, id_provider)
4
+ @configuration = configuration
5
+ @id_provider = id_provider
6
+ end
7
+
8
+ def all_entries
9
+ @all_entries ||= response_from(consul_service_urls)
10
+ .map { |node| ConsulEntry.new(node, configuration).set_id_provider(id_provider) }
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :configuration, :id_provider
16
+
17
+ def consul_service_urls
18
+ @configuration.consul_service_urls
19
+ end
20
+
21
+ def response_from(param)
22
+ if param.is_a?(String)
23
+ consul_service.services(param)
24
+ elsif param.is_a?(Array)
25
+ param.map { |e| response_from(e) }.flatten
26
+ end
27
+ end
28
+
29
+ def consul_service
30
+ @consul_service ||= ConsulService.new(configuration)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ module ContainersManager
2
+ class ConsulEntry
3
+ def initialize(data, configuration)
4
+ @data = data
5
+ @configuration = configuration
6
+ end
7
+
8
+ def set_id_provider(id_provider)
9
+ @id_provider = id_provider
10
+ end
11
+
12
+ def deregister_if_possible
13
+ deregister if can_deregister?
14
+ end
15
+
16
+ private
17
+
18
+ attr_reader :data, :configuration, :id_provider
19
+
20
+ def deregister
21
+ ConsulService.new(configuration).deregister(service_id)
22
+ end
23
+
24
+ def can_deregister?
25
+ id_provider.alive_ids.find { |id| service_id.include?(id) }
26
+ end
27
+
28
+ def service_id
29
+ @service_id ||= data['ServiceID']
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ module ContainersManager
2
+ class Docker
3
+ def initialize(configuration)
4
+ @configuration = configuration
5
+ end
6
+
7
+ def alive_ids
8
+ docker_service.alive_ids
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :configuration
14
+
15
+ def docker_service
16
+ @docker_service ||= DockerService.new(configuration)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ require 'net/http'
2
+
3
+ module ContainersManager
4
+ class ConsulService
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ end
8
+
9
+ def services(url)
10
+ uri = URI(url)
11
+ JSON.parse(Net::HTTP.get(uri))
12
+ end
13
+
14
+ def deregister(id)
15
+ system("curl -XPUT #{@configuration.consul_deregister_url}/#{id}")
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :configuration
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module ContainersManager
2
+ class DockerService
3
+ ALIVE_CONTAINER_ID_CMD = 'docker ps -q'
4
+
5
+ def initialize(configuration)
6
+ @configuration = configuration
7
+ end
8
+
9
+ def alive_ids
10
+ system_call(ALIVE_CONTAINER_ID_CMD).split("\n")
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :configuration
16
+
17
+ def system_call(cmd)
18
+ `DOCKER_HOST=:#{configuration.docker_host} #{cmd}`
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ module ContainersManager
2
+ class DeregisterTask < Task
3
+ def self.exec
4
+ deregister
5
+ end
6
+
7
+ private
8
+
9
+ def self.deregister
10
+ consul.all_entries.each(&:deregister_if_possible)
11
+ end
12
+
13
+ def self.consul
14
+ @consul ||= Consul.new(
15
+ configuration,
16
+ id_provider
17
+ )
18
+ end
19
+
20
+ def self.configuration
21
+ @configuration ||= Configuration.instance
22
+ end
23
+
24
+ def self.id_provider
25
+ @id_provider ||= Docker.new(configuration)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module ContainersManager
2
+ class Task
3
+ def self.exec
4
+ raise 'It should be overriden'
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module ContainersManager
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -1,7 +1,20 @@
1
- require "containers_manager/version"
2
- require "containers_manager/application"
3
- require "containers_manager/cli"
1
+ require 'containers_manager/version'
2
+
3
+ require 'containers_manager/configuration'
4
+
5
+ require 'containers_manager/services/consul_service'
6
+ require 'containers_manager/services/docker_service'
7
+
8
+ require 'containers_manager/entities/docker'
9
+ require 'containers_manager/entities/consul_entry'
10
+ require 'containers_manager/entities/consul'
11
+
12
+ require 'containers_manager/tasks/task'
13
+ require 'containers_manager/tasks/deregister_task'
14
+
15
+ require 'containers_manager/application'
16
+
17
+ require 'containers_manager/cli'
4
18
 
5
19
  module ContainersManager
6
- # Your code goes here...
7
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: containers_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Duc Le
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-25 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,6 +89,14 @@ files:
89
89
  - lib/containers_manager.rb
90
90
  - lib/containers_manager/application.rb
91
91
  - lib/containers_manager/cli.rb
92
+ - lib/containers_manager/configuration.rb
93
+ - lib/containers_manager/entities/consul.rb
94
+ - lib/containers_manager/entities/consul_entry.rb
95
+ - lib/containers_manager/entities/docker.rb
96
+ - lib/containers_manager/services/consul_service.rb
97
+ - lib/containers_manager/services/docker_service.rb
98
+ - lib/containers_manager/tasks/deregister_task.rb
99
+ - lib/containers_manager/tasks/task.rb
92
100
  - lib/containers_manager/version.rb
93
101
  homepage: https://github.com/lmduc/containers_manager
94
102
  licenses: