docktor 0.1.0 → 0.2.0

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: 749c40ad9a74da59aaa3a52c9ab9848bf52a10bd
4
- data.tar.gz: 14bb492a1232e1ffd85c0eae9c99ae9e3f0e6b81
3
+ metadata.gz: 668089227b7edce1c61b2969309282930668aca8
4
+ data.tar.gz: 4cc904b8dddf40bd913e603fb870d0a677a2c139
5
5
  SHA512:
6
- metadata.gz: c03b957271b5b1388354a52fb786a40a5bc5396fd94fbaf9bf3ee14434e42bbe367521f42d5e6defb8fa56dc38cafba259fd8da0ece623e9b88bf6f47c6a83ba
7
- data.tar.gz: 429aa3be22d6ab039be4132fac9d8c9ab9038ef82248ee194ce940d59a9669c16bc90646041c8d90f47a6579148e21313316578a2516bd6ac5cda4f04479c481
6
+ metadata.gz: 3b0755e53477032149692af0a46b1d6c37f9c3638af0a72263e85c49fd85bffd075ce176b9414f7c978a7638d02ccaff40f37ec71e1c137f0ac8b03abf0d0393
7
+ data.tar.gz: c1d39ce8a1361f9e0070580ccff5c6b84f846cb2452005a3022fbe1510a45dd20dda4ec7431607827da18224cad8d1d33b825df4ec37fb564e40da6f505e40be
data/Gemfile CHANGED
@@ -3,4 +3,5 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in docktor.gemspec
4
4
  gemspec
5
5
 
6
+ gem 'activesupport'
6
7
  gem 'virtus'
data/bin/doc CHANGED
@@ -6,4 +6,8 @@ require "docktor"
6
6
 
7
7
  include Docktor
8
8
 
9
- Docktor::Runner.new.invoke
9
+ begin
10
+ Docktor::Runner.new.invoke container_name: ARGV[0]
11
+ rescue Docktor::Runner::ContainerNotFound => e
12
+ puts e.message
13
+ end
data/lib/docktor.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+
1
4
  require "docktor/version"
2
5
  require "docktor/runner"
3
6
 
@@ -2,9 +2,9 @@ require "virtus"
2
2
 
3
3
  module Docktor
4
4
  class Container
5
- include Virtus.value_object
5
+ include Virtus.model
6
6
 
7
- OPTION_ATTRIBUTES = %i(volumes ports links environment)
7
+ OPTION_ATTRIBUTES = %i(volumes ports links environment detach)
8
8
 
9
9
  values do
10
10
  attribute :name, String
@@ -14,10 +14,11 @@ module Docktor
14
14
  attribute :ports, Array[String]
15
15
  attribute :links, Array[String]
16
16
  attribute :environment, Hash[String, String]
17
+ attribute :detach, Boolean, default: false
17
18
  end
18
19
 
19
20
  def options
20
- attributes.select { |key, value| (key != :name || key != :image) && !value.nil? && !value.empty? }
21
+ attributes = self.attributes.select { |key, value| OPTION_ATTRIBUTES.include?(key) && !value.try(:empty?) }
21
22
  end
22
23
  end
23
24
  end
@@ -3,27 +3,29 @@ module Docktor
3
3
  CONTAINER_NAME_PREFIX = "docktor_"
4
4
 
5
5
  def container_exists?(container)
6
- `docker ps -aqf name=#{container_name(container.name)}` != ""
6
+ `docker ps --all --quiet --filter name=#{container_name(container.name)}`.present?
7
7
  end
8
8
 
9
9
  def run(container)
10
- puts "docker run -d --name #{container_name(container.name)} #{parse_options(container.options)} #{container.image} #{container.command}"
11
- `docker run -d --name #{container_name(container.name)} #{parse_options(container.options)} #{container.image} #{container.command}`
10
+ `docker run --name #{container_name(container.name)} #{parse_options(container.options)} #{container.image} #{container.command}`
12
11
  end
13
12
 
14
13
  def start(container)
15
14
  `docker start #{container_name(container.name)}`
16
15
  end
17
16
 
17
+ private
18
+
18
19
  def parse_options(options)
19
20
  options.map do |name, value|
20
21
  case name
21
- when :volumes then "-v #{value.join(" ")}"
22
- when :ports then "-p #{value.join(" ")}"
23
- when :links then "--link #{value.join(" ")}"
22
+ when :detach then "--detach=#{value}"
23
+ when :volumes then "--volume #{value.join(" ")}"
24
+ when :ports then "--publish #{value.join(" ")}"
25
+ when :links
24
26
  value.map { |v| "--link #{container_name(v)}" }.join(" ")
25
27
  when :environment
26
- "-e #{value.map { |k, v| %(#{k}="#{v}") }.join(" ")}"
28
+ "--env #{value.map { |k, v| %(#{k}="#{v}") }.join(" ")}"
27
29
  end
28
30
  end.join(" ").strip
29
31
  end
@@ -0,0 +1,54 @@
1
+ require "virtus"
2
+ require "docktor/container"
3
+
4
+ module Docktor
5
+ class Manifest
6
+ include Virtus.model
7
+
8
+ class ContainerNotFound < StandardError; end
9
+
10
+ values do
11
+ attribute :containers_for_execution, Array[String]
12
+ attribute :containers, Array[Docktor::Container]
13
+ end
14
+
15
+ def select_container_for_execution(container_name)
16
+ select_containers_for_execution([container_name])
17
+ end
18
+
19
+ def select_containers_for_execution(container_names)
20
+ self.containers_for_execution = []
21
+
22
+ container_names.each do |name|
23
+ container = find_container_by_name(name)
24
+
25
+ fail ContainerNotFound, %(There is no container "#{name}") if container.nil?
26
+
27
+ containers_for_execution << container
28
+ self.containers_for_execution += find_container_links(container)
29
+ end
30
+
31
+ containers_for_execution.uniq!
32
+ sort_containers_for_execution!
33
+ end
34
+
35
+ def find_container_by_name(name)
36
+ containers.find { |container| container.name == name }
37
+ end
38
+
39
+ def find_container_links(container)
40
+ container.links.map { |link| find_container_by_name link }
41
+ end
42
+
43
+ def sort_containers_for_execution!
44
+ sorted_containers = []
45
+
46
+ containers_for_execution.each do |container|
47
+ links_indexes = container.links.map { |link| sorted_containers.index link }
48
+ sorted_containers.insert((links_indexes.max || -1) + 1, container)
49
+ end
50
+
51
+ self.containers_for_execution = sorted_containers
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ require "yaml"
2
+ require "docktor/manifest"
3
+ require "docktor/container"
4
+
5
+ module Docktor
6
+ class ManifestParser
7
+ def parse(yaml_path)
8
+ manifest_data = YAML.load_file(yaml_path)
9
+ manifest = Docktor::Manifest.new
10
+ default_containers = manifest_data.delete("doc_defaults")
11
+
12
+ manifest.containers = manifest_data.map do |container_name, options|
13
+ Container.new(
14
+ name: container_name,
15
+ **options.symbolize_keys
16
+ )
17
+ end
18
+
19
+ manifest.select_containers_for_execution default_containers
20
+
21
+ manifest
22
+ end
23
+ end
24
+ end
@@ -1,20 +1,31 @@
1
- require "docktor/yaml_parser"
1
+ require "docktor/manifest_parser"
2
2
  require "docktor/docker_client"
3
3
 
4
4
  module Docktor
5
5
  class Runner
6
+ class ContainerNotFound < StandardError; end
7
+
6
8
  DEFAULT_YAML_PATH = "./docktor.yml"
7
9
 
8
- def invoke(yaml_path: DEFAULT_YAML_PATH, parser: Docktor::YAMLParser.new, client: Docktor::DockerClient.new)
9
- containers = parser.parse(yaml_path)
10
+ def invoke(
11
+ container_name: nil,
12
+ yaml_path: DEFAULT_YAML_PATH,
13
+ parser: Docktor::ManifestParser.new,
14
+ client: Docktor::DockerClient.new
15
+ )
16
+ manifest = parser.parse(yaml_path)
17
+
18
+ manifest.select_container_for_execution(container_name) if container_name.present?
10
19
 
11
- containers.each do |container|
20
+ manifest.containers_for_execution.each do |container|
12
21
  if client.container_exists?(container)
13
22
  client.start container
14
23
  else
15
24
  client.run container
16
25
  end
17
26
  end
27
+ rescue Docktor::Manifest::ContainerNotFound => e
28
+ raise ContainerNotFound, e.message
18
29
  end
19
30
  end
20
31
  end
@@ -1,3 +1,3 @@
1
1
  module Docktor
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docktor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Munhoz Pélos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2015-09-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,9 +75,10 @@ files:
75
75
  - lib/docktor.rb
76
76
  - lib/docktor/container.rb
77
77
  - lib/docktor/docker_client.rb
78
+ - lib/docktor/manifest.rb
79
+ - lib/docktor/manifest_parser.rb
78
80
  - lib/docktor/runner.rb
79
81
  - lib/docktor/version.rb
80
- - lib/docktor/yaml_parser.rb
81
82
  homepage: https://github.com/mpelos/docktor
82
83
  licenses:
83
84
  - MIT
@@ -1,20 +0,0 @@
1
- require "yaml"
2
- require "docktor/container"
3
-
4
- module Docktor
5
- class YAMLParser
6
- def parse(yaml_path)
7
- YAML.load_file(yaml_path).map do |container_name, options|
8
- Container.new(
9
- name: container_name,
10
- image: options["image"],
11
- volumes: options["volumes"],
12
- ports: options["ports"],
13
- links: options["links"],
14
- command: options["command"],
15
- environment: options["environment"]
16
- )
17
- end
18
- end
19
- end
20
- end