file_copy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'net/ssh'
2
+ require 'net/sftp'
3
+
4
+ module BBFS
5
+ module FileCopy
6
+
7
+ # Creates ssh connection, assumes username and password
8
+ # or without password but with ssh keys.
9
+ def self.ssh_connect(username, password, server)
10
+ username = (username and username.length > 0) ? username : ENV['USER']
11
+ password = (password and password.length > 0) ? password : nil
12
+ port = 22 # 22 is a standart SSH port
13
+ raise "Undefined server" unless server
14
+ if (username and password)
15
+ Net::SSH.start(server, username,
16
+ :password => password,
17
+ :port => port)
18
+ elsif (username)
19
+ Net::SSH.start(server, username,
20
+ :port => port)
21
+ else
22
+ raise "Undefined username"
23
+ end
24
+ end
25
+
26
+ # Simply copy map files using sftp server on dest_server.
27
+ # Note: stfp.upload support parallel uploads - default is 2.
28
+ # TODO(kolman): packing files, all, not all, determine by part of file.
29
+ def self.sftp_copy(username, password, server, files_map)
30
+ ssh = FileCopy.ssh_connect(username, password, server)
31
+ ssh.sftp.connect do |sftp|
32
+ uploads = files_map.map { |from,to|
33
+ # TODO(kolman): Create destination directory is not exists.
34
+ p "Copying #{from} to #{to}"
35
+ sftp.upload(from, to)
36
+ }
37
+ uploads.each { |u| u.wait }
38
+ p "Done."
39
+ end # sftp.connect
40
+ end # def initialize
41
+
42
+ end
43
+ end
44
+
data/lib/file_copy.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative 'file_copy/copy'
2
+
3
+ module BBFS
4
+ module FileCopy
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,47 @@
1
+ require_relative '../../lib/file_copy/copy.rb'
2
+
3
+ module BBFS
4
+ module FileCopy
5
+ module Spec
6
+
7
+ describe 'FileCopy::ssh_connect' do
8
+ it 'should raise error when username not specified' do
9
+ ENV.stub(:[]).with(any_args()).and_return(nil)
10
+ expect { FileCopy::ssh_connect(nil, nil, 'a server') }.should raise_error "Undefined username"
11
+ end
12
+
13
+ it 'should raise error when server not specified' do
14
+ expect { FileCopy::ssh_connect('kuku', nil, nil) }.should raise_error "Undefined server"
15
+ end
16
+
17
+ it 'should try to connect if username is set explicitly' do
18
+ Net::SSH.should_receive(:start).with(any_args())
19
+ FileCopy::ssh_connect('kuku', nil, 'a server')
20
+ end
21
+ it 'should try to connect if username is set via ENV variable' do
22
+ Net::SSH.should_receive(:start).with(any_args())
23
+ ENV.stub(:[]).with("USER").and_return('kuku')
24
+ FileCopy::ssh_connect(nil, nil, 'a server')
25
+ end
26
+ end
27
+
28
+ # TODO(kolman): Very bad test, should rewrite and understand how to
29
+ # write test correctly.
30
+ describe 'FileCopy::sftp_copy' do
31
+ it 'should copy files' do
32
+ ssh_connection = double('Net::SSH::Connection')
33
+ FileCopy.should_receive(:ssh_connect).with(any_args()).and_return(ssh_connection)
34
+ sftp_session = double('Net::SFTP::Session')
35
+ ssh_connection.should_receive(:sftp).with(any_args()).and_return(sftp_session)
36
+ sftp_session.should_receive(:connect).with(any_args()).and_yield(sftp_session)
37
+ uploader = double('Operations::Upload')
38
+ uploader.should_receive(:wait).with(any_args()).and_return(true, true)
39
+ sftp_session.should_receive(:upload).with('a', 'b').and_return(uploader)
40
+ sftp_session.should_receive(:upload).with('c', 'd').and_return(uploader)
41
+ FileCopy::sftp_copy(nil, nil, nil, { 'a' => 'b', 'c' => 'd' })
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_copy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gena Petelko, Kolman Vornovitsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-08 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Library for copying files to remote servers.
15
+ email: kolmanv@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/file_copy.rb
21
+ - lib/file_copy/copy.rb
22
+ - test/file_copy/copy_spec.rb
23
+ homepage: http://github.com/kolmanv/bbfs
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.15
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Library for copying files remotely.
47
+ test_files:
48
+ - test/file_copy/copy_spec.rb