class Potamus::Config

Public Class Methods

new(root) click to toggle source
# File lib/potamus/config.rb, line 7
def initialize(root)
  @root = File.expand_path(root)

  potamus_file_path = File.join(@root, 'PotamusFile')
  if File.file?(potamus_file_path)
    @options = YAML.load_file(potamus_file_path)
  else
    raise Error, "No Potamus file found at #{potamus_file_path}"
  end
end

Public Instance Methods

branch() click to toggle source
# File lib/potamus/config.rb, line 75
def branch
  stdout, stderr, status = Open3.capture3("cd #{@root} && git symbolic-ref HEAD")
  unless status.success?
    raise Error, "Could not get current commit (#{stderr})"
  end

  stdout.strip.sub('refs/heads/', '')
end
branch_for_latest() click to toggle source
# File lib/potamus/config.rb, line 22
def branch_for_latest
  @options['branch_for_latest'] || 'master'
end
build_command() click to toggle source
# File lib/potamus/config.rb, line 84
def build_command
  [
    "cd #{@root}",
    '&&',
    buildkit? ? 'DOCKER_BUILDKIT=1' : nil,
    'docker', 'build', '.', '-t', image_name_with_commit,
    '--build-arg', 'commit_ref=' + commit,
    '--build-arg', 'branch=' + branch,
    build_options
  ].compact.join(' ')
end
build_options() click to toggle source
# File lib/potamus/config.rb, line 67
def build_options
  if @options['build_options'].is_a?(Array)
    @options['build_options'].map(&:to_s).join(' ')
  else
    @options['build_options']
  end
end
buildkit?() click to toggle source
# File lib/potamus/config.rb, line 63
def buildkit?
  @options['buildkit'] == true
end
commit() click to toggle source
# File lib/potamus/config.rb, line 51
def commit
  get_commit_ref(branch)
end
dirty?() click to toggle source
# File lib/potamus/config.rb, line 42
def dirty?
  stdout, stderr, status = Open3.capture3("cd #{@root} && git status --short")
  unless status.success?
    raise Error, "Could not determine git status using `git status` (#{stderr})"
  end

  !stdout.strip.empty?
end
git?() click to toggle source
# File lib/potamus/config.rb, line 38
def git?
  File.directory?(File.join(@root, '.git'))
end
image_name() click to toggle source
# File lib/potamus/config.rb, line 26
def image_name
  @options['image_name'] || raise(Error, "image_name is required in the PotamusFile")
end
image_name_with_commit() click to toggle source
# File lib/potamus/config.rb, line 30
def image_name_with_commit
  if @test_mode
    "#{image_name}:test"
  else
    "#{image_name}:#{commit}"
  end
end
push_commands() click to toggle source
# File lib/potamus/config.rb, line 111
def push_commands
  if @test_mode
    return { 'test' => push_command('test') }
  end

  hash = {}
  hash[commit] = push_command(commit)
  tags.each do |tag|
    hash[tag] = push_command(tag)
  end
  hash
end
pushed?() click to toggle source
# File lib/potamus/config.rb, line 59
def pushed?
  commit == remote_commit
end
remote_commit() click to toggle source
# File lib/potamus/config.rb, line 55
def remote_commit
  get_commit_ref("#{remote_name}/#{branch}")
end
remote_name() click to toggle source
# File lib/potamus/config.rb, line 18
def remote_name
  @options['remote_name'] || 'origin'
end
tag_commands() click to toggle source
# File lib/potamus/config.rb, line 105
def tag_commands
  tags.each_with_object({}) do |tag, hash|
    hash[tag] = tag_command(tag)
  end
end
tags() click to toggle source
# File lib/potamus/config.rb, line 96
def tags
  return [] if @test_mode

  array = []
  array << branch
  array << 'latest' if branch == branch_for_latest
  array
end
test_mode!() click to toggle source
# File lib/potamus/config.rb, line 124
def test_mode!
  @test_mode = true
end

Private Instance Methods

get_commit_ref(branch) click to toggle source
# File lib/potamus/config.rb, line 138
def get_commit_ref(branch)
  stdout, stderr, status = Open3.capture3("cd #{@root} && git log #{branch} -n 1 --pretty='%H'")
  unless status.success?
    raise Error, "Could not get commit for #{branch} (#{stderr})"
  end

  stdout.strip
end
push_command(tag) click to toggle source
# File lib/potamus/config.rb, line 134
def push_command(tag)
  ['docker', 'push', "#{image_name}:#{tag}"].join(' ')
end
tag_command(tag) click to toggle source
# File lib/potamus/config.rb, line 130
def tag_command(tag)
  ['docker', 'tag', image_name_with_commit, "#{image_name}:#{tag}"].join(' ')
end