Module: Merb::TarballHelper

Included in:
Cookbooks, Sandboxes
Defined in:
app/helpers/tarball_helper.rb

Defined Under Namespace

Classes: FileParameterException

Instance Method Summary collapse

Instance Method Details

#cookbook_baseObject



45
46
47
# File 'app/helpers/tarball_helper.rb', line 45

def cookbook_base
  [Chef::Config.cookbook_path].flatten.first
end

#cookbook_location(cookbook_name) ⇒ Object



49
50
51
# File 'app/helpers/tarball_helper.rb', line 49

def cookbook_location(cookbook_name)
  File.join(cookbook_base, cookbook_name)
end

#expand_tarball_and_put_in_repository(cookbook_name, file) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/tarball_helper.rb', line 76

def expand_tarball_and_put_in_repository(cookbook_name, file)
  # untar cookbook tarball into tempdir
  tempdir = File.join("#{file.path}.data")
  Chef::Log.debug("Creating #{tempdir} and untarring #{file.path} into it")
  FileUtils.mkdir_p(tempdir)
  raise "Could not untar file" unless system("tar", "xzf", file.path, "-C", tempdir)

  cookbook_path = cookbook_location(cookbook_name)
  tarball_path = cookbook_tarball_location(cookbook_name)

  # clear any existing cookbook components and move tempdir into the repository
  Chef::Log.debug("Moving #{tempdir} to #{cookbook_path}")
  FileUtils.rm_rf(cookbook_path)
  FileUtils.mkdir_p(cookbook_path)
  Dir[File.join(tempdir, cookbook_name, "*")].each{|e| FileUtils.mv(e, cookbook_path)}

  # clear the existing tarball (if exists) and move the downloaded tarball to the cache
  Chef::Log.debug("Moving #{file.path} to #{tarball_path}")
  FileUtils.mkdir_p(Chef::Config.cookbook_tarball_path)
  FileUtils.rm_f(tarball_path)
  FileUtils.mv(file.path, tarball_path)
end

#get_or_create_cookbook_tarball_location(cookbook_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/tarball_helper.rb', line 65

def get_or_create_cookbook_tarball_location(cookbook_name)
  tarball_location = cookbook_tarball_location(cookbook_name)
  unless File.exists? tarball_location
    args = ["tar", "-C", cookbook_base, "-czf", tarball_location, cookbook_name]
    Chef::Log.debug("Tarball for #{cookbook_name} not found, so creating at #{tarball_location} with '#{args.join(' ')}'")
    FileUtils.mkdir_p(Chef::Config.cookbook_tarball_path)
    system(*args)
  end
  tarball_location
end

#sandbox_baseObject



33
34
35
# File 'app/helpers/tarball_helper.rb', line 33

def sandbox_base
  Chef::Config.sandbox_path
end

#sandbox_checksum_location(sandbox_guid, checksum) ⇒ Object



41
42
43
# File 'app/helpers/tarball_helper.rb', line 41

def sandbox_checksum_location(sandbox_guid, checksum)
  File.join(sandbox_location(sandbox_guid), checksum)
end

#sandbox_location(sandbox_guid) ⇒ Object



37
38
39
# File 'app/helpers/tarball_helper.rb', line 37

def sandbox_location(sandbox_guid)
  File.join(sandbox_base, sandbox_guid)
end

#validate_file_parameter(cookbook_name, file_param) ⇒ Object



24
25
26
27
28
29
30
31
# File 'app/helpers/tarball_helper.rb', line 24

def validate_file_parameter(cookbook_name, file_param)
  raise FileParameterException, "missing required parameter: file" unless file_param
  raise FileParameterException, "invalid parameter: file must be a File" unless file_param.respond_to?(:has_key?) && file_param[:tempfile].respond_to?(:read)
  tarball_path = file_param[:tempfile].path
  raise FileParameterException, "invalid tarball: (try creating with 'tar czf cookbook.tar.gz cookbook/')" unless system("tar", "tzf", tarball_path)
  entry_roots = `tar tzf #{tarball_path}`.split("\n").map{|e|(e.split('/')-['.']).first}.uniq
  raise FileParameterException, "invalid tarball: tarball root must contain #{cookbook_name}" unless entry_roots.include?(cookbook_name)
end