module ContainedMr::TemplateLogic

Logic shared by {ContainedMr::Template} and {ContainedMr::Mock::Template}.

Attributes

id[R]

@return {String} the template’s unique identifier

image_id[R]

@return {String} image_id the unique ID of the Docker image used as a base

for images built by jobs derived from this template
item_count[R]

@return {Number} the number of mapper jobs specified by this template

name_prefix[R]

@return {String} prepended to Docker objects, for identification purposes

Public Instance Methods

image_tag() click to toggle source

@return {String} tag applied to the template’s base Docker image

# File lib/contained_mr/template_logic.rb, line 40
def image_tag
  "#{@name_prefix}/base.#{@id}"
end
mapper_dockerfile() click to toggle source

Computes the Dockerfile used to build a job’s mapper image.

@return {String} the Dockerfile

# File lib/contained_mr/template_logic.rb, line 28
def mapper_dockerfile
  job_dockerfile @_definition['mapper'] || {}, 'input'
end
mapper_env(i) click to toggle source

Computes the environment variables to be set in a mapper container.

@param {Number} i the mapper number @return {Array<String>} environment variables to be set in the mapper

# File lib/contained_mr/template_logic.rb, line 48
def mapper_env(i)
  [ "ITEM=#{i}", "ITEMS=#{@item_count.to_s}" ]
end
mapper_output_path() click to toggle source

@return {String} the map output’s path in the mapper Docker container

# File lib/contained_mr/template_logic.rb, line 60
def mapper_output_path
  (@_definition['mapper'] || {})['output'] || '/output'
end
new_job(id, json_options) click to toggle source

Creates a job using this template.

@param {String} id the job’s unique ID @param {Hash<String, Object>} json_options job options, extracted from JSON @return {ContainedMr::Job} a newly created job that uses this template

# File lib/contained_mr/template_logic.rb, line 21
def new_job(id, json_options)
  job_class.new self, id, json_options
end
reducer_dockerfile() click to toggle source

Computes the Dockerfile used to build a job’s reducer image.

@return {String} the Dockerfile

# File lib/contained_mr/template_logic.rb, line 35
def reducer_dockerfile
  job_dockerfile @_definition['reducer'] || {}, '.'
end
reducer_env() click to toggle source

Computes the environment variables to be set in the reducer container.

@return {Array<String>} environment variables to be set in the mapper

# File lib/contained_mr/template_logic.rb, line 55
def reducer_env
  [ "ITEMS=#{@item_count.to_s}" ]
end
reducer_output_path() click to toggle source

@return {String} the reducer output’s path in the reducer Docker container

# File lib/contained_mr/template_logic.rb, line 65
def reducer_output_path
  (@_definition['reducer'] || {})['output'] || '/output'
end

Private Instance Methods

job_dockerfile(job_definition, input_source) click to toggle source

@private common code from mapper_dockerfile and reducer_dockerfile

# File lib/contained_mr/template_logic.rb, line 70
  def job_dockerfile(job_definition, input_source)
    <<DOCKER_END
FROM #{image_tag}
ARG affinity:image=
COPY #{input_source} #{job_definition['input'] || '/input'}
WORKDIR #{job_definition['chdir'] || '/'}
ENTRYPOINT #{JSON.dump(job_definition['cmd'] || ['/bin/sh'])}
DOCKER_END
  end
read_definition(yaml_io) click to toggle source

Reads the template’s definition, using data at the given path.

@param {IO} yaml_io IO implementation that produces the .yaml file

containing the definition
# File lib/contained_mr/template_logic.rb, line 85
def read_definition(yaml_io)
  @_definition = YAML.load yaml_io.read
  @_definition.freeze

  @item_count = @_definition['items'] || 1
end