class SyncPointCollection

Represents and contains a configuration point stored on Amazon's S3

Public Class Methods

new() click to toggle source

Configures and returns a SyncPointCollection object. Connects to AWS S3 and builds a collection of SyncPoint objects.

# File lib/sync_point_collection.rb, line 45
def initialize
  bucket = AWS::S3.new(:access_key_id => $AWS_ID, :secret_access_key => $AWS_SECRET).buckets[$SYNC_BUCKET]
  times = bucket.as_tree.children.select(&:branch?).collect(&:prefix).map(&:chop)

  @points = Array.new

  times.each do |time|
    @points.push(SyncPoint.new(time, @points.count + 1))
  end
end
rm_point(time=nil) click to toggle source

removes the sync point of the given time @param [Time] time The time of the sync point to remove

# File lib/sync_point_collection.rb, line 10
def self.rm_point(time=nil)
  # ensure the time is a Time object if it's not nil
  failValidation if time.not_nil? and not time.is_a?(Time)

  # grab the bucket
  bucket = AWS::S3.new(:access_key_id => $AWS_ID, :secret_access_key => $AWS_SECRET).buckets[$SYNC_BUCKET]

  # Determine the prefix of the sync point
  if time.nil?
    # get all the folders in the sync bucket
    folders = bucket.as_tree.children.select(&:branch?).collect(&:prefix).map(&:chop)

    # Convert the folders to an integer. If the folder name isn't an integer, make it nil
    folders.map! do |x|
      begin
        Integer(x)
      rescue
        nil
      end
    end

    # remove nil entries
    folders.select! {|x| not x.nil?}

    # set the prefix
    prefix = "#{folders.max}/"
  else
    prefix = time.strftime('%s') + "/"
  end

  # delete all objects with this prefix
  bucket.objects.with_prefix(prefix).delete_all
end

Public Instance Methods

epoch_time(id) click to toggle source
# File lib/sync_point_collection.rb, line 72
def epoch_time(id)
  @points[id -1].epoch_time
end
get_point(id, dir) click to toggle source
# File lib/sync_point_collection.rb, line 76
def get_point(id, dir)
  failValidation if not id.is_a?(Integer)
  bucket = AWS::S3.new(:access_key_id => $AWS_ID, :secret_access_key => $AWS_SECRET).buckets[$SYNC_BUCKET]

  bucket.objects.with_prefix(@points[id - 1].epoch_time).each do |obj|
    suffix = obj.key.scan(/\d*\/(.*)/).flatten[0]
    filename = File.join(dir, suffix)
    FileUtils.mkdir_p(File.dirname(filename))
    File.open(filename, 'w') do |file|
      file.write(obj.read)
    end
  end
  #  next unless obj.key =~ /(.*?)\.deflated/
  #  suffix = obj.key.scan(/\d*\/(.*)/).flatten[0]
  #  filename = File.join(File.dirname(File.join(dir, suffix)), File.basename(suffix, '.deflated'))
  #  FileUtils.mkdir_p(File.dirname(filename))
  #
  #  File.open(filename, 'w') do |file|
  #    data = Zlib::Inflate.inflate(obj.read)
  #    file.write(data)
  #  end
  #end
end
has_point?(id) click to toggle source
# File lib/sync_point_collection.rb, line 57
def has_point?(id)
  failValidation if not id.is_a?(Integer)
  return false if id < 1
  return false if id > @points.count
  true
end
latest_id() click to toggle source
# File lib/sync_point_collection.rb, line 64
def latest_id
  if @points.count == 0
    nil
  else
    @points.count
  end
end
list(requirements={}) click to toggle source

Displays a listing of the available synchronization points, perhaps filtered based on the passed parameters @param [Hash] requirements The options hash which specifies the required components for each entry to be listed @option requirements [Boolean] :data Specifies if listed entries need to have database dumps @option requirements [Boolean] :config Specifies if listed entries need to have configurations

# File lib/sync_point_collection.rb, line 104
def list(requirements={})
  InsxSync::require_condition @points.count != 0, "No synchronization points found. Exiting..."
  req_data   = requirements.has_key?(:data)   ? requirements[:data]   : false
  req_config = requirements.has_key?(:config) ? requirements[:config] : false

  puts '    Date  ID    |   Epoch Time   |    Date and Time    | Databases | Configs '
  puts '-----------------------------------------------------------------------------'

  @points.each do |point|
    next if req_data   and not point.database?
    next if req_config and not point.configs?

    data = point.database? ? 'X' : ''
    conf = point.configs?  ? 'X' : ''


    puts "#{point.id.to_s.center(16)}|#{point.epoch_time.center(16)}|#{point.date_time.center(21)}|#{data.center(11)}|#{conf.center(9)}"
  end
end

Private Instance Methods

get_usage() click to toggle source
Calls superclass method ErrorHandlingIface#get_usage
# File lib/sync_point_collection.rb, line 125
def get_usage
  #Usage cases for all the usage in this class
  if @usage.nil?
    init_usage
    @usage['list']     = [':require_data => Boolean, require_config => Boolean']
    @usage['rm_point'] = ['time = Time']
  end

  super
end