Class: Fig::OperatingSystem
- Inherits:
-
Object
- Object
- Fig::OperatingSystem
- Defined in:
- lib/fig/operatingsystem.rb
Overview
Does things requiring real O/S interaction, primarilly taking care of file transfers and running external commands.
Constant Summary collapse
- SUCCESS =
0
- NOT_MODIFIED =
3
- NOT_FOUND =
4
Class Method Summary collapse
- .get_environment_variables(initial_values = nil) ⇒ Object
- .java? ⇒ Boolean
- .unix? ⇒ Boolean
- .windows? ⇒ Boolean
- .wrap_variable_name_with_shell_expansion(variable_name) ⇒ Object
Instance Method Summary collapse
- #copy(source, target, msg = nil) ⇒ Object
-
#create_archive(archive_name, files_to_archive) ⇒ Object
Expects files_to_archive as an Array of filenames.
- #delete_and_recreate_directory(dir) ⇒ Object
-
#download(url, path) ⇒ Object
Returns whether the file was not downloaded because the file already exists and is already up-to-date.
- #download_and_unpack_archive(url, dir) ⇒ Object
- #download_ftp_list(uri, dirs) ⇒ Object
- #download_list(url) ⇒ Object
- #download_resource(url, dir) ⇒ Object
- #ftp_login(ftp, host) ⇒ Object
- #get_password ⇒ Object
- #get_username ⇒ Object
-
#initialize(login) ⇒ OperatingSystem
constructor
A new instance of OperatingSystem.
- #list(dir) ⇒ Object
- #log_info(msg) ⇒ Object
- #move_file(dir, from, to) ⇒ Object
- #mtime(path) ⇒ Object
- #shell_exec(cmd) ⇒ Object
- #strip_paths_for_list(ls_output, packages, path) ⇒ Object
-
#unpack_archive(dir, file) ⇒ Object
This method can handle the following archive types: .tar.bz2 .tar.gz .tgz .zip.
- #upload(local_file, remote_file, user) ⇒ Object
- #write(path, content) ⇒ Object
Constructor Details
#initialize(login) ⇒ OperatingSystem
Returns a new instance of OperatingSystem.
28 29 30 31 32 |
# File 'lib/fig/operatingsystem.rb', line 28 def initialize(login) @login = login @username = ENV['FIG_USERNAME'] @password = ENV['FIG_PASSWORD'] end |
Class Method Details
.get_environment_variables(initial_values = nil) ⇒ Object
398 399 400 401 402 403 404 |
# File 'lib/fig/operatingsystem.rb', line 398 def self.get_environment_variables(initial_values = nil) if Fig::OperatingSystem.windows? return Fig::EnvironmentVariables::CaseInsensitive.new(initial_values) end return Fig::EnvironmentVariables::CaseSensitive.new(initial_values) end |
.java? ⇒ Boolean
368 369 370 |
# File 'lib/fig/operatingsystem.rb', line 368 def self.java? RUBY_PLATFORM == 'java' end |
.unix? ⇒ Boolean
372 373 374 |
# File 'lib/fig/operatingsystem.rb', line 372 def self.unix? !windows? end |
.windows? ⇒ Boolean
364 365 366 |
# File 'lib/fig/operatingsystem.rb', line 364 def self.windows? RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ end |
.wrap_variable_name_with_shell_expansion(variable_name) ⇒ Object
390 391 392 393 394 395 396 |
# File 'lib/fig/operatingsystem.rb', line 390 def self.wrap_variable_name_with_shell_expansion(variable_name) if Fig::OperatingSystem.windows? return "%#{variable_name}%" else return "$#{variable_name}" end end |
Instance Method Details
#copy(source, target, msg = nil) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/fig/operatingsystem.rb', line 291 def copy(source, target, msg = nil) if File.directory?(source) FileUtils.mkdir_p(target) Dir.foreach(source) do |child| if child != '.' and child != '..' copy(File.join(source, child), File.join(target, child), msg) end end else if !File.exist?(target) || File.mtime(source) != File.mtime(target) log_info "#{msg} #{target}" if msg FileUtils.mkdir_p(File.dirname(target)) FileUtils.cp(source, target) File.utime(File.atime(source), File.mtime(source), target) end end end |
#create_archive(archive_name, files_to_archive) ⇒ Object
Expects files_to_archive as an Array of filenames.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/fig/operatingsystem.rb', line 318 def create_archive(archive_name, files_to_archive) if Fig::OperatingSystem.java? `tar czvf #{archive_name} #{files_to_archive.join(' ')}` else # TODO: Need to verify files_to_archive exists. ::Archive.write_open_filename( archive_name, ::Archive::COMPRESSION_GZIP, ::Archive::FORMAT_TAR ) do |writer| files_to_archive.each do |file_name| writer.new_entry do |entry| entry.copy_lstat(file_name) entry.pathname = file_name if entry.symbolic_link? linked = File.readlink(file_name) entry.symlink = linked end writer.write_header(entry) if entry.regular? writer.write_data(open(file_name) {|f| f.binmode; f.read }) end end end end end end |
#delete_and_recreate_directory(dir) ⇒ Object
286 287 288 289 |
# File 'lib/fig/operatingsystem.rb', line 286 def delete_and_recreate_directory(dir) FileUtils.rm_rf(dir) FileUtils.mkdir_p(dir) end |
#download(url, path) ⇒ Object
Returns whether the file was not downloaded because the file already exists and is already up-to-date.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/fig/operatingsystem.rb', line 161 def download(url, path) FileUtils.mkdir_p(File.dirname(path)) uri = URI.parse(url) case uri.scheme when 'ftp' begin ftp = Net::FTP.new(uri.host) ftp_login(ftp, uri.host) if File.exist?(path) && ftp.mtime(uri.path) <= File.mtime(path) Fig::Logging.debug "#{path} is up to date." return false else log_download(url, path) ftp.getbinaryfile(uri.path, path, 256*1024) return true end rescue Net::FTPPermError => error Fig::Logging.debug error. raise Fig::NotFoundError.new rescue SocketError => error Fig::Logging.debug error. raise Fig::NotFoundError.new end when 'http' log_download(url, path) File.open(path, 'wb') do |file| file.binmode begin download_via_http_get(url, file) rescue SystemCallError => error Fig::Logging.debug error. raise Fig::NotFoundError.new rescue SocketError => error Fig::Logging.debug error. raise Fig::NotFoundError.new end end when 'ssh' # TODO need better way to do conditional download = File.exist?(path) ? File.mtime(path).to_i : 0 # Requires that remote installation of fig be at the same location as the local machine. cmd = `which fig-download`.strip + " #{} #{uri.path}" log_download(url, path) ssh_download(uri.user, uri.host, path, cmd) when 'file' begin FileUtils.cp(uri.path, path) return true rescue Errno::ENOENT raise Fig::NotFoundError.new end else Fig::Logging.fatal "Unknown protocol: #{url}" raise Fig::NetworkError.new("Unknown protocol: #{url}") end end |
#download_and_unpack_archive(url, dir) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/fig/operatingsystem.rb', line 225 def download_and_unpack_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$/ unpack_archive(dir, path) when /\.tgz$/ unpack_archive(dir, path) when /\.tar\.bz2$/ unpack_archive(dir, path) when /\.zip$/ unpack_archive(dir, path) else Fig::Logging.fatal "Unknown archive type: #{basename}" raise Fig::NetworkError.new("Unknown archive type: #{basename}") end end |
#download_ftp_list(uri, dirs) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/fig/operatingsystem.rb', line 120 def download_ftp_list(uri, dirs) # Run a bunch of these in parallel since they're slow as hell num_threads = (ENV['FIG_FTP_THREADS'] || '16').to_i threads = [] all_packages = [] (0..num_threads-1).each { |num| all_packages[num] = [] } (0..num_threads-1).each do |num| threads << Thread.new do packages = all_packages[num] ftp = Net::FTP.new(uri.host) ftp_login(ftp, uri.host) ftp.chdir(uri.path) pos = num while pos < dirs.length pkg = dirs[pos] begin ftp.nlst(dirs[pos]).each do |ver| packages << pkg + '/' + ver end rescue Net::FTPPermError # Ignore this error because it's indicative of the FTP library # encountering a file or directory that it does not have # permission to open. Fig needs to be able to have secure # repos/packages and there is no way easy way to deal with the # permissions issues other than consuming these errors. # # Actually, with FTP, you can't tell the difference between a # file not existing and not having permission to access it (which # is probably a good thing). end pos += num_threads end ftp.close end end threads.each { |thread| thread.join } all_packages.flatten.sort end |
#download_list(url) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fig/operatingsystem.rb', line 82 def download_list(url) begin uri = URI.parse(url) rescue Fig::Logging.fatal %Q<Unable to parse url: "#{url}"> raise Fig::NetworkError.new end case uri.scheme when 'ftp' ftp = Net::FTP.new(uri.host) ftp_login(ftp, uri.host) ftp.chdir(uri.path) dirs = ftp.nlst ftp.close download_ftp_list(uri, dirs) when 'ssh' packages = [] Net::SSH.start(uri.host, uri.user) do |ssh| ls = ssh.exec!("[ -d #{uri.path} ] && find #{uri.path}") strip_paths_for_list(ls, packages, uri.path) end packages when 'file' packages = [] return packages if ! File.exist?(uri.path) ls = '' Find.find(uri.path) { |file| ls << file.to_s; ls << "\n" } strip_paths_for_list(ls, packages, uri.path) return packages else Fig::Logging.fatal "Protocol not supported: #{url}" raise Fig::NetworkError.new("Protocol not supported: #{url}") end end |
#download_resource(url, dir) ⇒ Object
220 221 222 223 |
# File 'lib/fig/operatingsystem.rb', line 220 def download_resource(url, dir) FileUtils.mkdir_p(dir) download(url, File.join(dir, URI.parse(url).path.split('/').last)) end |
#ftp_login(ftp, host) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fig/operatingsystem.rb', line 42 def ftp_login(ftp, host) if @login rc = Net::Netrc.locate(host) if rc @username = rc.login @password = rc.password end ftp.login(get_username, get_password) else ftp.login() end ftp.passive = true end |
#get_password ⇒ Object
38 39 40 |
# File 'lib/fig/operatingsystem.rb', line 38 def get_password() @password ||= ask('Password: ') { |q| q.echo = false } end |
#get_username ⇒ Object
34 35 36 |
# File 'lib/fig/operatingsystem.rb', line 34 def get_username() @username ||= ask('Username: ') { |q| q.echo = true } end |
#list(dir) ⇒ Object
56 57 58 |
# File 'lib/fig/operatingsystem.rb', line 56 def list(dir) Dir.entries(dir) - ['.','..'] end |
#log_info(msg) ⇒ Object
313 314 315 |
# File 'lib/fig/operatingsystem.rb', line 313 def log_info(msg) Fig::Logging.info msg end |
#move_file(dir, from, to) ⇒ Object
309 310 311 |
# File 'lib/fig/operatingsystem.rb', line 309 def move_file(dir, from, to) Dir.chdir(dir) { FileUtils.mv(from, to, :force => true) } end |
#mtime(path) ⇒ Object
60 61 62 |
# File 'lib/fig/operatingsystem.rb', line 60 def mtime(path) File.mtime(path) end |
#shell_exec(cmd) ⇒ Object
376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/fig/operatingsystem.rb', line 376 def shell_exec(cmd) # Kernel#exec won't run Kernel#at_exit handlers. Fig::AtExit.execute() if ENV['FIG_COVERAGE'] SimpleCov.at_exit.call end if Fig::OperatingSystem.windows? Kernel.exec(ENV['ComSpec'], '/c', cmd.join(' ')) else Kernel.exec(ENV['SHELL'], '-c', cmd.join(' ')) end end |
#strip_paths_for_list(ls_output, packages, path) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/fig/operatingsystem.rb', line 72 def strip_paths_for_list(ls_output, packages, path) if not ls_output.nil? ls_output = ls_output.gsub(path + '/', '').gsub(path, '').split("\n") ls_output.each do |line| parts = line.gsub(/\\/, '/').sub(/^\.\//, '').sub(/:$/, '').chomp().split('/') packages << parts.join('/') if parts.size == 2 end end end |
#unpack_archive(dir, file) ⇒ Object
This method can handle the following archive types: .tar.bz2 .tar.gz .tgz .zip
350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/fig/operatingsystem.rb', line 350 def unpack_archive(dir, file) Dir.chdir(dir) do if Fig::OperatingSystem.java? `tar xzvf #{file}` else ::Archive.read_open_filename(file) do |reader| while entry = reader.next_header reader.extract(entry) end end end end end |
#upload(local_file, remote_file, user) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/fig/operatingsystem.rb', line 245 def upload(local_file, remote_file, user) Fig::Logging.debug "Uploading #{local_file} to #{remote_file}." uri = URI.parse(remote_file) case uri.scheme when 'ssh' ssh_upload(uri.user, uri.host, local_file, remote_file) when 'ftp' # fail unless system "curl -T #{local_file} --create-dirs --ftp-create-dirs #{remote_file}" require 'net/ftp' ftp_uri = URI.parse(ENV['FIG_REMOTE_URL']) ftp_root_path = ftp_uri.path ftp_root_dirs = ftp_uri.path.split('/') remote_publish_path = uri.path[0, uri.path.rindex('/')] remote_publish_dirs = remote_publish_path.split('/') # Use array subtraction to deduce which project/version folder to upload to, # i.e. [1,2,3] - [2,3,4] = [1] remote_project_dirs = remote_publish_dirs - ftp_root_dirs Net::FTP.open(uri.host) do |ftp| ftp_login(ftp, uri.host) # Assume that the FIG_REMOTE_URL path exists. ftp.chdir(ftp_root_path) remote_project_dirs.each do |dir| # Can't automatically create parent directories, so do it manually. if ftp.nlst().index(dir).nil? ftp.mkdir(dir) ftp.chdir(dir) else ftp.chdir(dir) end end ftp.putbinaryfile(local_file) end when 'file' FileUtils.mkdir_p(File.dirname(uri.path)) FileUtils.cp(local_file, uri.path) else Fig::Logging.fatal "Unknown protocol: #{uri}" raise Fig::NetworkError.new("Unknown protocol: #{uri}") end end |
#write(path, content) ⇒ Object
64 65 66 |
# File 'lib/fig/operatingsystem.rb', line 64 def write(path, content) File.open(path, 'wb') { |f| f.binmode; f << content } end |