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) ⇒ 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)
  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 if File.directory? f
    upload_file f, File.join(dest, File.basename(f)) if File.file? f
  end
end

#upload_folder(local_path, path) ⇒ Object



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

def upload_folder(local_path, path)
  upload_using_tarball(local_path, path) || upload_files_individually(local_path, path)
end

#upload_using_tarball(local_path, path) ⇒ 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
# File 'lib/nexussw/lxd/transport/mixins/helpers/upload_folder.rb', line 19

def upload_using_tarball(local_path, path)
  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
    # 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)
    execute("bash -c 'mkdir -p #{path} && cd #{path} && tar -xf #{fname} && rm -rf #{fname}'").error!
  ensure
    tfile.unlink
  end
end