Class: Fig::OS
- Inherits:
-
Object
- Object
- Fig::OS
- Defined in:
- lib/fig/os.rb
Constant Summary collapse
- SUCCESS =
0
- NOT_MODIFIED =
3
- NOT_FOUND =
4
Instance Method Summary collapse
- #clear_directory(dir) ⇒ Object
- #copy(source, target) ⇒ Object
- #download(url, path) ⇒ Object
- #download_archive(url, dir) ⇒ Object
- #download_resource(url, dir) ⇒ Object
- #exec(dir, command) ⇒ Object
- #exist?(path) ⇒ Boolean
- #list(dir) ⇒ Object
- #log_info(msg) ⇒ Object
- #mtime(path) ⇒ Object
- #read(path) ⇒ Object
- #upload(local_file, remote_file, user) ⇒ Object
- #write(path, content) ⇒ Object
Instance Method Details
#clear_directory(dir) ⇒ Object
129 130 131 132 |
# File 'lib/fig/os.rb', line 129 def clear_directory(dir) FileUtils.rm_rf(dir) FileUtils.mkdir_p(dir) end |
#copy(source, target) ⇒ Object
138 139 140 141 142 |
# File 'lib/fig/os.rb', line 138 def copy(source, target) FileUtils.mkdir_p(File.dirname(target)) File.copy(source, target) target end |
#download(url, path) ⇒ Object
34 35 36 37 38 39 40 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/fig/os.rb', line 34 def download(url, path) FileUtils.mkdir_p(File.dirname(path)) uri = URI.parse(url) case uri.scheme when "ftp" ftp = Net::FTP.new(uri.host) ftp.login if File.exist?(path) && ftp.mtime(uri.path) <= File.mtime(path) return false else puts "downloading #{url}" ftp.getbinaryfile(uri.path, path, 256*1024) return true end when "http" http = Net::HTTP.new(uri.host) puts "downloading #{url}" File.open(path, "w") do |file| http.get(uri.path) do |block| file.write(block) end end when "ssh" # TODO need better way to do conditional download # timestamp = `ssh #{uri.user + '@' if uri.user}#{uri.host} "ruby -e 'puts File.mtime(\\"#{uri.path}\\").to_i'"`.to_i out = nil = File.exist?(path) ? File.mtime(path).to_i : 0 tempfile = Tempfile.new("tmp") IO.popen("ssh #{uri.user + '@' if uri.user}#{uri.host} \"fig-download #{} #{uri.path}\"") do |io| first = true while bytes = io.read(4096) if first $stderr.puts "downloading #{url}" first = false end tempfile << bytes end end tempfile.close case $?.exitstatus when NOT_MODIFIED tempfile.delete return false when NOT_FOUND tempfile.delete raise "File not found: #{uri}" when SUCCESS FileUtils.mv(tempfile.path, path) return true else tempfile.delete $stderr.puts "Unable to download file: #{$?.exitstatus}" exit 1 end else raise "Unknown protocol: #{url}" end end |
#download_archive(url, dir) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fig/os.rb', line 98 def download_archive(url, dir) FileUtils.mkdir_p(dir) basename = URI.parse(url).path.split('/').last path = File.join(dir, basename) download(url, path) case basename when /\.tar\.gz$/ fail unless system "tar -C #{dir} -zxf #{path}" when /\.tgz$/ fail unless system "tar -C #{dir} -zxf #{path}" when /\.tar\.bz2$/ fail unless system "tar -C #{dir} -jxf #{path}" when /\.zip$/ fail unless system "unzip -q -d #{dir} #{path}" else raise "Unknown archive type: #{basename}" end end |
#download_resource(url, dir) ⇒ Object
93 94 95 96 |
# File 'lib/fig/os.rb', line 93 def download_resource(url, dir) FileUtils.mkdir_p(dir) download(url, File.join(dir, URI.parse(url).path.split('/').last)) end |
#exec(dir, command) ⇒ Object
134 135 136 |
# File 'lib/fig/os.rb', line 134 def exec(dir,command) Dir.chdir(dir) { raise "Command failed" unless system command } end |
#exist?(path) ⇒ Boolean
14 15 16 |
# File 'lib/fig/os.rb', line 14 def exist?(path) File.exist?(path) end |
#list(dir) ⇒ Object
10 11 12 |
# File 'lib/fig/os.rb', line 10 def list(dir) Dir.entries(dir) - ['.','..'] end |
#log_info(msg) ⇒ Object
144 145 146 |
# File 'lib/fig/os.rb', line 144 def log_info(msg) puts msg end |
#mtime(path) ⇒ Object
18 19 20 |
# File 'lib/fig/os.rb', line 18 def mtime(path) File.mtime(path) end |
#read(path) ⇒ Object
22 23 24 |
# File 'lib/fig/os.rb', line 22 def read(path) File.read(path) end |
#upload(local_file, remote_file, user) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fig/os.rb', line 117 def upload(local_file, remote_file, user) puts "uploading #{local_file} to #{remote_file}" uri = URI.parse(remote_file) if uri.scheme == "ssh" dir = uri.path[0, uri.path.rindex('/')] cmd = "mkdir -p #{dir} && cat > #{uri.path}" fail unless system "cat #{local_file} | ssh #{uri.user + '@' if uri.user}#{uri.host} '#{cmd}'" else fail unless system "curl -p -T #{local_file} --create-dirs --ftp-create-dirs #{remote_file}" end end |
#write(path, content) ⇒ Object
26 27 28 |
# File 'lib/fig/os.rb', line 26 def write(path, content) File.open(path, "w") { |f| f << content } end |