lib/dockerun/docker_container_helper.rb in dockerun-0.1.18 vs lib/dockerun/docker_container_helper.rb in dockerun-0.2.0

- old
+ new

@@ -1,17 +1,19 @@ require_relative 'docker_command_factory_helper' +require_relative 'bundler_helper' module Dockerun module CommandHelper module DockerContainerHelper include DockerCommandFactoryHelper + include BundlerHelper class DockerContainerBuildFailed < StandardError; end class DockerContainerStartFailed < StandardError; end - def run_docker_container(image_name, container_name, &block) + 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) reuse = nil @@ -37,11 +39,14 @@ # 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? # not running ress = dcFact.start_container(container_name).run raise DockerContainerStartFailed, "Failed to start container '#{container_name}'. Error was : #{ress.err_stream}" if ress.failed? @@ -54,40 +59,98 @@ dcFact.run_command_in_running_container(container_name, ucmd, tty: true, interactive: true).run end else - reqVolMap = block.call(:volume_mapping_required?) - mount = [] - if reqVolMap + #@workspace_root = "/opt" + #@shared_dirs = {} - loop do + #mount = [] + #sharedInsideDocker = [] + #res = find_local_dev_gems + #puts "Found #{res.length} local gems #{res}" + #if not res.empty? + # + # transferMapping = block.call(:transfer_dev_gem_mapping?) + # if transferMapping + # res.each do |name, path| + # tsrc = block.call(:workspace_root_inside_docker, @workspace_root, name, path) + # inPath = File.join(tsrc, name) + # mount << { path => inPath } + # @shared_dirs[name] = inPath + # end + # end - src = block.call(:source_prompt) - dest = block.call(:destination_prompt, src) - mount << { src => dest } - block.call(:add_mount_to_container, container_name, mount.last) - repeat = block.call(:add_more_volume_mapping?) - break if not repeat + #end - end + #mapProjectDir = block.call(:map_project_dir, @workspace_root) + #if not_empty?(mapProjectDir) + # mount << { Dir.getwd => mapProjectDir } + #end - end + #reqVolMap = block.call(:volume_mapping_required?) + #if reqVolMap - dcFact.create_container_from_image(image_name, interactive: true, tty: true, container_name: container_name, mount: mount).run + # loop do + # + # block.call(:already_mapped, mount) + # src = block.call(:source_prompt) + # dest = block.call(:destination_prompt, src) + # mount << { src => dest } + # + # add_to_bundle = block.call(:add_to_bundle?, dest) + # if add_to_bundle + # @shared_dirs[File.basename(dest)] = dest + # end + + # block.call(:add_mount_to_container, container_name, mount.last) + # repeat = block.call(:add_more_volume_mapping?) + # break if not repeat + + # end + + #end + + + #insideDockerConfig = File.join(File.dirname(__FILE__),"..","..","template","setup_ruby_devenv.rb.erb") + #if File.exist?(insideDockerConfig) + # + # @docker_init_file_path = File.join(Dir.getwd,"on_docker_config") + + # cont = File.read(insideDockerConfig) + # + # b = binding + + # res = ERB.new(cont) + # out = res.result(b) + + # # fixed this name to be used inside Dockerfile + # File.open(@docker_init_file_path, "w") do |f| + # f.write out + # end + + # block.call(:on_docker_init_file_path,@docker_init_file_path) + + #end + + + + dcFact.create_container_from_image(image_name, interactive: true, tty: true, container_name: container_name, mount: mount_points).run + end container_name end private def is_container_exist?(name) if not_empty?(name) - res = dcFact.find_from_all_container(name).run + 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]