Chef metadata.rb dependency needs to depend on itself - ruby

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.

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.

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.

chef - using recipes from same cookbook

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

Why chef does not execute recipe line by line?

Problem is that chef tries to install template first, and only then installs packages. If i comment template block, chef will install sphinxsearch package fine.
But if template block is not commented, sphinxsearch package is not installed, and chef fails with error
resource template[/etc/sphinxsearch/sphinx.conf] is configured to notify resource service[sphinxsearch] with action reload, but service[sphinxsearch] cannot be found in the resource collection`
Why this happens?
##
# Install system packages
##
node['website']['packages'].each do |pkg|
log 'Installing ' + pkg
package pkg
end
##
# Configure sphinx
##
template "/etc/sphinxsearch/sphinx.conf" do
source 'sphinx.erb'
owner 'root'
group 'root'
mode 00644
notifies :reload, 'service[sphinxsearch]', :delayed
end
notifies and subscribes in chef are both trying to reach out to resources that have been defined in your chef run. They will then call teh indicated action on those resources. In your case:
notifies :reload, 'service[sphinxsearch]', :delayed
is looking for a resource of type service named sphinxsearch and call the reload action on it. If, at the end of the resource gathering (compile) phase, chef cannot find a service[sphinxsearch] resource, then it throws the error. You don't see the package installed because chef never enters the execution phase. (See this answer for more on the two phase nature of chef)
As indicated by #IsabelHM, you could solve the problem by adding
service 'sphinxsearch' do
action [:enable, :start]
end
I suggest you use [:enable, :start] rather than :nothing as this will ensure that the service is always running, even if your template doesn't change. Also, please note that the service resource does not add a service config for you. So if the sphinxsearch package does not add a service config, you'll also need a cookbook_file, template, or remote_file resource to create the service config with.
Add this in your recipe.
service 'sphinxsearch' do
action :nothing
end

Deploy java app in Chef cookbook

I am already using application cookbook to deploy and install my java application. I tried it on my own.
application "saymetrix" do
path "/usr/local"
owner "chef"
group "chef"
end
And for the above code i get following error.
Error executing action `deploy` on resource 'application[saymetrix]'
================================================================================
NameError
---------
Cannot find a resource for saymetrix on ubuntu version 12.04
Cookbook Trace:
---------------
/var/chef/cache/cookbooks/application/providers/default.rb:82:in `before_deploy'
/var/chef/cache/cookbooks/application/providers/default.rb:27:in `block in class_from_file'
I don't get how to resolve this?
Refer to the application_java cookbook.
you need to add these in the metadata.rb of your saymatrix cookbook:
depends 'application'
depends 'application_java'
And add necessary details at least including repository and deployment target like tomcat. You can find the section Usage of the example here: application_java

Resources