Class: DRbQS::Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/drbqs/utility/transfer/transfer.rb,
lib/drbqs/utility/transfer/transfer_client.rb,
lib/drbqs/utility/transfer/transfer_file_list.rb,
lib/drbqs/utility/transfer/transfer_client_connect.rb

Overview

Class for path of files to send from server to a node.

Defined Under Namespace

Classes: Client, FileList

Class Method Summary collapse

Class Method Details

.compress_enqueue(path) ⇒ Object



45
46
47
# File 'lib/drbqs/utility/transfer/transfer.rb', line 45

def compress_enqueue(path)
  enqueue(path, :compress => true)
end

.decompress(server, filename) ⇒ Object

Decompress a file in the file directory of a server.

Parameters:

  • server (DRbQS::Server)

    Current server

  • filename (String)

    File path to decompress



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/drbqs/utility/transfer/transfer.rb', line 68

def decompress(server, filename)
  dir = server.transfer_directory
  path = File.join(dir, filename)
  if File.exist?(path)
    case path
    when /\.tar\.gz$/
      cmd = "tar xvzf #{path} -C #{dir} > /dev/null 2>&1"
    when /\.gz$/
      cmd = "gunzip #{path} > /dev/null 2>&1"
    else
      cmd = nil
    end
    system(cmd) if cmd
  end
end

.dequeueObject



49
50
51
# File 'lib/drbqs/utility/transfer/transfer.rb', line 49

def dequeue
  @files.deq
end

.dequeue_allObject



57
58
59
60
61
62
63
# File 'lib/drbqs/utility/transfer/transfer.rb', line 57

def dequeue_all
  files = []
  until empty?
    files << dequeue
  end
  files.empty? ? nil : files
end

.empty?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/drbqs/utility/transfer/transfer.rb', line 53

def empty?
  @files.empty?
end

.enqueue(path, opts = {}) ⇒ Object

Add path to queue of which files is going to be transfered to server.

Parameters:

  • path (String)

    The file path that we want to send to a server.

  • opts (Hash) (defaults to: {})

    The options for transfering a file.

Options Hash (opts):

  • :compress (Boolean)

    Compress the file by gzip before transfering.

  • :rename (String)

    Change basename to the specified name.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/drbqs/utility/transfer/transfer.rb', line 13

def enqueue(path, opts = {})
  if File.exist?(path)
    if opts[:rename]
      new_path = FileName.create(File.join(File.dirname(path), opts[:rename]), :directory => :parent)
      FileUtils.mv(path, new_path)
      path = new_path
    end
    if opts[:compress]
      if File.directory?(path)
        gz_path = "#{path.sub(/\/$/, '')}.tar.gz"
        cmd = "tar czf #{gz_path} -C #{File.dirname(path)} #{File.basename(path)} > /dev/null 2>&1"
      else
        gz_path = path + '.gz'
        cmd = "gzip --best #{path} > /dev/null 2>&1"
      end
      if File.exist?(gz_path)
        raise "File has already existed: #{gz_path}"
      elsif !system(cmd)
        raise "Can not compress: #{path}"
      end
      FileUtils.rm_r(path) if File.exist?(path)
      path_to_send = gz_path
    else
      path_to_send = path
    end
    @files.enq(path_to_send)
    File.basename(path_to_send)
  else
    nil
  end
end