Skip to content

Commit

Permalink
task(bosh_delete_*): enhance log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
o-orand committed Jan 8, 2020
1 parent 0c46982 commit b0cc6c2
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 15 deletions.
4 changes: 3 additions & 1 deletion concourse/tasks/bosh_delete_apply/delete_apply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ def initialize(list_command = Tasks::Bosh::ListDeployments.new, delete_command =

def process
expected_deployments = @config_repo_deployments.enabled_deployments
puts "Expected deployments: #{expected_deployments}"
protected_deployments = @config_repo_deployments.protected_deployments
puts "Protected deployments: #{protected_deployments}"

deployed_bosh_deployments = @list_command_holder.execute
puts "Active bosh deployments: #{deployed_bosh_deployments}"
puts "Filtering deployments (ie: removing expected and protected deployments)"
puts "Filtering deployments (ie: excluding expected and protected deployments)"
deployed_bosh_deployments.delete_if { |deployment_name| expected_deployments&.include?(deployment_name) || protected_deployments&.include?(deployment_name) }

puts "Deployments to delete: #{deployed_bosh_deployments}"
Expand Down
7 changes: 4 additions & 3 deletions concourse/tasks/bosh_delete_plan/delete_plan.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'json'


# Monkey patch String class to write using a few colors
class String
def black
"\e[30m#{self}\e[0m"
Expand All @@ -26,11 +26,13 @@ def process
deployments_file = ENV.fetch('OUTPUT_FILE', File.join('deployments-to-delete', 'file.txt'))

expected_deployments = @config_repo_deployments.enabled_deployments
puts "Expected deployments detected: #{expected_deployments}"
protected_deployments = @config_repo_deployments.protected_deployments
puts "Protected deployments detected: #{protected_deployments}"

deployed_bosh_deployments = @list_command_holder.execute
puts "Active bosh deployments: #{deployed_bosh_deployments}"
puts "Filtering deployments (ie: removing expected and protected deployments)"
puts "Filtering deployments (ie: excluding expected and protected deployments)"
deployed_bosh_deployments.delete_if { |deployment_name| expected_deployments&.include?(deployment_name) || protected_deployments&.include?(deployment_name) }

deployed_bosh_deployments.each do |name|
Expand All @@ -52,7 +54,6 @@ def display_inactive_message(name)
"\t - secrets does not enable this deployment\n" \
"\t - deployment secrets dir does not contain 'protect-deployment.yml' mark to skip deletion\n" \
"\tThis bosh deployment is going to be deleted on bosh, and files removed in secrets ('#{name}.yml', '#{name}-fingerprint.yml' and '#{name}-last-deployment-failure.yml').\n" \
"\tOtherwise deletion is run on an unknown deployment.\n" \
"\t! Waiting for manual approval !\n" \
''
end
Expand Down
34 changes: 25 additions & 9 deletions lib/tasks/config_repo/deployments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Tasks
module ConfigRepo
# ease config-repository manipulations
class Deployments
SPECIAL_DIRECTORIES = %w[terraform-config secrets cf-apps-deployments].freeze

def initialize(config_repo_name = 'config-resource')
root_deployment_name = ENV.fetch('ROOT_DEPLOYMENT_NAME', '')
raise Tasks::Bosh::EnvVarMissing, "missing environment variable: ROOT_DEPLOYMENT_NAME" if root_deployment_name.to_s.empty?
Expand All @@ -13,9 +15,7 @@ def initialize(config_repo_name = 'config-resource')

def filter_deployments(marker_filename)
deployment_files = Dir[File.join(@config_repo_name, @root_deployment_name, '**', marker_filename)]
deployments = deployment_files.map { |filename| File.dirname(filename)&.split("/")&.last }&.sort
puts "Selected deployments (matching #{marker_filename}: #{deployments}"
deployments
deployment_files.map { |filename| File.dirname(filename)&.split("/")&.last }&.sort
end

def protected_deployments
Expand All @@ -30,23 +30,24 @@ def bosh_deployments
deployments = []
Dir.each_child(@root_deployment_dir) do |deployment_dirname|
manifest_dir = File.join(@root_deployment_dir, deployment_dirname)
deployments << deployment_dirname if deployment?(manifest_dir, deployment_dirname)
deployments << deployment_dirname if self.class.deployment?(manifest_dir, deployment_dirname)
end
deployments.sort
end

def deployment?(manifest_dir, name)
return false if name == 'secrets'
def self.deployment?(manifest_dir, name)
return false if SPECIAL_DIRECTORIES.include?(name)

manifest_path = File.join(manifest_dir, name + '.yml')
manifest_failure_path = File.join(manifest_dir, name + '-last-deployment-failure.yml')
File.exist?(manifest_path) || File.exist?(manifest_failure_path)
enable_deployment_path = File.join(manifest_dir, 'enable-deployment.yml')
File.exist?(manifest_path) || File.exist?(manifest_failure_path) || File.exist?(enable_deployment_path)
end

def cleanup_disabled_deployments
puts "Cleanup deployments directory in config repository"
deployments_to_cleanup = disabled_deployments
deployments_to_cleanup.each { |deployment_name| puts cleanup_deployment(deployment_name) }
deployments_to_cleanup.each { |deployment_name| cleanup_deployment(deployment_name) }
deployments_to_cleanup
end

Expand All @@ -57,14 +58,29 @@ def cleanup_deployment(deployment_name)
full_path = File.join(base_path, filename)
File.delete(full_path) if File.exist?(full_path)
end
Dir.delete(base_path) if Dir.exist?(base_path) && Dir.empty?(base_path)
delete_empty_directory(base_path, deployment_name)
end

def disabled_deployments
expected_deployments_list = enabled_deployments
protected_deployments_list = protected_deployments
bosh_deployments.delete_if { |deployment_name| expected_deployments_list&.include?(deployment_name) || protected_deployments_list&.include?(deployment_name) }
end

private

def delete_empty_directory(base_path, deployment_name)
full_cleanup = false

if Dir.exist?(base_path) && Dir.empty?(base_path)
puts "Deleting #{deployment_name} directory as it is empty"
Dir.delete(base_path)
full_cleanup = true
else
puts "Skipping #{deployment_name} removal, directory not empty"
end
full_cleanup
end
end
end
end
Loading

0 comments on commit b0cc6c2

Please sign in to comment.