Class: Potamus::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/potamus/config.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
# 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

Instance Method Details

#branchObject



63
64
65
66
67
68
69
70
# File 'lib/potamus/config.rb', line 63

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_latestObject



22
23
24
# File 'lib/potamus/config.rb', line 22

def branch_for_latest
  @options['branch_for_latest'] || 'master'
end

#build_commandObject



72
73
74
75
76
77
78
79
80
# File 'lib/potamus/config.rb', line 72

def build_command
  [
    "cd #{@root}",
    '&&',
    'docker', 'build', '.', '-t', image_name_with_commit,
    '--build-arg', 'commit_ref=' + commit,
    '--build-arg', 'branch=' + branch
  ].join(' ')
end

#commitObject



51
52
53
# File 'lib/potamus/config.rb', line 51

def commit
  get_commit_ref(branch)
end

#dirty?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/potamus/config.rb', line 38

def git?
  File.directory?(File.join(@root, '.git'))
end

#image_nameObject



26
27
28
# 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_commitObject



30
31
32
33
34
35
36
# 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_commandsObject



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/potamus/config.rb', line 97

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?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/potamus/config.rb', line 59

def pushed?
  commit == remote_commit
end

#remote_commitObject



55
56
57
# File 'lib/potamus/config.rb', line 55

def remote_commit
  get_commit_ref("#{remote_name}/#{branch}")
end

#remote_nameObject



18
19
20
# File 'lib/potamus/config.rb', line 18

def remote_name
  @options['remote_name'] || 'origin'
end

#tag_commandsObject



91
92
93
94
95
# File 'lib/potamus/config.rb', line 91

def tag_commands
  tags.each_with_object({}) do |tag, hash|
    hash[tag] = tag_command(tag)
  end
end

#tagsObject



82
83
84
85
86
87
88
89
# File 'lib/potamus/config.rb', line 82

def tags
  return [] if @test_mode

  array = []
  array << branch
  array << 'latest' if branch == branch_for_latest
  array
end

#test_mode!Object



110
111
112
# File 'lib/potamus/config.rb', line 110

def test_mode!
  @test_mode = true
end