chef-dk 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +192 -0
  3. data/LICENSE +201 -0
  4. data/README.md +198 -0
  5. data/Rakefile +18 -0
  6. data/bin/chef +25 -0
  7. data/lib/chef-dk.rb +19 -0
  8. data/lib/chef-dk/builtin_commands.rb +31 -0
  9. data/lib/chef-dk/chef_runner.rb +83 -0
  10. data/lib/chef-dk/cli.rb +135 -0
  11. data/lib/chef-dk/command/base.rb +71 -0
  12. data/lib/chef-dk/command/exec.rb +33 -0
  13. data/lib/chef-dk/command/gem.rb +47 -0
  14. data/lib/chef-dk/command/generate.rb +97 -0
  15. data/lib/chef-dk/command/generator_commands.rb +417 -0
  16. data/lib/chef-dk/command/shell_init.rb +80 -0
  17. data/lib/chef-dk/command/verify.rb +226 -0
  18. data/lib/chef-dk/commands_map.rb +115 -0
  19. data/lib/chef-dk/component_test.rb +142 -0
  20. data/lib/chef-dk/cookbook_metadata.rb +36 -0
  21. data/lib/chef-dk/cookbook_omnifetch.rb +29 -0
  22. data/lib/chef-dk/cookbook_profiler/git.rb +95 -0
  23. data/lib/chef-dk/cookbook_profiler/identifiers.rb +79 -0
  24. data/lib/chef-dk/cookbook_profiler/null_scm.rb +32 -0
  25. data/lib/chef-dk/exceptions.rb +46 -0
  26. data/lib/chef-dk/generator.rb +70 -0
  27. data/lib/chef-dk/helpers.rb +95 -0
  28. data/lib/chef-dk/policyfile/chef_server_cookbook_source.rb +46 -0
  29. data/lib/chef-dk/policyfile/community_cookbook_source.rb +84 -0
  30. data/lib/chef-dk/policyfile/cookbook_sources.rb +20 -0
  31. data/lib/chef-dk/policyfile/cookbook_spec.rb +96 -0
  32. data/lib/chef-dk/policyfile/dsl.rb +148 -0
  33. data/lib/chef-dk/policyfile/null_cookbook_source.rb +37 -0
  34. data/lib/chef-dk/policyfile_compiler.rb +217 -0
  35. data/lib/chef-dk/policyfile_lock.rb +243 -0
  36. data/lib/chef-dk/shell_out.rb +36 -0
  37. data/lib/chef-dk/skeletons/code_generator/files/default/Berksfile +3 -0
  38. data/lib/chef-dk/skeletons/code_generator/files/default/chefignore +96 -0
  39. data/lib/chef-dk/skeletons/code_generator/files/default/converge_spec.rb +9 -0
  40. data/lib/chef-dk/skeletons/code_generator/files/default/gitignore +16 -0
  41. data/lib/chef-dk/skeletons/code_generator/files/default/spec_helper.rb +8 -0
  42. data/lib/chef-dk/skeletons/code_generator/metadata.rb +8 -0
  43. data/lib/chef-dk/skeletons/code_generator/recipes/app.rb +65 -0
  44. data/lib/chef-dk/skeletons/code_generator/recipes/attribute.rb +12 -0
  45. data/lib/chef-dk/skeletons/code_generator/recipes/cookbook.rb +50 -0
  46. data/lib/chef-dk/skeletons/code_generator/recipes/cookbook_file.rb +24 -0
  47. data/lib/chef-dk/skeletons/code_generator/recipes/lwrp.rb +23 -0
  48. data/lib/chef-dk/skeletons/code_generator/recipes/recipe.rb +9 -0
  49. data/lib/chef-dk/skeletons/code_generator/recipes/template.rb +32 -0
  50. data/lib/chef-dk/skeletons/code_generator/templates/default/README.md.erb +4 -0
  51. data/lib/chef-dk/skeletons/code_generator/templates/default/attribute.rb.erb +0 -0
  52. data/lib/chef-dk/skeletons/code_generator/templates/default/cookbook_file.erb +0 -0
  53. data/lib/chef-dk/skeletons/code_generator/templates/default/default_recipe.rb.erb +8 -0
  54. data/lib/chef-dk/skeletons/code_generator/templates/default/kitchen.yml.erb +16 -0
  55. data/lib/chef-dk/skeletons/code_generator/templates/default/metadata.rb.erb +8 -0
  56. data/lib/chef-dk/skeletons/code_generator/templates/default/provider.rb.erb +0 -0
  57. data/lib/chef-dk/skeletons/code_generator/templates/default/recipe.rb.erb +0 -0
  58. data/lib/chef-dk/skeletons/code_generator/templates/default/resource.rb.erb +0 -0
  59. data/lib/chef-dk/skeletons/code_generator/templates/default/template.erb +0 -0
  60. data/lib/chef-dk/version.rb +20 -0
  61. data/spec/shared/setup_git_cookbooks.rb +53 -0
  62. data/spec/spec_helper.rb +32 -0
  63. data/spec/test_helpers.rb +59 -0
  64. data/spec/unit/chef_runner_spec.rb +70 -0
  65. data/spec/unit/cli_spec.rb +151 -0
  66. data/spec/unit/command/base_spec.rb +88 -0
  67. data/spec/unit/command/exec_spec.rb +123 -0
  68. data/spec/unit/command/generate_spec.rb +102 -0
  69. data/spec/unit/command/generator_commands_spec.rb +504 -0
  70. data/spec/unit/command/shell_init_spec.rb +109 -0
  71. data/spec/unit/command/verify_spec.rb +311 -0
  72. data/spec/unit/commands_map_spec.rb +57 -0
  73. data/spec/unit/component_test_spec.rb +126 -0
  74. data/spec/unit/cookbook_metadata_spec.rb +62 -0
  75. data/spec/unit/cookbook_profiler/git_spec.rb +127 -0
  76. data/spec/unit/cookbook_profiler/identifiers_spec.rb +79 -0
  77. data/spec/unit/fixtures/chef-runner-cookbooks/test_cookbook/recipes/recipe_one.rb +9 -0
  78. data/spec/unit/fixtures/chef-runner-cookbooks/test_cookbook/recipes/recipe_two.rb +9 -0
  79. data/spec/unit/fixtures/command/cli_test_command.rb +26 -0
  80. data/spec/unit/fixtures/command/explicit_path_example.rb +7 -0
  81. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/.kitchen.yml +16 -0
  82. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/Berksfile +3 -0
  83. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/README.md +4 -0
  84. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/chefignore +96 -0
  85. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/metadata.rb +8 -0
  86. data/spec/unit/fixtures/cookbook_cache/baz-f59ee7a5bca6a4e606b67f7f856b768d847c39bb/recipes/default.rb +8 -0
  87. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/.kitchen.yml +16 -0
  88. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/Berksfile +3 -0
  89. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/README.md +4 -0
  90. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/chefignore +96 -0
  91. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/metadata.rb +8 -0
  92. data/spec/unit/fixtures/cookbook_cache/dep_of_bar-1.2.3/recipes/default.rb +8 -0
  93. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/.kitchen.yml +16 -0
  94. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/Berksfile +3 -0
  95. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/README.md +4 -0
  96. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/chefignore +96 -0
  97. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/metadata.rb +8 -0
  98. data/spec/unit/fixtures/cookbook_cache/foo-1.0.0/recipes/default.rb +8 -0
  99. data/spec/unit/fixtures/cookbooks_api/pruned_small_universe.json +1322 -0
  100. data/spec/unit/fixtures/cookbooks_api/small_universe.json +2987 -0
  101. data/spec/unit/fixtures/cookbooks_api/universe.json +1 -0
  102. data/spec/unit/fixtures/cookbooks_api/update_fixtures.rb +36 -0
  103. data/spec/unit/fixtures/dev_cookbooks/README.md +16 -0
  104. data/spec/unit/fixtures/dev_cookbooks/bar-cookbook.gitbundle +0 -0
  105. data/spec/unit/fixtures/eg_omnibus_dir/missing_apps/bin/.keep +0 -0
  106. data/spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/.keep +0 -0
  107. data/spec/unit/fixtures/eg_omnibus_dir/missing_apps/embedded/bin/.keep +0 -0
  108. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/bin/.keep +0 -0
  109. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/berkshelf/.keep +0 -0
  110. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/apps/test-kitchen/.keep +0 -0
  111. data/spec/unit/fixtures/eg_omnibus_dir/missing_component/embedded/bin/.keep +0 -0
  112. data/spec/unit/fixtures/eg_omnibus_dir/valid/bin/.keep +0 -0
  113. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/integration_test +2 -0
  114. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/berkshelf/verify_me +5 -0
  115. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef-dk/.keep +0 -0
  116. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/chef/verify_me +3 -0
  117. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/apps/test-kitchen/verify_me +2 -0
  118. data/spec/unit/fixtures/eg_omnibus_dir/valid/embedded/bin/.keep +0 -0
  119. data/spec/unit/fixtures/example_app/Policyfile.rb +0 -0
  120. data/spec/unit/fixtures/example_cookbook/.gitignore +17 -0
  121. data/spec/unit/fixtures/example_cookbook/.kitchen.yml +16 -0
  122. data/spec/unit/fixtures/example_cookbook/Berksfile +3 -0
  123. data/spec/unit/fixtures/example_cookbook/README.md +4 -0
  124. data/spec/unit/fixtures/example_cookbook/chefignore +96 -0
  125. data/spec/unit/fixtures/example_cookbook/metadata.rb +8 -0
  126. data/spec/unit/fixtures/example_cookbook/recipes/default.rb +8 -0
  127. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/.kitchen.yml +16 -0
  128. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/Berksfile +3 -0
  129. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/README.md +4 -0
  130. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/chefignore +96 -0
  131. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/metadata.rb +8 -0
  132. data/spec/unit/fixtures/local_path_cookbooks/local-cookbook/recipes/default.rb +8 -0
  133. data/spec/unit/fixtures/local_path_cookbooks/metadata-missing/README.md +2 -0
  134. data/spec/unit/policyfile/chef_server_cookbook_source_spec.rb +34 -0
  135. data/spec/unit/policyfile/community_cookbook_source_spec.rb +51 -0
  136. data/spec/unit/policyfile/cookbook_spec_spec.rb +200 -0
  137. data/spec/unit/policyfile/null_cookbook_source_spec.rb +35 -0
  138. data/spec/unit/policyfile_builder_spec.rb +489 -0
  139. data/spec/unit/policyfile_demands_spec.rb +484 -0
  140. data/spec/unit/policyfile_evaluation_spec.rb +284 -0
  141. data/spec/unit/shell_out_spec.rb +34 -0
  142. metadata +422 -0
@@ -0,0 +1,36 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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 'chef/cookbook/metadata'
19
+
20
+ module ChefDK
21
+
22
+ # Subclass of Chef's Cookbook::Metadata class that provides the API expected
23
+ # by CookbookOmnifetch
24
+ class CookbookMetadata < Chef::Cookbook::Metadata
25
+
26
+ def self.from_path(path)
27
+ metadata_rb_path = File.join(path, "metadata.rb")
28
+ new.tap { |m| m.from_file(metadata_rb_path) }
29
+ end
30
+
31
+ def cookbook_name
32
+ name
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,29 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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 'cookbook-omnifetch'
19
+ require 'chef-dk/shell_out'
20
+ require 'chef-dk/cookbook_metadata'
21
+
22
+ # Configure CookbookOmnifetch's dependency injection settings to use our classes and config.
23
+ CookbookOmnifetch.configure do |c|
24
+ c.cache_path = File.expand_path('~/.chefdk/cache')
25
+ c.storage_path = Pathname.new(File.expand_path('~/.chefdk/cache/cookbooks'))
26
+ c.shell_out_class = ChefDK::ShellOut
27
+ c.cached_cookbook_class = ChefDK::CookbookMetadata
28
+ end
29
+
@@ -0,0 +1,95 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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 'chef-dk/helpers'
19
+
20
+ module ChefDK
21
+ module CookbookProfiler
22
+ class Git
23
+
24
+ include Helpers
25
+
26
+ attr_reader :cookbook_path
27
+
28
+ def initialize(cookbook_path)
29
+ @cookbook_path = cookbook_path
30
+ end
31
+
32
+ def profile_data
33
+ {
34
+ "scm" => "git",
35
+ # To get this info, you need to do something like:
36
+ # figure out branch or assume 'master'
37
+ # git config --get branch.master.remote
38
+ # git config --get remote.opscode.url
39
+ "remote" => remote,
40
+ "revision" => revision,
41
+ "working_tree_clean" => clean?,
42
+ "published" => !unpublished_commits?,
43
+ "synchronized_remote_branches" => synchronized_remotes
44
+ }
45
+ end
46
+
47
+ def revision
48
+ git!("rev-parse HEAD").stdout.strip
49
+ end
50
+
51
+ def clean?
52
+ git!("diff-files --quiet", returns: [0,1]).exitstatus == 0
53
+ end
54
+
55
+ def unpublished_commits?
56
+ synchronized_remotes.empty?
57
+ end
58
+
59
+ def synchronized_remotes
60
+ @synchronized_remotes ||= git!("branch -r --contains #{revision}").stdout.lines.map(&:strip)
61
+ end
62
+
63
+ def remote
64
+ @remote_url ||=
65
+ if have_remote?
66
+ git!("config --get remote.#{remote_name}.url").stdout.strip
67
+ else
68
+ nil
69
+ end
70
+ end
71
+
72
+ def remote_name
73
+ @remote_name ||= git!("config --get branch.#{current_branch}.remote", returns: [0,1]).stdout.strip
74
+ end
75
+
76
+ def have_remote?
77
+ !remote_name.empty?
78
+ end
79
+
80
+ def current_branch
81
+ @current_branch ||= git!('rev-parse --abbrev-ref HEAD').stdout.strip
82
+ end
83
+
84
+ private
85
+
86
+ def git!(subcommand, options={})
87
+ options = { cwd: cookbook_path }.merge(options)
88
+ cmd = system_command("git #{subcommand}", options)
89
+ cmd.error!
90
+ cmd
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,79 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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 'chef/cookbook/cookbook_version_loader'
19
+ require 'digest/sha1'
20
+ require 'chef/digester'
21
+
22
+ module ChefDK
23
+ module CookbookProfiler
24
+ class Identifiers
25
+
26
+ attr_reader :cookbook_path
27
+
28
+ def initialize(cookbook_path)
29
+ @cookbook_path = cookbook_path
30
+ end
31
+
32
+ def semver_version
33
+ cookbook_version.version
34
+ end
35
+
36
+ def content_identifier
37
+ Digest::SHA1.new.hexdigest(fingerprint_text)
38
+ end
39
+
40
+ def dotted_decimal_identifier
41
+ hex_id = content_identifier
42
+ major = hex_id[0...14]
43
+ minor = hex_id[14...28]
44
+ patch = hex_id[28..40]
45
+ decimal_integers =[major, minor, patch].map {|hex| hex.to_i(16) }
46
+ decimal_integers.join(".")
47
+ end
48
+
49
+ def fingerprint_text
50
+ files_with_checksums.sort {|a,b| a[0] <=> b[0]}.inject("") do |fingerprint, file_spec|
51
+ fingerprint << "#{file_spec[0]}:#{file_spec[1]}\n"
52
+ end
53
+ end
54
+
55
+ def files_with_checksums
56
+ cookbook_files.inject([]) do |files_with_checksums, (_name, file_info)|
57
+ files_with_checksums << [file_info["path"], file_info["checksum"]]
58
+ end
59
+ end
60
+
61
+ def cookbook_files
62
+ @files ||= cookbook_version.manifest_records_by_path
63
+ end
64
+
65
+ def cookbook_version
66
+ @cookbook_version ||= cookbook_loader.cookbook_version
67
+ end
68
+
69
+ def cookbook_loader
70
+ @cookbook_loader ||=
71
+ begin
72
+ loader = Chef::Cookbook::CookbookVersionLoader.new(cookbook_path)
73
+ loader.load_cookbooks
74
+ loader
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,32 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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
+ module ChefDK
19
+ module CookbookProfiler
20
+ class NullSCM
21
+
22
+ def initialize(cookbook_path)
23
+ end
24
+
25
+ def profile_data
26
+ nil
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,46 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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
+ module ChefDK
19
+
20
+ class CachedCookbookNotFound < StandardError
21
+ end
22
+
23
+ class LocalCookbookNotFound < StandardError
24
+ end
25
+
26
+ class MalformedCookbook < StandardError
27
+ end
28
+
29
+ class MissingComponentError < RuntimeError
30
+ def initialize(component_name)
31
+ super("Component #{component_name} is missing.")
32
+ end
33
+ end
34
+
35
+ class OmnibusInstallNotFound < RuntimeError
36
+ def initialize()
37
+ super("Can not find omnibus installation directory for Chef.")
38
+ end
39
+ end
40
+
41
+ class UnsupportedFeature < StandardError
42
+ end
43
+
44
+ class PolicyfileError < StandardError
45
+ end
46
+ end
@@ -0,0 +1,70 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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
+ module ChefDK
19
+
20
+ module Generator
21
+
22
+ class Context
23
+
24
+ attr_accessor :have_git
25
+
26
+ attr_accessor :app_root
27
+ attr_accessor :cookbook_root
28
+ attr_accessor :app_name
29
+ attr_accessor :cookbook_name
30
+ attr_accessor :new_file_basename
31
+ attr_accessor :content_source
32
+ attr_accessor :author_name
33
+ attr_accessor :author_email
34
+ attr_accessor :license_description
35
+ attr_accessor :license_text
36
+
37
+ attr_accessor :skip_git_init
38
+ end
39
+
40
+ def self.reset
41
+ @context = nil
42
+ end
43
+
44
+ def self.context
45
+ @context ||= Context.new
46
+ end
47
+
48
+ module TemplateHelper
49
+
50
+ def self.delegate_to_app_context(name)
51
+ define_method(name) do
52
+ ChefDK::Generator.context.public_send(name)
53
+ end
54
+ end
55
+
56
+ # delegate all the attributes of app_config
57
+ delegate_to_app_context :app_root
58
+ delegate_to_app_context :cookbook_root
59
+ delegate_to_app_context :cookbook_name
60
+ delegate_to_app_context :new_file_basename
61
+ delegate_to_app_context :content_source
62
+ delegate_to_app_context :author_name
63
+ delegate_to_app_context :author_email
64
+ delegate_to_app_context :license_description
65
+ delegate_to_app_context :license_text
66
+
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,95 @@
1
+ #
2
+ # Copyright:: Copyright (c) 2014 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 'mixlib/shellout'
19
+ require 'chef-dk/exceptions'
20
+
21
+ module ChefDK
22
+ module Helpers
23
+
24
+ #
25
+ # Runs given commands using mixlib-shellout
26
+ #
27
+ def system_command(*command_args)
28
+ cmd = Mixlib::ShellOut.new(*command_args)
29
+ cmd.run_command
30
+ cmd
31
+ end
32
+
33
+ def err(message)
34
+ stderr.print("#{message}\n")
35
+ end
36
+
37
+ def msg(message)
38
+ stdout.print("#{message}\n")
39
+ end
40
+
41
+ def stdout
42
+ $stdout
43
+ end
44
+
45
+ def stderr
46
+ $stderr
47
+ end
48
+
49
+ #
50
+ # Locates the omnibus directories
51
+ #
52
+
53
+ def omnibus_root
54
+ @omnibus_root ||= omnibus_expand_path(Gem.ruby, "..", "..", "..")
55
+ end
56
+
57
+ def omnibus_apps_dir
58
+ @ominbus_apps_dir ||= omnibus_expand_path(omnibus_root, "embedded", "apps")
59
+ end
60
+
61
+ def omnibus_bin_dir
62
+ @omnibus_bin_dir ||= omnibus_expand_path(omnibus_root, "bin")
63
+ end
64
+
65
+ def omnibus_embedded_bin_dir
66
+ @omnibus_embedded_bin_dir ||= omnibus_expand_path(omnibus_root, "embedded", "bin")
67
+ end
68
+
69
+ private
70
+
71
+ def omnibus_expand_path(*paths)
72
+ dir = File.expand_path(File.join(paths))
73
+ raise OmnibusInstallNotFound.new() unless ( dir and File.directory?(dir) )
74
+ dir
75
+ end
76
+
77
+ #
78
+ # environment vars for omnibus
79
+ #
80
+
81
+ def omnibus_env
82
+ @omnibus_env ||=
83
+ begin
84
+ user_bin_dir = File.expand_path(File.join(Gem.user_dir, 'bin'))
85
+ {
86
+ 'PATH' => "#{omnibus_bin_dir}:#{user_bin_dir}:#{omnibus_embedded_bin_dir}:#{ENV['PATH']}",
87
+ 'GEM_ROOT' => Gem.default_dir.inspect,
88
+ 'GEM_HOME' => Gem.paths.home,
89
+ 'GEM_PATH' => Gem.path.join(':'),
90
+ }
91
+ end
92
+ end
93
+
94
+ end
95
+ end