Class: FC::Storage

Inherits:
DbBase show all
Defined in:
lib/fc/storage.rb

Class Attribute Summary collapse

Attributes inherited from DbBase

#database_fields, #id

Class Method Summary collapse

Instance Method Summary collapse

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_limitObject

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_mutexObject

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_timeObject

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_hostObject



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_delayObject



45
46
47
# File 'lib/fc/storage.rb', line 45

def check_time_delay
  Time.new.to_i - check_time.to_i
end

#copy_path(local_path, file_name) ⇒ Object

copy local_path to storage



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fc/storage.rb', line 54

def copy_path(local_path, file_name)
  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 -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\""
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0

  cmd = self.class.curr_host == host ?
    "cp -r #{local_path.shellescape} #{dst_path.shellescape}" :
    "scp -r -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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fc/storage.rb', line 70

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 -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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fc/storage.rb', line 84

def delete_file(file_name)
  dst_path = "#{self.path}#{file_name}"
  cmd = self.class.curr_host == host ? 
    "rm -rf #{dst_path.shellescape}" : 
    "ssh -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 -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



100
101
102
103
104
105
106
107
108
109
# File 'lib/fc/storage.rb', line 100

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 -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

#get_copy_storagesObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/fc/storage.rb', line 29

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



122
123
124
125
126
# File 'lib/fc/storage.rb', line 122

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



112
113
114
115
116
117
118
119
# File 'lib/fc/storage.rb', line 112

def md5_sum(file_name)
  dst_path = "#{self.path}#{file_name}"
  cmd = "find #{dst_path} -type f -exec md5sum {} \\; | awk '{print $1}' | sort | md5sum"
  cmd = "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} \"#{cmd}\"" if self.class.curr_host != host
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  r.to_s[0..31]
end

#up?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/fc/storage.rb', line 49

def up?
  check_time_delay < self.class.check_time_limit
end

#update_check_timeObject



40
41
42
43
# File 'lib/fc/storage.rb', line 40

def update_check_time
  self.check_time = Time.new.to_i
  save
end