Class: PicsolveDockerBuilder::Helpers::ConfigVersionUpdate

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/picsolve_docker_builder/helpers/config_version_update.rb

Overview

Parses the config file

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#base_dir, #config_paths, #create_logger, #default_config, #log, #read_config, #validate_config

Constructor Details

#initialize(path = nil) ⇒ ConfigVersionUpdate

Returns a new instance of ConfigVersionUpdate.



30
31
32
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 30

def initialize(path = nil)
  @path = path || config_file
end

Class Method Details

.update_taskObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 11

def self.update_task
  log = Logger.new(STDOUT)

  if ENV['service_name'].nil? || ENV['service_tag'].nil?
    log.info 'No update needed as environment variables service_name '\
      'and service_tag are not in place'

    return
  end

  cvu = new
  cvu.update_commit_push(
    ENV['service_name'],
    ENV['service_tag'],
    ENV['service_commit'],
    ENV['service_url']
  )
end

Instance Method Details

#check_for_uncommittedObject



34
35
36
37
38
39
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 34

def check_for_uncommitted
  return unless git.status.changed.keys.include? config_path

  log.error "Uncommited changes to '#{config_path}'. Can't continue ..."
  exit(1)
end

#commit_and_push(container_name, tag, git_commit, git_url) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 41

def commit_and_push(
  container_name,
  tag,
  git_commit,
  git_url
)
  git.add config_path
  message = "Upgrades version of '#{container_name}'"\
    " to tag '#{tag}'"

  message += "\n  git_commit=#{git_commit}" if git_commit
  message += "\n  git_url=#{git_url}" if git_url
  git.commit message
  git.push(git_remote, git_branch)
rescue Git::GitExecuteError => exception
  log.warn "Problem during commiting: #{exception}"
end

#configObject



129
130
131
132
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 129

def config
  return @config unless @config.nil?
  read
end

#config_fileObject



71
72
73
74
75
76
77
78
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 71

def config_file
  unless File.file?(config_path)
    File.open(config_path, 'w') do |file|
      file.write('')
    end
  end
  File.new config_path
end

#config_pathObject



80
81
82
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 80

def config_path
  '.docker-builder.version.yml'
end

#defaultObject



125
126
127
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 125

def default
  {}
end

#gitObject



84
85
86
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 84

def git
  @git ||= Git.open('.')
end

#git_branchObject



67
68
69
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 67

def git_branch
  git_remote_branch.split('/')[1]
end

#git_remoteObject



63
64
65
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 63

def git_remote
  git_remote_branch.split('/')[0]
end

#git_remote_branchObject



59
60
61
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 59

def git_remote_branch
  ENV['GIT_BRANCH'] || 'origin/develop'
end

#readObject



134
135
136
137
138
139
140
141
142
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 134

def read
  @config = default
  begin
    yaml = Psych.load_file @path
    @config = @config.deep_merge(yaml) if yaml
  rescue Errno::ENOENT
    raise "cannot find config at '#{path}'"
  end
end

#update(container_name, tag, git_commit, git_url) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 94

def update(
  container_name,
  tag,
  git_commit,
  git_url
)
  read
  config = {
    'compose' => {
      'containers' => {
        container_name => {
          'tag' => tag
        }
      }
    }
  }
  c = config['compose']['containers'][container_name]

  c['git_commit'] = git_commit unless git_commit.nil?
  c['git_url'] = git_url unless git_url.nil?

  @config = @config.deep_merge(config)
  write
end

#update_commit_push(*args) ⇒ Object



88
89
90
91
92
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 88

def update_commit_push(*args)
  check_for_uncommitted
  update(*args)
  commit_and_push(*args)
end

#writeObject



119
120
121
122
123
# File 'lib/picsolve_docker_builder/helpers/config_version_update.rb', line 119

def write
  File.open(@path, 'w') do |file|
    file.write(Psych.dump(config))
  end
end