docktor 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/bin/doc +5 -1
- data/lib/docktor.rb +3 -0
- data/lib/docktor/container.rb +4 -3
- data/lib/docktor/docker_client.rb +9 -7
- data/lib/docktor/manifest.rb +54 -0
- data/lib/docktor/manifest_parser.rb +24 -0
- data/lib/docktor/runner.rb +15 -4
- data/lib/docktor/version.rb +1 -1
- metadata +4 -3
- data/lib/docktor/yaml_parser.rb +0 -20
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 668089227b7edce1c61b2969309282930668aca8
         | 
| 4 | 
            +
              data.tar.gz: 4cc904b8dddf40bd913e603fb870d0a677a2c139
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3b0755e53477032149692af0a46b1d6c37f9c3638af0a72263e85c49fd85bffd075ce176b9414f7c978a7638d02ccaff40f37ec71e1c137f0ac8b03abf0d0393
         | 
| 7 | 
            +
              data.tar.gz: c1d39ce8a1361f9e0070580ccff5c6b84f846cb2452005a3022fbe1510a45dd20dda4ec7431607827da18224cad8d1d33b825df4ec37fb564e40da6f505e40be
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/bin/doc
    CHANGED
    
    
    
        data/lib/docktor.rb
    CHANGED
    
    
    
        data/lib/docktor/container.rb
    CHANGED
    
    | @@ -2,9 +2,9 @@ require "virtus" | |
| 2 2 |  | 
| 3 3 | 
             
            module Docktor
         | 
| 4 4 | 
             
              class Container
         | 
| 5 | 
            -
                include Virtus. | 
| 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 | 
| 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  | 
| 6 | 
            +
                  `docker ps --all --quiet --filter name=#{container_name(container.name)}`.present?
         | 
| 7 7 | 
             
                end
         | 
| 8 8 |  | 
| 9 9 | 
             
                def run(container)
         | 
| 10 | 
            -
                   | 
| 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 : | 
| 22 | 
            -
                    when : | 
| 23 | 
            -
                    when : | 
| 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 | 
            -
                      " | 
| 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
         | 
    
        data/lib/docktor/runner.rb
    CHANGED
    
    | @@ -1,20 +1,31 @@ | |
| 1 | 
            -
            require "docktor/ | 
| 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( | 
| 9 | 
            -
                   | 
| 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 | 
            -
                   | 
| 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
         | 
    
        data/lib/docktor/version.rb
    CHANGED
    
    
    
        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. | 
| 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- | 
| 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
         | 
    
        data/lib/docktor/yaml_parser.rb
    DELETED
    
    | @@ -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
         |