chef - using recipes from same cookbook - ruby

I have cookbook base and cookbook myapp
base has 2 recipes - my_java and java_with_custom_stuff
in java_with_custom_stuff I want to use the recipe for my_java (same cookbook). something like
include_recipe 'my_java'
bash 'custom stuff' do
...
end
in myapp I do
include_recipe "base::java_with_custom_stuff"
yet it complains that it cannot find my_java
Is there a way to use a recipe from the same cookbook?

include_recipe uses the first part every time as the cookbook name. So you have to specify the cookbook - name + recipe name:
include_recipe '::my_java' # works still after you rename your cookbook
include_recipe 'base::my_java' # works only as long as your cookbook name is base

Related

Chef::Exceptions::CookbookNotFound: Cookbook sudo not found and Chef-Solo “undefined method `[]' for nil:NilClass”

I'm trying to make a cookbook that has some dependencies, but it doesn't work
/recipe
ls
default.rb
Recipe:
include_recipe 'sudo::default'
include_recipe 'user'
def will_create_user?(username, user)
return false if node['etc']['passwd'].key?(username)
return true if user['action'] == 'create' || !user.key?('action')
false
end
node['authorization']['sudo']['groups'].each do |group|
group group
end
node['vms']['users'].each do |username, user|
send_notification = will_create_user? username, user
user_account username do
comment user['comment'] if user['comment']
ssh_keys user['ssh_keys']
groups user['groups'] if user['groups']
if send_notification
notifies :run, "execute[reset_password_#{username}]", :delayed
end
end
execute "reset_password_#{username}" do
command "passwd -d #{username} && chage -d 0 #{username}"
action :nothing
end
end
Metadata.rb
...
version '0.1.0'
chef_version '>= 14.0'
depends "sudo"
depends "user"
Berksfile.lock
DEPENDENCIES
vms-users
path: .
metadata: true
GRAPH
sudo (5.4.5)
user (0.7.0)
vms-users (0.1.0)
sudo (>= 0.0.0)
user (>= 0.0.0)
Attributes/default.rb
{
"vms": {
"users": {
'magrini' => {
'comment' => 'Bruna Magrini',
'groups' => ['sysadmin'],
'ssh_keys' => ['chave ssh'],
},
}
}
}
I'm executing using chef-client --local-mode default.rb
Error: Chef::Exceptions::CookbookNotFound: Cookbook sudo not found
Recipe `sudo::default` is not in the run_list, and cookbook 'sudo'
is not a dependency of any cookbook in the run_list. To load this recipe,
first add a dependency on cookbook 'sudo' in the cookbook you're
including it from in that cookbook's metadata.
Running handlers:
[2019-12-19T20:42:12+00:00] ERROR: Running exception handlers
Running handlers complete
[2019-12-19T20:42:12+00:00] ERROR: Exception handlers complete
Chef Infra Client failed. 0 resources updated in 01 seconds
[2019-12-19T20:42:12+00:00] FATAL: Stacktrace dumped to /home/chef-repo/.chef/local-mode-cache/cache/chef-stacktrace.out
[2019-12-19T20:42:12+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2019-12-19T20:42:12+00:00] FATAL: Chef::Exceptions::CookbookNotFound: Cookbook sudo not found. If you're loading sudo from another cookbook, make sure you configure the dependency in your metadata
i have a feeling that you are confusing something, since the title of your question mentions chef-solo when you are really using chef-zero (--local-mode).
you should definitely favor chef-zero over chef-solo (but i won't get into the reasons why).
in both cases (using chef-zero or chef-solo), you will have to download all the cookbooks and make verify chef-client knows where are the cookbooks located.
if you ase using chef-zero, here are some references:
Local mode does not require a configuration file, instead it will look for a directory named /cookbooks and will set chef_repo_path to be just above that. (Local mode will honor the settings in a configuration file, if desired.) If the client.rb file is not found and no configuration file is specified, local mode will search for a config.rb file.
client.rb settings:
chef_repo_path: The path to the chef-repo containing cookbooks and other files, such as environments or data bags, when running Chef Infra Client in local mode.
cookbook_path: The sub-directory for Chef Infra Client cookbooks. This value can be a string or an array of file system locations, processed in the specified order. The last cookbook is considered to override local modifications.
since i see that you are using berkshelf, you can use vendor sub-command to download all the cookbooks dependencies and place them in the same directory. then, have a custom configuration for chef-client, that sets the value of cookbook_path to the same directory which you used in conjunction with berks vendor, and finally execute chef-client.

Chef metadata.rb dependency needs to depend on itself

I am getting a foodcritic error when I try to include a recipie within the default.rb for the apache cookbook lesson:
#
# Cookbook:: apache
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.
# Install apached package
package 'apache2' do
package_name 'httpd'
action :install
end
service 'apache2' do
service_name 'httpd'
action [:start, :enable]
end
include_recipe 'apache::websites'
websites.rb
file 'default www' do
path '/var/ww/html/index.html'
content 'Hello world!'
end
When I run foodcritic on default.rb
FC007: Ensure recipe dependencies are reflected in cookbook metadata:
default.rb:17
When I follow the recomendation and add the dependency in the metadata.rb and run knife cookbook upload apache I get this:
ERROR: RuntimeError: Cookbook depends on itself in cookbook apache, please
remove the this unnecessary self-dependency
Below is my metadeta.rb
name 'apache'
maintainer 'xxxxx'
maintainer_email 'xxxxx'
license 'All Rights Reserved'
description 'Installs/Configures apache'
long_description 'Installs/Configures apache'
version '0.1.1'
chef_version '>= 12.1' if respond_to?(:chef_version)
depends 'apache'
#The `issues_url` points to the location where issues for this cookbook are
# tracked. A `View Issues` link will be displayed on this cookbook's page
when
# uploaded to a Supermarket.
#
# issues_url 'https://github.com/<insert_org_here>/apache/issues'
# The `source_url` points to the development repository for this cookbook.
A
# `View Source` link will be displayed on this cookbook's page when uploaded
to
# a Supermarket.
#
# source_url 'https://github.com/<insert_org_here>/apache'
New to chef and ruby, first question on Stack Overflow.
Am I adding the dependency wrong?
Does anything jump out that would cause this issue?
The FC007 activation is wrong. Are you sure you're running foodcritic correctly? Just run foodcritic . on the base of the cookbook folder (i.e. the same place as the metadata.rb is). That should detect the name of the cookbook and not flag the include.

confused with berkshelf, chef and vagrant

I've took a look on BerkShelf documentation. I've been able to figure out it stands for managing cookbook dependencies. So, guess I'd like to build a machine with java. I've first generated my custom cookbook:
chef generate cookbook custom-java
My Berksfile is:
source 'https://supermarket.chef.io'
metadata
cookbook 'java'
and my metadata.rb is:
name 'custom-java'
...
version '0.1.0'
After that, I've simply run berks install, so all dependencies have been resolved and located under ~\.berkshelf\cookbooks.
Nevertheless, I don't quite figure out how to use my custom-java into my vagrant configuration. What do I need to do in order for vagrant to provistion this cookbook into my machines?
My vagrant structure is:
VagrantFile
├───chef
│ ├───cookbooks
│ │ ├───(((1))) <<<<<<<<<<<<<<<<<<<<<<
│ ├───roles
│ │ ├───java-dev-workstation.rb
Vagrantfile content is:
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.provision "chef_solo" do |chef|
chef.roles_path = "chef/roles"
chef.cookbooks_path = "chef/cookbooks"
chef.add_role "java-dev-workstation"
end
end
And java-dev-workstation.rb:
name "java-dev-workstation"
default_attributes(
# Because it's Oracle, we need to agree to the terms and conditions.
:custom-java => { >>>>>>>>> (((2)))
:install_flavor => 'oracle',
:jdk_version => '8',
:accept_license_agreement => true,
:oracle => { "accept_oracle_download_terms" => true }
}
)
run_list(
"recipe[java]"
)
I'm using Chef 12.18.31.
On (((1))): Do I need to "import" my custom-java cookbook, how? Where is it located?
On (((2))): How should I configure java?
EDIT
I've set chef.cookbooks_path:
config.vm.provision "chef_solo" do |chef|
chef.roles_path = "chef/roles"
chef.cookbooks_path = 'D:\projects\living\vagrant\production\berk\custom-java'
chef.add_role "java-dev-workstation"
end
EDIT2
Nevertheless, custom-java dependencies are not resolved now:
================================================================================
Error Resolving Cookbooks for Run List:
================================================================================
Missing Cookbooks:
------------------
No such cookbook: yum
Expanded Run List:
------------------
* custom-java
Platform:
---------
x86_64-linux
My metadata.rb content is:
name 'berk'
...
version '0.1.0'
supports 'centos'
depends 'yum'
Currently, all dependencies are located in ~/.berkshelf/cookbooks. It seems shef-solo is not looking for in it...
You did take it backward for a wrapper cookbook pattern, your custom_java should depends on java and change its default behavior (overriding node attributes).
Your custom-java metadata.rb should contain a line like this, what is in the berksfile will never be interpreted by the chef-client run, it's an outside dependency resolver to create coherent bundles.
depends 'java', '~> 1.47'
And the default.rb a line
include_recipe 'java'
Then your Berksfile can omit the cookbook line (unless you're pulling a version elsewhere than the source at top)
Next your role should use custom-java as recipe and not java, then the chef-client run (the vagrant part before edit sounds ok and should pull both cookbooks) will compute attributes from the java coobooks, overwrite those defined in custom-java and you should end up with the desired behavior.
Alternatively, you can just set the runlist to custom-java and avoid the role altogether, that will works.
I highly recommend you to go through all the tutorials on https://learn.chef.io to get a better overview.

override attributes in elasticsearch cookbook

I am trying to use "elasticsearch/cookbook-elasticsearch" cookbook for with my wrapper cookbook. I want to override following default attributes from cookbook-elasticsearch in my wrapper cookbook.
default.elasticsearch[:rpm_url] = "https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-0.90.12.noarch.rpm"
default.elasticsearch[:rpm_sha] = "ab7ea2e00d8f1b73642e3ea44d9647b11e6b0b96"
Cookbook : https://github.com/elasticsearch/cookbook-elasticsearch
How do I do this in my-elasticsearch cookbook ?
cat site-cookbooks/my-elasticsearch/attributes/default.rb
override.elasticsearch[:rpm_url] = "https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.4.noarch.rpm"
override.elasticsearch[:rpm_sha] = "ec8b41c54a6d897479645b2507476e0824bc71db"
is this correct one ?
I want to use this cookbook for chef 10
any help
To add information to #mark-oconnor comment:
Documentation about attributes in chef 10.
Recommended notation would be override['elasticsearch']['rpm_url'] = "new_value" the method and symbol way to access attributes have been problematic in the past.
As the cookbook loading order in chef 10 is not always clearly predictable you have to use override level to ensure the correct value is used when compîling recipes.
Edit after comments:
In the elasticsearch cookbook in version 0.3.13 the default recipe install from tarball.
If you wish to use a packaged install you have to call the corresponding recipe before the default, as in the default recipe there's a guard to not install tarball if elasticsearch is already installed.
The correct recipe in wrapper cookbook for this particular case is:
include_recipe 'elasticsearch::rpm' # Take the overriden attributes and install package
include_recipe 'elasticsearch' # no need to ::default, if omitted it's the recipe loaded

Can you mix chef-zero chef-metal, chef-metal-vagrant (Vagrant) and berkshelf?

I want to leverage chef-metal and chef-zero with my existing cookbooks and chef-repo (already leveraging berkshelf and vagrant for dev)
I started with the example provided at https://github.com/opscode/chef-metal#vagrant
I've got a vagrant_linux.rb
require 'chef_metal_vagrant'
vagrant_box 'CentOS-6.4-x86_64' do
url 'http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130427.box'
end
with_machine_options :vagrant_options => {
'vm.box' => 'CentOS-6.4-x86_64'
}
I also have dev_server.rb
require 'chef_metal'
with_chef_local_server :chef_repo_path => '~/workspace/git/my-chef-repo'
machine 'dev_server' do
tag 'dev_server'
recipe 'myapp'
converge true
end
If I put my myapp cookbook under ~/workspace/git/my-chef-repo/cookbooks, the above works fine
using the following command, I've got a vagrant managed vm named dev_server converging (applying myapp recipe)
chef-client -z vagrant_linux.rb dev_server.rb
But now, I'd like to keep my cookbooks folder empty and use berkshelf,
It does not look supported by chef-zero at the moment, is it ?
How could I do that ?
You can pass :cookbook_path that contains multiple paths as an Array like so: https://github.com/opscode/ec-metal/blob/master/cookbooks/ec-harness/recipes/vagrant.rb#L12-L13
with_chef_local_server :chef_repo_path => repo_path,
:cookbook_path => [ File.join(repo_path, 'cookbooks'),
File.join(repo_path, 'vendor', 'cookbooks') ]
Then you can use berks to vendor upstream cookbooks into a different path (vendor/cookbooks/), while putting your own cookbooks into cookbooks/ like so: https://github.com/opscode/ec-metal/blob/master/Rakefile#L114
berks vendor vendor/cookbooks/
The "berks vendor" command is how I generally do that--use "berks vendor" and add the vendored path to your cookbook path.

Resources