Class: FC::Storage
Class Attribute Summary collapse
-
.check_time_limit ⇒ Object
Returns the value of attribute check_time_limit.
-
.get_copy_storages_mutex ⇒ Object
Returns the value of attribute get_copy_storages_mutex.
-
.storages_cache_time ⇒ Object
Returns the value of attribute storages_cache_time.
Attributes inherited from DbBase
Class Method Summary collapse
Instance Method Summary collapse
- #check_time_delay ⇒ Object
-
#copy_path(local_path, file_name, try_move = false) ⇒ Object
copy local_path to storage.
-
#copy_to_local(file_name, local_path) ⇒ Object
copy object to local_path.
-
#delete_file(file_name) ⇒ Object
delete object from storage.
-
#file_size(file_name, ignore_errors = false) ⇒ Object
return object size on storage.
- #free ⇒ Object
- #free_rate ⇒ Object
- #get_copy_storages ⇒ Object
-
#get_proper_storage_for_copy(size, exclude = []) ⇒ Object
get available storage for copy by size.
-
#initialize(params = {}) ⇒ Storage
constructor
A new instance of Storage.
-
#md5_sum(file_name) ⇒ Object
return object md5_sum on storage.
- #size_rate ⇒ Object
- #up? ⇒ Boolean
- #update_check_time ⇒ Object
Methods inherited from DbBase
create_from_fiels, #delete, find, #reload, #save, set_table, where
Constructor Details
#initialize(params = {}) ⇒ Storage
Returns a new instance of Storage.
19 20 21 22 23 24 25 26 27 |
# File 'lib/fc/storage.rb', line 19 def initialize(params = {}) path = (params['path'] || params[:path]) if path && !path.to_s.empty? path += '/' unless path[-1] == '/' raise "Storage path must be like '/bla/bla../'" unless path.match(/^\/.*\/$/) params['path'] = params[:path] = path end super params end |
Class Attribute Details
.check_time_limit ⇒ Object
Returns the value of attribute check_time_limit.
9 10 11 |
# File 'lib/fc/storage.rb', line 9 def check_time_limit @check_time_limit end |
.get_copy_storages_mutex ⇒ Object
Returns the value of attribute get_copy_storages_mutex.
9 10 11 |
# File 'lib/fc/storage.rb', line 9 def get_copy_storages_mutex @get_copy_storages_mutex end |
.storages_cache_time ⇒ Object
Returns the value of attribute storages_cache_time.
9 10 11 |
# File 'lib/fc/storage.rb', line 9 def storages_cache_time @storages_cache_time end |
Class Method Details
.curr_host ⇒ Object
15 16 17 |
# File 'lib/fc/storage.rb', line 15 def self.curr_host @uname || @uname = `uname -n`.chomp end |
Instance Method Details
#check_time_delay ⇒ Object
57 58 59 |
# File 'lib/fc/storage.rb', line 57 def check_time_delay Time.new.to_i - check_time.to_i end |
#copy_path(local_path, file_name, try_move = false) ⇒ Object
copy local_path to storage
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fc/storage.rb', line 66 def copy_path(local_path, file_name, try_move = false) dst_path = "#{self.path}#{file_name}" cmd = "rm -rf #{dst_path.shellescape}; mkdir -p #{File.dirname(dst_path).shellescape}" cmd = self.class.curr_host == host ? cmd : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 op = try_move && self.class.curr_host == host && File.stat(local_path).dev == File.stat(File.dirname(dst_path)).dev ? 'mv' : 'cp -r' cmd = self.class.curr_host == host ? "#{op} #{local_path.shellescape} #{dst_path.shellescape}" : "scp -r -q -oBatchMode=yes -oStrictHostKeyChecking=no #{local_path.shellescape} #{self.host}:\"#{dst_path.shellescape}\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 end |
#copy_to_local(file_name, local_path) ⇒ Object
copy object to local_path
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fc/storage.rb', line 83 def copy_to_local(file_name, local_path) src_path = "#{self.path}#{file_name}" r = `rm -rf #{local_path.shellescape}; mkdir -p #{File.dirname(local_path).shellescape} 2>&1` raise r if $?.exitstatus != 0 cmd = self.class.curr_host == host ? "cp -r #{src_path.shellescape} #{local_path.shellescape}" : "scp -r -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host}:\"#{src_path.shellescape}\" #{local_path.shellescape}" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 end |
#delete_file(file_name) ⇒ Object
delete object from storage
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/fc/storage.rb', line 97 def delete_file(file_name) dst_path = "#{self.path}#{file_name}" cmd = self.class.curr_host == host ? "rm -rf #{dst_path.shellescape}" : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"rm -rf #{dst_path.shellescape}\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 cmd = self.class.curr_host == host ? "ls -la #{dst_path.shellescape}" : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"ls -la #{dst_path.shellescape}\"" r = `#{cmd} 2>/dev/null` raise "Path #{dst_path} not deleted" unless r.empty? end |
#file_size(file_name, ignore_errors = false) ⇒ Object
return object size on storage
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fc/storage.rb', line 113 def file_size(file_name, ignore_errors = false) dst_path = "#{self.path}#{file_name}" cmd = self.class.curr_host == host ? "du -sb #{dst_path.shellescape}" : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"du -sb #{dst_path.shellescape}\"" r = ignore_errors ? `#{cmd} 2>/dev/null` : `#{cmd} 2>&1` raise r if $?.exitstatus != 0 r.to_i end |
#free ⇒ Object
29 30 31 |
# File 'lib/fc/storage.rb', line 29 def free size_limit - size end |
#free_rate ⇒ Object
37 38 39 |
# File 'lib/fc/storage.rb', line 37 def free_rate free.to_f / size_limit end |
#get_copy_storages ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/fc/storage.rb', line 41 def get_copy_storages self.class.get_copy_storages_mutex.synchronize do unless @copy_storages_cache && Time.new.to_i - @get_copy_storages_time.to_i < self.class.storages_cache_time @get_copy_storages_time = Time.new.to_i names = copy_storages.to_s.split(',').map{|s| "'#{s}'"}.join(',') @copy_storages_cache = names.empty? ? [] : FC::Storage.where("name IN (#{names}) ORDER BY FIELD(name, #{names})") end end @copy_storages_cache end |
#get_proper_storage_for_copy(size, exclude = []) ⇒ Object
get available storage for copy by size
136 137 138 139 140 |
# File 'lib/fc/storage.rb', line 136 def get_proper_storage_for_copy(size, exclude = []) get_copy_storages.select do |storage| !exclude.include?(storage.name) && storage.up? && storage.size + size < storage.size_limit end.first end |
#md5_sum(file_name) ⇒ Object
return object md5_sum on storage
125 126 127 128 129 130 131 132 133 |
# File 'lib/fc/storage.rb', line 125 def md5_sum(file_name) dst_path = "#{self.path}#{file_name}" cmd = self.class.curr_host == host ? "find #{dst_path.shellescape} -type f -exec md5sum {} \\; | awk '{print $1}' | sort | md5sum" : "ssh -q -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"find #{dst_path.shellescape} -type f -exec md5sum {} \\; | awk '{print \\$1}' | sort | md5sum\"" r = `#{cmd} 2>&1` raise r if $?.exitstatus != 0 r.to_s[0..31] end |
#size_rate ⇒ Object
33 34 35 |
# File 'lib/fc/storage.rb', line 33 def size_rate size.to_f / size_limit end |
#up? ⇒ Boolean
61 62 63 |
# File 'lib/fc/storage.rb', line 61 def up? check_time_delay < self.class.check_time_limit end |
#update_check_time ⇒ Object
52 53 54 55 |
# File 'lib/fc/storage.rb', line 52 def update_check_time self.check_time = Time.new.to_i save end |