class GenDockerfile

Public Class Methods

arrayPartsToString(command, array) click to toggle source
# File lib/gendockerfile.rb, line 85
def self.arrayPartsToString(command, array)
  parts = ""
  (0..array.length - 1).each do |index|
    parts += command + " " + array[index] + $NEWLINE
  end

  return parts
end
configHash() click to toggle source
# File lib/gendockerfile.rb, line 12
def self.configHash
  @configHash
end
envPairsAsString(command, envArray) click to toggle source
# File lib/gendockerfile.rb, line 74
def self.envPairsAsString(command, envArray)
  envBlock = ""
  (0..envArray.length - 1).each do |index|
    envHash =  envArray[index]
    envHash.each do |key, value|
      envBlock += command + " " + key + " " + value + $NEWLINE
    end
  end
  return envBlock
end
init() click to toggle source
# File lib/gendockerfile.rb, line 16
def self.init()
  $LOG.debug("GenDockerfile.init called")
  $LOG.debug("GenDockerfile.init reading env var GEN_DOCKERFILE_CONFIG")

  config = ENV['GEN_DOCKERFILE_CONFIG']
  if config.nil?
    config = "null"
  end

  $LOG.debug("GenDockerfile.init config: " + config)

  if !File.exist?(config)
    errMsg = "env var GEN_DOCKERFILE_CONFIG not set to a valid file: " + config
    puts errMsg
    $LOG.error(errMsg)
  else
    $LOG.info(config + " exists")
    file = File.read(config)
    begin
      @configHash = JSON.parse(file)
      $LOG.debug(@configHash)
    rescue JSON::ParserError => e
      errMsg = "unable to parse " + config
      puts errMsg
      puts "see logfile for details: " + $LOGFILE
      $LOG.error(errMsg)
      $LOG.error(e)
    end
  end
  $LOG.debug("GenDockerfile.init done")
end
write() click to toggle source
# File lib/gendockerfile.rb, line 48
def self.write
  $LOG.debug("GenDockerfile.write called")

  template = Ate.parse(
    "lib/Dockerfile.ate",
      FROM: @configHash ['from'],
      MAINTAINER: @configHash ['maintainer'],
      EXPOSE: @configHash ['expose'],
      CMD: @configHash ['cmd'],
      RUN: arrayPartsToString("RUN", @configHash ['run']),
      ENV: envPairsAsString("ENV", @configHash ['env'])
  )
  content = template.render
  puts content

  now = Time.now.strftime("%Y%m%d-%H%M%S")
  outFilename = "Dockerfile." + now
  file = File.open(outFilename, "w")
  file.write(content)

  puts "generated dockerfile written to " + outFilename

  $LOG.debug("generated dockerfile written to " + outFilename)
  $LOG.debug("GenDockerfile.write done")
end