Skip to content

Commit

Permalink
rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
kinnalru committed Aug 1, 2022
1 parent badc1af commit c3ac95d
Show file tree
Hide file tree
Showing 12 changed files with 269 additions and 90 deletions.
6 changes: 2 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AllCops:
Exclude:
- "storage/**/*"

Layout/TrailingBlankLines:
Layout/TrailingEmptyLines:
Enabled: true
AutoCorrect: true
EnforcedStyle: final_blank_line
Expand Down Expand Up @@ -32,7 +32,7 @@ Layout/EndAlignment:
EnforcedStyleAlignWith: variable
AutoCorrect: true

Layout/AlignHash:
Layout/HashAlignment:
EnforcedColonStyle: table
EnforcedHashRocketStyle: table

Expand Down Expand Up @@ -106,8 +106,6 @@ Style/RaiseArgs:
Style/SpecialGlobalVars:
Enabled: false

Style/BracesAroundHashParameters:
EnforcedStyle: context_dependent

Style/NumericPredicate:
Enabled: false
Expand Down
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ source 'https://rubygems.org'

gemspec




3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
docker-api
fugit
optparse
tzinfo-data

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -66,6 +67,8 @@ GEM
unicode-display_width (>= 1.1.1, < 3)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2022.1)
tzinfo (>= 1.0.0)
unicode-display_width (2.2.0)
zeitwerk (2.6.0)

Expand Down
59 changes: 34 additions & 25 deletions bin/gitlab-janitor
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env ruby

#cwd = __dir__
#$root = "#{cwd}/"
#$: << $root
# cwd = __dir__
# $root = "#{cwd}/"
# $: << $root

#lib = File.expand_path('lib', __dir__)
#$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
# lib = File.expand_path('lib', __dir__)
# $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)


#require 'rubygems'
#require 'bundler'
#require 'bundler/setup'
#Bundler.require(:default)
# require 'rubygems'
# require 'bundler'
# require 'bundler/setup'
# Bundler.require(:default)

require 'active_support/all'
require 'docker-api'
Expand All @@ -38,67 +38,76 @@ GitlabJanitor::Util.setup
parser = OptionParser.new do |o|
o.banner = "Usage: #{UTIL} [options] "

o.on("--clean-delay=#{@opts[:clean_delay]}", 'Delay between clean operation ENV[CLEAN_DELAY]') do |pattern|
o.on("--clean-delay=#{@opts[:clean_delay]}",
'Delay between clean operation ENV[CLEAN_DELAY]') do |pattern|
@opts[:clean_delay] = pattern.strip
end

o.on("--include=#{@opts[:includes].join(',')}", '<List> Include container for removal. ENV[INCLUDE]') do |pattern|
o.on("--include=#{@opts[:includes].join(',')}",
'<List> Include container for removal. ENV[INCLUDE]') do |pattern|
@opts[:includes] += pattern.split(/[,;]/)
end

o.on("--exclude=#{@opts[:excludes].join(',')}", '<List> Exclude container from removal by name. ENV[EXCLUDE]') do |pattern|
o.on("--exclude=#{@opts[:excludes].join(',')}",
'<List> Exclude container from removal by name. ENV[EXCLUDE]') do |pattern|
@opts[:excludes] += pattern.split(/[,;]/)
end

o.on("--container-deadline=#{@opts[:container_deadline]}", 'Maximum container run duration. ENV[CONTAINER_DEADLINE]') do |pattern|
o.on("--container-deadline=#{@opts[:container_deadline]}",
'Maximum container run duration. ENV[CONTAINER_DEADLINE]') do |pattern|
@opts[:container_deadline] = pattern.strip
end

o.on("--volume-deadline=#{@opts[:volume_deadline]}", 'Maximum volume life dudation. ENV[VOLUME_DEADLINE]') do |pattern|
o.on("--volume-deadline=#{@opts[:volume_deadline]}",
'Maximum volume life dudation. ENV[VOLUME_DEADLINE]') do |pattern|
@opts[:volume_deadline] = pattern.strip
end

o.on("--image-deadline=#{@opts[:image_deadline]}", 'Maximum image life duration. ENV[IMAGE_DEADLINE]') do |pattern|
o.on("--image-deadline=#{@opts[:image_deadline]}",
'Maximum image life duration. ENV[IMAGE_DEADLINE]') do |pattern|
@opts[:image_deadline] = pattern.strip
end

o.on("--remove", 'Real remove instead of dry run. ENV[REMOVE]') do |value|
o.on('--remove', 'Real remove instead of dry run. ENV[REMOVE]') do |value|
@opts[:remove] = value.strip.to_bool
end

o.on("--docker=#{@opts[:docker_host]}", 'Docker api endpoint. ENV[DOCKER_HOST]') do |url|
o.on("--docker=#{@opts[:docker_host]}", 'Docker api endpoint. ENV[DOCKER_HOST]') do |_url|
@opts[:docker_host] = value.strip
end

end
parser.parse!

Docker.url = @opts[:docker_host]

GitlabJanitor::Util::logger.debug do
GitlabJanitor::Util.logger.debug do
"Config: #{JSON.pretty_generate(@opts)}"
end

containers = GitlabJanitor::ContainerCleaner.new(
delay: Fugit::Duration.parse(@opts[:clean_delay]).to_sec,
delay: Fugit::Duration.parse(@opts[:clean_delay]).to_sec,
includes: @opts[:includes],
excludes: @opts[:excludes],
deadline: Fugit::Duration.parse(@opts[:container_deadline]).to_sec
)

volumes = GitlabJanitor::VolumeCleaner.new(
delay: Fugit::Duration.parse(@opts[:clean_delay]).to_sec,
delay: Fugit::Duration.parse(@opts[:clean_delay]).to_sec,
deadline: Fugit::Duration.parse(@opts[:volume_deadline]).to_sec
)

images = GitlabJanitor::ImageCleaner.new(
delay: Fugit::Duration.parse(@opts[:clean_delay]).to_sec,
deadline: Fugit::Duration.parse(@opts[:image_deadline]).to_sec
)

images.clean(remove: @opts[:remove])
exit 1

while !$exiting do
until $exiting
containers.clean(remove: @opts[:remove])
volumes.clean(remove: @opts[:remove])

sleep 3
end




7 changes: 4 additions & 3 deletions gitlab-janitor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@ Gem::Specification.new 'gitlab-janitor' do |spec|
spec.homepage = 'https://github.com/RnD-Soft/gitlab-janitor'
spec.license = 'MIT'

spec.files = Dir['bin/**/*', 'lib/**/*', 'Gemfile*', 'LICENSE', 'README.md', 'Dockerfile*', 'docker-compose.yml', '*.gemspec']
spec.files = Dir['bin/**/*', 'lib/**/*', 'Gemfile*', 'LICENSE', 'README.md',
'Dockerfile*', 'docker-compose.yml', '*.gemspec']
spec.bindir = 'bin'
spec.executables = spec.files.grep(%r{^bin/gitlab-janitor}) {|f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_development_dependency 'awesome_print'
spec.add_development_dependency 'bundler', '~> 2.0', '>= 2.0.1'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'rspec_junit_formatter'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'simplecov-console'
spec.add_development_dependency 'awesome_print'

spec.add_runtime_dependency 'tzinfo-data'
spec.add_runtime_dependency 'activesupport', '~> 6.0'
spec.add_runtime_dependency 'docker-api'
spec.add_runtime_dependency 'fugit'
spec.add_runtime_dependency 'optparse'
spec.add_runtime_dependency 'tzinfo-data'
end

4 changes: 3 additions & 1 deletion lib/gitlab-janitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
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/volume-cleaner'
require_relative 'gitlab-janitor/image-cleaner'

14 changes: 10 additions & 4 deletions lib/gitlab-janitor/base-cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module GitlabJanitor
class BaseCleaner

class Model

attr_reader :model

def initialize(m)
@model = m
end
Expand All @@ -20,23 +22,26 @@ def respond_to?(*args)
def respond_to_missing?(method_name, include_private = false)
model.send(:respond_to_missing?, method_name, include_private) || super
end

end

attr_reader :delay, :deadline, :logger

def initialize delay: 10, deadline: 1.second, logger: GitlabJanitor::Util::logger, **args
def initialize(delay: 10, deadline: 1.second, logger: GitlabJanitor::Util.logger, **_args)
@delay = delay
@deadline = deadline
@logger = logger.tagged(self.class.to_s)
end

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

do_clean(remove: remove)

@cleaned_at = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
return true
true
end

def log_exception(text)
Expand All @@ -47,4 +52,5 @@ def log_exception(text)
end

end
end
end

50 changes: 26 additions & 24 deletions lib/gitlab-janitor/container-cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GitlabJanitor
class ContainerCleaner < BaseCleaner

class Model < BaseCleaner::Model

def initialize(v)
super(v)

Expand All @@ -13,102 +14,103 @@ def created_at
end

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

def age
info['_Age']
end

end

attr_reader :excludes, :includes

def initialize includes: [''], excludes: [''], **args
def initialize(includes: [''], excludes: [''], **args)
super(**args)
@includes = includes
@excludes = excludes
@deadline = deadline
end

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

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

if remove
logger.info "Removing containers..."
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"
logger.debug ' Removing...'
log_exception('Stop') { c.stop }
log_exception('Wait') { c.wait(15) }
log_exception('Remove') { c.remove }
logger.debug ' Removing COMPLETED'
end
end
else
logger.info "Skip removal due to dry run"
logger.info 'Skip removal due to dry run'
end
end
end


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

@logger.debug("Filtering containers by excludes #{@excludes}...")
to_remove = reject_by_name(to_remove)
if to_remove.empty?
@logger.info("Noting to remove.")
@logger.info('Noting to remove.')
return [], containers
end
@logger.info("Filtered containers: \n#{to_remove.map{|c| " + #{format_item(c)}"}.join("\n")}")
@logger.info("Filtered containers: \n#{to_remove.map{|c| " + #{format_item(c)}" }.join("\n")}")

@logger.debug("Filtering containers 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.")
@logger.info('Noting to remove.')
return [], containers
end
@logger.info("Filtered containers: \n#{to_remove.map{|c| " + #{format_item(c)}"}.join("\n")}")
@logger.info("Filtered containers: \n#{to_remove.map{|c| " + #{format_item(c)}" }.join("\n")}")

[to_remove, containers - to_remove]
end

def format_item c
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)}"
end

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

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

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

end
end
end

Loading

0 comments on commit c3ac95d

Please sign in to comment.