Class: Fig::OperatingSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/operating_system.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

Instance Method Summary collapse

Constructor Details

#initialize(login) ⇒ OperatingSystem

Returns a new instance of OperatingSystem.



28
29
30
31
32
# File 'lib/fig/operating_system.rb', line 28

def initialize()
  @login = 
  @username = ENV['FIG_USERNAME']
  @password = ENV['FIG_PASSWORD']
end

Class Method Details

.get_environment_variables(initial_values = nil) ⇒ Object



407
408
409
410
411
412
413
# File 'lib/fig/operating_system.rb', line 407

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

Returns:

  • (Boolean)


377
378
379
# File 'lib/fig/operating_system.rb', line 377

def self.java?
  RUBY_PLATFORM == 'java'
end

.unix?Boolean

Returns:

  • (Boolean)


381
382
383
# File 'lib/fig/operating_system.rb', line 381

def self.unix?
  !windows?
end

.windows?Boolean

Returns:

  • (Boolean)


373
374
375
# File 'lib/fig/operating_system.rb', line 373

def self.windows?
  RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end

.wrap_variable_name_with_shell_expansion(variable_name) ⇒ Object



399
400
401
402
403
404
405
# File 'lib/fig/operating_system.rb', line 399

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



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/fig/operating_system.rb', line 300

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.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/fig/operating_system.rb', line 327

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



295
296
297
298
# File 'lib/fig/operating_system.rb', line 295

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.



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
219
220
# File 'lib/fig/operating_system.rb', line 163

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, 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.message
      raise Fig::NotFoundError.new error.message, url
    rescue SocketError => error
      Fig::Logging.debug error.message
      raise Fig::NotFoundError.new error.message, url
    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.message
        raise Fig::NotFoundError.new error.message, url
      rescue SocketError => error
        Fig::Logging.debug error.message
        raise Fig::NotFoundError.new error.message, url
      end
    end
  when 'ssh'
    # TODO need better way to do conditional download
    timestamp = 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 + " #{timestamp} #{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 => error
      raise Fig::NotFoundError.new error.message, url
    end
  else
    Fig::Logging.fatal "Unknown protocol: #{url}"
    raise Fig::NetworkError.new("Unknown protocol: #{url}")
  end
end

#download_and_unpack_archive(url, download_directory) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fig/operating_system.rb', line 234

def download_and_unpack_archive(url, download_directory)
  basename, path = download_resource(url, download_directory)

  case basename
  when /\.tar\.gz$/
    unpack_archive(download_directory, path)
  when /\.tgz$/
    unpack_archive(download_directory, path)
  when /\.tar\.bz2$/
    unpack_archive(download_directory, path)
  when /\.zip$/
    unpack_archive(download_directory, path)
  else
    Fig::Logging.fatal "Unknown archive type: #{basename}"
    raise Fig::NetworkError.new("Unknown archive type: #{basename}")
  end

  return
end

#download_ftp_list(uri, dirs) ⇒ Object



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
158
159
# File 'lib/fig/operating_system.rb', line 122

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, 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



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
119
120
# File 'lib/fig/operating_system.rb', line 84

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, 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, download_directory) ⇒ Object

Returns the basename and full path to the download.



223
224
225
226
227
228
229
230
231
232
# File 'lib/fig/operating_system.rb', line 223

def download_resource(url, download_directory)
  FileUtils.mkdir_p(download_directory)

  basename = URI.parse(url).path.split('/').last
  path     = File.join(download_directory, basename)

  download(url, path)

  return basename, path
end

#ftp_login(ftp, host) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fig/operating_system.rb', line 44

def (ftp, host)
  if @login
    rc = Net::Netrc.locate(host)
    if rc
      @username = rc.
      @password = rc.password
    end
    ftp.(get_username, get_password)
  else
    ftp.()
  end
  ftp.passive = true
end

#get_passwordObject



39
40
41
42
# File 'lib/fig/operating_system.rb', line 39

def get_password()
  # #ask() comes from highline
  @password ||= ask('Password: ') { |q| q.echo = false }
end

#get_usernameObject



34
35
36
37
# File 'lib/fig/operating_system.rb', line 34

def get_username()
  # #ask() comes from highline
  @username ||= ask('Username: ') { |q| q.echo = true }
end

#list(dir) ⇒ Object



58
59
60
# File 'lib/fig/operating_system.rb', line 58

def list(dir)
  Dir.entries(dir) - ['.','..']
end

#log_info(msg) ⇒ Object



322
323
324
# File 'lib/fig/operating_system.rb', line 322

def log_info(msg)
  Fig::Logging.info msg
end

#move_file(dir, from, to) ⇒ Object



318
319
320
# File 'lib/fig/operating_system.rb', line 318

def move_file(dir, from, to)
  Dir.chdir(dir) { FileUtils.mv(from, to, :force => true) }
end

#mtime(path) ⇒ Object



62
63
64
# File 'lib/fig/operating_system.rb', line 62

def mtime(path)
  File.mtime(path)
end

#shell_exec(cmd) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/fig/operating_system.rb', line 385

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



74
75
76
77
78
79
80
81
82
# File 'lib/fig/operating_system.rb', line 74

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



359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/fig/operating_system.rb', line 359

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) ⇒ Object



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
285
286
287
288
289
290
291
292
293
# File 'lib/fig/operating_system.rb', line 254

def upload(local_file, remote_file)
  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, 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



66
67
68
# File 'lib/fig/operating_system.rb', line 66

def write(path, content)
  File.open(path, 'wb') { |f| f.binmode; f << content }
end