Using a chef cookbook in my own cookbook error - ruby

So I am new to chef and want to use chef-patch in my own recipes. I added cookbook 'patch', '~> 2.2.1' to my Berksfile and did sudo berks install
and it installs it. But when I run my recipe it says ERROR: undefined method 'replace' for #<Chef::Recipe:0x00000003cab450>
Is there something I need to add to my fix.rb code.
replace "/etc/login.defs" do
replace "PASS_MAX_DAYS 60"
with "PASS_MAX_DAYS 59"
end

There is no direct link between Berkshelf and either of those. You need to use berks upload to send things to the Chef server for use with chef-client, and for chef-solo the workflow is up to you (though will probably involve berks vendor to write the cookbooks out at some point). You also need depends 'patch' in your metadata.rb.

Related

chef windows_package encoding errors

I am trying to use Chef to install R onto windows 7.
I installed chef_handler and windows using knife
knife cookbook site install windows
knife cookbook site install chef_handler
and created a new cookbook
chef generate cookbook cookbooks/awesome_machine_config
inside default.rb I have
windows_package 'R for Windows 3.3.2' do
source 'd:/Downloads/R-3.3.2-win.exe'
action :install
end
inside metadata.rb
I have:
depends 'windows'
when I run:
chef-client --local-mode recipe[awesome_machine_config]
It synchronises the cookbooks fine and installs the cookbook gems fine.
But then I get a:
Error executing action `install` on resource 'windows_package[R for Windows 3.3.2]'
Encoding::InvalidByteSequenceError
incomplete "\x00" on UTF-16LE
chef is running as administrator so shouldn' be a permissions fault?
Any ideas how to debug?

Chef with omnibus using 3rd party gems

Could someone please help with a question regarding use of a 3rd party gem with an omnibus installation running Chef recipes?
I would like to use a 'p4ruby' gem that has been packaged up with omnibus and should be available for use in a recipe using
require 'P4'
I get the error
cannot load such file -- P4
In the installation directory I can see
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/service/gem/ruby/2.1.0/cache/p4ruby-2015.1.0.gem
./embedded/service/gem/ruby/2.1.0/specifications/p4ruby-2015.1.0.gemspec
./embedded/service/gem/ruby/2.1.0/build_info/p4ruby-2015.1.0.info
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/lib/ruby/gems/2.1.0/cache/p4ruby-2015.1.0.gem
I have read through the Chef docs and see that I could use chef_gem or gem_package for example
gem_package 'p4ruby' do
end
However this always tries to compile the gem and this leads to errors in our target installation environment as we cannot expect dev tools such as make and g++ to be available and we do not want to force installation of them.
How can I get Chef to 'see' this gem or to specifiy a 3rd party gem without compilation running? Is this possible?
For further info if I do use gem_package on a machine that does have make and g++ I end up with the following in the install directory (which looks slightly different than before). Is this a path issue?? I've searched all over but cannot find a solution
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/service/gem/ruby/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/service/gem/ruby/2.1.0/cache/p4ruby-2015.1.0.gem
./embedded/service/gem/ruby/2.1.0/specifications/p4ruby-2015.1.0.gemspec
./embedded/service/gem/ruby/2.1.0/build_info/p4ruby-2015.1.0.info
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubyconf.h
./embedded/lib/ruby/gems/2.1.0/gems/p4ruby-2015.1.0/ext/P4/p4rubydebug.h
./embedded/lib/ruby/gems/2.1.0/cache/p4ruby-2015.1.0.gem
./embedded/lib/ruby/gems/2.1.0/specifications/p4ruby-2015.1.0.gemspec
Many thanks for any help
How can I get Chef to 'see' this gem or to specifiy a 3rd party gem
without compilation running? Is this possible?
No, if a gem has some C extension, it must be compiled and will need build tools, that's why there's the build-essential cookbook.
The differences between chef_gem and gem_package are this:
gem_package will install to system ruby (or default to chef embedded ruby is none present) where chef_gem will always install in chef embedded ruby.
gem_package run in converge phase where chef_gem run at compile time
The main reason is that chef_gem is aimed at installing gem to be used in recipes where gem_package is aimed at managing system gems for use out of chef.
Think I found a way of doing this...
$:.unshift *Dir[File.path('<my_path_to_gems>/**/lib')]
I can then
require 'P4'
in my recipes

Failed to configure an environment for GSWD lessons

I want to configure an evirinment to take Django lessons from enter link description here
The main problem that I'm getting errors with instalation of some Ruby gems.
At first I've got an error that told that mixlib is not installed because it requires ruby 1.9.3.
So, I've changed 11th string of the postinstall.sh file like this
The version of Ruby to be installed supporting the Chef and Puppet gems
ruby_ver="1.9.3-p429"
So then I've rerun postinstall.sh script. But I've got an error
ERROR: Error installing chef:
ohai requires Ruby version >= 2.0.0.
To fix that I've installed Ruby 2.1 and set it to use by default, then installed chef.
After I've logoff, run vagrant ssh, typed the pass. The inputed ls and there weren't synced files from host machine. I've run sudo ./postinstall.sh again and got the ohai version error.
Is there any ideas how to fix it?
That example is badly out of date with the following list of problems:
Chef is now installed using an omnibus package which contains an
embedded ruby. No more mucking around with gem installs.
Vagrant now supports a chef zero provisioner, big improvement over chef-solo and will also install chef.
Old format Vagrantfile. V2 has introduced a number of incompatible changes.
That example uses Ubuntu 12.04 which is no longer supported.
The "aws" cookbook under the "cookbooks" directory has invalid metadata causing chef to fail.... In fact all the cookbooks look out of date.
==> default: [2015-04-11T20:04:06+00:00] ERROR: Cookbook loaded at path(s) [/tmp/vagrant-chef/046103d02209a201a2d21ece6c21437c/cookbooks/aws] has invalid metadata: The `name' attribute is required in cookbook metadata
Tutorial needs a substantial re-write, sorry. (If the code was in github that might encourage others to help refactor it)

berks install found nothing to rewrite

I am using a Windows 7 x64 computer, with ChefDK 0.3.0-1 installed, with the berkshelf, and omnibus gems installed as well. I setup my Berksfile and metadata.rb files as such
metadata.rb
depends 'users', '~> 1.7'
depends 'sudo', '~> 2.6'
Berksfile
cookbook 'users', git: 'https://github.com/sethvargo-cookbooks/users.git', rel: 'cookbooks/users', tag: 'v1.7.0'
cookbook 'sudo', git: 'https://github.com/opscode-cookbooks/sudo.git', rel: 'cookbooks/sudo', tag: 'v2.6.0'
I set the environment variable in my System Environment Variables to:
BERKSHELF_PATH=C:/
However, when I issue the command:
> berks install
I get the following error:
C:\c\c\cookbook_test> berks install
Resolving cookbook dependencies...
Fetching 'addm_setup' from source at .
Fetching 'sudo' from https://github.com/opscode-cookbooks/sudo.git (at v2.6.0/cookbooks/sudo)
Git error: command `git filter-branch --subdirectory-filter "cookbooks/sudo" --force` failed. If this error persists, try removing the cache directory at 'C:/chef/.cache/git/6f5ef5c786d127e1b549253a13b5d48c181aedeb'.Output from the command:
Found nothing to rewrite
I figured out I can't use the Cygwin Git, either because I don't know what to configure for it or something, and that the Git from git-scm works so far. I'm also using Windows Command Prompt to execute these commands. I'm not sure what to do from here, or what this is trying to tell me.
Unfortunately, and fortunately, this was a simple answer. I was receiving the errors listed in the question because I had the
rel: 'cookbooks/sudo'
What that was doing was looking for a pre-existing repository, but that didn't exist. So, once I removed that, the cookbooks were downloaded and included for use.
The new results were:
C:\c\c\cookbook_test> berks install
Resolving cookbook dependencies...
Fetching 'cookbook_test' from source at .
Fetching 'sudo' from https://github.com/opscode-cookbooks/sudo.git (at v2.6.0)
Fetching 'users' from https://github.com/sethvargo-cookbooks/users.git (at v1.7.0)
Fetching cookbook index from https://supermarket.getchef.com...
Using cookbook_test (0.1.0) from source at .
Using sudo (2.6.0) from https://github.com/opscode-cookbooks/sudo.git (at v2.6.0)
Using users (1.7.0) from https://github.com/sethvargo-cookbooks/users.git (at v1.7.0)

How to install gems with system wide rbenv by using chef

I installed rbenv system wide (in /usr/local/rbenv) in my VPS.
And I want to install gems by using chef.
I tried like this, but the gem is installed in system default ruby.
gem_package "daemons" do
action :install
end
How can I install gems in system wide rbenv?
By following coderanger's advice, I installed rbenv cookbook by Berkshelf.
After that I edited recipe file like this:
include_recipe "rbenv::default"
include_recipe "rbenv::ruby_build"
rbenv_ruby "2.0.0-p451"
rbenv_gem "daemons" do
ruby_version "2.0.0-p451"
end
Then I got a error ERROR: No resource, method, or local variable named `rbenv_root_path' for `Chef::Recipe "default"'. How can I solve this problem?
You need to use the rbenv_gem resource.

Resources