chef-cli 3.1.6 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/chef-cli.gemspec +1 -1
- data/lib/chef-cli/command/export.rb +14 -6
- data/lib/chef-cli/command/generate.rb +5 -3
- data/lib/chef-cli/command/generator_commands/cookbook.rb +32 -2
- data/lib/chef-cli/command/generator_commands/recipe.rb +7 -0
- data/lib/chef-cli/helpers.rb +4 -4
- data/lib/chef-cli/policyfile_services/export_repo.rb +9 -13
- data/lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb +50 -24
- data/lib/chef-cli/skeletons/code_generator/recipes/recipe.rb +11 -3
- data/lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb +36 -0
- data/lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb +18 -0
- data/lib/chef-cli/version.rb +1 -1
- data/spec/unit/command/export_spec.rb +18 -1
- data/spec/unit/command/generate_spec.rb +7 -0
- data/spec/unit/command/generator_commands/cookbook_spec.rb +91 -226
- data/spec/unit/command/generator_commands/recipe_spec.rb +34 -0
- data/spec/unit/helpers_spec.rb +111 -0
- data/spec/unit/policyfile_services/export_repo_spec.rb +51 -2
- metadata +7 -14
- data/lib/chef-cli/command/generator_commands/build_cookbook.rb +0 -126
- data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md +0 -146
- data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml +0 -21
- data/lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb +0 -8
- data/lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json +0 -17
- data/lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb +0 -175
- data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb +0 -7
- data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb +0 -10
- data/lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb +0 -9
- data/spec/unit/command/generator_commands/build_cookbook_spec.rb +0 -377
@@ -35,4 +35,38 @@ describe ChefCLI::Command::GeneratorCommands::Recipe do
|
|
35
35
|
|
36
36
|
end
|
37
37
|
|
38
|
+
context "when YAML recipe flag is passed" do
|
39
|
+
|
40
|
+
let(:argv) { %w{some_recipe --yaml} }
|
41
|
+
let(:expected_cookbook_root) { tempdir }
|
42
|
+
let(:cookbook_name) { "example_cookbook" }
|
43
|
+
let(:cookbook_path) { File.join(tempdir, cookbook_name) }
|
44
|
+
|
45
|
+
let(:generator_name) { "recipe" }
|
46
|
+
let(:generated_files) do
|
47
|
+
[ "recipes/some_recipe.yml",
|
48
|
+
"spec/spec_helper.rb",
|
49
|
+
"spec/unit/recipes/some_recipe_spec.rb",
|
50
|
+
"test/integration/default/some_recipe_test.rb",
|
51
|
+
]
|
52
|
+
end
|
53
|
+
let(:new_file_name) { "some_recipe" }
|
54
|
+
|
55
|
+
before do
|
56
|
+
FileUtils.cp_r(File.join(fixtures_path, "example_cookbook"), tempdir)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "creates a new recipe" do
|
60
|
+
Dir.chdir(cookbook_path) do
|
61
|
+
allow(recipe_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
|
62
|
+
recipe_generator.run
|
63
|
+
end
|
64
|
+
|
65
|
+
generated_files.each do |expected_file|
|
66
|
+
expect(File).to exist(File.join(cookbook_path, expected_file))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
38
72
|
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# Copyright:: Copyright (c) 2014-2018 Chef Software Inc.
|
2
|
+
# License:: Apache License, Version 2.0
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
#
|
16
|
+
|
17
|
+
require "spec_helper"
|
18
|
+
require "chef-cli/helpers"
|
19
|
+
|
20
|
+
describe ChefCLI::Helpers do
|
21
|
+
context "path_check!" do
|
22
|
+
|
23
|
+
before do
|
24
|
+
allow(Gem).to receive(:ruby).and_return(ruby_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when installed via omnibus" do
|
28
|
+
before do
|
29
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_install?).and_return true
|
30
|
+
end
|
31
|
+
|
32
|
+
context "on unix" do
|
33
|
+
|
34
|
+
let(:user_bin_dir) { File.expand_path(File.join(Gem.user_dir, "bin")) }
|
35
|
+
let(:omnibus_embedded_bin_dir) { "/opt/chef-workstation/embedded/bin" }
|
36
|
+
let(:omnibus_bin_dir) { "/opt/chef-workstation/bin" }
|
37
|
+
let(:expected_PATH) { [ omnibus_bin_dir, user_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"].split(File::PATH_SEPARATOR) ] }
|
38
|
+
let(:expected_GEM_ROOT) { Gem.default_dir }
|
39
|
+
let(:expected_GEM_HOME) { Gem.user_dir }
|
40
|
+
let(:expected_GEM_PATH) { Gem.path.join(File::PATH_SEPARATOR) }
|
41
|
+
let(:ruby_path) { "/opt/chef-workstation/embedded/bin/ruby" }
|
42
|
+
|
43
|
+
it "#omnibus_env path" do
|
44
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_bin_dir).and_return("/opt/chef-workstation/bin")
|
45
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_embedded_bin_dir).and_return("/opt/chef-workstation/embedded/bin")
|
46
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_env).and_return(
|
47
|
+
"PATH" => expected_PATH.flatten.uniq.join(File::PATH_SEPARATOR),
|
48
|
+
"GEM_ROOT" => expected_GEM_ROOT,
|
49
|
+
"GEM_HOME" => expected_GEM_HOME,
|
50
|
+
"GEM_PATH" => expected_GEM_PATH
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "on windows" do
|
56
|
+
let(:ruby_path) { "c:/opscode/chef-workstation/embedded/bin/ruby.exe" }
|
57
|
+
let(:user_bin_dir) { File.expand_path(File.join(Gem.user_dir, "bin")) }
|
58
|
+
let(:omnibus_embedded_bin_dir) { "c:/opscode/chef-workstation/embedded/bin" }
|
59
|
+
let(:omnibus_bin_dir) { "c:/opscode/chef-workstation/bin" }
|
60
|
+
let(:expected_GEM_ROOT) { Gem.default_dir }
|
61
|
+
let(:expected_GEM_HOME) { Gem.user_dir }
|
62
|
+
let(:expected_GEM_PATH) { Gem.path.join(File::PATH_SEPARATOR) }
|
63
|
+
let(:omnibus_root) { "c:/opscode/chef-workstation" }
|
64
|
+
let(:expected_PATH) { [ omnibus_bin_dir, user_bin_dir, omnibus_embedded_bin_dir, ENV["PATH"].split(File::PATH_SEPARATOR) ] }
|
65
|
+
|
66
|
+
before do
|
67
|
+
allow(ChefCLI::Helpers).to receive(:expected_omnibus_root).and_return(ruby_path)
|
68
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_install?).and_return(true)
|
69
|
+
allow(Chef::Platform).to receive(:windows?).and_return(true)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "#omnibus_env path" do
|
73
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_bin_dir).and_return("c:/opscode/chef-workstation/bin")
|
74
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_embedded_bin_dir).and_return("c:/opscode/chef-workstation/embedded/bin")
|
75
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_env).and_return(
|
76
|
+
"PATH" => expected_PATH.flatten.uniq.join(File::PATH_SEPARATOR),
|
77
|
+
"GEM_ROOT" => expected_GEM_ROOT,
|
78
|
+
"GEM_HOME" => expected_GEM_HOME,
|
79
|
+
"GEM_PATH" => expected_GEM_PATH
|
80
|
+
)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when not installed via omnibus" do
|
86
|
+
|
87
|
+
before do
|
88
|
+
allow(ChefCLI::Helpers).to receive(:omnibus_install?).and_return false
|
89
|
+
end
|
90
|
+
let(:ruby_path) { "/Users/bog/.lots_o_rubies/2.1.2/bin/ruby" }
|
91
|
+
let(:expected_root_path) { "/Users/bog/.lots_o_rubies" }
|
92
|
+
|
93
|
+
before do
|
94
|
+
allow(File).to receive(:exist?).with(expected_root_path).and_return(false)
|
95
|
+
|
96
|
+
%i{
|
97
|
+
omnibus_root
|
98
|
+
omnibus_bin_dir
|
99
|
+
omnibus_embedded_bin_dir
|
100
|
+
}.each do |method_name|
|
101
|
+
allow(ChefCLI::Helpers).to receive(method_name).and_raise(ChefCLI::OmnibusInstallNotFound.new)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
it "skips the sanity check without error" do
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#
|
2
|
-
# Copyright:: Copyright (c)
|
2
|
+
# Copyright:: Copyright (c) Chef Software Inc.
|
3
3
|
# License:: Apache License, Version 2.0
|
4
4
|
#
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -42,12 +42,15 @@ describe ChefCLI::PolicyfileServices::ExportRepo do
|
|
42
42
|
|
43
43
|
let(:archive) { false }
|
44
44
|
|
45
|
+
let(:policy_group) { nil }
|
46
|
+
|
45
47
|
subject(:export_service) do
|
46
48
|
described_class.new(policyfile: policyfile_rb_explicit_name,
|
47
49
|
root_dir: working_dir,
|
48
50
|
export_dir: export_dir,
|
49
51
|
archive: archive,
|
50
|
-
force: force_export
|
52
|
+
force: force_export,
|
53
|
+
policy_group: policy_group)
|
51
54
|
end
|
52
55
|
|
53
56
|
it "uses Policyfile.rb as the default Policyfile name" do
|
@@ -306,6 +309,52 @@ describe ChefCLI::PolicyfileServices::ExportRepo do
|
|
306
309
|
expect(File).to exist(readme_path)
|
307
310
|
end
|
308
311
|
|
312
|
+
context "when the policy_group is changed" do
|
313
|
+
let(:policy_group) { "production" }
|
314
|
+
it "creates a policy_group file for a specified policy group with the revision id of the exported policy" do
|
315
|
+
exported_policy_group_path = File.join(export_dir, "policy_groups", "production.json")
|
316
|
+
exported_policy_group_data = FFI_Yajl::Parser.parse(IO.read(exported_policy_group_path))
|
317
|
+
|
318
|
+
expected_data = { "policies" => { "install-example" => { "revision_id" => revision_id } } }
|
319
|
+
|
320
|
+
expect(exported_policy_group_data).to eq(expected_data)
|
321
|
+
end
|
322
|
+
|
323
|
+
it "creates a working local mode configuration file with the changed policy_group" do
|
324
|
+
expected_config_text = <<~CONFIG
|
325
|
+
### Chef Infra Client Configuration ###
|
326
|
+
# The settings in this file will configure chef to apply the exported policy in
|
327
|
+
# this directory. To use it, run:
|
328
|
+
#
|
329
|
+
# chef-client -z
|
330
|
+
#
|
331
|
+
|
332
|
+
policy_name 'install-example'
|
333
|
+
policy_group 'production'
|
334
|
+
|
335
|
+
use_policyfile true
|
336
|
+
policy_document_native_api true
|
337
|
+
|
338
|
+
# In order to use this repo, you need a version of Chef Infra Client and Chef Zero
|
339
|
+
# that supports policyfile "native mode" APIs:
|
340
|
+
current_version = Gem::Version.new(Chef::VERSION)
|
341
|
+
unless Gem::Requirement.new(">= 12.7").satisfied_by?(current_version)
|
342
|
+
puts("!" * 80)
|
343
|
+
puts(<<-MESSAGE)
|
344
|
+
This Chef Repo requires features introduced in Chef Infra Client 12.7, but you are using
|
345
|
+
Chef \#{Chef::VERSION}. Please upgrade to Chef Infra Client 12.7 or later.
|
346
|
+
MESSAGE
|
347
|
+
puts("!" * 80)
|
348
|
+
exit!(1)
|
349
|
+
end
|
350
|
+
|
351
|
+
CONFIG
|
352
|
+
config_path = File.join(export_dir, ".chef", "config.rb")
|
353
|
+
expect(File).to exist(config_path)
|
354
|
+
expect(IO.read(config_path)).to eq(expected_config_text)
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
309
358
|
end
|
310
359
|
|
311
360
|
context "when the export dir is empty" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chef-cli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chef Software, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mixlib-cli
|
@@ -240,7 +240,6 @@ files:
|
|
240
240
|
- lib/chef-cli/command/generator_commands.rb
|
241
241
|
- lib/chef-cli/command/generator_commands/attribute.rb
|
242
242
|
- lib/chef-cli/command/generator_commands/base.rb
|
243
|
-
- lib/chef-cli/command/generator_commands/build_cookbook.rb
|
244
243
|
- lib/chef-cli/command/generator_commands/chef_exts/generator_desc_resource.rb
|
245
244
|
- lib/chef-cli/command/generator_commands/chef_exts/quieter_doc_formatter.rb
|
246
245
|
- lib/chef-cli/command/generator_commands/chef_exts/recipe_dsl_ext.rb
|
@@ -328,13 +327,9 @@ files:
|
|
328
327
|
- lib/chef-cli/service_exceptions.rb
|
329
328
|
- lib/chef-cli/shell_out.rb
|
330
329
|
- lib/chef-cli/skeletons/code_generator/files/default/Berksfile
|
331
|
-
- lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/README.md
|
332
|
-
- lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/kitchen.yml
|
333
|
-
- lib/chef-cli/skeletons/code_generator/files/default/build_cookbook/test-fixture-recipe.rb
|
334
330
|
- lib/chef-cli/skeletons/code_generator/files/default/chefignore
|
335
331
|
- lib/chef-cli/skeletons/code_generator/files/default/cookbook_readmes/README-policy.md
|
336
332
|
- lib/chef-cli/skeletons/code_generator/files/default/cookbook_readmes/README.md
|
337
|
-
- lib/chef-cli/skeletons/code_generator/files/default/delivery-config.json
|
338
333
|
- lib/chef-cli/skeletons/code_generator/files/default/delivery-project.toml
|
339
334
|
- lib/chef-cli/skeletons/code_generator/files/default/gitignore
|
340
335
|
- lib/chef-cli/skeletons/code_generator/files/default/repo/README.md
|
@@ -354,7 +349,6 @@ files:
|
|
354
349
|
- lib/chef-cli/skeletons/code_generator/files/default/spec_helper_policyfile.rb
|
355
350
|
- lib/chef-cli/skeletons/code_generator/metadata.rb
|
356
351
|
- lib/chef-cli/skeletons/code_generator/recipes/attribute.rb
|
357
|
-
- lib/chef-cli/skeletons/code_generator/recipes/build_cookbook.rb
|
358
352
|
- lib/chef-cli/skeletons/code_generator/recipes/cookbook.rb
|
359
353
|
- lib/chef-cli/skeletons/code_generator/recipes/cookbook_file.rb
|
360
354
|
- lib/chef-cli/skeletons/code_generator/recipes/helpers.rb
|
@@ -372,10 +366,8 @@ files:
|
|
372
366
|
- lib/chef-cli/skeletons/code_generator/templates/default/Policyfile.rb.erb
|
373
367
|
- lib/chef-cli/skeletons/code_generator/templates/default/README.md.erb
|
374
368
|
- lib/chef-cli/skeletons/code_generator/templates/default/attribute.rb.erb
|
375
|
-
- lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/Berksfile.erb
|
376
|
-
- lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/metadata.rb.erb
|
377
|
-
- lib/chef-cli/skeletons/code_generator/templates/default/build_cookbook/recipe.rb.erb
|
378
369
|
- lib/chef-cli/skeletons/code_generator/templates/default/cookbook_file.erb
|
370
|
+
- lib/chef-cli/skeletons/code_generator/templates/default/delivery-project.toml.erb
|
379
371
|
- lib/chef-cli/skeletons/code_generator/templates/default/helpers.rb.erb
|
380
372
|
- lib/chef-cli/skeletons/code_generator/templates/default/inspec_default_test.rb.erb
|
381
373
|
- lib/chef-cli/skeletons/code_generator/templates/default/kitchen.yml.erb
|
@@ -383,6 +375,7 @@ files:
|
|
383
375
|
- lib/chef-cli/skeletons/code_generator/templates/default/kitchen_policyfile.yml.erb
|
384
376
|
- lib/chef-cli/skeletons/code_generator/templates/default/metadata.rb.erb
|
385
377
|
- lib/chef-cli/skeletons/code_generator/templates/default/recipe.rb.erb
|
378
|
+
- lib/chef-cli/skeletons/code_generator/templates/default/recipe.yml.erb
|
386
379
|
- lib/chef-cli/skeletons/code_generator/templates/default/recipe_spec.rb.erb
|
387
380
|
- lib/chef-cli/skeletons/code_generator/templates/default/repo/gitignore.erb
|
388
381
|
- lib/chef-cli/skeletons/code_generator/templates/default/resource.rb.erb
|
@@ -415,7 +408,6 @@ files:
|
|
415
408
|
- spec/unit/command/generate_spec.rb
|
416
409
|
- spec/unit/command/generator_commands/attribute_spec.rb
|
417
410
|
- spec/unit/command/generator_commands/base_spec.rb
|
418
|
-
- spec/unit/command/generator_commands/build_cookbook_spec.rb
|
419
411
|
- spec/unit/command/generator_commands/chef_exts/generator_desc_resource_spec.rb
|
420
412
|
- spec/unit/command/generator_commands/chef_exts/recipe_dsl_ext_spec.rb
|
421
413
|
- spec/unit/command/generator_commands/cookbook_file_spec.rb
|
@@ -528,6 +520,7 @@ files:
|
|
528
520
|
- spec/unit/fixtures/local_path_cookbooks/noignore-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/metadata.rb
|
529
521
|
- spec/unit/fixtures/local_path_cookbooks/noignore-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/recipes/default.rb
|
530
522
|
- spec/unit/generator_spec.rb
|
523
|
+
- spec/unit/helpers_spec.rb
|
531
524
|
- spec/unit/kitchen/provisioner/chef_zero_capture_spec.rb
|
532
525
|
- spec/unit/pager_spec.rb
|
533
526
|
- spec/unit/policyfile/artifactory_cookbook_source_spec.rb
|
@@ -594,7 +587,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
594
587
|
requirements:
|
595
588
|
- - ">="
|
596
589
|
- !ruby/object:Gem::Version
|
597
|
-
version: '2.
|
590
|
+
version: '2.6'
|
598
591
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
599
592
|
requirements:
|
600
593
|
- - ">="
|
@@ -630,7 +623,6 @@ test_files:
|
|
630
623
|
- spec/unit/command/generate_spec.rb
|
631
624
|
- spec/unit/command/generator_commands/attribute_spec.rb
|
632
625
|
- spec/unit/command/generator_commands/base_spec.rb
|
633
|
-
- spec/unit/command/generator_commands/build_cookbook_spec.rb
|
634
626
|
- spec/unit/command/generator_commands/chef_exts/generator_desc_resource_spec.rb
|
635
627
|
- spec/unit/command/generator_commands/chef_exts/recipe_dsl_ext_spec.rb
|
636
628
|
- spec/unit/command/generator_commands/cookbook_file_spec.rb
|
@@ -743,6 +735,7 @@ test_files:
|
|
743
735
|
- spec/unit/fixtures/local_path_cookbooks/noignore-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/metadata.rb
|
744
736
|
- spec/unit/fixtures/local_path_cookbooks/noignore-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/recipes/default.rb
|
745
737
|
- spec/unit/generator_spec.rb
|
738
|
+
- spec/unit/helpers_spec.rb
|
746
739
|
- spec/unit/kitchen/provisioner/chef_zero_capture_spec.rb
|
747
740
|
- spec/unit/pager_spec.rb
|
748
741
|
- spec/unit/policyfile/artifactory_cookbook_source_spec.rb
|
@@ -1,126 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Copyright:: Copyright (c) 2016-2019 Chef Software Inc.
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
require_relative "base"
|
19
|
-
require_relative "../../dist"
|
20
|
-
|
21
|
-
module ChefCLI
|
22
|
-
module Command
|
23
|
-
module GeneratorCommands
|
24
|
-
|
25
|
-
class BuildCookbook < Base
|
26
|
-
|
27
|
-
banner "Usage: #{ChefCLI::Dist::EXEC} generate build-cookbook NAME [options]"
|
28
|
-
|
29
|
-
attr_reader :errors
|
30
|
-
|
31
|
-
attr_reader :cookbook_name_or_path
|
32
|
-
|
33
|
-
option :pipeline,
|
34
|
-
long: "--pipeline PIPELINE",
|
35
|
-
description: "Use PIPELINE to set target branch to something other than master for the build_cookbook",
|
36
|
-
default: "master"
|
37
|
-
|
38
|
-
options.merge!(SharedGeneratorOptions.options)
|
39
|
-
|
40
|
-
def initialize(params)
|
41
|
-
@params_valid = true
|
42
|
-
@cookbook_name = nil
|
43
|
-
super
|
44
|
-
end
|
45
|
-
|
46
|
-
def run
|
47
|
-
read_and_validate_params
|
48
|
-
if params_valid?
|
49
|
-
setup_context
|
50
|
-
chef_runner.converge
|
51
|
-
0
|
52
|
-
else
|
53
|
-
err(opt_parser)
|
54
|
-
1
|
55
|
-
end
|
56
|
-
rescue ChefCLI::ChefRunnerError => e
|
57
|
-
err("ERROR: #{e}")
|
58
|
-
1
|
59
|
-
end
|
60
|
-
|
61
|
-
def setup_context
|
62
|
-
super
|
63
|
-
Generator.add_attr_to_context(:workflow_project_dir, workflow_project_dir)
|
64
|
-
|
65
|
-
Generator.add_attr_to_context(:workflow_project_git_initialized, workflow_project_git_initialized?)
|
66
|
-
Generator.add_attr_to_context(:build_cookbook_parent_is_cookbook, build_cookbook_parent_is_cookbook?)
|
67
|
-
|
68
|
-
Generator.add_attr_to_context(:pipeline, pipeline)
|
69
|
-
end
|
70
|
-
|
71
|
-
def pipeline
|
72
|
-
config[:pipeline]
|
73
|
-
end
|
74
|
-
|
75
|
-
def recipe
|
76
|
-
"build_cookbook"
|
77
|
-
end
|
78
|
-
|
79
|
-
def build_cookbook_parent_is_cookbook?
|
80
|
-
metadata_json_path = File.join(workflow_project_dir, "metadata.json")
|
81
|
-
metadata_rb_path = File.join(workflow_project_dir, "metadata.rb")
|
82
|
-
|
83
|
-
File.exist?(metadata_json_path) || File.exist?(metadata_rb_path)
|
84
|
-
end
|
85
|
-
|
86
|
-
def workflow_project_dir
|
87
|
-
project_dir = File.expand_path(cookbook_name_or_path, Dir.pwd)
|
88
|
-
# Detect if we were invoked with arguments like
|
89
|
-
#
|
90
|
-
# chef generate build-cookbook project/.delivery/build_cookbook
|
91
|
-
#
|
92
|
-
# If so, normalize paths so we don't make a directory structure like
|
93
|
-
# `.delivery/.delivery/build_cookbook`.
|
94
|
-
#
|
95
|
-
# Note that we don't check the name of the build cookbook the user
|
96
|
-
# asked for and we hard-code to naming it "build_cookbook". We also
|
97
|
-
# don't catch the case that the user requested something like
|
98
|
-
# `project/.delivery/build_cookbook/extra-thing-that-shouldn't-be-here`
|
99
|
-
Pathname.new(project_dir).ascend do |dir|
|
100
|
-
if File.basename(dir) == ".delivery"
|
101
|
-
project_dir = File.dirname(dir)
|
102
|
-
end
|
103
|
-
end
|
104
|
-
project_dir
|
105
|
-
end
|
106
|
-
|
107
|
-
def workflow_project_git_initialized?
|
108
|
-
File.exist?(File.join(workflow_project_dir, ".git"))
|
109
|
-
end
|
110
|
-
|
111
|
-
def read_and_validate_params
|
112
|
-
arguments = parse_options(params)
|
113
|
-
@cookbook_name_or_path = arguments[0]
|
114
|
-
unless @cookbook_name_or_path
|
115
|
-
@params_valid = false
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def params_valid?
|
120
|
-
@params_valid
|
121
|
-
end
|
122
|
-
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|