Class: FiverrCopy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/fiverr_copy/server.rb

Constant Summary collapse

@@recipe =
nil

Class Method Summary collapse

Class Method Details

.recipeObject



9
10
11
# File 'lib/fiverr_copy/server.rb', line 9

def self.recipe
  @@recipe
end

.recipe=(recipe) ⇒ Object



5
6
7
# File 'lib/fiverr_copy/server.rb', line 5

def self.recipe=(recipe)
  @@recipe = recipe
end

.run!Object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fiverr_copy/server.rb', line 13

def self.run!
  threads = []
  files_to_sync = Dir[self.recipe.filter]

  compressed_file_name = "fiverr_copy_compressed_#{Time.now.to_i}.tgz"

  if !(self.recipe.manual_client_activation?)

    last_hop =  self.recipe.initialized_clients.shift
    
    puts "Last Hop: #{last_hop.inspect}"
    threads << Thread.new(last_hop) do |current_client|
      current_client.setup!
      Net::SSH.start(current_client.client_ip, current_client.username) do |ssh|
        gem_exists = ssh.exec("which fiverr_copy") == "" ? false : true
        if !(gem_exists)
          puts "Please install the fiverr_copy gem on all clients."
          exit 0
        end
        ssh.exec("fiverr_copy --client --port #{self.recipe.port} --filename #{compressed_file_name} --chunk #{self.recipe.chunk_size} \&")
      end
    end

    self.recipe.initialized_clients.each do |client|
      threads << Thread.new(client) do |current_client|
        current_client.setup!
        puts "Connecting to #{current_client.client_ip}:#{self.recipe.port}"
        Net::SSH.start(current_client.client_ip, current_client.username) do |ssh|
          gem_exists = ssh.exec("which fiverr_copy") == "" ? false : true
          if !(gem_exists)
            puts "Please install the fiverr_copy gem on all clients."
            exit 0
          end
          ssh.exec("fiverr_copy --client --port #{self.recipe.port} --filename #{compressed_file_name} --chunk #{self.recipe.chunk_size} --nexthop #{current_client.next_client} \&")
        end
      end
    end
  end

  #threads.collect(&:join)
  puts "Booting clients"

  attempts = 1
  begin
    puts "Attempting Connection to #{self.recipe.initialized_clients.last.client_ip}:#{self.recipe.port}"
    client_session = TCPSocket.new(self.recipe.initialized_clients.last.client_ip, self.recipe.port)
    puts "Compressing: tar cvzf #{compressed_file_name} #{self.recipe.filter}"
    `tar cvzf #{compressed_file_name} #{self.recipe.filter}`
  rescue Errno::ECONNREFUSED => e
    if attempts < 10
      attempts += 1
      sleep 1.5
      print "Retry #{attempts}: "
      retry
    else
      puts "ERROR: unable to connect to #{self.recipe.initialized_clients.last.client_ip} => #{e.to_s}"
      exit 0
    end
  end

  puts "Validating Compressed file"
  if !(FileTest.exists?("#{compressed_file_name}"))
    puts "Could not find the compressed file to transfer:"
    puts `ls *.tgz`
    puts "exiting"
    exit 0
  end

  puts "Transmitting..."
  file_size = File.size(compressed_file_name)

  File.open("#{compressed_file_name}") do |transmitted_file|
    puts "Transmitting #{transmitted_file.path.to_s} (#{file_size.to_f / 1024})"
    sent = 0
    while !(client_session.closed?) && (buffer = transmitted_file.read(self.recipe.chunk_size)) do
      client_session.write buffer
      sent += self.recipe.chunk_size
      print "\r#{"%.2f" % (sent.to_f / file_size.to_f * 100)}% " if Time.now.to_i % 3 == 0
    end
    puts "Done!"
  end

end