This repository has been archived by the owner on Mar 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathcookbook_object.rb
181 lines (154 loc) · 4.54 KB
/
cookbook_object.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module Ridley
class CookbookObject < Ridley::ChefObject
include Ridley::Logging
FILE_TYPES = [
:resources,
:providers,
:recipes,
:definitions,
:libraries,
:attributes,
:files,
:templates,
:root_files
].freeze
set_chef_id "cookbook_name"
set_chef_type "cookbook"
set_chef_json_class "Chef::Cookbook"
attribute :name,
required: true
attribute :attributes,
type: Array,
default: Array.new
attribute :cookbook_name,
type: String
attribute :definitions,
type: Array,
default: Array.new
attribute :files,
type: Array,
default: Array.new
attribute :libraries,
type: Array,
default: Array.new
attribute :metadata,
type: Hashie::Mash
attribute :providers,
type: Array,
default: Array.new
attribute :recipes,
type: Array,
default: Array.new
attribute :resources,
type: Array,
default: Array.new
attribute :root_files,
type: Array,
default: Array.new
attribute :templates,
type: Array,
default: Array.new
attribute :version,
type: String
attribute :frozen?,
type: Buff::Boolean
# Download the entire cookbook
#
# @param [String] destination (Dir.mktmpdir)
# the place to download the cookbook too. If no value is provided the cookbook
# will be downloaded to a temporary location
#
# @return [String]
# the path to the directory the cookbook was downloaded to
def download(destination = Dir.mktmpdir)
destination = File.expand_path(destination)
log.debug { "downloading cookbook: '#{name}'" }
FILE_TYPES.each do |filetype|
next unless manifest.has_key?(filetype)
manifest[filetype].each do |file|
file_destination = File.join(destination, file[:path].gsub('/', File::SEPARATOR))
FileUtils.mkdir_p(File.dirname(file_destination))
download_file(filetype, file[:path], file_destination)
end
end
destination
end
# Download a single file from a cookbook
#
# @param [#to_sym] filetype
# the type of file to download. These are broken up into the following types in Chef:
# - attribute
# - definition
# - file
# - library
# - provider
# - recipe
# - resource
# - root_file
# - template
# these types are where the files are stored in your cookbook's structure. For example, a
# recipe would be stored in the recipes directory while a root_file is stored at the root
# of your cookbook
# @param [String] path
# path of the file to download
# @param [String] destination
# where to download the file to
#
# @return [nil]
def download_file(filetype, path, destination)
file_list = case filetype.to_sym
when :attribute, :attributes; attributes
when :definition, :definitions; definitions
when :file, :files; files
when :library, :libraries; libraries
when :provider, :providers; providers
when :recipe, :recipes; recipes
when :resource, :resources; resources
when :root_file, :root_files; root_files
when :template, :templates; templates
else
raise Errors::UnknownCookbookFileType.new(filetype)
end
file = file_list.find { |f| f[:path] == path }
return nil if file.nil?
destination = File.expand_path(destination)
log.debug { "downloading '#{filetype}' file: #{file} to: '#{destination}'" }
resource.connection.stream(file[:url], destination)
end
# A hash containing keys for all of the different cookbook filetypes with values
# representing each file of that type this cookbook contains
#
# @example
# {
# root_files: [
# {
# :name => "afile.rb",
# :path => "files/ubuntu-9.10/afile.rb",
# :checksum => "2222",
# :specificity => "ubuntu-9.10"
# },
# ],
# templates: [ manifest_record1, ... ],
# ...
# }
#
# @return [Hash]
def manifest
{}.tap do |manifest|
FILE_TYPES.each do |filetype|
manifest[filetype] = get_attribute(filetype)
end
end
end
# Reload the attributes of the instantiated resource
#
# @return [Ridley::CookbookObject]
def reload
mass_assign(resource.find(self, self.version)._attributes_)
self
end
def to_s
"#{name}: #{manifest}"
end
end
end