Module: FileLoader
- Extended by:
- FileLoader
- Included in:
- FileLoader
- Defined in:
- lib/file_loader.rb,
lib/file_loader/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Options [:permissions] b.e.
Instance Method Summary collapse
- #build_url(opts) ⇒ Object
- #download(src, dst, opts = {}) ⇒ Object
- #parse_url(url) ⇒ Object
- #upload(src, dst, opts = {}) ⇒ Object
Instance Attribute Details
#defaults ⇒ Object
Options
- :permissions
-
b.e. ‘0777’
- :speed_limit
-
in Kbit/s
- :user
-
default user, used if url has no user
- :password
- :logger
-
Logger instance
- :retries
-
count of retries, default 1
- :delay
-
sleep seconds between retries
- :dry_run
18 19 20 |
# File 'lib/file_loader.rb', line 18 def defaults @defaults end |
Instance Method Details
#build_url(opts) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/file_loader.rb', line 29 def build_url(opts) res = '' res += "#{opts[:protocol]}://" if opts[:protocol] res += opts[:user].to_s res += ":#{opts[:password]}" if opts[:password] res += '@' if opts[:user] res += opts[:host] if opts[:host] res += ':' if opts[:host] && opts[:path] res += opts[:path] if opts[:path] res end |
#download(src, dst, opts = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/file_loader.rb', line 41 def download(src, dst, opts = {}) pdst = parse_url(dst = dst.shellescape) raise "Unsupported dst protocol #{pdst[:protocol]}" if pdst[:protocol] opts = defaults.merge(opts) psrc = parse_url(src = src.shellescape) case psrc[:protocol] when 'http', 'https', 'ftp' cmd = "wget #{src} -O #{dst}" cmd += " --limit-rate #{opts[:speed_limit] * 1024 / 8}" if opts[:speed_limit] when 'scp' if Socket.gethostname == psrc[:host] cmd = cp_cmd(psrc[:path], dst) else cmd = "scp -r -oBatchMode=yes -oStrictHostKeyChecking=no " cmd += "-l #{opts[:speed_limit]} " if opts[:speed_limit] cmd += '"' cmd += build_url(user: psrc[:user] || opts[:user], password: psrc[:password] || opts[:password], host: psrc[:host], path: psrc[:path]) cmd += '" "' cmd += dst cmd += '"' end when nil cmd = cp_cmd(src, dst) else raise "Unsupported src protocol #{psrc[:protocol]}" end cmd = mkdir_cmd(dst, opts) + ' && ' + cmd if cmd exec_cmd(cmd, opts) end |
#parse_url(url) ⇒ Object
25 26 27 |
# File 'lib/file_loader.rb', line 25 def parse_url(url) url.match(/^(?:(?<protocol>\w+):\/\/(?:(?<user>.+?)(?:\:(?<password>.+?))?@)?(?<host>.+?):?)?(?<path>\/.+)?$/) or raise("Invalid url #{url}") end |
#upload(src, dst, opts = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/file_loader.rb', line 73 def upload(src, dst, opts = {}) psrc = parse_url(src = src.shellescape) raise "Unsupported src protocol #{psrc[:protocol]}" if psrc[:protocol] opts = defaults.merge(opts) pdst = parse_url(dst = dst.shellescape) case pdst[:protocol] when 'scp' if Socket.gethostname == pdst[:host] cmd = cp_cmd(src, pdst[:path]) cmd = mkdir_cmd(pdst[:path], opts) + ' && ' + cmd if cmd else cmd = "ssh -oBatchMode=yes -oStrictHostKeyChecking=no " cmd += build_url(user: pdst[:user] || opts[:user], password: pdst[:password], host: pdst[:host]) cmd += ' "' cmd += mkdir_cmd(pdst[:path], opts) cmd += '" && scp -r -oBatchMode=yes -oStrictHostKeyChecking=no ' cmd += "-l #{opts[:speed_limit]} " if opts[:speed_limit] cmd += "#{src} \"" cmd += build_url(user: pdst[:user] || opts[:user], password: pdst[:password], host: pdst[:host], path: pdst[:path]) cmd += '"' end when nil cmd = cp_cmd(src, dst) cmd = mkdir_cmd(pdst[:path], opts) + ' && ' + cmd if cmd else raise "Unsupported protocol #{parsed_src[:protocol]}" end exec_cmd(cmd, opts) end |