lib/hitcher/providers/docker/dsl.rb in Hitcher-0.1.2 vs lib/hitcher/providers/docker/dsl.rb in Hitcher-0.1.3
- old
+ new
@@ -1,7 +1,9 @@
require "toolrack"
+require 'etc'
+
require_relative 'template'
require_relative 'command_builder'
require_relative 'command_runner'
module Hitcher
@@ -45,12 +47,12 @@
res << m
elsif e =~ /:/
# handle host:docker ==> /home/xxx/dev:/opt/dev
ee = e.split(":")
m = Mount.new
- m.host = translate(ee[0])
- m.docker = ee[1]
+ m.host = translate(ee[0].strip)
+ m.docker = ee[1].strip
res << m
end
end
end
@@ -85,10 +87,12 @@
#
# class Dockerfile
#
class Dockerfile
+ include Antrapol::ToolRack::ConditionUtils
+
attr_reader :cont
def parse(blc)
@cont = []
if not (blc.nil? or blc.empty?)
blc.each do |e|
@@ -99,19 +103,49 @@
else
# todo extract parameters
end
elsif e.strip != "end"
- @cont << e
+ @cont << translate(e.strip)
end
end
end
+
end
def is_empty?
@cont.nil? or @cont.empty?
end
+
+ private
+ def translate(line)
+
+ if not line.strip.empty?
+ if line =~ /\$USER/
+ line = line.gsub("$USER", user_info.name)
+ end
+
+ if line =~ /\$UID/
+ line = line.gsub("$UID", "#{user_info.uid}")
+ end
+
+ if line =~ /\$GID/
+ line = line.gsub("$GID", "#{user_info.gid}")
+ end
+ end
+
+ line
+ end
+
+ def user_info
+ if @userInfo.nil?
+ login = Etc.getlogin
+ @userInfo = Etc.getpwnam(login)
+ end
+ @userInfo
+ end
+
end # class Dockerfile
##
## Module Dsl
##
@@ -194,16 +228,17 @@
def gen(depth = :all)
@logger.debug "Generate output files!"
te = Hitcher::Docker::TemplateEngine.new
- if depth == :dockerfile or depth == :all and not @dockerfile.nil?
+ if (depth == :dockerfile or depth == :all) and not @df.nil?
te.build_template(:dockerfile, { image: @image, dockerfile: @df.cont })
@logger.debug "Dockerfile generated"
end
if depth == :build_container or depth == :all
te.build_template(:build_container, { image_name: @image })
+ @logger.debug "build_container generated"
end
if depth == :run_script or depth == :all
# run script
mm = @mounts.collect { |m| m.hashify }