Class: ChefSync::Cookbook

Inherits:
ChefComponent show all
Defined in:
lib/chef_sync/chef_component/cookbook.rb

Constant Summary collapse

CHANGE_LOG_SUMMARIES =
{
	:create => " was created.",
	:update => " was updated.",
	:version_regressed => " is newer than the local version.",
	:version_changed => " has changed without a version number increase."
}
FILE_CHANGE_LOG_SUMMARIES =
{
	:file_changed => " has changed.",
	:file_missing => " does not exist locally."
}

Constants inherited from ChefComponent

ChefSync::ChefComponent::ACTIONABLE_CHANGES, ChefSync::ChefComponent::FILE_EXTENSION

Instance Attribute Summary collapse

Attributes inherited from ChefComponent

#change, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ChefComponent

#actionable_change?, #changed?, changes, each, #get_local_resource, make_local_knife, make_remote_knife, #resource_path, #sync

Constructor Details

#initialize(local_version_number:, remote_version_number:, **opts) ⇒ Cookbook

Returns a new instance of Cookbook.



25
26
27
28
29
30
31
# File 'lib/chef_sync/chef_component/cookbook.rb', line 25

def initialize(local_version_number:, remote_version_number:, **opts)
	@local_version_number = Mixlib::Versioning.parse(local_version_number)
	@remote_version_number = Mixlib::Versioning.parse(remote_version_number)
	@file_change_log = {}

	super(opts)
end

Instance Attribute Details

#file_change_logObject (readonly)

Returns the value of attribute file_change_log.



23
24
25
# File 'lib/chef_sync/chef_component/cookbook.rb', line 23

def file_change_log
  @file_change_log
end

Class Method Details

.get_local_resources(local_knife, remote_knife) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/chef_sync/chef_component/cookbook.rb', line 33

def self.get_local_resources(local_knife, remote_knife)
	local_cookbooks = local_knife.list
	remote_cookbooks = remote_knife.list

	return local_cookbooks.map do |cb, local_ver|
		{name: cb, local_version_number: local_ver, remote_version_number: remote_cookbooks[cb]}
	end
end

Instance Method Details

#compare_cookbook_filesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef_sync/chef_component/cookbook.rb', line 66

def compare_cookbook_files
	remote_cookbook_files = self.get_remote_resource

	remote_cookbook_files.each do |remote_file|
		local_file_path = "#{self.resource_path}/#{remote_file['path']}"
		begin
			local_file_checksum = Chef::CookbookVersion.checksum_cookbook_file(File.open(local_file_path))
			@file_change_log[local_file_path] = :file_changed unless local_file_checksum == remote_file['checksum']
		rescue Errno::ENOENT => e
			@file_change_log[local_file_path] = :file_missing
		end
	end
	return @file_change_log
end

#compare_local_and_remote_versionsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef_sync/chef_component/cookbook.rb', line 81

def compare_local_and_remote_versions
	local_ver = @local_version_number
	remote_ver = @remote_version_number

	case
	when !@remote_version_number
		@change = :create
	when @local_version_number < @remote_version_number
		@change = :version_regressed
	when @local_version_number == @remote_version_number
		@change = :version_changed unless self.compare_cookbook_files.empty?
	when @local_version_number > @remote_version_number
		@change = :update
	end

	return @change
end

#get_remote_resourceObject



55
56
57
58
59
60
# File 'lib/chef_sync/chef_component/cookbook.rb', line 55

def get_remote_resource
	ridley = Ridley.from_chef_config
	remote_cookbook = ridley.cookbook.find(@name, @remote_version_number)
	remote_cookbook_files = Chef::CookbookVersion::COOKBOOK_SEGMENTS.collect { |d| remote_cookbook.method(d).call }.flatten
	return remote_cookbook_files
end

#summarize_changesObject



46
47
48
49
50
51
52
53
# File 'lib/chef_sync/chef_component/cookbook.rb', line 46

def summarize_changes
	self.actionable_change? ? prefix = "" : prefix = "WARNING: "
	summary = [prefix + self.resource_path + CHANGE_LOG_SUMMARIES[@change]]
	if self.version_changed?
		summary << @file_change_log.map {|file, file_action| file + FILE_CHANGE_LOG_SUMMARIES[file_action]}
	end
	return summary
end

#upload_resourceObject



62
63
64
# File 'lib/chef_sync/chef_component/cookbook.rb', line 62

def upload_resource
	return @remote_knife.upload(@name, '--freeze')
end

#version_changed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/chef_sync/chef_component/cookbook.rb', line 42

def version_changed?
	return @change == :version_changed
end