Last active
December 21, 2018 14:54
-
-
Save wolfoo2931/03d5089ea2c273ae16334ee520ebf781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/var/vcap/packages/ruby/bin/ruby | |
require 'tempfile' | |
class CleanerCommand | |
attr_reader :deployment_list_filename | |
def self.run!(deployment_list_filename) | |
new(deployment_list_filename).execute! | |
end | |
def initialize(deployment_list_filename) | |
@deployment_list_filename = deployment_list_filename | |
end | |
def execute! | |
parse_control_script | |
execute_rails_command | |
end | |
private | |
def parse_control_script | |
ctl_script_file = "/var/vcap/jobs/anynines-service-broker/config/export_env.sh" | |
multi_line = false | |
File.readlines(ctl_script_file).each do |line| | |
if multi_line || line.start_with?("export") | |
multi_line = true if line =~ /="/ && line !~ /=".*"/ | |
multi_line = false if multi_line && line !~ /="/ && line =~ /"/ | |
exports_file.write(line) | |
end | |
end | |
exports_file.flush | |
end | |
def exports_file | |
@exports_file ||= Tempfile.new("exports.sh") | |
end | |
def execute_rails_command | |
package_dir = "/var/vcap/packages/anynines-service-broker" | |
self_filename = File.basename(__FILE__) | |
self_dir = __dir__ | |
system(". #{exports_file.path} && cd #{package_dir} && bundle exec rails r \"require_relative '#{self_dir}/#{self_filename}'; Cleaner.run!('#{deployment_list_filename}')\"") | |
end | |
end | |
class Cleaner | |
attr_reader :deployment_list_filename | |
def self.run!(deployment_list_filename) | |
new(deployment_list_filename).execute! | |
end | |
def initialize(deployment_list_filename) | |
@deployment_list_filename = deployment_list_filename | |
end | |
def execute! | |
puts "Clean up instances: #{instance_names}" | |
Anynines::Instance.where(guid_at_tenant: instance_names).all.each do |instance| | |
delete_instance(instance) if instance.state != Anynines::Instance::STATE_DELETED | |
end | |
end | |
private | |
def instance_names | |
@instance_names ||= [].tap do |instance_names| | |
File.readlines(deployment_list_filename).each do |line| | |
instance_names << line.strip | |
end | |
end | |
end | |
def delete_instance(instance) | |
puts "Delete instance: ##{instance.id} #{instance.guid_at_tenant} (#{instance.deployment_name})" | |
old_deployment_strategy = instance.deployment_strategy | |
instance.update_column(:deployment_strategy, "none") | |
instance.state = Anynines::Instance::STATE_FAILED | |
instance.save! | |
if instance.metadata | |
simon.delete_provisioned_instance(args_for_simon(instance)) | |
else | |
instance.check_deletable! | |
instance.mark_as_deleted | |
end | |
instance.update_column(:deployment_strategy, old_deployment_strategy) | |
instance.save! | |
end | |
def simon | |
@simon ||= Anynines::Simon.new | |
end | |
def args_for_simon(instance) | |
{ | |
instance_guid_at_tenant: instance.guid_at_tenant, | |
service_guid: instance.metadata['service_guid'], | |
plan_guid: instance.metadata['plan_guid'], | |
tenant_id: Anynines::ApiAdapters::CloudfoundryApiAdapter::ANYNINES_TENANT_ID | |
} | |
end | |
end | |
if __FILE__ == $0 | |
raise "Please provide a list with instances to clean up." unless ARGV[0] | |
deployment_list_filename = File.expand_path(ARGV[0], __dir__) | |
CleanerCommand.run!(deployment_list_filename) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment