photocopier 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in photocopier.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ photocopier (0.0.1)
5
+ activesupport
6
+ escape
7
+ i18n
8
+ net-scp
9
+ net-ssh
10
+ net-ssh-gateway
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ activesupport (3.2.8)
16
+ i18n (~> 0.6)
17
+ multi_json (~> 1.0)
18
+ diff-lcs (1.1.3)
19
+ escape (0.0.4)
20
+ i18n (0.6.1)
21
+ jruby-pageant (1.1.1)
22
+ multi_json (1.3.6)
23
+ net-scp (1.0.4)
24
+ net-ssh (>= 1.99.1)
25
+ net-ssh (2.6.0)
26
+ jruby-pageant (>= 1.1.1)
27
+ net-ssh-gateway (1.1.0)
28
+ net-ssh (>= 1.99.1)
29
+ rspec (2.11.0)
30
+ rspec-core (~> 2.11.0)
31
+ rspec-expectations (~> 2.11.0)
32
+ rspec-mocks (~> 2.11.0)
33
+ rspec-core (2.11.1)
34
+ rspec-expectations (2.11.3)
35
+ diff-lcs (~> 1.1.3)
36
+ rspec-mocks (2.11.3)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ photocopier!
43
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Stefano Verna
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,108 @@
1
+ # Photocopier
2
+
3
+ Photocopier provides handy FTP/SSH adapters to abstract away file and directory copying.
4
+ To move directories to/from the remote server, it wraps efficient tools like lftp and rsync.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'photocopier'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install photocopier
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require 'photocopier'
24
+
25
+ ssh = Photocopier::SSH.new(
26
+ host: 'my_host',
27
+ user: 'my_user'
28
+ )
29
+
30
+ # downloads a file and returns the content
31
+ puts ssh.get('remote_file.txt')
32
+
33
+ # downloads a file and saves the content
34
+ ssh.get('remote_file.txt', './local_file.txt')
35
+
36
+ # uploads a file with the specified content
37
+ ssh.put('foobar!', 'remote_file.txt')
38
+
39
+ # uploads a file
40
+ ssh.put('./local_file.txt', 'remote_file.txt')
41
+
42
+ # mirros the remote directory into the local machine (needs rsync on the local machine)
43
+ ssh.get_directory('remote_dir', './local_dir')
44
+
45
+ # and viceversa
46
+ ssh.put_directory('./local_dir', 'remote_dir')
47
+ ```
48
+ The very same commands are valid for the `Photocopier::FTP` adapter.
49
+
50
+ ## FTP
51
+
52
+ `Photocopier::FTP.new` accepts the following parameters (you need to pass them
53
+ all).
54
+
55
+ ```ruby
56
+ {
57
+ host: '',
58
+ user: '',
59
+ password: ''
60
+ }
61
+ ```
62
+ For performance reasons, the `.get_directory` and `.put_directory` commands make
63
+ use of `lftp`.
64
+
65
+ ## SSH Gotchas
66
+
67
+ `Photocopier::SSH.new` accepts the following parameters (you DON'T need
68
+ to pass them all).
69
+
70
+ ```ruby
71
+ {
72
+ host: '',
73
+ user: '',
74
+ password: '',
75
+ port: '',
76
+ gateway: {
77
+ host: '',
78
+ user: '',
79
+ password: '',
80
+ port: ''
81
+ }
82
+ }
83
+ ```
84
+
85
+ **TL;DR:** Avoid specifying passwords on Photocopier::SSH, and use more secure
86
+ and reliable ways to authenticate (`ssh-copy-id` anyone?).
87
+
88
+ The passwords specified will be used during `.get` and `.put` commands (which use
89
+ the pure-ruby `net-ssh` gem). For performance reasons, the `.get_directory` and
90
+ `.put_directory` commands make use of `rsync`. There's no easy way to pass SSH
91
+ passwords to `rsync`: the only way is to install a tool called [`sshpass`](http://sourceforge.net/projects/sshpass/)
92
+ on your machine (and on the gateway machine, if you also need to specify the password
93
+ of the final machine).
94
+
95
+ On Linux, you can install it with your standard package manager. On Mac, you can
96
+ have it via [`brew`](https://github.com/mxcl/homebrew):
97
+
98
+ ```
99
+ sudo brew install https://raw.github.com/gist/1513663/3e98bf9e03feb7e31eeddcd08f89ca86163a376d/sshpass.rb
100
+ ```
101
+
102
+ ## Contributing
103
+
104
+ 1. Fork it
105
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
106
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
107
+ 4. Push to the branch (`git push origin my-new-feature`)
108
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,36 @@
1
+ require 'escape'
2
+ require 'tempfile'
3
+ require 'logger'
4
+
5
+ module Photocopier
6
+
7
+ class Adapter
8
+ attr_accessor :logger
9
+
10
+ def get(remote_path, file_path = nil); end
11
+
12
+ def put(file_path_or_string, remote_path)
13
+ if File.exists? file_path_or_string
14
+ put_file(file_path_or_string, remote_path)
15
+ else
16
+ file = Tempfile.new('put')
17
+ file.write file_path_or_string
18
+ file.close
19
+ put_file(file.path, remote_path)
20
+ file.unlink
21
+ end
22
+ end
23
+
24
+ def delete(remote_path); end
25
+
26
+ def get_directory(remote_path, local_path); end
27
+ def put_directory(local_path, remote_path); end
28
+
29
+ protected
30
+
31
+ def run(*args)
32
+ system Escape.shell_command(args)
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,89 @@
1
+ require 'net/ftp'
2
+ require 'photocopier/adapter'
3
+ require 'fileutils'
4
+
5
+ module Photocopier
6
+ class FTP < Adapter
7
+
8
+ def initialize(options)
9
+ @options = options
10
+ end
11
+
12
+ def options
13
+ @options.clone
14
+ end
15
+
16
+ def get(remote_path, file_path = nil)
17
+ session.get remote_path, file_path
18
+ end
19
+
20
+ def put_file(file_path, remote_path)
21
+ session.put file_path, remote_path
22
+ end
23
+
24
+ def delete(remote_path)
25
+ session.delete(remote_path)
26
+ end
27
+
28
+ def get_directory(remote_path, local_path)
29
+ FileUtils.mkdir_p(local_path)
30
+ lftp(local_path, remote_path, false)
31
+ end
32
+
33
+ def put_directory(local_path, remote_path)
34
+ lftp(local_path, remote_path, true)
35
+ end
36
+
37
+ private
38
+
39
+ def lftp(local, remote, reverse)
40
+ run "lftp",
41
+ "-c",
42
+ [
43
+ "set ftp:list-options -a",
44
+ "open #{remote_ftp_url}",
45
+ "mkdir -p #{remote}",
46
+ "cd #{remote}",
47
+ "lcd #{local}",
48
+ lftp_mirror_arguments(reverse)
49
+ ].join("; ")
50
+ end
51
+
52
+ def remote_ftp_url
53
+ url = "ftp://"
54
+ if options[:user].present?
55
+ url << options[:user]
56
+ url << ":#{options[:password]}" if options[:password].present?
57
+ url << "@"
58
+ end
59
+ url << options[:host]
60
+ url
61
+ end
62
+
63
+ def lftp_mirror_arguments(reverse)
64
+ mirror = "mirror --delete --use-cache --verbose --allow-chown --allow-suid --no-umask --parallel=2"
65
+ mirror << " --reverse" if reverse
66
+ mirror
67
+ end
68
+
69
+ def lftp_mirrir_arguments(local, remote, reverse)
70
+
71
+ arguments = []
72
+ if gateway_options.any?
73
+ arguments << ssh_command(gateway_options)
74
+ end
75
+ arguments << ssh_command(options)
76
+ arguments.join(" ")
77
+ end
78
+
79
+ def session
80
+ opts = options
81
+ host = opts.delete(:host)
82
+ user = opts.delete(:user)
83
+ password = opts.delete(:password)
84
+ @session ||= Net::FTP.open(host, user, password)
85
+ end
86
+
87
+ end
88
+
89
+ end
@@ -0,0 +1,93 @@
1
+ require 'net/ssh'
2
+ require 'net/ssh/gateway'
3
+ require 'net/scp'
4
+ require 'fileutils'
5
+
6
+ require 'photocopier/adapter'
7
+
8
+ module Photocopier
9
+ class SSH < Adapter
10
+
11
+ def initialize(options)
12
+ @options = options
13
+ end
14
+
15
+ def options
16
+ @options.clone
17
+ end
18
+
19
+ def get(remote_path, file_path = nil)
20
+ session.scp.download! remote_path, file_path
21
+ end
22
+
23
+ def put_file(file_path, remote_path)
24
+ session.scp.upload! file_path, remote_path
25
+ end
26
+
27
+ def delete(remote_path)
28
+ session.exec!("rm -rf #{remote_path}")
29
+ end
30
+
31
+ def get_directory(remote_path, local_path)
32
+ FileUtils.mkdir_p(local_path)
33
+ rsync ":#{remote_path}", local_path
34
+ end
35
+
36
+ def put_directory(local_path, remote_path)
37
+ rsync local_path, ":#{remote_path}"
38
+ end
39
+
40
+ private
41
+
42
+ def rsync(source, destination)
43
+ run "rsync", "--progress", "-e", rsh_arguments, "--archive", "--compress",
44
+ "--omit-dir-times", "--delete", "#{source}/", destination
45
+ end
46
+
47
+ def rsh_arguments
48
+ arguments = []
49
+ if gateway_options.any?
50
+ arguments << ssh_command(gateway_options, options[:sshpass])
51
+ end
52
+ arguments << ssh_command(options, gateway_options[:sshpass])
53
+ arguments.join(" ")
54
+ end
55
+
56
+ def ssh_command(opts, use_sshpass=true)
57
+ command = "ssh "
58
+ command << "-p #{opts[:port]} " if opts[:port].present?
59
+ command << "#{opts[:user]}@" if opts[:user].present?
60
+ command << opts[:host]
61
+ if opts[:password] && use_sshpass
62
+ command = "sshpass -p #{opts[:password]} #{command}"
63
+ end
64
+ command
65
+ end
66
+
67
+ def session
68
+ opts = options
69
+ host = opts.delete(:host)
70
+ user = opts.delete(:user)
71
+ opts.delete(:gateway)
72
+ opts.delete(:sshpass)
73
+ @session ||= if gateway_options.any?
74
+ gateway.ssh(host, user, opts)
75
+ else
76
+ Net::SSH.start(host, user, opts)
77
+ end
78
+ end
79
+
80
+ def gateway
81
+ opts = gateway_options
82
+ host = opts.delete(:host)
83
+ user = opts.delete(:user)
84
+ opts.delete(:sshpass)
85
+ @gateway ||= Net::SSH::Gateway.new(host, user, opts)
86
+ end
87
+
88
+ def gateway_options
89
+ options[:gateway] || {}
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,3 @@
1
+ module Photocopier
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "active_support/core_ext"
2
+
3
+ require "photocopier/ssh"
4
+ require "photocopier/ftp"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/photocopier/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Stefano Verna"]
6
+ gem.email = ["stefano.verna@welaika.com"]
7
+ gem.description = %q{Photocopier provides FTP/SSH adapters to abstract away file and directory copying.}
8
+ gem.summary = %q{Photocopier provides FTP/SSH adapters to abstract away file and directory copying.}
9
+ gem.homepage = "https://github.com/stefanoverna/photocopier"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "photocopier"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Photocopier::VERSION
17
+
18
+ gem.add_dependency "activesupport"
19
+ gem.add_dependency "i18n"
20
+ gem.add_dependency "net-ssh"
21
+ gem.add_dependency "net-scp"
22
+ gem.add_dependency "net-ssh-gateway"
23
+ gem.add_dependency "escape"
24
+
25
+ gem.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: photocopier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefano Verna
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: i18n
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: net-ssh
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: net-scp
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: net-ssh-gateway
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: escape
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Photocopier provides FTP/SSH adapters to abstract away file and directory
127
+ copying.
128
+ email:
129
+ - stefano.verna@welaika.com
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - .gitignore
135
+ - Gemfile
136
+ - Gemfile.lock
137
+ - LICENSE
138
+ - README.md
139
+ - Rakefile
140
+ - lib/photocopier.rb
141
+ - lib/photocopier/adapter.rb
142
+ - lib/photocopier/ftp.rb
143
+ - lib/photocopier/ssh.rb
144
+ - lib/photocopier/version.rb
145
+ - photocopier.gemspec
146
+ homepage: https://github.com/stefanoverna/photocopier
147
+ licenses: []
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 1.8.23
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: Photocopier provides FTP/SSH adapters to abstract away file and directory
170
+ copying.
171
+ test_files: []