Skip to content

Commit

Permalink
units
Browse files Browse the repository at this point in the history
  • Loading branch information
kinnalru committed Aug 2, 2022
1 parent a8964db commit ea016d4
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 240 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Metrics/BlockLength:
- "spec/**/*.rb"

Metrics/LineLength:
Max: 100
Max: 120
IgnoredPatterns: ['(\A|\s)#']

Metrics/AbcSize:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN set -ex \
&& apk add --no-cache docker-cli

ADD Gemfile Gemfile.lock gitlab-janitor.gemspec /home/app/
ADD lib/gitlab-janitor/version.rb /home/app/lib/gitlab-janitor/
ADD lib/gitlab-_anitor/version.rb /home/app/lib/gitlab_janitor/

RUN set -ex \
&& gem install bundler && gem update bundler \
Expand Down
3 changes: 1 addition & 2 deletions bin/gitlab-janitor
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# require 'bundler/setup'
# Bundler.require(:default)

require 'active_support/all'
require 'docker-api'
require 'fugit'
require 'optparse'
Expand Down Expand Up @@ -101,7 +100,7 @@ images = GitlabJanitor::ImageCleaner.new(
deadline: Fugit::Duration.parse(@opts[:image_deadline]).to_sec
)

until $exiting
until GitlabJanitor.exiting?
containers.clean(remove: @opts[:remove])
volumes.clean(remove: @opts[:remove])
images.clean(remove: @opts[:remove])
Expand Down
2 changes: 1 addition & 1 deletion gitlab-janitor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

# Maintain your gem's version:
require 'gitlab-janitor/version'
require 'gitlab_janitor/version'

Gem::Specification.new 'gitlab-janitor' do |spec|
spec.version = ENV['BUILDVERSION'].to_i > 0 ? "#{Lusnoc::VERSION}.#{ENV['BUILDVERSION'].to_i}" : GitlabJanitor::VERSION
Expand Down
7 changes: 1 addition & 6 deletions lib/gitlab-janitor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
require_relative 'gitlab-janitor/version'
require_relative 'gitlab-janitor/utils'
require_relative 'gitlab-janitor/base-cleaner'
require_relative 'gitlab-janitor/container-cleaner'
require_relative 'gitlab-janitor/volume-cleaner'
require_relative 'gitlab-janitor/image-cleaner'
require_relative './gitlab_janitor'

153 changes: 0 additions & 153 deletions lib/gitlab-janitor/image-cleaner.rb

This file was deleted.

7 changes: 7 additions & 0 deletions lib/gitlab_janitor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require_relative 'gitlab_janitor/version'
require_relative 'gitlab_janitor/utils'
require_relative 'gitlab_janitor/base_cleaner'
require_relative 'gitlab_janitor/container_cleaner'
require_relative 'gitlab_janitor/volume_cleaner'
require_relative 'gitlab_janitor/image_cleaner'

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Model

attr_reader :model

def initialize(m)
@model = m
def initialize(model)
@model = model
end

def method_missing(method, *args, &block)
Expand Down Expand Up @@ -34,9 +34,7 @@ def initialize(delay: 10, deadline: 1.second, logger: GitlabJanitor::Util.logger
end

def clean(remove: false)
if @cleaned_at && (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - @cleaned_at) < @delay.seconds
return nil
end
return nil if @cleaned_at && (::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - @cleaned_at) < @delay.seconds

do_clean(remove: remove)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class ContainerCleaner < BaseCleaner

class Model < BaseCleaner::Model

def initialize(v)
super(v)
def initialize(model)
super(model)

info['_Age'] = (Time.now - Time.at(created_at)).round(0)
end
Expand All @@ -14,13 +14,17 @@ def created_at
end

def name
@anme ||= info['Names'].first.sub(%r{^/}, '')
@name ||= info['Names'].first.sub(%r{^/}, '')
end

def age
info['_Age']
end

def age_text
Fugit::Duration.parse(age).deflate.to_plain_s
end

end

attr_reader :excludes, :includes
Expand All @@ -35,25 +39,23 @@ def initialize(includes: [''], excludes: [''], **args)
def do_clean(remove: false)
to_remove, keep = prepare(Docker::Container.all(all: true).map{|m| Model.new(m) })

unless to_remove.empty?
keep.each do |c|
logger.debug(" KEEP #{c.name}")
end
return if to_remove.empty?

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

if remove
logger.info 'Removing containers...'
to_remove.each do |c|
logger.tagged(c.name) do
logger.debug ' Removing...'
log_exception('Stop') { c.stop }
log_exception('Wait') { c.wait(15) }
log_exception('Remove') { c.remove }
logger.debug ' Removing COMPLETED'
end
if remove
logger.info 'Removing containers...'
to_remove.each do |c|
logger.tagged(c.name) do
logger.debug ' Removing...'
log_exception('Stop') { c.stop }
log_exception('Wait') { c.wait(15) }
log_exception('Remove') { c.remove }
logger.debug ' Removing COMPLETED'
end
else
logger.info 'Skip removal due to dry run'
end
else
logger.info 'Skip removal due to dry run'
end
end

Expand Down Expand Up @@ -85,29 +87,29 @@ def prepare(containers)
[to_remove, containers - to_remove]
end

def format_item(c)
"#{Time.at(c.created_at)} Age:#{Fugit::Duration.parse(c.age).deflate.to_plain_s.ljust(10)} #{c.name.first(60).ljust(60)}"
def format_item(model)
"#{Time.at(model.created_at)} Age:#{model.age_text.ljust(10)} #{model.name.first(60).ljust(60)}"
end

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

def reject_by_name(containers)
containers.reject do |c|
containers.reject do |model|
@excludes.any? do |pattern|
File.fnmatch(pattern, c.name)
File.fnmatch(pattern, model.name)
end
end
end

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

Expand Down
Loading

0 comments on commit ea016d4

Please sign in to comment.