Module: NexusSW::LXD::Transport::Mixins::Helpers::UploadFolder

Included in:
CLI, Rest
Defined in:
lib/nexussw/lxd/transport/mixins/helpers/upload_folder.rb

Instance Method Summary collapse

Instance Method Details

#upload_files_individually(local_path, path, options = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/nexussw/lxd/transport/mixins/helpers/upload_folder.rb', line 11

def upload_files_individually(local_path, path, options = {})
  Dir.entries(local_path).map { |f| (f == '.' || f == '..') ? nil : File.join(local_path, f) }.compact.each do |f|
    dest = File.join(path, File.basename(local_path))
    upload_files_individually f, dest, options if File.directory? f
    upload_file f, File.join(dest, File.basename(f)), options if File.file? f
  end
end

#upload_folder(local_path, path, options = {}) ⇒ Object



7
8
9
# File 'lib/nexussw/lxd/transport/mixins/helpers/upload_folder.rb', line 7

def upload_folder(local_path, path, options = {})
  upload_using_tarball(local_path, path, options) || upload_files_individually(local_path, path, options)
end

#upload_using_tarball(local_path, path, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nexussw/lxd/transport/mixins/helpers/upload_folder.rb', line 19

def upload_using_tarball(local_path, path, options = {})
  return false unless can_archive?
  # TODO: should I return false upon error?  i.e. retry with individual file uploads if this fails?
  #   lets see how this does in the wild before deciding
  flag, ext = compression
  begin
    tfile = Tempfile.new(container_name)
    tfile.close
    `tar -c#{flag}f #{tfile.path} -C #{File.dirname local_path} ./#{File.basename local_path}`
    # on that above note we'll do this at least
    # raise "Unable to create archive #{tfile.path}" if File.zero? tfile.path
    if File.zero? tfile.path
      @can_archive = false
      return false
    end
    fname = '/tmp/' + File.basename(tfile.path) + ".tar#{ext}"
    upload_file tfile.path, fname, options
    # TODO: serious: make sure the tar extract does an overwrite of existing files
    #   multiple converge support as well as CI cycle/dev updated files get updated instead of .1 suffixed (?)
    #   I think I need a flag (it's been a while)
    myuid = options[:uid] || uid || (0 if is_a?(Mixins::CLI))
    mygid = options[:gid] || gid || (0 if is_a?(Mixins::CLI))
    mymode = options[:file_mode] || file_mode
    chown = " && chown -R #{myuid}:#{mygid} #{File.basename(local_path)}" if myuid
    chmod = " && chmod -R #{mymode} #{File.basename(local_path)}" if mymode
    execute("bash -c 'mkdir -p #{path} && cd #{path} && tar -xf #{fname} && rm -rf #{fname}#{chmod}#{chown}'").error!
  ensure
    tfile.unlink
  end
end