<commit_before>require 'fog'
require 'pp'
$: << File.join(File.dirname(__FILE__), "../lib")
require 'ec2_helper'

# This class is responsible of the snapshotting of given disks to EC2
# EC2 related permissions in IAM
# Sid": "Stmt1344254048404",
#      "Action": [
#        "ec2:CreateSnapshot",
#        "ec2:DeleteSnapshot",
#        "ec2:DescribeSnapshots",
#        "ec2:CreateTags",
#        "ec2:DescribeTags",
#        "ec2:DescribeVolumes"
#      ],
#      "Effect": "Allow",
#      "Resource": [
#        "*"
#      ]
#

class NoSuchVolumeException < Exception
  def initialize(instance, volume, details)
    @instance, @volume, @details = instance, volume, details
  end

  def to_s
    "Unable to locate volume \"#{@volume}\" on #{@instance}\nKnow volumes for this instance are:\n#{@details.inspect}"
  end
end

def log s
 $stderr.puts "[#{Time.now}]: #{s}"
end

class EC2VolumeSnapshotter
  NAME_PREFIX='Volume Snapshot' # TODO: make this not a constant

  # Kind of snapshot and their expiration in days
  KINDS = { 'test' => 1,
    'snapshot' => 0,
    'daily' => 7,
    'weekly' => 31,
    'monthly' => 300,
    'yearly' => 0}

  attr_reader :instance_id

  # If not provided, attempt to fetch current instance_id
  def initialize(aki, sak, instance_id = nil)
    @ec2 = EC2Helper.new(aki, sak)
    @compute = Fog::Compute.new(@ec2.connection)
    @instance_id = instance_id.nil? ? @ec2.instance_id : instance_id
  end

  # Snapshots the list of devices 
  # devices is an array of device attached to the instance (/dev/foo)
  # name if the name of the snapshot
  def snapshot_devices(devices, name = "#{@instance_id}", kind = "test", limit = KINDS[kind], comments = {})

    log "Snapshot of kind #{kind}, limit set to #{limit} (0 means never purge)"
    ts = DateTime.now.to_s
    name = "#{NAME_PREFIX}:" + name
    volumes = {}
    devices.each do |device|
      volumes[device] = find_volume_for_device(device)
    end
    sn = []
    volumes.each do |device, volume|
      log "Creating volume snapshot for #{device} on instance #{@instance_id}"
      snapshot = volume.snapshots.new
      snapshot.description = name+" #{device}"
      snapshot.save
      sn << snapshot
      snapshot.reload

      @compute.tags.create(:resource_id => snapshot.id, :key =>"application", :value => NAME_PREFIX)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"device", :value => device)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"instance_id", :value =>@instance_id)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"date", :value => ts)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"kind", :value => kind)
      if comments.is_a? Hash
        comments.each do |k, v = nil|
          @compute.tags.create(:resource_id => snapshot.id, :key => k, :value => v)
        end<commit_msg>I want friendlier tags to choose from when I restore from snapshot.<commit_after>require 'fog'
require 'pp'
$: << File.join(File.dirname(__FILE__), "../lib")
require 'ec2_helper'

# This class is responsible of the snapshotting of given disks to EC2
# EC2 related permissions in IAM
# Sid": "Stmt1344254048404",
#      "Action": [
#        "ec2:CreateSnapshot",
#        "ec2:DeleteSnapshot",
#        "ec2:DescribeSnapshots",
#        "ec2:CreateTags",
#        "ec2:DescribeTags",
#        "ec2:DescribeVolumes"
#      ],
#      "Effect": "Allow",
#      "Resource": [
#        "*"
#      ]
#

class NoSuchVolumeException < Exception
  def initialize(instance, volume, details)
    @instance, @volume, @details = instance, volume, details
  end

  def to_s
    "Unable to locate volume \"#{@volume}\" on #{@instance}\nKnow volumes for this instance are:\n#{@details.inspect}"
  end
end

def log s
 $stderr.puts "[#{Time.now}]: #{s}"
end

class EC2VolumeSnapshotter
  NAME_PREFIX='Volume Snapshot' # TODO: make this not a constant

  # Kind of snapshot and their expiration in days
  KINDS = { 'test' => 1,
    'snapshot' => 0,
    'daily' => 7,
    'weekly' => 31,
    'monthly' => 300,
    'yearly' => 0}

  attr_reader :instance_id

  # If not provided, attempt to fetch current instance_id
  def initialize(aki, sak, instance_id = nil)
    @ec2 = EC2Helper.new(aki, sak)
    @compute = Fog::Compute.new(@ec2.connection)
    @instance_id = instance_id.nil? ? @ec2.instance_id : instance_id
  end

  # Snapshots the list of devices 
  # devices is an array of device attached to the instance (/dev/foo)
  # name if the name of the snapshot
  def snapshot_devices(devices, name = "#{@instance_id}", kind = "test", limit = KINDS[kind], comments = {})

    log "Snapshot of kind #{kind}, limit set to #{limit} (0 means never purge)"
    ts = DateTime.now.to_s
    t = Time.new
    backup_id = sprintf("%4d%02d%02d.%02d", t.year, t.month, t.day, t.hour)
    name = "#{NAME_PREFIX}:" + name
    volumes = {}
    devices.each do |device|
      volumes[device] = find_volume_for_device(device)
    end
    sn = []
    volumes.each do |device, volume|
      log "Creating volume snapshot for #{device} on instance #{@instance_id}"
      snapshot = volume.snapshots.new
      snapshot.description = name+" #{device}"
      snapshot.save
      sn << snapshot
      snapshot.reload

      @compute.tags.create(:resource_id => snapshot.id, :key =>"application", :value => NAME_PREFIX)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"device", :value => device)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"instance_id", :value =>@instance_id)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"date", :value => ts)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"kind", :value => kind)
      @compute.tags.create(:resource_id => snapshot.id, :key =>"backup_id", :value => backup_id)
      if comments.is_a? Hash
        comments.each do |k, v = nil|
          @compute.tags.create(:resource_id => snapshot.id, :key => k, :value => v)
        end
{'fraction_non_alphanumeric': 0.1477997984548203, 'fraction_numerical': 0.013604299630500504, 'mean_word_length': 3.2780172413793105, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 54, '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': '790878', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1936, 'n_words': 534}