Module: NexusSW::LXD::Transport::Mixins::Helpers::FolderTxfr

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

Instance Method Summary collapse

Instance Method Details

#download_files_individually(path, local_path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/nexussw/lxd/transport/mixins/helpers/folder_txfr.rb', line 27

def download_files_individually(path, local_path)
  dest = File.join(local_path, File.basename(path))
  execute("bash -c 'cd #{path} && find -type d'").error!.stdout.each_line do |line|
    newdir = line.strip.sub(/^\./, dest)
    Dir.mkdir newdir unless Dir.exist? newdir
  end
  execute("bash -c 'cd #{path} && find ! -type d'").error!.stdout.each_line do |line|
    download_file line.strip.sub(/^\./, path), line.strip.sub(/^\./, dest)
  end
end

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



14
15
16
# File 'lib/nexussw/lxd/transport/mixins/helpers/folder_txfr.rb', line 14

def download_folder(path, local_path, options = {})
  download_using_tarball(path, local_path, options) || download_files_individually(path, local_path)
end

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

gzip(-z) or bzip2(-j) (these are the only 2 on trusty atm)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nexussw/lxd/transport/mixins/helpers/folder_txfr.rb', line 39

def download_using_tarball(path, local_path, options = {})
  if options[:auto_detect] && execute("test -d #{path}").error?
    download_file(path, File.join(local_path, File.basename(path)))
    return true
  end

  return false unless can_archive?
  tfile = Transport.remote_tempname(container_name)
  tarball_name = File.join Transport.local_tempdir, File.basename(tfile) + ".tgz"
  execute("tar -czf #{tfile} -C #{File.dirname path} #{File.basename path}").error!

  download_file tfile, tarball_name

  Archive::Tar::Minitar.unpack Zlib::GzipReader.new(File.open(tarball_name, "rb")), local_path
  true
ensure
  if tarball_name
    File.delete tarball_name if File.exist? tarball_name
    execute "rm -rf #{tfile}"
  end
end

#upload_files_individually(local_path, path) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/nexussw/lxd/transport/mixins/helpers/folder_txfr.rb', line 18

def upload_files_individually(local_path, path)
  dest = File.join(path, File.basename(local_path))
  execute("mkdir -p " + dest).error! # for parity with tarball extract
  Dir.entries(local_path).map { |f| (f == "." || f == "..") ? nil : File.join(local_path, f) }.compact.each do |f|
    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



10
11
12
# File 'lib/nexussw/lxd/transport/mixins/helpers/folder_txfr.rb', line 10

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/nexussw/lxd/transport/mixins/helpers/folder_txfr.rb', line 61

def upload_using_tarball(local_path, path)
  return false unless can_archive?
  begin
    tfile = Tempfile.new(container_name)
    tfile.close
    Transport.chdir_mutex.synchronize do
      Dir.chdir File.dirname(local_path) do
        Archive::Tar::Minitar.pack File.basename(local_path), Zlib::GzipWriter.new(File.open(tfile.path, "wb"))
      end
    end
    # `tar -c#{flag}f #{tfile.path} -C #{File.dirname local_path} ./#{File.basename local_path}`
    fname = "/tmp/" + File.basename(tfile.path) + ".tgz"
    upload_file tfile.path, fname

    execute("bash -c 'mkdir -p #{path} && cd #{path} && tar -xf #{fname} && rm -rf #{fname}'").error!
  ensure
    tfile.unlink
  end
end