require "tty-prompt" require 'tty-command' require 'toolrack' require_relative "command_builder" require_relative "../../command_runner" module Hitcher module Docker module CommandRunner include Antrapol::ToolRack::ConditionUtils include Hitcher::Docker::CommandBuilder include Hitcher::CommandRunner def initialize(docker) @dockerConf = docker end def image_exist?(opts = { }) name = opts[:image_name] || @dockerConf.image tag = opts[:image_tag] || "" cmd = cb_find_image(name, tag) run_command(cmd) end def container_exist?(opts = { }) name = opts[:container_name] || @dockerConf.cName if opts[:new_instance] name = "#{name}_#{Time.now.localtime.strftime("%Y%m%d_%H%M%S_%L%N")}" opts[:new_container_name] = name end cmd = cb_find_running_container(name, opts) res, out, err = run_command(cmd) if is_empty?(out) cmd2 = cb_find_all_container(name, opts) res2, out2, err2 = run_command(cmd2) if is_empty?(out2) [false, ""] else [true, :stopped, out2] end else [true,:running, out] end end # build new image based on Dockerfile def build_image(opts = { }) df = File.join(Dir.getwd, "Dockerfile") if not @dockerConf.df.is_empty? File.open(df,"w") do |f| f.write @dockerConf.df.cont.join("\n") end elsif not File.exist?(df) raise Exception, "No Dockerfile available to build image." end name = opts[:image_name] || @dockerConf.image cmd = cb_build_image(name, opts) run_command(cmd) end # build_image def create_container(opts = { }) cName = opts[:container_name] || @dockerConf.cName iName = opts[:image_name] || @dockerConf.image opt = { } opt.merge!(opts) if opts[:new_instance] opt[:container_name] = opts[:new_container_name] else opt[:container_name] = cName end opt[:mounts] = @dockerConf.mounts.collect { |m| m.hashify } opt[:ports] = @dockerConf.exposes.collect { |e| e.hashify } cmd = cb_create_container_from_image(iName, opt) begin v = run_in_new_terminal(cmd) [:term,v] rescue TerminalNotDefined => ex logger.debug "Terminal is not set. Creating the shell script instead" path = File.join(Dir.getwd,"create_container.sh") File.open(path,"w") do |f| f.puts "#!/bin/sh" f.puts "" f.puts cmd f.puts end FileUtils.chmod "+x", path [:file, path] end end def start_container(opts = { }) name = opts[:name] || @dockerConf.cName if opts[:new_instance] name = opts[:new_container_name] end cmd = cb_start_container(name, opts) run_command(cmd) end def run_command_in_container(command = "", opts = { }) name = opts[:container_name] || @dockerConf.cName if opts[:new_instance] name = opts[:new_container_name] end opt = { } opt.merge!(opts) opt[:command] = command cmd = cb_run_command_in_container(name, opt) # if command is empty default is "/bin/bash --login" if is_empty?(command) or opts[:tty] == true or opts[:interactive] == true begin v = run_in_new_terminal(cmd) [:term, v] rescue TerminalNotDefined => ex path = File.join(Dir.getwd,"prompt.sh") logger.debug "Terminal is not set. Creating the shell script for run command in container instead [#{File.basename(path)}]" File.open(path,"w") do |f| f.puts "#!/bin/sh" f.puts "" f.puts cmd f.puts end FileUtils.chmod "+x", path [:file, path] end else run_command(cmd) end end end # module CommandRunner class DockerCommandExecutor include Hitcher::Docker::CommandRunner end end end