lib/dockage/docker/shell.rb in dockage-0.1.2 vs lib/dockage/docker/shell.rb in dockage-0.1.3

- old
+ new

@@ -3,11 +3,11 @@ class Shell DOCKER_DEFAULT_HOST = 'unix:///var/run/docker.sock' def initialize - @env = "DOCKER_HOST=#{Dockage.settings[:docker_host] || DOCKER_DEFAULT_HOST}" + @env = "export DOCKER_HOST=#{Dockage.settings[:docker_host] || DOCKER_DEFAULT_HOST}" end def pull(image) invoke("pull #{image}", attach_std: true) end @@ -20,13 +20,14 @@ invoke("start #{name}", catch_errors: true) end def stop(name) unless container_running?(name) - Dockage.logger("Container #{name.bold.yellow} is not running") + Dockage.logger("Container #{name.bold.yellow} is not running. Nothing to do") return end + Dockage.logger("Stopping container #{name.bold.yellow}") invoke("stop #{name}", catch_errors: true) end def destroy(name) unless container_exists?(name) @@ -57,20 +58,23 @@ containers.reject! { |con| con[:name] != name } if name containers end def up(container) + Dockage.logger("Bringing up #{container[:name].yellow.bold}") + return reload(container) if should_be_reload?(container) if container_running?(container[:name]) Dockage.logger("Container #{container[:name].bold} is already up. Nothing to do") return end return start(container[:name]) if container_exists?(container[:name]) pull(container[:image]) if container[:keep_fresh] run(container[:image], container) end def reload(container) + return unless dependences_satisfied?(container) stop(container[:name]) if container_running?(container[:name]) destroy(container[:name]) if container_exists?(container[:name]) up(container) end @@ -111,23 +115,54 @@ ps(name, true).any? end def run(image, opts = {}) command = "run" \ - "#{opts[:detach] == false || ' -d'}" \ + "#{opts[:detach] == false || ' -d'}" \ "#{opts[:links] && opts[:links].map { |link| " --link #{link}" }.join}" \ "#{opts[:volumes] && opts[:volumes].map { |volume| " -v #{volume}" }.join}" \ "#{opts[:ports] && opts[:ports].map { |port| " -p #{port}" }.join}" \ "#{opts[:name] && " --name #{opts[:name]}"}" \ " #{image}" \ - "#{opts[:cmd] && " /bin/sh -c '#{opts[:cmd]}'"}" + "#{opts[:cmd] && " /bin/sh -c '#{opts[:cmd]}'"}" invoke(command) end private + def dependences_satisfied?(container) + return true unless container[:links] && container[:links].any? + active_containers = ps + container[:links].each do |link| + dependency_name = link.split(':').first + next if active_containers.select { |con| con[:name] == dependency_name }.any? + dependency_container = Dockage.settings.containers.select { |con| con[:name] == dependency_name }.first + unless dependency_container + Dockage.error("#{dependency_name.bold} is required for " \ + "#{container[:name]} but does not specified " \ + "in config file") + end + up(dependency_container) + end + end + + def should_be_reload?(container) + return false unless container[:links] + containers = ps(nil, true) + return false unless containers.select { |con| con[:name] == container[:name] }.any? + links = container[:links].map { |link| link.split(':').first } + dependency_containers = containers.select { |con| links.include?(con[:name]) } + dependency_containers.each do |dep_con| + unless dep_con[:linked_with].include?(container[:name]) + Dockage.logger("Container #{container[:name].bold} has missing links and should be reloaded") + return true + end + end + false + end + def invoke(cmd, opts = {}) - command = "#{@env} docker #{cmd}" + command = "#{@env} && docker #{cmd}" Dockage.verbose(command) if opts[:attach_std] output = sys_exec(command, opts[:catch_errors]) else output = `#{command}`