Class: FPM::Fry::DockerFile::Build

Inherits:
Struct
  • Object
show all
Defined in:
lib/fpm/fry/docker_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, variables, recipe, options = {}) ⇒ Build

Returns a new instance of Build.



105
106
107
108
109
110
# File 'lib/fpm/fry/docker_file.rb', line 105

def initialize(base, variables, recipe, options = {})
  variables = variables.dup.freeze
  raise Fry::WithData('unknown flavour', 'flavour' => variables[:flavour]) unless ['debian','redhat'].include? variables[:flavour]
  @options = options.dup.freeze
  super(base, variables, recipe)
end

Instance Attribute Details

#baseObject

Returns the value of attribute base

Returns:

  • (Object)

    the current value of base



100
101
102
# File 'lib/fpm/fry/docker_file.rb', line 100

def base
  @base
end

#recipeObject

Returns the value of attribute recipe

Returns:

  • (Object)

    the current value of recipe



100
101
102
# File 'lib/fpm/fry/docker_file.rb', line 100

def recipe
  @recipe
end

#variablesObject

Returns the value of attribute variables

Returns:

  • (Object)

    the current value of variables



100
101
102
# File 'lib/fpm/fry/docker_file.rb', line 100

def variables
  @variables
end

Instance Method Details

#build_shObject



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fpm/fry/docker_file.rb', line 162

def build_sh
  df = ['#!/bin/bash']
  df << 'set -e'
  recipe.steps.each do |v|
    if v.respond_to? :name
      df << "echo -e '\\e[1;32m====> #{Shellwords.escape v.name}\\e[0m'"
    end
    df << v.to_s
  end
  df << ''
  return df.join("\n")
end

#dockerfileObject



112
113
114
115
116
117
118
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
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/fpm/fry/docker_file.rb', line 112

def dockerfile
  df = {
    source: [],
    dependencies: [],
    build: []
  }
  df[:source] << "FROM #{base}"
  workdir = '/tmp/build'
  # TODO: get this from cache, not from the source itself
  if recipe.source.respond_to? :to
    to = recipe.source.to || ""
    workdir = File.expand_path(to, workdir)
  end
  df[:source] << "WORKDIR #{workdir}"

  # need to add external sources before running any command
  recipe.build_mounts.each do |source, target|
    df[:dependencies] << "COPY #{source} ./#{target}"
  end

  recipe.before_dependencies_steps.each do |step|
    df[:dependencies] << "RUN #{step.to_s}"
  end

  if build_dependencies.any?
    case(variables[:flavour])
    when 'debian'
      update = ''
      if options[:update]
        update = 'apt-get update && '
      end
      df[:dependencies] << "ARG DEBIAN_FRONTEND=noninteractive"
      df[:dependencies] << "RUN #{update}apt-get install --yes #{Shellwords.join(build_dependencies)}"
    when 'redhat'
      df[:dependencies] << "RUN yum -y install #{Shellwords.join(build_dependencies)}"
    else
      raise "Unknown flavour: #{variables[:flavour]}"
    end
  end

  recipe.before_build_steps.each do |step|
    df[:build] << "RUN #{step.to_s}"
  end

  df[:build] << "COPY .build.sh #{workdir}/"
  df[:build] << "CMD #{workdir}/.build.sh"
  recipe.apply_dockerfile_hooks(df)
  return [*df[:source],*df[:dependencies],*df[:build],""].join("\n")
end

#tar_ioObject



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/fpm/fry/docker_file.rb', line 175

def tar_io
  sio = StringIO.new
  tar = Gem::Package::TarWriter.new(sio)
  tar.add_file('.build.sh','0777') do |io|
    io.write(build_sh)
  end
  tar.add_file(NAME,'0777') do |io|
    io.write(dockerfile)
  end
  recipe.build_mounts.each do |source, _|
    tar.add_file(source,'0777') do |io|
      io.write(File.read(source))
    end
  end
  #tar.close
  sio.rewind
  return sio
end