Top Level Namespace
Defined Under Namespace
Modules: CookbookSDK, CookbookSdk
Classes: Chef, String
Instance Method Summary
collapse
-
#_run_command(cmd, base_dir) ⇒ Object
-
#banner(text) ⇒ Object
-
#berks_update(base_dir) ⇒ Object
-
#berks_vendor(base_dir, target_folder) ⇒ Object
-
#cache_path_config(target_folder) ⇒ Object
-
#chefdk_export(base_dir, target_folder) ⇒ Object
-
#chefdk_update(base_dir) ⇒ Object
-
#clean(target_folder) ⇒ Object
-
#copy_attributes_file(base_dir, target_folder) ⇒ Object
-
#copy_data_bags(base_dir, target_folder) ⇒ Object
-
#create_custom_client_rb(target_folder, configuration_file, copy_original) ⇒ Object
-
#foodcritic(exit_on_error = true) ⇒ Object
-
#help_bump ⇒ Object
-
#help_kitchen ⇒ Object
-
#output_help(title, body) ⇒ Object
-
#prepare_handlers(handlers) ⇒ Object
-
#read_configuration(configuration_file) ⇒ Object
-
#rspec(exit_on_error = true) ⇒ Object
-
#rubocop(exit_on_error = true) ⇒ Object
-
#run_chef_zero(target_folder, custom_named_run_list = nil, run_list = nil, debug = false) ⇒ Object
-
#run_command(cmd, exit_on_error = false) ⇒ Object
Instance Method Details
#_run_command(cmd, base_dir) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 50
def _run_command(cmd, base_dir)
banner("Running '#{cmd}' in #{base_dir}...")
Dir.chdir base_dir do
run_command(cmd, true)
end
end
|
#banner(text) ⇒ Object
22
23
24
25
|
# File 'lib/cookbook_sdk/rake_tasks.rb', line 22
def banner(text)
puts
puts text.cyan
end
|
#berks_update(base_dir) ⇒ Object
57
58
59
60
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 57
def berks_update(base_dir)
_run_command('berks install', base_dir)
_run_command('berks update', base_dir)
end
|
#berks_vendor(base_dir, target_folder) ⇒ Object
62
63
64
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 62
def berks_vendor(base_dir, target_folder)
_run_command("berks vendor #{target_folder}/cookbooks", base_dir)
end
|
#cache_path_config(target_folder) ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 110
def cache_path_config(target_folder)
%(
# To enable chef-client without sudo.
# https://docs.chef.io/ctl_chef_client.html#run-as-non-root-user
cache_path "#{target_folder}/.chef"
)
end
|
#chefdk_export(base_dir, target_folder) ⇒ Object
70
71
72
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 70
def chefdk_export(base_dir, target_folder)
_run_command("chef export #{target_folder} --force", base_dir)
end
|
#chefdk_update(base_dir) ⇒ Object
66
67
68
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 66
def chefdk_update(base_dir)
_run_command('chef update', base_dir)
end
|
#clean(target_folder) ⇒ Object
183
184
185
186
187
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 183
def clean(target_folder)
cmd = "rm -rf #{target_folder}/cookbooks"
banner("Cleanning up the cookbooks cache folder with '#{cmd}' ...")
run_command(cmd, true)
end
|
#copy_attributes_file(base_dir, target_folder) ⇒ Object
153
154
155
156
157
158
159
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 153
def copy_attributes_file(base_dir, target_folder)
attributes_file = File.join(base_dir, 'attributes.json')
return unless File.exist?(attributes_file)
banner("Copying attributes file from #{attributes_file} to #{target_folder}...")
FileUtils.cp_r(attributes_file, target_folder)
end
|
#copy_data_bags(base_dir, target_folder) ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 145
def copy_data_bags(base_dir, target_folder)
data_bags_directory = File.join(base_dir, 'data_bags')
return unless File.directory?(data_bags_directory)
banner("Copying data_bags folder from #{data_bags_directory} to #{target_folder}...")
FileUtils.cp_r(data_bags_directory, target_folder)
end
|
#create_custom_client_rb(target_folder, configuration_file, copy_original) ⇒ Object
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
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 118
def create_custom_client_rb(target_folder, configuration_file, copy_original)
banner("Creating custom 'client.rb' in #{target_folder} ...")
original_client_rb = File.join(target_folder, 'client.rb')
custom_client_rb = File.join(target_folder, 'custom_client.rb')
config = read_configuration(configuration_file)
begin
FileUtils.copy_file(original_client_rb, custom_client_rb) if copy_original
open_mode = copy_original ? 'a' : 'w'
File.open(custom_client_rb, open_mode) do |output|
output.write(cache_path_config(target_folder))
if config.nil?
puts 'No configuration file found: custom_client.rb will not have any user custom configuration.'
else
output.write(prepare_handlers(config[:handlers]))
end
end
rescue Errno::EACCES => e
puts "Problem creating #{custom_client_rb} - #{e}"
end
puts "Writed a custom client.rb to '#{custom_client_rb}"
end
|
#foodcritic(exit_on_error = true) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/cookbook_sdk/rake_tasks/test_tasks.rb', line 39
def foodcritic(exit_on_error = true)
foodcritic_rules = File.join(File.dirname(__FILE__), '../../foodcritic/rules')
cmd = "chef exec foodcritic -f any -P --include #{foodcritic_rules} ."
banner("Running '#{cmd}' ...")
run_command(cmd, exit_on_error)
end
|
#help_bump ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/cookbook_sdk/rake_tasks/helper_tasks.rb', line 14
def help_bump
title = 'Help: Bumping cookbooks'.gray.bold
body = <<BODY
To bump your cookbook run the following command:
chef exec knife spork bump -z
Note: -z stands for local mode (chef-zero)
For more about 'knife spork' go to: http://jonlives.github.io/knife-spork/
BODY
output_help(title, body)
end
|
#help_kitchen ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/cookbook_sdk/rake_tasks/helper_tasks.rb', line 29
def help_kitchen
title = 'Help: Test Kitchen'.gray.bold
body = <<BODY
Test kitchen is an awsome testing tool. It enables you to test your cookbook against
some virtual machine, container, or an instance in several cloud virtualization tecnologies.
Run rhe folling command to have the real kitchen help
chef exec knife --help
Note: You don't get nothing when run chef-provisioning cookbooks with kitchen.
For more about 'kitchen' go to: http://kitchen.ci/
BODY
output_help(title, body)
end
|
#output_help(title, body) ⇒ Object
46
47
48
49
|
# File 'lib/cookbook_sdk/rake_tasks/helper_tasks.rb', line 46
def output_help(title, body)
puts title
puts body
end
|
#prepare_handlers(handlers) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 90
def prepare_handlers(handlers)
config_rb = ''
handlers[:enabled].each do |enabled_handler|
handler_config = handlers[:config][enabled_handler.to_sym]
config_rb += %(
# #{enabled_handler} handler configuration
require 'cookbook_sdk/handlers/#{enabled_handler}'
#{enabled_handler}_handler_options = #{handler_config}
#{enabled_handler}_handler = Chef::Handler::Slack.new(#{enabled_handler}_handler_options)
start_handlers << #{enabled_handler}_handler
report_handlers << #{enabled_handler}_handler
exception_handlers << #{enabled_handler}_handler
)
end
config_rb
end
|
#read_configuration(configuration_file) ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 74
def read_configuration(configuration_file)
file = nil
if File.exist?(configuration_file)
file = File.read(configuration_file)
elsif File.exist?("../#{configuration_file}")
file = File.read(configuration_file)
end
return nil if file.nil?
data_hash = JSON.parse(file, symbolize_names: true)
data_hash
rescue Errno::ENOENT, Errno::EACCES, JSON::ParserError => e
puts "Problem reading #{configuration_file} - #{e}"
end
|
#rspec(exit_on_error = true) ⇒ Object
46
47
48
49
50
51
|
# File 'lib/cookbook_sdk/rake_tasks/test_tasks.rb', line 46
def rspec(exit_on_error = true)
files = FileList[File.join(Dir.pwd, 'spec', 'unit', '**/*_spec.rb')]
cmd = "chef exec rspec #{files}"
banner("Running '#{cmd}' ...")
run_command(cmd, exit_on_error)
end
|
#rubocop(exit_on_error = true) ⇒ Object
53
54
55
56
57
|
# File 'lib/cookbook_sdk/rake_tasks/test_tasks.rb', line 53
def rubocop(exit_on_error = true)
cmd = 'chef exec rubocop'
banner("Running '#{cmd}' ...")
run_command(cmd, exit_on_error)
end
|
#run_chef_zero(target_folder, custom_named_run_list = nil, run_list = nil, debug = false) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/cookbook_sdk/rake_tasks/provisioning_tasks.rb', line 161
def run_chef_zero(target_folder, custom_named_run_list = nil, run_list = nil, debug = false)
named_run_list = custom_named_run_list.nil? ? '' : "-n #{custom_named_run_list}"
run_list = run_list.nil? ? '' : "-o #{run_list}"
debug = !debug ? '' : '-l debug'
attributes_file = File.join(target_folder, 'attributes.json')
attributes = File.exist?(attributes_file) ? '-j attributes.json' : ''
timestamp = Time.now.to_i
cache_pid_file = "#{target_folder}/.chef/cache/chef-client-running_#{timestamp}.pid"
lockfile = "--lockfile=#{cache_pid_file}"
cmd = 'chef exec chef-client -c custom_client.rb -z '
cmd += "#{named_run_list} #{run_list} #{debug} #{attributes} #{lockfile}"
banner("Running '#{cmd}' inside folder '#{target_folder}' ...")
Dir.chdir target_folder do
run_command(cmd, true)
end
end
|
#run_command(cmd, exit_on_error = false) ⇒ Object
Runs a shell command. If all good, return false. If error, return true. If error and exit_on_error, exit with the error code.
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/cookbook_sdk/rake_tasks.rb', line 11
def run_command(cmd, exit_on_error = false)
Bundler.with_clean_env do
system(cmd)
status = $CHILD_STATUS.exitstatus
return false unless status != 0
return true unless exit_on_error
exit status
end
end
|