lib/dockerun/docker_container_helper.rb in dockerun-0.1.15 vs lib/dockerun/docker_container_helper.rb in dockerun-0.1.16

- old
+ new

@@ -1,32 +1,24 @@ require_relative 'docker_command_factory_helper' module Dockerun module CommandHelper - - # - # Common functions for docker container management - # module DockerContainerHelper include DockerCommandFactoryHelper class DockerContainerBuildFailed < StandardError; end class DockerContainerStartFailed < StandardError; end - class DockerContainerStopFailed < StandardError; end def run_docker_container(image_name, container_name, &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) @@ -36,27 +28,23 @@ break end end else - 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 + st, _ = is_container_exist?(container_name) + if st + reuse = true + else + # if not found shall drop into the next block's else clause + end end if reuse == true - - 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? - if not is_container_running?(container_name) + + res = dcFact.find_running_container(container_name).run + if not res.failed? and res.is_out_stream_empty? # 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") @@ -66,14 +54,12 @@ dcFact.run_command_in_running_container(container_name, ucmd, tty: true, interactive: true).run end else - Dockerun.udebug "Container '#{container_name}' doesn't exist. Proceed to create new container." reqVolMap = block.call(:volume_mapping_required?) - mount = block.call(:existing_volume_mapping) || [] - #mount = [] + mount = [] if reqVolMap loop do src = block.call(:source_prompt) @@ -115,23 +101,9 @@ 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).run - not res.failed? and not res.is_out_stream_empty? - else - false - 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