file_copy 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/file_copy/copy.rb +26 -4
  2. metadata +2 -2
@@ -6,11 +6,12 @@ module BBFS
6
6
 
7
7
  # Creates ssh connection, assumes username and password
8
8
  # or without password but with ssh keys.
9
- def self.ssh_connect(username, password, server)
9
+ def ssh_connect(username, password, server)
10
10
  username = (username and username.length > 0) ? username : ENV['USER']
11
11
  password = (password and password.length > 0) ? password : nil
12
12
  port = 22 # 22 is a standart SSH port
13
13
  raise "Undefined server" unless server
14
+ p "Trying to connect(ssh): #{username}, #{password}, #{server}."
14
15
  if (username and password)
15
16
  Net::SSH.start(server, username,
16
17
  :password => password,
@@ -22,22 +23,43 @@ module BBFS
22
23
  raise "Undefined username"
23
24
  end
24
25
  end
26
+ module_function :ssh_connect
25
27
 
26
28
  # Simply copy map files using sftp server on dest_server.
27
29
  # Note: stfp.upload support parallel uploads - default is 2.
28
30
  # TODO(kolman): packing files, all, not all, determine by part of file.
29
- def self.sftp_copy(username, password, server, files_map)
31
+ def sftp_copy(username, password, server, files_map)
30
32
  ssh = FileCopy.ssh_connect(username, password, server)
31
33
  ssh.sftp.connect do |sftp|
32
34
  uploads = files_map.map { |from,to|
33
- # TODO(kolman): Create destination directory is not exists.
35
+ remote_dir = File.dirname(to)
36
+ sftp_mkdir_recursive(sftp, File.dirname(to))
34
37
  p "Copying #{from} to #{to}"
35
38
  sftp.upload(from, to)
36
39
  }
37
40
  uploads.each { |u| u.wait }
38
41
  p "Done."
39
- end # sftp.connect
42
+ end # ssh.sftp.connect
40
43
  end # def initialize
44
+ module_function :sftp_copy
45
+
46
+ def sftp_mkdir_recursive(sftp, path)
47
+ dir_stat = nil
48
+ begin
49
+ p "Stat remote dir: #{path}."
50
+ dir_stat = sftp.stat!(path).directory?
51
+ p "Stat result #{dir_stat}."
52
+ rescue Net::SFTP::StatusException
53
+ end
54
+ if !dir_stat
55
+ p "Directory does not exists: #{path}."
56
+ sftp_mkdir_recursive sftp, File.dirname(path)
57
+ sftp.mkdir!(path)
58
+ end
59
+ end
60
+ module_function :sftp_mkdir_recursive
61
+
62
+
41
63
 
42
64
  end
43
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_copy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-08 00:00:00.000000000Z
12
+ date: 2012-04-11 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Library for copying files to remote servers.
15
15
  email: kolmanv@gmail.com