Class: VagrantPlugins::PackerBuild::Commons::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-packer-plugin/commons/engine.rb

Instance Method Summary collapse

Instance Method Details

#create_work_dir(work_dir) ⇒ Object



25
26
27
28
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 25

def create_work_dir(work_dir)
	FileUtils.mkdir_p("#{work_dir}")
	puts "[packer-info] Created #{work_dir} folder\n"
end

#fetch_cookbook_versionObject



15
16
17
18
19
20
21
22
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 15

def fetch_cookbook_version
	result = ""
	begin
		result = open("#{Dir.pwd}/metadata.rb").grep(/version/).first.split(" ").last.gsub(/\"/,'')
	rescue
	end
	result
end

#get_artifact(work_dir, url, artifact_name, params = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 76

def get_artifact(work_dir, url, artifact_name, params=nil)
  # Download and uncompress Chef artifacts (in a Berkshelf package format)
  if url and url.length != 0
		Downloader.get(url, "#{work_dir}/#{artifact_name}.tar.gz",params)
  	FileUtils.rm_rf("#{work_dir}/#{artifact_name}")
  	Unpacker.tar("#{artifact_name}.tar.gz", work_dir)
    print "Unpacked #{work_dir}/#{artifact_name}.tar.gz into #{work_dir}\n"
  end
end

#get_instance_templates(work_dir, instance_templates) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 31

def get_instance_templates(work_dir, instance_templates)
	json_ret = {}
	instance_templates.each_with_index do |instance_template,index|
		node_name = JSON.parse(File.read(instance_template))['name']
		json_ret[instance_template] = get_json(work_dir, "attributes-#{node_name}.json", instance_template)
	end
  return json_ret
end

#get_json(work_dir, file_name, url) ⇒ Object



40
41
42
43
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 40

def get_json(work_dir, file_name, url)
	Downloader.get(url, "#{work_dir}/#{file_name}")
  return JSON.parse(File.read("#{work_dir}/#{file_name}"))
end

#get_node_attrs(work_dir, chef_node_name) ⇒ Object

Not needed anymore def get_chef_items(nodes, work_dir, cookbooks_url, databags_url) nodes.each do |filename,node| node = {}

attr_file = File.open(“#work_dir/attributes-#'name'.json”, ‘w’) attr_file.write(node.to_json) attr_file.close() end end



56
57
58
59
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 56

def get_node_attrs(work_dir, chef_node_name)
  box_attrs = File.read("#{work_dir}/attributes-#{chef_node_name}.json")
  return JSON.parse(box_attrs)
end

#invoke_berkshelf(work_dir, path_name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 61

def invoke_berkshelf(work_dir, path_name)
	puts "[packer-info] Trying to delete Berksfile.lock"
	begin
		File.delete("#{Dir.pwd}/Berksfile.lock")
		puts "[packer-info] local Berksfile.lock removed!"
	rescue Errno::ENOENT
		puts "[packer-info] File not found, continuing normally.."
	end

	puts "[packer-info] Packaging Chef Cookbooks repo with berks vendor..."
	Berkshelf::Cli.start(["vendor","#{work_dir}/#{path_name}"])
	# TODO - consider also params.berksfile, but not working yet
	# Berkshelf::Cli.start(["package",@params.cookbooks_url.split('/')[-1],"-b #{@params.berksfile}"])
end

#yaml_to_json(yaml) ⇒ Object



86
87
88
89
# File 'lib/vagrant-packer-plugin/commons/engine.rb', line 86

def yaml_to_json(yaml)
	 data = YAML::load(yaml)
	 return JSON.dump(data)
end