lib/cide/cli.rb in cide-0.9.1 vs lib/cide/cli.rb in cide-0.9.2

- old
+ new

@@ -55,12 +55,10 @@ desc: 'Path to a ssh key to import into the docker image', aliases: ['-s'], default: '~/.ssh/id_rsa' def exec - setup_docker - tag = name_to_tag options.name banner 'Config' config = ConfigFile.load(Dir.pwd) say_status :config, config.inspect @@ -91,11 +89,11 @@ return unless options.export banner 'Export' guest_dir = options.guest_export_dir || config.export_dir if guest_dir.nil? - puts "Ignoring, missing export_dir" + puts 'Ignoring, missing export_dir' return end runner.export!( guest_dir: guest_dir, @@ -139,11 +137,11 @@ method_option 'aws_bucket', desc: 'AWS_BUCKET', default: ENV['AWS_BUCKET'] def package - fail 'missing AWS_BUCKET' if options.upload && !options.aws_bucket + raise 'missing AWS_BUCKET' if options.upload && !options.aws_bucket tag = name_to_tag options.name build_root = File.expand_path('.package') guest_export_dir = '/cide/package' @@ -246,12 +244,10 @@ default: File.basename(Dir.pwd) method_option 'user', desc: 'User to run under', default: 'cide' def debug - setup_docker - tag = name_to_tag options.name ## Config ## banner 'Config' config = ConfigFile.load(Dir.pwd) @@ -285,48 +281,49 @@ method_option 'count', desc: 'Maximum number of images to keep', default: 20, type: :numeric def clean - setup_docker - days_to_keep = options[:days] max_images = options[:count] - x = docker('images', '--no-trunc', capture: true) - iter = x.lines.each - iter.next - cide_image_ids = iter - .map { |line| line.split(/\s+/) } - .select { |line| line[0] =~ %r{^cide[/-]} || line[0] == '<none>' } - .map { |line| line[2] } + # Delete failed images + failed_filter = 'label=cide.build.complete=false' + cide_failed_image_ids = docker_image_ids(filter_by: failed_filter) + if cide_failed_image_ids.any? + docker('rmi', '--force', *cide_failed_image_ids) + end + # Retrieve all other cide images + cide_image_ids = docker_image_ids(filter_by: 'label=cide') + if cide_image_ids.empty? puts 'No images found to be cleaned' return end x = docker('inspect', *cide_image_ids, capture: true) - cide_images = JSON.parse(x.strip) + cide_images = + JSON + .parse(x.strip) .each { |image| image['Created'] = Time.iso8601(image['Created']) } .sort { |a, b| a['Created'] <=> b['Created'] } - if cide_images.size > max_images - old_cide_images = cide_images[0..-max_images] - .map { |image| image['Id'] } - else - old_times = Time.now - (days_to_keep * 24 * 60 * 60) - old_cide_images = cide_images - .select { |image| image['Created'] < old_times } - .map { |image| image['Id'] } - end + to_destroy = cide_images[max_images..-1] + leftover_images = cide_images[0..(max_images - 1)] - if old_cide_images.empty? + expire_from = Time.now - (days_to_keep * 24 * 60 * 60) + to_destroy << + leftover_images.select { |image| image['Created'] < expire_from } + + to_destroy_ids = to_destroy.map { |image| image['Id'] } + + if to_destroy_ids.empty? puts 'No images found to be cleaned' return end - docker('rmi', '--force', *old_cide_images) + docker('rmi', '--force', *to_destroy_ids) end desc 'init', "Creates a blank #{CONFIG_FILES.first} in the project" def init puts "Creating #{CONFIG_FILES.first} with default values"