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.



17
18
19
20
21
22
23
24
25
# File 'lib/fc/storage.rb', line 17

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

Class Method Details

.curr_hostObject



13
14
15
# File 'lib/fc/storage.rb', line 13

def self.curr_host
  @uname || @uname = `uname -n`.chomp
end

Instance Method Details

#check_time_delayObject



32
33
34
# File 'lib/fc/storage.rb', line 32

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



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

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 -rB #{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



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

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 -rB #{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



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

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



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

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

#up?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/fc/storage.rb', line 36

def up?
  check_time_delay < self.class.check_time_limit
end

#update_check_timeObject



27
28
29
30
# File 'lib/fc/storage.rb', line 27

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