cloudamatic/mu

View on GitHub
bin/mu-cleanup

Summary

Maintainability
Test Coverage
#!/usr/local/ruby-current/bin/ruby
#
# Copyright:: Copyright (c) 2014 eGlobalTech, Inc., all rights reserved
#
# Licensed under the BSD-3 license (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the root of the project or at
#
#     http://egt-labs.com/mu/LICENSE.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require File.realpath(File.expand_path(File.dirname(__FILE__)+"/mu-load-config.rb"))
# now we have our global config available as the read-only hash $MU_CFG

require 'rubygems'
require 'bundler/setup'
require 'optimist'
require 'mu'
Dir.chdir(MU.installDir)

credentials = []
MU::Cloud.availableClouds.each { |cloud|
  credentials.concat(MU::Cloud.cloudClass(cloud).listCredentials)
}
credentials.uniq!

$opts = Optimist::options do
  banner <<-EOS
Usage:
#{$0} [-i] [-n] [-s] [-o] [-w] [-v] <deploy_id>
  EOS
  opt :ignoremaster, "Ignore the MU-MASTER-IP tag when identifying resources to terminate. Caution- this can result in accidentally terminating someone else's resources.", :default => false, :type => :boolean
  opt :noop, "Don't actually do anything, just show what we would do. Equivalent to specifying -o and -k together.", :default => false, :type => :boolean
  opt :skipsnapshots, "Skip creation of final snapshots for EBS volumes and RDS instances.", :default => false, :type => :boolean
  opt :onlycloud, "Only purge cloud resources, and skip all Mu master deployment metadata cleanup.", :require => false, :default => false, :type => :boolean
  opt :skipcloud, "Only purge Mu master deployment metadata, and skip all cloud resources.", :require => false, :default => false, :type => :boolean
  opt :web, "Generate web-friendly (HTML) output.", :require => false, :default => false, :type => :boolean
  opt :verbose, "Display debugging output.", :require => false, :default => false, :type => :boolean
  opt :credentials, "Restrict to operating on a subset of available credential sets, instead of all that we know about.", :require => false, :default => credentials, :type => :strings
  opt :regions, "Restrict to operating on a subset of available regions, instead of all that we know about.", :require => false, :type => :strings
  opt :habitats, "Restrict to operating on the named accounts/projects/subscriptions, instead of search all habitats visible to our credentials.", :required => false, :type => :strings
  opt :quiet, "Display minimal output.", :require => false, :default => false, :type => :boolean
end
verbosity = MU::Logger::NORMAL
verbosity = MU::Logger::LOUD if $opts[:verbose]
verbosity = MU::Logger::QUIET if $opts[:quiet]

if $opts[:verbose] and $opts[:quiet]
  MU.log "Cannot set both --verbose and --quiet", MU::ERR
  exit 1
end

MU.setLogging(verbosity, $opts[:web])

if (!ARGV[0] or ARGV[0].empty?) and !$opts[:deploy]
  MU.log "Must specify a deploy id. Visible deploys:", MU::WARN
  puts MU::MommaCat.listDeploys.sort.join("\n")
  exit 1
else
  $opts[:deploy] = ARGV[0]
end


MU::Cleanup.run(
  $opts[:deploy],
  noop: $opts[:noop],
  skipsnapshots: $opts[:skipsnapshots],
  onlycloud: $opts[:onlycloud],
  verbosity: verbosity,
  web: $opts[:web],
  skipcloud: $opts[:skipcloud],
  ignoremaster: $opts[:ignoremaster],
  credsets: $opts[:credentials],
  regions: $opts[:regions],
  habitats: $opts[:habitats]
)