<commit_before>require 'automat/base'
require 'automat/ec2/errors'

module Automat::Ec2
  class Image < Automat::Base
    add_option :instance, :name, :environment, :prune

    include Automat::Mixins::Utils

    def find_inst
      inst = nil
      if !instance.nil?
        inst = ec2.instances[instance]
      end
      inst
    end

    def create
      inst = find_inst
      myname = default_image_name
      logger.info "Creating image #{myname} for #{inst.id}"
      newami=ec2.images.create(:instance_id=>instance,:name=>myname,:no_reboot=>true)
      if prune == true
        set_prunable(newami)
      end
    end

    def is_more_than_month_old?(mytime)
      if mytime.class == Time && mytime<Time.now.utc-(60*60*24*30)
        true
      else
        false
      end
    end

    def default_image_name()
      stime = Time.new.iso8601.gsub(/:/,'-')
      return name + "-" + stime
    end

    def check_for_ebs(block_devices)
      foundit=false
      block_devices.each do |device|
        if (!device.nil? && !device[:ebs].nil? && !device[:ebs][:snapshot_id].nil?)
          foundit=true
        end
      end
      return foundit
    end

    def set_prunable(newami)
      logger.info "setting prunable for AMI #{newami.image_id}"
      newami.tags["CanPrune"]="yes"
      until check_for_ebs(newami.block_devices)
        logger.info "waiting 5sec for a valid snapshot so that we can tag it"
        sleep 5
      end
      newami.block_devices.each do |device|
        if device[:ebs]
          logger.info "setting prunable for snapshot #{ec2.snapshots[device[:ebs][:snapshot_id]].id}"
          ec2.snapshots[device[:ebs][:snapshot_id]].tags["CanPrune"]="yes"
        end
      end
    end

    def deregister_images(snaplist)
      my_images=ec2.images.with_owner('self')
      my_images.each do |oneimage|
        next unless oneimage.state == :available
        if oneimage.tags["CanPrune"]=="yes" and snaplist.include?(oneimage.block_devices[0][:ebs][:snapshot_id])
          logger.info "deregistering AMI #{oneimage.id}"
          oneimage.delete
        end
      end
    end

    def delete_snapshots(snaplist)
      allsnapshots=ec2.snapshots.with_owner('self')
      allsnapshots.each do |onesnapshot|
        next unless onesnapshot.status == :completed
        if snaplist.include?(onesnapshot.id)
          logger.info "deleting snapshot #{onesnapshot.id}"
          onesnapshot.delete
        end
      end
    end

    def prune_amis()
      condemnedsnaps=[]
      allsnapshots=ec2.snapshots.with_owner('self')
      allsnapshots.each do |onesnapshot|
        next unless onesnapshot.status == :completed
        mycreatetime=onesnapshot.start_time
        if is_more_than_month_old?(mycreatetime) and onesnapshot.tags["CanPrune"]=="yes"
          logger.info "adding snapshot #{onesnapshot.id} to condemed list"
          condemnedsnaps.push(onesnapshot.id)
        end
      end
      deregister_images(condemnedsnaps)
      delete_snapshots(condemnedsnaps)
    end<commit_msg>Enhancements to baker: refactoring to make functions cleaner. don't deregister amis or delete snapshot if nothing condemned is found.<commit_after>require 'automat/base'
require 'automat/ec2/errors'

module Automat::Ec2
  class Image < Automat::Base
    add_option :instance, :name, :environment, :prune

    include Automat::Mixins::Utils

    def find_inst
      inst = nil
      if !instance.nil?
        inst = ec2.instances[instance]
      end
      inst
    end

    def create
      inst = find_inst
      myname = default_image_name
      logger.info "Creating image #{myname} for #{inst.id}"
      newami=ec2.images.create(:instance_id=>instance,:name=>myname,:no_reboot=>true)
      if prune == true
        set_prunable(newami)
      end
    end

    def is_more_than_month_old?(mytime)
      if mytime.class == Time && mytime<Time.now.utc-(60*60*24*30)
        true
      else
        false
      end
    end

    def default_image_name()
      stime = Time.new.iso8601.gsub(/:/,'-')
      return name + "-" + stime
    end

    def check_for_ebs(block_devices)
      block_devices.each do |device|
        if (!device.nil? && !device[:ebs].nil? && !device[:ebs][:snapshot_id].nil?)
          return true
        end
      end
      return false
    end

    def set_prunable(newami)
      logger.info "setting prunable for AMI #{newami.image_id}"
      newami.tags["CanPrune"]="yes"
      until check_for_ebs(newami.block_devices)
        logger.info "waiting 5sec for a valid snapshot so that we can tag it"
        sleep 5
      end
      newami.block_devices.each do |device|
        if device[:ebs]
          logger.info "setting prunable for snapshot #{ec2.snapshots[device[:ebs][:snapshot_id]].id}"
          ec2.snapshots[device[:ebs][:snapshot_id]].tags["CanPrune"]="yes"
        end
      end
    end

    def deregister_images(snaplist)
      my_images=ec2.images.with_owner('self')
      my_images.each do |oneimage|
        next unless oneimage.state == :available
        if oneimage.tags["CanPrune"]=="yes" and snaplist.include?(oneimage.block_devices[0][:ebs][:snapshot_id])
          logger.info "deregistering AMI #{oneimage.id}"
          oneimage.delete
        end
      end
    end

    def delete_snapshots(snaplist)
      allsnapshots=ec2.snapshots.with_owner('self')
      allsnapshots.each do |onesnapshot|
        next unless onesnapshot.status == :completed
        if snaplist.include?(onesnapshot.id)
          logger.info "deleting snapshot #{onesnapshot.id}"
          onesnapshot.delete
        end
      end
    end

    def prune_amis()
      condemnedsnaps=[]
      allsnapshots=ec2.snapshots.with_owner('self')
      allsnapshots.each do |onesnapshot|
        next unless onesnapshot.status == :completed
        mycreatetime=onesnapshot.start_time
        if is_more_than_month_old?(mycreatetime) and onesnapshot.tags["CanPrune"]=="yes"
          logger.info "adding snapshot #{onesnapshot.id} to condemed list"
          condemnedsnaps.push(onesnapshot.id)
        end
      end
      if !condemnedsnaps.empty?
        deregister_images(condemnedsnaps)
        delete_snapshots(condemnedsnaps)
      end
    end
{'fraction_non_alphanumeric': 0.1043520465944022, 'fraction_numerical': 0.007765733700048536, 'mean_word_length': 2.60466472303207, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1844455', 'n_tokens_mistral': 2041, 'n_tokens_neox': 2009, 'n_words': 408}