Class: ChatopsDeployer::Project
- Inherits:
-
Object
- Object
- ChatopsDeployer::Project
- Includes:
- Logger
- Defined in:
- lib/chatops_deployer/project.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#branch_directory ⇒ Object
readonly
Returns the value of attribute branch_directory.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#env ⇒ Object
Returns the value of attribute env.
-
#sha1 ⇒ Object
readonly
Returns the value of attribute sha1.
Instance Method Summary collapse
- #copy_files_from_deployer ⇒ Object
- #fetch_repo ⇒ Object
-
#initialize(repository, branch, config_file = "chatops_deployer.yml") ⇒ Project
constructor
A new instance of Project.
- #read_config ⇒ Object
- #setup_cache_directories ⇒ Object
- #update_cache ⇒ Object
Methods included from Logger
Constructor Details
#initialize(repository, branch, config_file = "chatops_deployer.yml") ⇒ Project
Returns a new instance of Project.
17 18 19 20 21 22 23 24 |
# File 'lib/chatops_deployer/project.rb', line 17 def initialize(repository, branch, config_file="chatops_deployer.yml") @sha1 = Digest::SHA1.hexdigest(repository + branch) @repository = repository @branch = branch @config_file = config_file @env = {} setup_project_directory end |
Instance Attribute Details
#branch_directory ⇒ Object (readonly)
Returns the value of attribute branch_directory.
15 16 17 |
# File 'lib/chatops_deployer/project.rb', line 15 def branch_directory @branch_directory end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
15 16 17 |
# File 'lib/chatops_deployer/project.rb', line 15 def config @config end |
#env ⇒ Object
Returns the value of attribute env.
16 17 18 |
# File 'lib/chatops_deployer/project.rb', line 16 def env @env end |
#sha1 ⇒ Object (readonly)
Returns the value of attribute sha1.
15 16 17 |
# File 'lib/chatops_deployer/project.rb', line 15 def sha1 @sha1 end |
Instance Method Details
#copy_files_from_deployer ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chatops_deployer/project.rb', line 51 def copy_files_from_deployer copy_list = @config['copy'].to_a return if copy_list.empty? logger.info "Copying files from deployer to project" copy_list.each do |copy_string| source, destination = copy_string.split(':') # source is from COPY_SOURCE_DIR if source doesn't start with ./ source = File.join(COPY_SOURCE_DIR, source) unless source.match(/^\.\//) if File.extname(source) == '.erb' destination ||= File.basename(source, '.erb') logger.info "Processing ERB template #{source} into #{destination}" Template.new(source).inject(@env).write(destination) else destination ||= File.basename source logger.info "Copying #{source} into #{destination}" FileUtils.cp source, destination end end end |
#fetch_repo ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/chatops_deployer/project.rb', line 26 def fetch_repo logger.info "Fetching #{@repository}:#{@branch}" unless Dir.entries('.').size == 2 logger.info "Branch already cloned. Deleting everything before cloning again" FileUtils.rm_rf '.' end logger.info "Cloning branch #{@repository}:#{@branch}" git_clone = Command.run(command: ['git', 'clone', "--branch=#{@branch}", '--depth=1', @repository, '.'], logger: logger) unless git_clone.success? raise_error("Cannot clone git repository: #{@repository}, branch: #{@branch}") end end |
#read_config ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/chatops_deployer/project.rb', line 39 def read_config @config = if File.exists?(@config_file) begin YAML.load_file(@config_file) || {} rescue StandardError => e raise_error("Cannot parse YAML content in #{@config_file}") end else {} end end |
#setup_cache_directories ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/chatops_deployer/project.rb', line 71 def setup_cache_directories cache_directory_list = @config['cache'].to_h cache_directory_list.each do |directory, _| cache_dir = File.join(@common_cache_dir, directory) target_cache_dir = File.join(@branch_directory, directory) FileUtils.mkdir_p cache_dir FileUtils.mkdir_p target_cache_dir FileUtils.rm_rf target_cache_dir FileUtils.cp_r(cache_dir, target_cache_dir) end end |
#update_cache ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/chatops_deployer/project.rb', line 83 def update_cache cache_directory_list = @config['cache'].to_h cache_directory_list.each do |directory, service_paths| service_paths.each do |service, path| ps = Command.run(command: ["docker-compose", "ps", "-q", service], logger: logger) container = ps.output.chomp next if container.empty? cache_dir = File.join(@common_cache_dir, directory) tmp_dir = File.join(@project_directory, 'tmp_cache') copy_from_container = Command.run(command: ["docker", "cp", "#{container}:#{path}", tmp_dir], logger: logger) raise_error("Cannot copy '#{path}' from container '#{container}' of service '#{service}'") unless copy_from_container.success? FileUtils.rm_rf(cache_dir) FileUtils.mv(tmp_dir, cache_dir) FileUtils.rm_rf(tmp_dir) end end end |