Module: ChefWorkstationInitialize::SelfBootstrap::NoChef::SelfBootstrapHelpers
Instance Attribute Summary collapse
#workstationobj
Instance Method Summary
collapse
#default_workstation_data, #get_workstation, #set_workstation_data, #set_workstation_resource, #swap_workstation, #workstation_resource, #workstation_resource_keys
#auto_repo, #bootstrapping_progress_file, #change_unix_permission, #create_main_generator, #main_generator_name, #new_cookbook_name, #node_infra_chef, #prepare_new_resource, #project_name, #workstation_acl_path, #workstation_berks_vendor_dir, #workstation_cache_dir, #workstation_cache_options_syntaxe_path, #workstation_cache_path, #workstation_checksum_path, #workstation_chef_environments_dir, #workstation_chef_repo_path, #workstation_client_d_dir, #workstation_client_key_path, #workstation_client_path, #workstation_config_d_dir, #workstation_config_dir, #workstation_container_path, #workstation_cookbook_artifact_path, #workstation_cookbooks_dir, #workstation_data_bags_dir, #workstation_download_dir, #workstation_file, #workstation_file_backup_path, #workstation_file_cache_path, #workstation_generators_dir, #workstation_group_path, #workstation_libraries_dir, #workstation_logs_dir, #workstation_nodes_dir, #workstation_ohai_segment_plugin_path, #workstation_policy_dir, #workstation_policy_files_dir, #workstation_policy_group_dir, #workstation_resources_dir, #workstation_roles_dir, #workstation_scripts_dir, #workstation_solo_d_dir, #workstation_syntax_check_cache_path, #workstation_trusted_certs_dir, #workstation_user_path
#create_group, #create_user, #generate_secret, #generate_ssh_user_key, #generate_user_data, #secretdatabag, #secretdatabagitem, #secretdatabagkey, #set_cookbook_user_secret_key, #user_fully_created?, #userdatabag
#commit_state
#get_solo_cmd, #is_knife?, #knife, #knife_get_node_attribute, #knife_search_self_cmd, #knife_self_bootstrap_cmd, #self_bootstrap_options
#create_attribute_file, #create_chef_additionnal_dir, #create_chef_infra_cookbook, #create_cookbook, #create_generator, #create_recipe, #create_template_file, #define_cron_job, #ensure_main_environment, #project_environment_json, #project_role_json, #render_template, #write_Solo_file, #write_kitchen_file, #write_main_environment, #write_main_role, #write_main_role_environment, #write_role_environment
#building_kitchen, #generate_kitchen, #generate_machine, #is_kitchen_command?, #kitchen, #kitchen_machine, #kitchen_root
#chef_zero_provisioner, #provisioners
#default_platforms
#default_suites
#inspec_verifier, #verifiers
#chef, #chef_client, #install_chef_workstation, #is_chef_cli_command?, #is_chef_client_command?, #is_chef_command?, #is_chef_enabled?, #is_chef_installed?, #is_chef_profile_set?, #is_chefworkstation_available?, #is_knife_gem_install?
#berks, #berks_vendor, #berks_vendor_all_cookbook, #berks_vendor_cookbook, #berks_vendor_init, #delete_all_berkslock, #delete_cookbook_berkslock, #reset_berks_vendor_dir, #self_berks_vendor
Methods included from GitHelpers
#all_cookbooks, #generate_git_submodules, #generate_json_repo, #get_git_path, #get_git_relative_path, #get_git_server, #get_git_submodule, #get_self_git, #git_exec, #git_submodule_init, #repository_list
Methods included from SSHHelpers
#delete_hostkey, #delete_hostkey_ip_or_name, #generate_private_key, #ssh, #ssh_command, #ssh_keygen
#base_command, #get_homedir, #is_mixlib_disabled?, #main_command, #run_options
#analyse_object, #check_install_dir, #debug_worklog, #default_chefzero_portrange, #default_hostname, #default_install_dir, #define_resource_requirements, #error_worklog, #generate_default_install_dir, #generate_directory, #get_out_of_cache_path, #get_out_of_folder, #get_out_of_local_chef_path, #get_path, #is_chef_constant_enabled?, #parent_nil?, #require_implement_method, #search_local_project_folder, #warning_worklog, #worklog, #worklog_counter
Instance Attribute Details
#force_solo ⇒ Object
Returns the value of attribute force_solo.
20
21
22
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 20
def force_solo
@force_solo
end
|
Instance Method Details
#boostrapp_once ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 134
def boostrapp_once
create_chef_additionnal_dir
berks_vendor_init unless is_self_bootsrapping?
unless ::File.exist?(bootstrapping_progress_file)
FileUtils.touch bootstrapping_progress_file unless is_self_bootsrapping?
change_unix_permission if is_self_bootsrapping?
bootstrap_self_command
change_unix_permission if is_self_bootsrapping?
remove_bootstrap_file
exit 0 if is_self_bootsrapping?
end
end
|
#boostrapped? ⇒ Boolean
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 183
def boostrapped?
return false if for_search_local_node?
begin
result = JSON.parse(knife_search_self_cmd)
debug_worklog('Searching result' + result.to_s)
rows = result['rows']
get_runlist = nil
rows.each do |row|
unless row['run_list'].nil?
get_runlist += row['run_list']
end
end
!get_runlist.nil?
rescue JSON::ParserError => json_parse_error
worklog "Force solo as not able to parse json in: #{json_parse_error.inspect}"
boostrapped = false
@force_solo = true
rescue StandardError => std_bootstrap_err
worklog "Not able to validate if bootrapped as: #{std_bootstrap_err.inspect}"
@force_solo = true
boostrapped = false
end
boostrapped
end
|
#bootstrap_self ⇒ Object
162
163
164
165
166
167
168
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 162
def bootstrap_self
unless skip_boostrap? || boostrapped?
worklog 'machine not boostrapped'
boostrapp_once
end
end
|
#bootstrap_self_command ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 111
def bootstrap_self_command
if is_solo?
debug_worklog 'boostrapped with solo'
if chefworkstation_available?
if chef_enabled?
prepend ChefWorkstationInitialize::SelfBootstrap::WithChef
bootstrap_self_command
else
require 'kitchen'
require 'chef'
require 'chef/workstation_config_loader'
end
elsif run_as_root?
chef_client_self_bootstrap_cmd
else
restart_bootstrap
end
else
knife_self_bootstrap_cmd
end
debug_worklog 'bootstrap self command completed'
end
|
#chef_client_self_bootstrap_cmd ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 147
def chef_client_self_bootstrap_cmd
debug_worklog 'boostrapped with solo and chef-client'
chef_client_options = [self_bootstrap_options]
chef_client_options << "--runlist #{project_name}"
if ::File.exist?('solo.rb')
chef_client_options << '-c solo.rb'
else
chef_client_options << chef_solo_options_command
chef_client_options << "--chef-zero-host #{default_hostname}"
chef_client_options << "--chef-zero-port #{default_chefzero_portrange}"
end
chef_client chef_client_options, debug: true
end
|
#chef_solo_options ⇒ Object
54
55
56
57
58
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 54
def chef_solo_options
solo_options.each do |opt, value|
send(opt, JSON.generate(JSON.generate(value).encode)) unless value.nil? || !respond_to?(opt)
end
end
|
#chef_solo_options_command ⇒ Object
68
69
70
71
72
73
74
75
76
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 68
def chef_solo_options_command
solo_options.map do |opt, value|
encode_value = JSON.generate(value).encode
unless value.is_a? String
encode_value = JSON.generate(JSON.generate(value).encode)
end
"--config-option #{opt}=#{encode_value}"
end.join(' ')
end
|
#chef_solo_options_encode ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 60
def chef_solo_options_encode
chef_solo_options_str = ''
solo_options.each do |opt, value|
chef_solo_options_str += "#{opt} #{JSON.generate(JSON.generate(value).encode)}\n"
end
chef_solo_options_str
end
|
#for_search_local_node? ⇒ Boolean
26
27
28
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 26
def for_search_local_node?
is_knife? && ARGV.join(' ').include?("search node name:#{default_hostname} --format json -z")
end
|
#for_solo? ⇒ Boolean
22
23
24
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 22
def for_solo?
is_knife? && ARGV.join(' ').include?('config show solo --format json')
end
|
#install_this_gem ⇒ Object
82
83
84
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 82
def install_this_gem
chef 'gem install selfbootstrap', { as_system: true }
end
|
#is_boostrapping? ⇒ Boolean
34
35
36
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 34
def is_boostrapping?
is_chef_enabled? ? ::File.exist?(bootstrapping_progress_file) : false
end
|
#is_self_bootsrapping? ⇒ Boolean
30
31
32
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 30
def is_self_bootsrapping?
is_kitchen_command? && ARGV.join(' ').include?('list bootstrap self')
end
|
#is_solo? ⇒ Boolean
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 170
def is_solo?
force_solo || (
if skip_boostrap?
debug_worklog('Checking solo from workstation resource')
workstation_resource[:solo]
else
json_parse = JSON.parse(get_solo_cmd)
debug_worklog("is solo parse = #{json_parse}")
json_parse.nil? ? workstation_resource[:solo] : json_parse['solo']
end
)
end
|
#remove_bootstrap_file ⇒ Object
78
79
80
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 78
def remove_bootstrap_file
FileUtils.rm bootstrapping_progress_file if ::File.exist?(bootstrapping_progress_file)
end
|
#restart_bootstrap ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 86
def restart_bootstrap
rerun_opt = { sudo: true }
if run_as_root?
install_chef_workstation
set_chef_profile
install_this_gem
rerun_opt[:as_system] = true
end
worklog 'Self bootstrap with sudo command'
debug_worklog 'boostrapped with solo and kitchen and root'
remove_bootstrap_file
exit base_command('selfbootstrap', ARGV, rerun_opt)
end
|
#run_as_root? ⇒ Boolean
42
43
44
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 42
def run_as_root?
ENV['USER'] == 'root'
end
|
#set_chef_profile ⇒ Object
101
102
103
104
105
106
107
108
109
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 101
def set_chef_profile
bash_file = '/etc/bash.bashrc'
chef_shell_cmd = 'eval "$(chef shell-init bash)"'
debug_worklog "Set chef profile to #{bash_file}"
open(bash_file, 'a') do |f|
f.puts chef_shell_cmd
end unless ::File.read(bash_file).include?(chef_shell_cmd)
end
|
#skip_boostrap? ⇒ Boolean
46
47
48
49
50
51
52
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 46
def skip_boostrap?
debug_worklog("for_solo = #{for_solo?}")
debug_worklog("for_search_local_node = #{for_search_local_node?}")
debug_worklog("unauthorized_to_boostrap = #{unauthorized_to_boostrap?}")
debug_worklog("is_boostrapping = #{is_boostrapping?}")
for_solo? || for_search_local_node? || unauthorized_to_boostrap? || is_boostrapping?
end
|
#solo_options ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 210
def solo_options
{
chef_repo_path: get_path(workstation_chef_repo_path),
cookbook_path: [
get_path(workstation_cookbooks_dir),
get_path(workstation_libraries_dir),
get_path(workstation_resources_dir),
get_path(workstation_berks_vendor_dir),
],
data_bags_path: get_path(workstation_data_bags_dir),
environment_path: get_path(workstation_chef_environments_dir),
node_path: get_path(workstation_nodes_dir),
policy_group_path: get_path(workstation_policy_group_dir),
policy_path: get_path(workstation_policy_dir),
role_path: get_path(workstation_roles_dir),
solo: is_solo?,
log_location: ::File.join(get_path(workstation_logs_dir), "#{project_name}_#{workstation_resource[:user]}.log"),
cache_options: {
path: get_path(workstation_cache_options_syntaxe_path),
},
cache_path: get_path(workstation_cache_path),
checksum_path: get_path(workstation_checksum_path),
acl_path: get_path(workstation_acl_path),
client_d_dir: get_path(workstation_client_d_dir),
client_key_path: get_path(workstation_client_key_path),
client_path: get_path(workstation_client_path),
color: true,
config_d_dir: get_path(workstation_config_d_dir),
config_dir: get_path(workstation_config_dir),
container_path: get_path(workstation_container_path),
cookbook_artifact_path: get_path(workstation_cookbook_artifact_path),
enable_reporting: false,
file_backup_path: get_path(workstation_file_backup_path),
file_cache_path: get_path(workstation_file_cache_path),
group: workstation_resource[:group],
group_path: get_path(workstation_group_path),
lockfile: ::File.join(get_path(workstation_file_cache_path), 'chef-client-running.pid'),
node_name: default_hostname,
ohai_segment_plugin_path: get_path(workstation_ohai_segment_plugin_path),
solo_d_dir: get_path(workstation_solo_d_dir),
user: workstation_resource[:user],
user_home: workstation_resource[:home],
user_path: get_path(workstation_user_path),
syntax_check_cache_path: get_path(workstation_syntax_check_cache_path),
trusted_certs_dir: get_path(workstation_trusted_certs_dir),
validation_client_name: "#{project_name}-chef-validator",
data_bag_encrypt_version: 3,
}
end
|
#unauthorized_to_boostrap? ⇒ Boolean
38
39
40
|
# File 'lib/selfbootstrap/nochef/selfbootstrap.rb', line 38
def unauthorized_to_boostrap?
is_chef_command? || is_chef_client_command? || is_chef_cli_command?
end
|