Class: GitlabJanitor::VolumeCleaner

Inherits:
BaseCleaner show all
Defined in:
lib/gitlab_janitor/volume_cleaner.rb

Defined Under Namespace

Classes: Model

Constant Summary collapse

SHA_RX =
/^[a-zA-Z0-9]{64}$/.freeze

Instance Attribute Summary collapse

Attributes inherited from BaseCleaner

#deadline, #delay, #logger

Instance Method Summary collapse

Methods inherited from BaseCleaner

#clean, #exiting?, #log_exception

Constructor Details

#initialize(includes: [''], **kwargs) ⇒ VolumeCleaner

Returns a new instance of VolumeCleaner.



36
37
38
39
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 36

def initialize(includes: [''], **kwargs)
  super(**kwargs)
  @includes = includes
end

Instance Attribute Details

#includesObject (readonly)

Returns the value of attribute includes.



34
35
36
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 34

def includes
  @includes
end

Instance Method Details

#do_clean(remove: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 41

def do_clean(remove: false)
  to_remove, keep = prepare(Docker::Volume.all.map{|m| Model.new(m) })

  return if to_remove.empty?

  keep.each {|m| logger.debug("  KEEP #{m.name}") }

  if remove
    logger.info 'Removing volumes...'
    to_remove.each do |model|
      return false if exiting?

      logger.tagged(model.name.first(10)) do
        logger.debug '   Removing...'
        log_exception('Remove') { model.remove }
        logger.debug '   Removing COMPLETED'
      end
    end
  else
    logger.info 'Skip removal due to dry run'
  end
end

#format_item(model) ⇒ Object



92
93
94
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 92

def format_item(model)
  "#{Time.parse(model.created_at)} Age:#{model.age_text.ljust(13)} #{model.name.first(10).ljust(10)} #{model.mountpoint}"
end

#prepare(volumes) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 64

def prepare(volumes)
  @logger.debug('Selecting unnamed volumes...')
  to_remove = select_unnamed(volumes)
  if to_remove.empty?
    @logger.info('Noting to remove.')
    return [], volumes
  end
  @logger.info("Selected volumes: \n#{to_remove.map{|c| "  + #{format_item(c)}" }.join("\n")}")

  @logger.debug("Selecting volumes by includes #{@includes}...")
  to_remove += select_by_name(volumes)
  if to_remove.empty?
    @logger.info('Noting to remove.')
    return [], images
  end
  @logger.info("Selected volumes: \n#{to_remove.map{|c| "  + #{format_item(c)}" }.join("\n")}")

  @logger.info("Filtering volumes by deadline: older than #{Fugit::Duration.parse(@deadline).deflate.to_plain_s}...")
  to_remove = select_by_deadline(to_remove)
  if to_remove.empty?
    @logger.info('Noting to remove.')
    return [], volumes
  end
  @logger.info("Filtered volumes: \n#{to_remove.map{|c| "  !! #{format_item(c)}" }.join("\n")}")

  [to_remove, (volumes - to_remove)]
end

#select_by_deadline(volumes) ⇒ Object



112
113
114
115
116
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 112

def select_by_deadline(volumes)
  volumes.select do |model|
    model.age > deadline
  end
end

#select_by_name(volumes) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 96

def select_by_name(volumes)
  volumes.select do |model|
    @includes.any? do |pattern|
      File.fnmatch(pattern, model.name)
    end
  end
end

#select_unnamed(volumes) ⇒ Object



106
107
108
109
110
# File 'lib/gitlab_janitor/volume_cleaner.rb', line 106

def select_unnamed(volumes)
  volumes.select do |model|
    SHA_RX.match(model.name)
  end
end