Class: FreezerBurn::Rotation

Inherits:
Object
  • Object
show all
Defined in:
lib/freezer_burn/rotation.rb

Direct Known Subclasses

Cxtracker, Passivedns

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fridge = FreezerBurn::Settings.fridge) ⇒ Rotation

Returns a new instance of Rotation.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/freezer_burn/rotation.rb', line 18

def initialize(fridge=FreezerBurn::Settings.fridge)
  # Seconds since epoch
  @unixtime = Time.now.utc.to_i
  @interval = 86_400

  # UT END of TODAY(uteot).
  @uteot = (Time.now + @interval).change(hour: 0).to_i

  # UT START of TODAY(utsot).
  @utsot = @uteot - @interval

  @collection = _build_collection(fridge)

  self.class.update_settings!
  return self
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/freezer_burn/rotation.rb', line 6

def collection
  @collection
end

Class Method Details

.rotate(fridge = FreezerBurn::Settings.fridge) ⇒ Object



13
14
15
16
# File 'lib/freezer_burn/rotation.rb', line 13

def self.rotate(fridge=FreezerBurn::Settings.fridge)
  rotation = self.new(fridge)
  rotation.rotate
end

.update_settings!Object

Stub

Raises:



9
10
11
# File 'lib/freezer_burn/rotation.rb', line 9

def self.update_settings!
  raise ClassError, "method stub called, child method missing"
end

Instance Method Details

#_build_collection(dir) ⇒ Object

Stub

Raises:



52
53
54
# File 'lib/freezer_burn/rotation.rb', line 52

def _build_collection(dir)
  raise ClassError, "method stub called, child method missing"
end

#_get_file_epoch_cxtracker(filename) ⇒ Object

Stub

Raises:



57
58
59
# File 'lib/freezer_burn/rotation.rb', line 57

def _get_file_epoch_cxtracker(filename)
  raise ClassError, "method stub called, child method missing"
end

#_rotate_intervalObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/freezer_burn/rotation.rb', line 61

def _rotate_interval
  puts "Searching #{@utsot} -> #{@uteot}: #{@collection.size} files" if Settings.verbose
  daylist = []
  @collection.each do |file|
    daylist.push(file) if (@utsot..@uteot) === file[:file_epoch]
  end
  unless daylist.empty?
    puts "#{daylist.first[:filename]} .. #{daylist.last[:filename]}" if Settings.verbose
    @collection -= daylist

    # compress tarball daylist, write filename with earliest.lastest.gz.tar epoch time.
    # best way to handle thousands of files is with gnu-tar (tar must support -T flag)
    Tempfile.open('tar-ball-listing') do |f|
      daylist.each { |fileref| f.puts(fileref[:filename]) }
      f.close
      # write filenames to a filelist.
      cmd_string = "#{Settings.gnu_tar_command} -T #{f.path} --append -z #{Settings.remove_files}-f #{Settings.freezer}/#{Settings.prefix}.#{daylist.first[:file_epoch]}-#{daylist.last[:file_epoch]}.tar.gz > /dev/null 2>&1"
      puts cmd_string if Settings.verbose
      `#{cmd_string}` unless Settings.dryrun
    end

  end # unless

end

#rotateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/freezer_burn/rotation.rb', line 35

def rotate
  begin
    _rotate_interval

    # Fall back interval.
    @uteot -= @interval
    @utsot -= @interval

    # limit selecting to only one year
    @collection = [] if @unixtime - Settings.max_scan_time_in_sec > @uteot

  end while !@collection.empty?
end