Class: SFTP
Instance Method Summary collapse
-
#download(config, vault) ⇒ Object
download the vault via sftp to the location specified in the configuration.
-
#file_exists(sftp, file) ⇒ Object
check if the file exists on the given sftp connection.
-
#get_dir_listing(sftp, dir) ⇒ Object
get the file listing of a remote directory.
-
#get_sftp_config(config) ⇒ Object
check config for section lockr and subsection sftp.
-
#rotate_sftp_file(sftp, file, limit) ⇒ Object
rotate the provided file with a maximum of ‘limit’ backups renamed filed will be named file_0, file_1, …
-
#upload(config, vault) ⇒ Object
upload the vault via sftp to the location specified in the configuration.
Methods included from LockrFileUtils
calculate_sha512_hash, copy, #load_from_vault, load_obj_yaml, rotate_file, #save_to_vault, store_obj_yaml
Instance Method Details
#download(config, vault) ⇒ Object
download the vault via sftp to the location specified in the configuration
63 64 65 66 67 68 69 70 71 |
# File 'lib/lockr/sftp.rb', line 63 def download( config, vault) cfg_sftp = get_sftp_config( config) Net::SFTP.start( cfg_sftp[:hostname], cfg_sftp[:username]) do |sftp| # download a file or directory from the remote host sftp.download!( File.join( cfg_sftp[:directory], File.basename(vault)), vault) puts "Downloaded vault from host '#{cfg_sftp[:hostname]}' by SFTP" end end |
#file_exists(sftp, file) ⇒ Object
check if the file exists on the given sftp connection
46 47 48 49 |
# File 'lib/lockr/sftp.rb', line 46 def file_exists( sftp, file) files = get_dir_listing( sftp, File.dirname(file)) files.include?( File.basename(file)) end |
#get_dir_listing(sftp, dir) ⇒ Object
get the file listing of a remote directory
52 53 54 55 56 57 58 59 60 |
# File 'lib/lockr/sftp.rb', line 52 def get_dir_listing( sftp, dir) list = [] sftp.dir.foreach(dir) do |entry| list << entry.name end Set.new(list) end |
#get_sftp_config(config) ⇒ Object
check config for section lockr and subsection sftp. then check for keys :hostname, :username and :directory. if anything is missing, raise ArgumentError returns sftp configuration hash
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lockr/sftp.rb', line 76 def get_sftp_config( config) unless config.config.has_key?( :lockr) raise ArgumentError, 'config has no "lockr" section' end cfg = config.config[:lockr] unless cfg.has_key?( :sftp) raise ArgumentError, 'config has no "sftp" section' end cfg_sftp = cfg[:sftp] unless cfg_sftp.has_key?( :hostname) and cfg_sftp.has_key?( :username) and cfg_sftp.has_key?( :directory) raise ArgumentError, 'config "sftp" section must have keys :host, :username and :directory' end cfg_sftp end |
#rotate_sftp_file(sftp, file, limit) ⇒ Object
rotate the provided file with a maximum of ‘limit’ backups renamed filed will be named file_0, file_1, …
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lockr/sftp.rb', line 26 def rotate_sftp_file( sftp, file, limit) return unless file_exists( sftp, file) # move old files first max_files = limit - 1 max_files.downto( 0) { |i| if i == 0 sftp.rename( file, "#{file}_#{i}") else j = i - 1 next unless file_exists( sftp, "#{file}_#{j}") sftp.rename( "#{file}_#{j}", "#{file}_#{i}") end } puts "Rotated remote vault file(s)" end |
#upload(config, vault) ⇒ Object
upload the vault via sftp to the location specified in the configuration
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/lockr/sftp.rb', line 10 def upload( config, vault) cfg_sftp = get_sftp_config( config) remote_file = File.join( cfg_sftp[:directory], File.basename(vault)) Net::SFTP.start( cfg_sftp[:hostname], cfg_sftp[:username]) do |sftp| # TODO check remote checksum to make sure upload is necessary rotate_sftp_file( sftp, remote_file, 3) # upload a file or directory to the remote host sftp.upload!( vault, remote_file) puts "Uploaded vault to host '#{cfg_sftp[:hostname]}' by SFTP" end end |