require 'toolrack' require 'erb' require_relative 'command_builder' module Hitcher module Docker class TemplateEngine include Hitcher::Docker::CommandBuilder def build_template(temp, val) dest_root = Dir.getwd if val[:dest_root].nil? or val[:dest_root].empty? case temp when :dockerfile path = File.join(File.dirname(__FILE__),"templates","Dockerfile.erb") bind_template(path, File.join(dest_root,"Dockerfile"), val) else if Antrapol::ToolRack::RuntimeUtils.on_windows? raise Hitcher::Error, "Not ready yet" else case temp when :build_container, :build path = File.join(File.dirname(__FILE__),"templates","linux_mac","1.build.sh.erb") opts = {} opts.merge!(val) cmd = cb_build_image(opts[:image_name], opts) f = File.join(dest_root,"1.build.sh") bind_template(path,f, { command: cmd }) FileUtils.chmod "+x", f when :run_container, :run path = File.join(File.dirname(__FILE__),"templates","linux_mac","2.run_container.sh.erb") opts = {} opts.merge!(val) cmd = cb_create_container_from_image(opts[:image], opts) f = File.join(dest_root,"2.run_container.sh") bind_template(path,f, { command: cmd }) FileUtils.chmod "+x", f when :start_container, :start path = File.join(File.dirname(__FILE__),"templates","linux_mac","3.start_container.sh.erb") opts = {} opts.merge!(val) cmd = cb_start_container(opts[:container_name], opts) f = File.join(dest_root,"3.start_container.sh") bind_template(path,f, { command: cmd }) FileUtils.chmod "+x", f when :container_prompt, :prompt path = File.join(File.dirname(__FILE__),"templates","linux_mac","4.container_prompt.sh.erb") opts = {} opts.merge!(val) cmd = cb_run_command_in_container(opts[:container_name], opts) f = File.join(dest_root,"4.container_prompt.sh") bind_template(path,f, { command: cmd }) FileUtils.chmod "+x", f end end end end def bind_template(source,dest, val) File.open(source,"r") do |f| cont = f.read b = binding val.each do |k,v| b.local_variable_set(k,v) end File.open(dest,"w") do |ff| ff.write ERB.new(cont).result(b) end end end end # TemplateEngine end # Docker end # Hitcher