Class: ChefDK::Command::GeneratorCommands::Cookbook

Inherits:
Base
  • Object
show all
Defined in:
lib/chef-dk/command/generator_commands/cookbook.rb

Overview

## CookbookFile chef generate cookbook path/to/basename –generator-cookbook=path/to/generator

Generates a basic cookbook directory structure. Most file types are omitted, the user is expected to add additional files as needed using the relevant generators.

Instance Attribute Summary collapse

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#chef_runner, #generator_cookbook_name, #generator_cookbook_path, #have_git?

Methods included from ChefDK::Configurable

#chef_config, #chefdk_config, #config_loader, #generator_config, #knife_config

Methods inherited from Base

#needs_help?, #needs_version?, #run_with_default_options

Methods included from Helpers

#chefdk_home, #err, #git_bin_dir, #git_windows_bin_dir, #msg, #omnibus_apps_dir, #omnibus_bin_dir, #omnibus_chefdk_location, #omnibus_embedded_bin_dir, #omnibus_env, #omnibus_install?, #omnibus_root, #stderr, #stdout, #system_command, #usr_bin_path, #usr_bin_prefix

Constructor Details

#initialize(params) ⇒ Cookbook

Returns a new instance of Cookbook.



73
74
75
76
77
78
79
80
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 73

def initialize(params)
  @params_valid = true
  @cookbook_name = nil
  @berks_mode = true
  @enable_delivery = true
  @verbose = false
  super
end

Instance Attribute Details

#cookbook_name_or_pathObject (readonly)

Returns the value of attribute cookbook_name_or_path.



36
37
38
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 36

def cookbook_name_or_path
  @cookbook_name_or_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



34
35
36
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 34

def errors
  @errors
end

Instance Method Details

#berks_mode?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 168

def berks_mode?
  @berks_mode
end

#contains_delivery_cli_toml?(path) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
201
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 198

def contains_delivery_cli_toml?(path)
  delivery_cli_path = path.join(".delivery/cli.toml")
  delivery_cli_path.exist?
end

#cookbook_full_pathObject



164
165
166
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 164

def cookbook_full_path
  File.expand_path(cookbook_name_or_path, Dir.pwd)
end

#cookbook_nameObject



156
157
158
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 156

def cookbook_name
  File.basename(cookbook_full_path)
end

#cookbook_path_in_git_repo?Boolean

Returns:

  • (Boolean)


232
233
234
235
236
237
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 232

def cookbook_path_in_git_repo?
  Pathname.new(cookbook_full_path).ascend do |dir|
    return true if File.directory?(File.join(dir.to_s, ".git"))
  end
  false
end

#cookbook_rootObject



160
161
162
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 160

def cookbook_root
  File.dirname(cookbook_full_path)
end

#emit_post_create_messageObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 100

def emit_post_create_message
  if have_delivery_config?
    msg("Your cookbook is ready. To setup the pipeline, type `cd #{cookbook_name_or_path}`, then run `delivery init`")
  else
    msg("Your cookbook is ready. Type `cd #{cookbook_name_or_path}` to enter it.")
    msg("\nThere are several commands you can run to get started locally developing and testing your cookbook.")
    msg("Type `delivery local --help` to see a full list.")
    msg("\nWhy not start by writing a test? Tests for the default recipe are stored at:\n")
    msg("test/smoke/default/default_test.rb")
    msg("\nIf you'd prefer to dive right in, the default recipe can be found at:")
    msg("\nrecipes/default.rb\n")
  end
end

#enable_delivery?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 172

def enable_delivery?
  @enable_delivery
end

#have_delivery_config?Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 180

def have_delivery_config?
  # delivery-cli's logic is to look recursively upward for
  # .delivery/cli.toml starting from pwd:
  # https://github.com/chef/delivery-cli/blob/22cbef3987ebd0aee98405b7e161a100edc87e49/src/delivery/config/mod.rs#L225-L247

  path_to_check = File.expand_path(Dir.pwd)
  result = false

  Pathname.new(path_to_check).ascend do |path|
    if contains_delivery_cli_toml?(path)
      result = true
      break
    end
  end

  result
end

#params_valid?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 228

def params_valid?
  @params_valid
end

#pipelineObject



136
137
138
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 136

def pipeline
  config[:pipeline]
end

#policy_nameObject



140
141
142
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 140

def policy_name
  cookbook_name
end

#policy_run_listObject



144
145
146
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 144

def policy_run_list
  "#{cookbook_name}::#{recipe_name}"
end

#read_and_validate_paramsObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 203

def read_and_validate_params
  arguments = parse_options(params)
  @cookbook_name_or_path = arguments[0]
  if !@cookbook_name_or_path
    @params_valid = false
  elsif /-/ =~ File.basename(@cookbook_name_or_path)
    msg("Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/ctl_chef.html#chef-generate-cookbook for more information.")
  end

  if config[:berks] && config[:policy]
    err("Berkshelf and Policyfiles are mutually exclusive. Please specify only one.")
    @params_valid = false
  end

  if config[:policy]
    @berks_mode = false
  end

  if config[:verbose]
    @verbose = true
  end

  true
end

#recipeObject



148
149
150
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 148

def recipe
  "cookbook"
end

#recipe_nameObject



152
153
154
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 152

def recipe_name
  "default"
end

#runObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 82

def run
  read_and_validate_params
  if params_valid?
    setup_context
    msg("Generating cookbook #{cookbook_name}")
    chef_runner.converge
    msg("")
    emit_post_create_message
    0
  else
    err(opt_parser)
    1
  end
rescue ChefDK::ChefRunnerError => e
  err("ERROR: #{e}")
  1
end

#setup_contextObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 114

def setup_context
  super
  Generator.add_attr_to_context(:skip_git_init, cookbook_path_in_git_repo?)
  Generator.add_attr_to_context(:cookbook_root, cookbook_root)
  Generator.add_attr_to_context(:cookbook_name, cookbook_name)
  Generator.add_attr_to_context(:recipe_name, recipe_name)
  Generator.add_attr_to_context(:include_chef_repo_source, false)
  Generator.add_attr_to_context(:policy_name, policy_name)
  Generator.add_attr_to_context(:policy_run_list, policy_run_list)
  Generator.add_attr_to_context(:policy_local_cookbook, ".")

  Generator.add_attr_to_context(:enable_delivery, enable_delivery?)
  Generator.add_attr_to_context(:delivery_project_dir, cookbook_full_path)
  Generator.add_attr_to_context(:build_cookbook_parent_is_cookbook, true)
  Generator.add_attr_to_context(:delivery_project_git_initialized, have_git? && !cookbook_path_in_git_repo?)

  Generator.add_attr_to_context(:verbose, verbose?)

  Generator.add_attr_to_context(:use_berkshelf, berks_mode?)
  Generator.add_attr_to_context(:pipeline, pipeline)
end

#verbose?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/chef-dk/command/generator_commands/cookbook.rb', line 176

def verbose?
  @verbose
end