require 'picsolve_docker_builder/base' require 'psych' require 'git' module PicsolveDockerBuilder module Helpers # Parses the config file class ConfigVersionUpdate include PicsolveDockerBuilder::Base 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 def initialize(path = nil) @path = path || config_file end 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 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 def git_remote_branch ENV['GIT_BRANCH'] || 'origin/develop' end def git_remote git_remote_branch.split('/')[0] end def git_branch git_remote_branch.split('/')[1] end def config_file unless File.file?(config_path) File.open(config_path, 'w') do |file| file.write('') end end File.new config_path end def config_path '.docker-builder.version.yml' end def git @git ||= Git.open('.') end def update_commit_push(*args) check_for_uncommitted update(*args) commit_and_push(*args) end 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 def write File.open(@path, 'w') do |file| file.write(Psych.dump(config)) end end def default {} end def config return @config unless @config.nil? read end 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 end end end