lib/dockerun/docker_container_helper.rb in dockerun-0.2.3 vs lib/dockerun/docker_container_helper.rb in dockerun-0.3.0

- old
+ new

@@ -2,25 +2,33 @@ require_relative 'docker_command_factory_helper' require_relative 'bundler_helper' module Dockerun module CommandHelper + + # + # Common functions for docker container management + # module DockerContainerHelper include DockerCommandFactoryHelper include BundlerHelper class DockerContainerBuildFailed < StandardError; end class DockerContainerStartFailed < StandardError; end + class DockerContainerStopFailed < StandardError; end def run_docker_container(image_name, container_name, mount_points = [], &block) raise DockerContainerBuildFailed, "block is required" if not block raise DockerContainerBuildFailed, "Image name is required" if is_empty?(image_name) + Dockerun.udebug("Running image '#{image_name}', container '#{container_name}'") + reuse = nil if is_empty?(container_name) + Dockerun.udebug "Container name empty. Creating new container" container_name = block.call(:new_container_name) loop do st, _ = is_container_exist?(container_name) if st reuse = block.call(:container_name_exist, container_name) @@ -30,26 +38,28 @@ break end end else - st, _ = is_container_exist?(container_name) - if st - reuse = true - else - # if not found shall drop into the next block's else clause - end + reuse, _ = is_container_exist?(container_name) + Dockerun.udebug "Container name not empty. Is container exist? : #{reuse}" + #if st + # reuse = true + #else + # # if not found shall drop into the next block's else clause + #end end if reuse == true - # - # container already exist so no configuration is required - # - res = dcFact.find_running_container(container_name).run - if not res.failed? and res.is_out_stream_empty? + Dockerun.udebug "Find out of container '#{container_name}' is running..." + #res = dcFact.find_running_container(container_name).run + #if not res.failed? and res.is_out_stream_empty? + st, _ = is_container_running?(container_name) + if not st # not running + Dockerun.udebug "Container '#{container_name}' does not seems to be running. Starting container." ress = dcFact.start_container(container_name).run raise DockerContainerStartFailed, "Failed to start container '#{container_name}'. Error was : #{ress.err_stream}" if ress.failed? end ucmd = cli.ask("Command to be run inside the container. Empty to attach to existing session : ", value: "/bin/bash") @@ -147,11 +157,10 @@ def is_container_exist?(name) if not_empty?(name) res = dcFact.find_from_all_container("^#{name}\\z").run raise DockerContainerBuildFailed, "Failed to find container. Error was : #{res.err_stream}" if res.failed? - p res.out_stream if res.is_out_stream_empty? # nothing found [false, ""] else [true, res.out_stream] @@ -164,9 +173,30 @@ def run_container(name) res = dcFact.start_container(name) raise DockerContainerStartFailed, "Failed to start docker container name '#{name}'. Error was : #{res.err_stream}" if res.failed? + end + + def is_container_running?(name) + if not_empty?(name) + res = dcFact.find_running_container("^#{name}\\z").run + raise DockerContainerBuildFailed, "Failed to find is running container. Error was : #{res.err_stream}" if res.failed? + + if res.is_out_stream_empty? + # nothing found + [false, ""] + else + [true, res.out_stream] + end + else + [false, nil] + end + end + + def stop_container(name) + res = dcFact.stop_container(name).run + raise DockerContainerStopFailed, "Failed to stop docker container '#{name}'. Error was : #{res.err_stream}" if res.failed? end end end