fiverr_copy 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bin/fiverr_copy +55 -0
- data/fiverr_copy.gemspec +19 -0
- data/lib/fiverr_copy/client.rb +76 -0
- data/lib/fiverr_copy/recipe.rb +28 -0
- data/lib/fiverr_copy/server.rb +43 -0
- data/lib/fiverr_copy/version.rb +3 -0
- data/lib/fiverr_copy.rb +15 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Elad Meidar
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# FiverrCopy
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fiverr_copy'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fiverr_copy
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/fiverr_copy
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'lib/fiverr_copy'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
|
8
|
+
optparse = OptionParser.new do |opts|
|
9
|
+
# Set a banner, displayed at the top
|
10
|
+
# of the help screen.
|
11
|
+
opts.banner = "Usage: fiverr_copy [options]"
|
12
|
+
|
13
|
+
options[:mode] = :client
|
14
|
+
opts.on( '-c', '--client', 'Run as client' ) do
|
15
|
+
options[:mode] = :client
|
16
|
+
end
|
17
|
+
|
18
|
+
opts.on( '-s', '--server', 'Run as server' ) do
|
19
|
+
options[:mode] = :server
|
20
|
+
end
|
21
|
+
|
22
|
+
options[:port] = 1234
|
23
|
+
opts.on( '-p', '--port PORT', 'Select a port. (default: 1234)' ) do |port|
|
24
|
+
options[:port] = port.to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
options[:recipe] = nil
|
28
|
+
opts.on( '-r', '--recipe FILE', 'Specify a recipe file' ) do |recipe|
|
29
|
+
options[:recipe] = recipe
|
30
|
+
end
|
31
|
+
|
32
|
+
options[:next_hop] = nil
|
33
|
+
opts.on( '-n', '--next_hop IP/HOST', 'Specify the next hop for distribution' ) do |next_hop|
|
34
|
+
options[:next_hop] = next_hop
|
35
|
+
end
|
36
|
+
|
37
|
+
# This displays the help screen, all programs are
|
38
|
+
# assumed to have this option.
|
39
|
+
opts.on( '-h', '--help', 'Display this screen' ) do
|
40
|
+
puts opts
|
41
|
+
exit
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
optparse.parse!
|
46
|
+
|
47
|
+
if options[:recipe].nil? || !(FileTest.exists?(options[:recipe]))
|
48
|
+
puts "No recipe specified, do it with -r or --recipe"
|
49
|
+
exit 0
|
50
|
+
end
|
51
|
+
|
52
|
+
require options[:recipe]
|
53
|
+
|
54
|
+
FiverrCopy::Server.run!
|
55
|
+
|
data/fiverr_copy.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/fiverr_copy/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Elad Meidar"]
|
6
|
+
gem.email = ["elad@fiverr.com"]
|
7
|
+
gem.description = %q{A gem that automates efficient distribution of files between servers.}
|
8
|
+
gem.summary = %q{This gem uses pipes and streaming to distribute a file in a column of servers.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "fiverr_copy"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = FiverrCopy::VERSION
|
17
|
+
gem.add_dependency "net-ssh"
|
18
|
+
gem.add_dependency "OptionParser"
|
19
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
class FiverrCopy::Client
|
2
|
+
|
3
|
+
attr_reader :next_client, :client_ip, :username, :port
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
#options.assert_valid_keys :client_ip, :next_client, :username, :port
|
7
|
+
@next_client = options[:next_client] ||= nil
|
8
|
+
@username = options[:username] ||= "admin"
|
9
|
+
@client_ip = options[:client_ip] || raise("No client specified")
|
10
|
+
@port = options[:port] ||= 1234
|
11
|
+
@ready = false
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def forward?
|
16
|
+
!(@next_client.nil?)
|
17
|
+
end
|
18
|
+
|
19
|
+
def ready?
|
20
|
+
@ready
|
21
|
+
end
|
22
|
+
|
23
|
+
def setup!
|
24
|
+
puts "Setting up #{self.client_ip}"
|
25
|
+
Net::SSH.start(self.client_ip, self.username) do |ssh|
|
26
|
+
bundler_exists = ssh.exec!("which bundle") == "" ? false : true
|
27
|
+
if !(bundler_exists)
|
28
|
+
puts "Installing bundler on #{client_ip}."
|
29
|
+
puts ssh.exec!("gem install bundler")
|
30
|
+
end
|
31
|
+
|
32
|
+
fiverr_copy_exists = ssh.exec!("gem list | grep fiverr_copy") == "" ? false : true
|
33
|
+
|
34
|
+
if !(fiverr_copy_exists)
|
35
|
+
puts "Installing fiverr_copy."
|
36
|
+
puts ssh.exec!("gem install fiverr_copy")
|
37
|
+
end
|
38
|
+
|
39
|
+
@ready = true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def run!(port, next_client)
|
45
|
+
server = TCPServer.new(port)
|
46
|
+
client_session = nil
|
47
|
+
if !(next_client.nil?)
|
48
|
+
client_session = TCPSocket.new(next_client, port)
|
49
|
+
end
|
50
|
+
|
51
|
+
@real_file_name = ""
|
52
|
+
temp_file = "fiverr_copy_#{Time.now.to_i}"
|
53
|
+
TempFile.open(temp_file) do |f|
|
54
|
+
while (session = server.accept)
|
55
|
+
Thread.start do
|
56
|
+
## I want to be sure to output something on the server side
|
57
|
+
## to show that there has been a connection
|
58
|
+
puts "log: Connection from #{session.peeraddr[2]} at #{session.peeraddr[3]}"
|
59
|
+
|
60
|
+
stream = session.gets
|
61
|
+
|
62
|
+
if (stream.slice(0...8) == "FILENAME")
|
63
|
+
@real_file_name = stream.chomp.split(":").last
|
64
|
+
puts "log: Recieving #{@real_file_name}"
|
65
|
+
else
|
66
|
+
puts "log: got #{stream.size} bytes"
|
67
|
+
f.write stream
|
68
|
+
end
|
69
|
+
client_session.puts(stream) if !(next_client.nil?)
|
70
|
+
session.puts "OK:#{stream.size}"
|
71
|
+
end #end thread conversation
|
72
|
+
end #end loop
|
73
|
+
end
|
74
|
+
FileUtils.mv(temp_file, "~/#{@real_file_name}")
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class FiverrCopy::Recipe
|
2
|
+
attr_accessor :name, :description, :filter, :clients, :username, :port
|
3
|
+
|
4
|
+
attr_reader :initialized_clients
|
5
|
+
|
6
|
+
def initialize(name = nil, description = "A FiverrCopy Recipe", &block)
|
7
|
+
raise "Please specifiy a name for a Recipe" if name.nil?
|
8
|
+
self.name = name
|
9
|
+
self.description = description
|
10
|
+
self.port = 1234
|
11
|
+
@initialized_clients = []
|
12
|
+
yield(self)
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def clients=(clients_array = [])
|
17
|
+
raise "No clients specified" if clients_array.empty?
|
18
|
+
clients_array.uniq!
|
19
|
+
next_hop = nil
|
20
|
+
clients_array.each do |client|
|
21
|
+
self.initialized_clients << FiverrCopy::Client.new(:client_ip => client,
|
22
|
+
:username => self.username,
|
23
|
+
:next_client => next_hop,
|
24
|
+
:port => self.port)
|
25
|
+
next_hop = client
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class FiverrCopy::Server
|
2
|
+
|
3
|
+
@@recipe = nil
|
4
|
+
|
5
|
+
def self.recipe=(recipe)
|
6
|
+
@@recipe = recipe
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.recipe
|
10
|
+
@@recipe
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.run!
|
14
|
+
threads = []
|
15
|
+
files_to_sync = Dir[self.recipe.filter]
|
16
|
+
puts "Total Clients: #{self.recipe.initialized_clients.size}"
|
17
|
+
self.recipe.initialized_clients.each do |client|
|
18
|
+
threads << Thread.new(client) do |current_client|
|
19
|
+
current_client.setup!
|
20
|
+
Net::SSH.start(current_client.client_ip, current_client.username) do |ssh|
|
21
|
+
ssh.exec("fiverr_copy --client -p #{current_client.port} -n #{current_client.next_client}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
threads.collect(&:join)
|
27
|
+
compressed_file_name = "fiverr_copy_compressed_#{Time.now.to_i}"
|
28
|
+
|
29
|
+
begin
|
30
|
+
client_session = TCPSocket.new(self.recipe.initialized_clients.first.client_ip, self.recipe.port)
|
31
|
+
puts "Compressing: #{files_to_sync.inspect}"
|
32
|
+
`tar cv #{files_to_sync.join(" ")} #{compressed_file_name}`
|
33
|
+
rescue Errno::ECONNREFUSED => e
|
34
|
+
puts "ERROR: unable to connect to #{self.recipe.initialized_clients.first.client_ip} => #{e.to_s}"
|
35
|
+
end
|
36
|
+
|
37
|
+
File.open(compressed_file_name) do |transmitted_file|
|
38
|
+
while (buffer = transmitted_file.read(size)) do
|
39
|
+
client_session.puts buffer
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/fiverr_copy.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
require "fiverr_copy/version"
|
3
|
+
require 'fiverr_copy/server'
|
4
|
+
require 'fiverr_copy/client'
|
5
|
+
require 'fiverr_copy/recipe'
|
6
|
+
|
7
|
+
require 'net/ssh'
|
8
|
+
require 'socket'
|
9
|
+
require 'tempfile'
|
10
|
+
require 'fileutils'
|
11
|
+
require 'optparse'
|
12
|
+
|
13
|
+
module FiverrCopy
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fiverr_copy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Elad Meidar
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-04-01 00:00:00 +03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: net-ssh
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: OptionParser
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
description: A gem that automates efficient distribution of files between servers.
|
50
|
+
email:
|
51
|
+
- elad@fiverr.com
|
52
|
+
executables:
|
53
|
+
- fiverr_copy
|
54
|
+
extensions: []
|
55
|
+
|
56
|
+
extra_rdoc_files: []
|
57
|
+
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- Gemfile
|
61
|
+
- LICENSE
|
62
|
+
- README.md
|
63
|
+
- Rakefile
|
64
|
+
- bin/fiverr_copy
|
65
|
+
- fiverr_copy.gemspec
|
66
|
+
- lib/fiverr_copy.rb
|
67
|
+
- lib/fiverr_copy/client.rb
|
68
|
+
- lib/fiverr_copy/recipe.rb
|
69
|
+
- lib/fiverr_copy/server.rb
|
70
|
+
- lib/fiverr_copy/version.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: ""
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.3.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: This gem uses pipes and streaming to distribute a file in a column of servers.
|
105
|
+
test_files: []
|
106
|
+
|