Module: FileLoader
- Extended by:
- FileLoader
- Included in:
- FileLoader
- Defined in:
- lib/file_loader.rb,
lib/file_loader/version.rb
Constant Summary collapse
- VERSION =
"0.2.3"
Instance Attribute Summary collapse
-
#defaults ⇒ Object
Options [:permissions] b.e.
Instance Method Summary collapse
- #build_url(opts) ⇒ Object
- #download(src, dst, opts = {}) ⇒ Object
- #exists?(url, opts = {}) ⇒ Boolean
- #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
68 69 70 71 72 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 |
# File 'lib/file_loader.rb', line 68 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 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 |
#exists?(url, opts = {}) ⇒ Boolean
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 |
# File 'lib/file_loader.rb', line 41 def exists?(url, opts = {}) opts = defaults.merge(opts) purl = parse_url(url = url.shellescape) case purl[:protocol] when 'http', 'https' cmd = "wget --spider \"#{url}\"" when 'ftp' cmd = "curl -sI \"#{url}\"" when 'scp' if purl[:host] == Socket.gethostname cmd = 'test -f ' + purl[:path] else cmd = "ssh -oBatchMode=yes -oStrictHostKeyChecking=no " cmd += build_url(user: purl[:user] || opts[:user], password: purl[:password] || opts[:password], host: purl[:host]) cmd += ' "test -f ' cmd += purl[:path] cmd += '"' end when nil cmd = 'test -f ' + purl[:path] else return false end opts[:logger].debug(cmd) if opts[:logger] system(cmd + ' 2>/dev/null 1>/dev/null') 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
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/file_loader.rb', line 99 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] || opts[: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 |