Trouble with chef-solo "Missing Cookbooks" - ruby

I am trying to run chef-solo, and I have the following configuration
/var/chef
├── cookbooks
│   ├── cc_db
│   │   ├── Berksfile
│   │   ├── Berksfile.lock
│   │   ├── chefignore
│   │   ├── metadata.rb
│   │   ├── recipes
│   │   │   └── default.rb
├── node.json
└── solo.rb
node.json
{
"run_list": [ "recipe[cc_db]" ]
}
solo.rb
file_cache_path "/var/chef"
cookbook_path "/var/chef/cookbooks"
json_attribs "/var/chef/node.json"
metadata.rb
depends 'database', '~> 5.1.2'
depends 'mysql2_chef_gem', '~> 1.0'
I've gotten this to work in a kitchen-test environment, but I'm trying it now on a different server using chef-solo.
Whenever I run:
chef-solo -c solo.rb
I get:
resolving cookbooks for run list: ["cc_db"]
================================================================================
Error Resolving Cookbooks for Run List:
================================================================================
Missing Cookbooks:
------------------
No such cookbook: database
Expanded Run List:
------------------
* cc_db
Platform:
---------
x86_64-linux
Running handlers:
[2016-09-20T15:34:05-04:00] ERROR: Running exception handlers
Running handlers complete
[2016-09-20T15:34:05-04:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated in 02 seconds
[2016-09-20T15:34:05-04:00] FATAL: Stacktrace dumped to /var/chef/chef-stacktrace.out
[2016-09-20T15:34:05-04:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report
[2016-09-20T15:34:05-04:00] ERROR: 412 "Precondition Failed"
[2016-09-20T15:34:05-04:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
I'm confused because the database cookbook seem to be installed with berks, but chef isn't picking it up.
any help would be greatly appreciated.
thanks.
myles.

Chef doesn't directly integrate with Berkshelf, so chef-solo knows nothing about that. Normally you would use something like knife-solo which ties them together, but you could also do it manually. Run berks vendor /path/to/a/folder and then set that folder as the cookbook_path.

I ended up just going around the problem.
I set up a chef-server on a virtual host, and do some final level testing on that. I created nodes, bootstrapped them, then ran chef-client on the node.
I also don't have a very large server configuration, so I just uploaded the cookbooks and dependent cookbooks by hands. Which I'm pretty sure is why chef-zero was failing. At some point in the future I will get Berkshelf working in conjunction with everything to help ease the cookbook dependency process.

Related

golang unknown revision module/vX.Y.Z and importing package properly

I have a golang application structure like this:
.
├── calc
│   ├── go.mod
│   ├── main.go
│   └── Makefile
├── go.mod
├── LICENSE
├── num
│   ├── go.mod
│   └── num.go
└── README.md
Where calc is an "application" where I'm importing the num package to add 2 numbers.
calc/go.mod
go 1.15
require github.com/github_username/goapp/num v0.2.1
num/go.mod
module github.com/github_username/goapp/num/v0.2.1
go 1.15
go.mod
module github.com/github_username/goapp/v0.2.1
go 1.15
When in /calc, and I run go run main.go, I get the following:
go: github.com/github_username/goapp/num#v0.2.1: reading github.com/github_username/goapp/num/num/go.mod at revision num/v0.2.1: unknown revision num/v0.2.1
What am I doing wrong? The github repo has the annotated tags.
For further context, I'm mimicking a production setup where we have six different mini golang services in folders such as calc, calc2, etc. where each "calc" service has a go.mod file.
module github.com/github_username/goapp/num/v0.2.1
Is nonsense. The semver version tag "v0.2.1" does not belong into the module name.
(Note that for major versions > 1, e.g. 4.3.1, the major version becomes part of the name like in module github.com/user/proj/folder/v4).
And one more: There are no source code belonging to the root go.mod so this module makes no sense whatsoever.
You really should not make that many modules.
Are you working with private repositories?
if yes, then you need to configure OAuth authentication:
export GITHUB_TOKEN=MY_GITHUB_TOKEN
git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic#github.com/".insteadOf "https://github.com/"
Now, if you don't have the necessity to use private repositories!
Turn your repositories to the public that will solve the problem too.

Automatic Ansible custom modules installation with Ansible Galaxy

Is there any nice way to use Ansible Galaxy order to install and enable Ansible (2.7.9) custom modules?
My requirement allows Ansible Galaxy to download the right Ansible role which embeds my custom module. Once ansible-galaxy install --roles-path ansible/roles/ -r roles/requirements.yml, I get the following structure (non-exhaustive):
├── ansible
│   ├── roles
│   │   ├── mymodule (being imported by Galaxy)
│   │   │   ├── library
│   │   │   │   └── mymodule.py
By looking this part of the documentation, it seems like my module is at the right place and does not require any further configuration: https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html?highlight=library#directory-layout
But when I found this part of the documentation I got confused. Is ANSIBLE_LIBRARY related to the custom modules?
DEFAULT_MODULE_PATH
Description: Colon separated paths in which Ansible will search for Modules.
Type: pathspec
Default: ~/.ansible/plugins/modules:/usr/share/ansible/plugins/modules
Ini Section: defaults
Ini Key: library
Environment: ANSIBLE_LIBRARY
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-module-path
When calling my module,
- name: Test of my Module
mymodule:
I get the following error:
ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.
I expected not to have to configure the ANSIBLE_LIBRARY and the module being automatically callable. Am I understanding correctly or should I also trick this var?
If your custom module is in a role, you need to include the role in your playbook, so at the very least:
---
- hosts: myhosts
roles:
- role: mymodule
tasks:
- name: Test of my Module
mymodule:

Ruby Uninitialized Constant Error when Inheriting in Chef

In my LWRP extending a supermarket cookbook, I got the following working.
module PIWebsphereCookBook
class WebsphereJbdc < WebsphereCookBook::WebsphereBase
Whereas when I introduced my own class in between these 2, I am getting the 'Unitialized Constant Error'
require_relative './pi_websphere_base'
module PIWebsphereCookBook
class WebsphereJbdc < PIWebsphereBase
The base class I am trying to extend is as shown below:
module PIWebsphereCookBook
class PIWebsphereBase < WebsphereCookbook::WebsphereBase
I see from the logs that the referenced class/file is being loaded
[2017-06-23T18:37:28-04:00] DEBUG: Loading cookbook pi_websphere's resources from /var/chef/cache/cookbooks/pi_websphere/resources/pi_websphere_base.rb
[2017-06-23T18:37:28-04:00] DEBUG: Loaded contents of /var/chef/cache/cookbooks/pi_websphere/resources/pi_websphere_base.rb into resource pi_websphere_pi_websphere_base (Custom resource pi_websphere_pi_websphere_base from cookbook pi_websphere)
[2017-06-23T18:37:28-04:00] DEBUG: Loading cookbook pi_websphere's resources from /var/chef/cache/cookbooks/pi_websphere/resources/websphere_j2c.rb
[2017-06-23T18:37:28-04:00] DEBUG: Loaded contents of /var/chef/cache/cookbooks/pi_websphere/resources/websphere_j2c.rb into resource pi_websphere_websphere_j2c (Custom resource pi_websphere_websphere_j2c from cookbook pi_websphere)
[2017-06-23T18:37:28-04:00] DEBUG: Loading cookbook pi_websphere's resources from /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb
[2017-06-23T18:37:28-04:00] DEBUG: Filtered backtrace of compile error: /var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:3:in `<module:PIWebsphereCookBook>',/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:2:in `class_from_file'
[2017-06-23T18:37:28-04:00] DEBUG: Backtrace entry for compile error: '/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:3:in `<module:PIWebsphereCookBook>''
[2017-06-23T18:37:28-04:00] DEBUG: Line number of compile error: '3'
Recipe Compile Error in
/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb
NameError
---------
uninitialized constant #
<Class:0x000000052796d8>::PIWebsphereCookBook::PIWebsphereBase
Cookbook Trace:
---------------
/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:3:in ` <module:PIWebsphereCookBook>'
/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:2:in ` class_from_file'
Relevant File Content:
----------------------
/var/chef/cache/cookbooks/pi_websphere/resources/websphere_jdbc.rb:
1: require_relative './pi_websphere_base'
2: module PIWebsphereCookBook
3>> class WebsphereJbdc < PIWebsphereBase
4: require_relative 'helper'
5:
The folder structure is given below
[root#localhost pi_websphere]# tree
.
├── attributes
│   └── default.rb
├── Gemfile
├── jaas_auth.py
├── libraries
│   ├── abc.rb
│   ├── artifactory_helper.rb
│    ├── pi_websphere_base.rb
│   └── websphere_base.rb
├── metadata.rb
├── README.md
├── resources
│   ├── artifactory_client.rb
│   ├── helper.rb
│   ├── websphere_j2c.rb
│   ├── websphere_jdbc.rb
You need to make sure the file containing PIWebsphereBase is loaded first. Normally cookbook library files are loaded in ASCII-betical order, but you can force things a little more with the require_relative you have there but is commented out. You probably want require_relative './pi_websphere_base' or something like that, or tweak the filenames.

Using Chef Environments with Berkshelf

For my current project, I have an "Environment Cookbook" that has a folder structure like this:
.
├── Berksfile
├── Vagrantfile
├── environments
│   └── development.rb
├── metadata.rb
└── recipes
└── default.rb
In my Vagrantfile I use a chef_solo provisioner with the following configuration:
config.vm.provision :chef_solo do |chef|
chef.environments_path = 'environments'
chef.environment = 'development'
chef.run_list = [
"recipe[service-ldap::default]"
]
end
I have the vagrant-berkshelf plugin installed and the provisioning works, besides that I do not get any attributes that I have set in the environments/development.rb.
My question is, whether - and if yes, how - it is possible to use Chef Environments with Berkshelf or whether this is not part of the development workflow in Berkshelf. Maybe you could provide me a hint, how to achieve a workaround then.

Simple pattern to export rake tasks defined in gem to the outside world upon installation

This question follows the "Make rake task from gem available everywhere?" for which I'm not fully satisfied with the Railties approach since it induces a dependency on Rails3 which seems to me overkilling compared to what I want.
Also, I dislike the idea to create an extra binary as suggested in this question
So, assuming I have the below layout:
.
├── CHANGELOG.md
├── Gemfile
├── Gemfile.lock
├── LICENCE.md
├── README.md
├── Rakefile
├── my_gem.gemspec
├── lib
│   ├── my_gem
│   │   ├── common.rb
│   │   ├── loader.rb
│   │   ├── tasks
│   │   │   └── sometasks.rake
│   │   └── version.rb
│   └── my_gem.rb
where lib/my_gem/tasks/sometasks.rake has some nested rake tasks definition:
#.....................
namespace :n1 do
#.....................
namespace :n2 do
desc "Tasks n1:n2:t1"
task :t1 do |t|
puts "Task 1"
end
desc "Tasks n1:n2:t2"
task :t2 do |t|
puts "Task 2"
end
end # namespace n1:n2
end # namespace n1
How can I easily share these tasks in another external Rakefile with a simple syntax such as require "my_gem/sometasks" once the gem is installed?
I tested the following configuration in a separate directory with success yet I still think it's a complicated syntax. Any help to simplify the load / include/ require would be welcome:
add a GemFile containing
gem 'my_gem', :path => '~/path/to/my_gem'
add a Rakefile with the following content:
require "my_gem"
load "my_gem/tasks/sometasks.rake"
desk "Test my_gem"
task :toto do |t|
puts "task toto"
end
In the above configuration, it works:
$> bundle exec rake -T
rake n1:n2:t1 # Task n1:n2:t1
rake n1:n2:t1 # Task n1:n2:t2
rake toto # Test my_gem
Yet if I get rid of .rake extension in the Rakefile, I have a load error
rake aborted!
LoadError: cannot load such file -- my_gem/tasks/sometasks
Any hint?
I found a way, inspired from Bundler to offer a simplified interface (and [gem_tasks.rb](https://github.com/bundler/bundler/blob/master/lib/bundler/gem_tasks.rb):
create a file lib/my_gem/sometasks_tasks.rb with an install_tasks method
require 'rake'
module MyGem
class SomeTasks
include Rake::DSL if defined? Rake::DSL
def install_tasks
load 'my_gem/tasks/sometasks.rake'
end
end
end
MyGem::SomeTasks.new.install_tasks
In the application’s Rakefile, it is now enough to add require 'my_gem/sometasks_tasks'

Resources