Module: Utils

Included in:
Cryo, Database, Mysql, Postgres, Redis, Store
Defined in:
lib/cryo/utils.rb

Instance Method Summary collapse

Instance Method Details

#delete_file(path) ⇒ Object



8
9
10
# File 'lib/cryo/utils.rb', line 8

def delete_file(path)
  File.delete(path) if File.exists?(path)
end

#get_age_from_key_name(key_name) ⇒ Object

returns the age of the snapshot in mins



100
101
102
103
104
# File 'lib/cryo/utils.rb', line 100

def get_age_from_key_name(key_name)
  snapshot_time = get_utc_time_from_key_name(key_name)
  age_in_mins_as_float = (@time - snapshot_time) / 60
  age_in_mins_as_int = age_in_mins_as_float.to_i
end

#get_tempfileObject



13
14
15
16
17
18
19
# File 'lib/cryo/utils.rb', line 13

def get_tempfile
  #    Tempfile.new('redis-backup','/mnt/cryo').path
  tmp_file = File.join(@tmp_path,"tmp-#{rand 9999}")
  at_exit {delete_file tmp_file}
  FileUtils.touch tmp_file
  tmp_file
end

#get_timstamped_key_nameObject



88
89
90
# File 'lib/cryo/utils.rb', line 88

def get_timstamped_key_name()
  "#{@snapshot_prefix}#{@timestamp}Z.cryo"
end

#get_utc_timeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cryo/utils.rb', line 68

def get_utc_time
  retries = 5
  begin
    Net::NTP.get("us.pool.ntp.org").time.getutc
  rescue Object => o
    retries -= 1
    if retries > 0
      logger.debug "retrying ntp query again..."
      sleep 2
      retry
    end
    throw o
  end
end

#get_utc_time_from_key_name(key_name) ⇒ Object



92
93
94
95
96
97
# File 'lib/cryo/utils.rb', line 92

def get_utc_time_from_key_name(key_name)
  logger.debug "getting time for #{key_name}"
  year,month,day,time = key_name.split('/')
  hour,min,sec = time.split(':')
  Time.utc(year,month,day,hour,min,sec) 
end

#get_utc_timestampObject



83
84
85
86
# File 'lib/cryo/utils.rb', line 83

def get_utc_timestamp()
  @time ||= get_utc_time  # don't change the endpoint!!! 
  @timestamp ||= @time.strftime("%Y/%m/%d/%H:%M:%S")
end

#gzip_file(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cryo/utils.rb', line 41

def gzip_file(path)
  # given a path to a file, return a gzipped version of it
  tempfile = get_tempfile
  #logger.info "gzipping #{path} to #{tempfile}"

  # stream the gzipped content into a file as we compute it
  Zlib::GzipWriter.open(tempfile) do |gz|
    File.open(path) do |f|
      # write 1M chunks at a time
      gz.write f.read(1024*1024) until f.eof?
    end
  end
  #logger.info "done unzipping"
  tempfile
end

#need_to_archive?(old_snapshot_age, new_archive_age) ⇒ Boolean

find out if we have an archive that is more recent than the snapshot period

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
# File 'lib/cryo/utils.rb', line 107

def need_to_archive?(old_snapshot_age,new_archive_age)
  logger.debug "checking to see if we should archive"
  logger.debug "oldest snapshot age is #{old_snapshot_age}"
  logger.debug "newest archive time is #{new_archive_age}"
  logger.debug "@snapshot_period is #{@archive_frequency}"
  answer = (new_archive_age - old_snapshot_age) > @archive_frequency
  logger.debug "returning #{answer.inspect}"
  return answer
end

#safe_run(command) ⇒ Object



57
58
59
60
61
62
# File 'lib/cryo/utils.rb', line 57

def safe_run(command)
  #logger.debug "about to run #{command}"
  output = `bash -c "set -o pipefail && #{command}"`.chomp
  raise "command '#{command}' failed!\nOutput was:\n#{output}" unless $?.success?
  true
end

#ungzip_file(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cryo/utils.rb', line 22

def ungzip_file(path)
  # get a temp file
  tempfile = get_tempfile
  #logger.info "unzipping #{path} to #{tempfile}..."
  
  # stream the gzipped file into an uncompressed file
  Zlib::GzipReader.open(path) do |gz|
    File.open(tempfile,'w') do |open_file|
      # write 1M chunks at a time
      open_file.write gz.read(1024*1024) until gz.eof?
    end
  end
  #logger.info "finished unzipping file"

  # return unzipped file
  tempfile
end

#verify_system_dependency(command) ⇒ Object



64
65
66
# File 'lib/cryo/utils.rb', line 64

def verify_system_dependency(command)
  raise "system dependency #{command} is not unstalled" unless system "which #{command} > /dev/null"
end