Module: Docklean::Containers
- Included in:
- Docklean
- Defined in:
- lib/docklean/containers.rb
Instance Method Summary collapse
Instance Method Details
#cleanup_containers ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/docklean/containers.rb', line 38 def cleanup_containers() @images_to_keep = Array.new() self.get_containers() @containers.each do |container,| case [:is_running].chomp when 'false' # check it's older than MAX_AGE if [:age] >= @max_age then # only remove container if it doesn't match keep_filter, or if keep_filter is empty if ! [:name].match(%r{#{self.get_conf('keep_filter')}}) or self.get_conf('keep_filter').to_s.empty?() then %x{#{@docker_bin} rm #{container.to_s.chomp}} puts "info: deleting #{container.to_s.chomp} (container)" else # do not delete this image_id @images_to_keep.push([:image_id]) end else # do not delete this image_id @images_to_keep.push([:image_id]) puts "info: #{container} (container) not old enough, do not delete #{[:image_id]} (image)" end when 'true' # do not delete this image_id @images_to_keep.push([:image_id]) puts "info: #{[:image_id]} (image) in use by #{container} (container) do not delete" end end end |
#get_containers ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/docklean/containers.rb', line 21 def get_containers() @containers = Hash.new() %x{#{@docker_bin} ps --no-trunc=true -q -a}.split(/\n/).each do |container| container.chomp() name,image_id,is_running,start_at,end_at = %x{#{@docker_bin} inspect --format "{{.Name}};{{.Image}};{{.State.Running}};{{.State.StartedAt}};{{.State.FinishedAt}}" #{container}}.split(/;/) @containers[container.to_sym] = { :name => name.gsub(/^\//,''), :image_id => image_id.chomp(), :is_running => is_running.chomp(), :start_at => DateTime.parse(start_at.chomp().to_s).strftime('%s'), :end_at => DateTime.parse(end_at.chomp().to_s).strftime('%s'), :run_time => (DateTime.parse(end_at.chomp().to_s).strftime('%s').to_i - DateTime.parse(start_at.chomp().to_s).strftime('%s').to_i), :age => (Time.now.to_i - DateTime.parse(start_at.chomp().to_s).strftime('%s').to_i) } end end |