Class: Bosh::Agent::FileAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh_agent/file_aggregator.rb

Defined Under Namespace

Classes: DirectoryNotFound, Error, PackagingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileAggregator

Returns a new instance of FileAggregator.



12
13
14
# File 'lib/bosh_agent/file_aggregator.rb', line 12

def initialize
  @used_dirs = []
end

Instance Attribute Details

#matcherObject

Returns the value of attribute matcher.



10
11
12
# File 'lib/bosh_agent/file_aggregator.rb', line 10

def matcher
  @matcher
end

Instance Method Details

#cleanupObject



36
37
38
39
40
# File 'lib/bosh_agent/file_aggregator.rb', line 36

def cleanup
  @used_dirs.each do |dir|
    FileUtils.rm_rf(dir) if File.directory?(dir)
  end
end

#copy_files(dst_directory) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bosh_agent/file_aggregator.rb', line 42

def copy_files(dst_directory)
  raise Error, "no matcher provided" unless @matcher

  unless File.directory?(@matcher.base_dir)
    raise DirectoryNotFound, "Base directory #{@matcher.base_dir} not found"
  end

  copied = 0
  base_dir = realpath(@matcher.base_dir)

  Dir.chdir(base_dir) do
    @matcher.globs.each do |glob|
      Dir[glob].each do |file|
        path = File.expand_path(file)

        next unless File.file?(file)
        next unless path[0..base_dir.length-1] == base_dir

        dst_filename = File.join(dst_directory, path[base_dir.length..-1])
        FileUtils.mkdir_p(File.dirname(dst_filename))
        FileUtils.cp(realpath(path), dst_filename, :preserve => true)
        copied += 1
      end
    end
  end

  copied
end

#generate_tarballObject

Generates a tarball including all the requested entries

Returns:

  • tarball path



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bosh_agent/file_aggregator.rb', line 18

def generate_tarball
  tmpdir = Dir.mktmpdir
  out_dir = Dir.mktmpdir
  @used_dirs << out_dir

  copy_files(tmpdir)
  tarball_path = File.join(out_dir, "files.tgz")

  Dir.chdir(tmpdir) do
    tar_out = `tar -czf #{tarball_path} . 2>&1`
    raise PackagingError, "Cannot create tarball: #{tar_out}" unless $?.exitstatus == 0
  end

  tarball_path
ensure
  FileUtils.rm_rf(tmpdir) if tmpdir && File.directory?(tmpdir)
end