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.



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

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.



8
9
10
# File 'lib/fc/storage.rb', line 8

def check_time_limit
  @check_time_limit
end

Class Method Details

.curr_hostObject



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

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

Instance Method Details

#check_time_delayObject



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

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



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

def copy_path(local_path, file_name)
  dst_path = "#{self.path}#{file_name}"
  
  cmd = self.class.curr_host == host ? 
    "mkdir -p #{File.dirname(dst_path)}" : 
    "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} 'mkdir -p #{File.dirname(dst_path)}'"
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  
  cmd = self.class.curr_host == host ? 
    "cp -r #{local_path} #{dst_path}" : 
    "scp -rB #{local_path} #{self.host}:#{dst_path}"
  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 = `mkdir -p #{File.dirname(local_path)} 2>&1`
  raise r if $?.exitstatus != 0
  
  cmd = self.class.curr_host == host ? 
    "cp -r #{src_path} #{local_path}" : 
    "scp -rB #{self.host}:#{src_path} #{local_path}"
  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}" : 
    "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} 'rm -rf #{dst_path}'"
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  
  cmd = self.class.curr_host == host ? 
    "ls -la #{dst_path}" : 
    "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} 'ls -la #{dst_path}'"
  r = `#{cmd} 2>/dev/null`
  raise "Path #{dst_path} not deleted" unless r.empty?
end

#file_size(file_name) ⇒ 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)
  dst_path = "#{self.path}#{file_name}"
  
  cmd = self.class.curr_host == host ? 
    "du -sb #{dst_path}" : 
    "ssh -oBatchMode=yes -oStrictHostKeyChecking=no #{self.host} 'du -sb #{dst_path}'"
  r = `#{cmd} 2>&1`
  raise r if $?.exitstatus != 0
  r.to_i
end

#up?Boolean

Returns:

  • (Boolean)


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

def up?
  check_time_delay < self.class.check_time_limit
end

#update_check_timeObject



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

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