class Oneds::Mrproper::Cleaners::ImageCleaner

Attributes

datastore_manager[R]
image_manager[R]

Public Class Methods

new(client, dry_run) click to toggle source
Calls superclass method Oneds::Mrproper::Cleaners::Cleaner::new
# File lib/oneds/mrproper/cleaners/image_cleaner.rb, line 7
def initialize(client, dry_run)
  super(dry_run)

  @datastore_manager = Oneds::Mrproper::One::Managers::DatastoreManager.new client
  @image_manager = Oneds::Mrproper::One::Managers::ImageManager.new client
end

Public Instance Methods

clean() click to toggle source
# File lib/oneds/mrproper/cleaners/image_cleaner.rb, line 14
def clean
  logger.info 'Cleaning image datastores'
  datastores = datastore_manager.image_datastores
  datastores.each { |datastore| clean_datastore datastore }
end
clean_datastore(datastore) click to toggle source
# File lib/oneds/mrproper/cleaners/image_cleaner.rb, line 20
def clean_datastore(datastore)
  ds_path = datastore['BASE_PATH']
  logger.info "Cleaning datastore #{datastore.name.inspect} with path #{ds_path.inspect}"

  unless File.exist? ds_path
    logger.warn "Path #{ds_path.inspect} not found on headnode, skipping"
    return
  end

  fs_files = Dir[File.join(ds_path, '*')].reject { |name| name =~ /^.*\.snap$/ }.sort
  logger.debug "Files available on filesystem: #{fs_files.inspect}"
  ds_files = datastore.img_ids.map { |id| image_manager.image_file id }.compact.sort
  logger.debug "Files registered in OpenNebula: #{ds_files.inspect}"

  files_to_remove = fs_files - ds_files
  logger.debug "Files that will be removed: #{files_to_remove.inspect}"

  remove_files files_to_remove
end

Private Instance Methods

remove_files(files) click to toggle source
# File lib/oneds/mrproper/cleaners/image_cleaner.rb, line 42
def remove_files(files)
  files.each do |file|
    logger.info "Removing file #{file.inspect}"
    File.delete file unless dry_run
    snap_dir = "#{file}.snap"
    if File.exist? snap_dir
      logger.info "Removing snapshot directory #{snap_dir.inspect}"
      FileUtils.rm_r snap_dir unless dry_run
    end
  end
end