Class: Baha::Dockerfile

Inherits:
Object
  • Object
show all
Defined in:
lib/baha/dockerfile.rb

Constant Summary collapse

LOG =
Baha::Log.for_name("Dockerfile")

Class Method Summary collapse

Class Method Details

.append_attr(image, key, value) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/baha/dockerfile.rb', line 78

def append_attr(image,key,value)
  image[key] = [] unless image.has_key?(key)
  case value
  when Array
    image[key].concat(value)
  else
    image[key].push(value)
  end
end

.as_array(args) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/baha/dockerfile.rb', line 62

def as_array(args)
  begin
    return JSON.parse(args)
  rescue
    return CSV.parse_line(args,{:col_sep => " ", })
  end
end

.as_cmd(cmd) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/baha/dockerfile.rb', line 70

def as_cmd(cmd)
  begin
    return JSON.parse(cmd)
  rescue
    cmd
  end
end

.parse(file) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/baha/dockerfile.rb', line 22

def parse(file)
  LOG.info { "Parsing Dockerfile: #{file}" }
  linenum = 0
  image = {
    'run' => [],
    'pre_build' => [],
    'config' => {}
  }
  multiline = []
  f = File.open(file, "r")
  begin
    f.each_line do |rawline|
      linenum = linenum + 1
      line = rawline.chomp.gsub(/^\s*/,'')
      # Skip empty lines
      next if line.empty?
      # Skip comments
      next if line.match(/^#/)
      
      # Buffer multi-lines
      if line.match(/\\$/)
        multiline << line.chop
        next
      end
      multiline << line


      line_to_parse = multiline.join("\n")
      LOG.debug { "Parsing #{linenum}: #{line_to_parse}" }
      unless parse_line(image,line_to_parse)
        raise DockerfileParseError.new(file,linenum, line_to_parse)
      end
      multiline = []
    end
  ensure
    f.close
  end
  image
end

.parse_line(image, line) ⇒ Object

Parse a line and configure the image



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/baha/dockerfile.rb', line 119

def parse_line(image,line)
  cmd, args =  line.split(/\s+/,2)
  cmd = cmd.downcase.to_sym
  case cmd
  when :from
    image['parent'] = args
  when :maintainer
    image['maintainer'] = args
  when :expose
    append_attr(image['config'],'exposedports',resolve_env(image,args))
  when :volume
    append_attr(image['config'],'volumes',as_array(resolve_env(image,args)))
  when :entrypoint
    image['config']['entrypoint'] = as_cmd(args)
  when :cmd
    image['config']['cmd'] = as_cmd(args)
  when :run
    image['run'] << as_cmd(args)
  when :user
    image['config']['user'] = resolve_env(image,args)
  when :workdir
    set_workdir(image,args)
  when :env
    set_env(image,resolve_env(image,args))
  else
    return false
  end
  true
end

.resolve_env(image, value) ⇒ Object

Resolve environment variables in the string



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/baha/dockerfile.rb', line 89

def resolve_env(image,value)
  if image['config'].has_key?('env')
    env = image['config']['env']
    env.keys.reduce(value) do |v,k|
      r = Regexp.new("(?<!\\\\)[$](?:\\{#{k}\\}|#{k})")
      v.gsub(r,env[k])
    end
  else
    value
  end
end

.set_env(image, value) ⇒ Object



112
113
114
115
116
# File 'lib/baha/dockerfile.rb', line 112

def set_env(image,value)
  k,v = value.split(/\s+/,2)
  image['config']['env'] ||= {}
  image['config']['env'][k] = v
end

.set_workdir(image, value) ⇒ Object

Sets the workdir on the image config



102
103
104
105
106
107
108
109
110
# File 'lib/baha/dockerfile.rb', line 102

def set_workdir(image,value)
  wd = image['config']['workingdir']
  if wd
    wd = (Pathname.new(wd) + value).to_s
  else
    wd = value
  end
  image['config']['workingdir'] = resolve_env(image,wd)
end