require_relative '../../global' require 'toolrack' require 'etc' require 'ptools' module Hitcher module Docker module CommandBuilder include Antrapol::ToolRack::ConditionUtils def docker_exe path = File.which("docker") if path.nil? "docker" else path end end def cb_find_image(name, tag = "", opts = { }) raise Hitcher::Error, "Name is mandatory" if is_empty?(name) cmd = [] cmd << docker_exe cmd << "images" cmd << "-q" if not_empty?(tag) cmd << "#{name}:#{tag}" else cmd << name end ccmd = cmd.join(" ") logger.debug "Find image: #{ccmd}" ccmd end def cb_find_running_container(name, opts = { }) raise Hitcher::Error, "Name is mandatory" if is_empty?(name) cmd = [] cmd << docker_exe cmd << "ps" cmd << "-q" cmd << "-f" cmd << "name=#{name}" ccmd = cmd.join(" ") logger.debug "Find container: #{ccmd}" ccmd end def cb_find_all_container(name, opts = { }) raise Hitcher::Error, "Name is mandatory" if is_empty?(name) cmd = [] cmd << docker_exe cmd << "ps" cmd << "-aq" cmd << "-f" cmd << "name=#{name}" ccmd = cmd.join(" ") logger.debug "Find all container: #{ccmd}" ccmd end def cb_build_image(name = "", opts = { }) cmd = [] cmd << docker_exe cmd << "build" if not (name.nil? or name.empty?) cmd << "-t #{name}" end cmd << "." ccmd = cmd.join(" ") Hitcher::Global.instance.glog.debug "Build Image : #{ccmd}" ccmd end # build_image def cb_create_container_from_image(image, opts = { }) cmd = [] cmd << docker_exe cmd << "run" cmd << "-i" if opts[:interactive] == true cmd << "-t" if opts[:tty] == true if not (opts[:container_name].nil? or opts[:container_name].empty?) cname = opts[:container_name] cmd << "--name #{cname}" end cmd << process_mount(opts) cmd << process_port(opts) #userInfo = Etc.getpwnam(Etc.getlogin) #cmd << "--user #{userInfo.uid}:#{userInfo.gid}" cmd << image if not_empty?(opts[:command]) cmd << "\"#{opts[:command]}\"" end ccmd = cmd.join(" ") Hitcher::Global.instance.glog.debug "Run Container from Image : #{ccmd}" ccmd end # run_container_from_image def cb_start_container(container, opts = { }) cmd = [] cmd << docker_exe cmd << "container" cmd << "start" cmd << container ccmd = cmd.join(" ") Hitcher::Global.instance.glog.debug "Start Container : #{ccmd}" ccmd end def cb_run_command_in_container(container, opts = { }) cmd = [] cmd << docker_exe cmd << "container" cmd << "exec" if is_empty?(opts[:command]) cmd << "-t" if not_empty?(opts[:tty]) and opts[:tty] == true cmd << "-i" if not_empty?(opts[:interactive]) and opts[:interactive] == true end cmd << container if is_empty?(opts[:command]) cmd << "/bin/bash --login" else cmd << opts[:command] end ccmd = cmd.join(" ") Hitcher::Global.instance.glog.debug "Run command in container : #{ccmd}" ccmd end # container_prompt private def logger if @logger.nil? @logger = Hitcher::Global.instance.glog end @logger end def process_mount(opts) if not (opts[:mounts].nil? or opts[:mounts].empty?) m = opts[:mounts] m = [m] if not m.is_a?(Array) res = [] m.each do |mm| l = [] l << "-v " l << mm[:host] l << ":" l << mm[:docker] res << l.join end res.join(" ") else "" end end # process_mount def process_port(opts) if not (opts[:ports].nil? or opts[:ports].empty?) po = opts[:ports] po = [po] if not po.is_a?(Array) res = [] po.each do |ppo| l = [] l << "-p " l << ppo[:host] l << ":" l << ppo[:docker] res << l.join end res.join(" ") else "" end end end class DockerCommandBuilder include Hitcher::Docker::CommandBuilder end end end