Class: Fig::OS

Inherits:
Object
  • Object
show all
Defined in:
lib/fig/os.rb

Constant Summary collapse

SUCCESS =
0
NOT_MODIFIED =
3
NOT_FOUND =
4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(login) ⇒ OS

Returns a new instance of OS.



19
20
21
22
23
# File 'lib/fig/os.rb', line 19

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

Class Method Details

.java?Boolean

Returns:

  • (Boolean)


335
336
337
# File 'lib/fig/os.rb', line 335

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

.unix?Boolean

Returns:

  • (Boolean)


339
340
341
# File 'lib/fig/os.rb', line 339

def self.unix?
  !windows?
end

.windows?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/fig/os.rb', line 331

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

Instance Method Details

#clear_directory(dir) ⇒ Object



260
261
262
263
# File 'lib/fig/os.rb', line 260

def clear_directory(dir)
  FileUtils.rm_rf(dir)
  FileUtils.mkdir_p(dir)
end

#copy(source, target, msg = nil) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/fig/os.rb', line 265

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.



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/fig/os.rb', line 292

def create_archive(archive_name, files_to_archive)
  if OS.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 |ar|
      files_to_archive.each do |fn|
        ar.new_entry do |entry|
          entry.copy_stat(fn)
          entry.pathname = fn
          ar.write_header(entry)
          if !entry.directory?
            ar.write_data(open(fn) {|f| f.binmode; f.read })
          end
        end
      end
    end
  end
end

#download(url, path) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
# File 'lib/fig/os.rb', line 141

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)
        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
      Logging.warn error.message
      raise NotFoundError.new
    rescue SocketError => error
      Logging.warn error.message
      raise NotFoundError.new
    end
  when 'http'
    http = Net::HTTP.new(uri.host)
    log_download(url, path)
    File.open(path, 'wb') do |file|
      file.binmode
      http.get(uri.path) do |block|
        file.write(block)
      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 => e
      raise NotFoundError.new
    end
  else
    Logging.fatal "Unknown protocol: #{url}"
    raise NetworkError.new("Unknown protocol: #{url}")
  end
end

#download_archive(url, dir) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/fig/os.rb', line 199

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$/
    unpack_archive(dir, path)
  when /\.tgz$/
    unpack_archive(dir, path)
  when /\.tar\.bz2$/
    unpack_archive(dir, path)
  when /\.zip$/
    unpack_archive(dir, path)
  else
    Logging.fatal "Unknown archive type: #{basename}"
    raise NetworkError.new("Unknown archive type: #{basename}")
  end
end

#download_ftp_list(uri, dirs) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fig/os.rb', line 107

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.
        end
        pos += num_threads
      end
      ftp.close
    end
  end
  threads.each { |thread| thread.join }
  all_packages.flatten.sort
end

#download_list(url) ⇒ 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
104
105
# File 'lib/fig/os.rb', line 73

def download_list(url)
  begin
    uri = URI.parse(url)
  rescue
    Logging.fatal %Q<Unable to parse url: "#{url}">
    raise NetworkError.new(%Q<Unable to parse url: "#{url}">)
  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 = []
    ls = %x<[ -d #{uri.path} ] && find #{uri.path}>
    strip_paths_for_list(ls, packages, uri.path)
    return packages
  else
    Logging.fatal "Protocol not supported: #{url}"
    raise NetworkError.new("Protocol not supported: #{url}")
  end
end

#download_resource(url, dir) ⇒ Object



194
195
196
197
# File 'lib/fig/os.rb', line 194

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



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fig/os.rb', line 33

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



29
30
31
# File 'lib/fig/os.rb', line 29

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

#get_usernameObject



25
26
27
# File 'lib/fig/os.rb', line 25

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

#list(dir) ⇒ Object



47
48
49
# File 'lib/fig/os.rb', line 47

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

#log_info(msg) ⇒ Object



287
288
289
# File 'lib/fig/os.rb', line 287

def log_info(msg)
  Logging.info msg
end

#move_file(dir, from, to) ⇒ Object



283
284
285
# File 'lib/fig/os.rb', line 283

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

#mtime(path) ⇒ Object



51
52
53
# File 'lib/fig/os.rb', line 51

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

#shell_exec(cmd) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/fig/os.rb', line 343

def shell_exec(cmd)
  if OS.windows?
    Windows.shell_exec_windows(cmd)
  else
    shell_exec_unix(cmd)
  end
end

#strip_paths_for_list(ls_output, packages, path) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/fig/os.rb', line 63

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



317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/fig/os.rb', line 317

def unpack_archive(dir, file)
  Dir.chdir(dir) do
    if OS.java?
      `tar xzvf #{file}`
    else
      ::Archive.read_open_filename(file) do |ar|
        while entry = ar.next_header
          ar.extract(entry)
        end
      end
    end
  end
end

#upload(local_file, remote_file, user) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/fig/os.rb', line 219

def upload(local_file, remote_file, user)
  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
    Logging.fatal "Unknown protocol: #{uri}"
    raise NetworkError.new("Unknown protocol: #{uri}")
  end
end

#write(path, content) ⇒ Object



55
56
57
# File 'lib/fig/os.rb', line 55

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