undefined method `nssm' for cookbook - jmeter

I am trying to install a jmeter service using nssm through cookbook.
but while running chef converge I am getting below error
FATAL: NoMethodError: undefined method `nssm' for cookbook: (chef-apply cookbook), recipe: (chef-apply recipe) :Chef::Recipe
here is the code
first I have installed the nssm to my local VM using below command in chef
chocolatey_package 'nssm' do
action :install
version '2.24.101.20180116'
end
then wants to install the jmeter service using nssm in chef
nssm 'jmeter_service' do
program 'C:\ProgramData\chocolatey\lib\jmeter\tools\apache-jmeter-5.2.1\bin\jmeter-server.bat'
parameters(
AppDirectory: 'C:\ProgramData\chocolatey\lib\jmeter\tools\apache-jmeter-5.2.1\bin',
AppStdout: 'C:\ProgramData\chocolatey\lib\jmeter\tools\apache-jmeter-5.2.1\bin\output.log',
AppStderr: 'C:\ProgramData\chocolatey\lib\jmeter\tools\apache-jmeter-5.2.1\bin\error.log',
#AppRotateFiles: 1
)
action %i[install start]
end
Thanks in advance

You seem to be using nssm supermarket cookbook, and the failure seems to indicate that you are running chef-apply, that can only run a single recipe.
But since you are trying to use a custom resource from another cookbook, you need to:
Have a cookbook of your own
Have the nssm (and its dependent windows cookbook) locally or on Chef server
Define the dependency for the nssm cookbook in your cookbooks metadata.rb
The chocolatey_package will install the nssm binary, which is different from nssm 'something' do
For an example of local setup:
Download the nssm and windows cookbooks from Supermarket to cookbooks path
Create another simple cookbook (e.g. mycookbook) with your recipe and metadata.rb
Define dependency in metadata.rb:
depends 'nssm', '~> 4.0.1'
Optionally remove the chocolatey_package resource
Run your cookbook with chef-client or chef-zero
chef-client -z -r recipe[mycookbook]

Related

Chef recipe 'include_recipe' takes precedence over other code and resources

I'm attempting to create a Chef cookbook that, for now, is mostly just a wrapper cookbook for another cookbook (the audit cookbook). I'm still learning Chef, but from what I can gather from the About Recipes documentation and the Resources Reference documentation, Chef recipes should execute in the order that they're defined (via Ruby code and/or Chef resources).
In the About Recipes documentation, it mentions that
When a recipe is included, the resources found in that recipe will be
inserted (in the same exact order) at the point where the
include_recipe keyword is located.
In the Resources Reference documentation, they have an apt_update resource that presumably executes before the include_recipe method due to the fact that it's defined earlier in the recipe.
My wrapper cookbook has a single recipe, default.rb, which is literally these two lines:
package 'ruby-dev'
include_recipe 'audit'
However, during a chef-client or chef-solo run I see that the audit::inspec recipe runs before the security::default recipe which causes things to break because InSpec has some other dependencies that need to be installed beforehand. Before I used the package resource I was using the execute resource to explicitly run apt-get install ruby-dev or yum install ruby-dev depending on the platform using a case statement, but same problem (all that code was skipped and the include_recipe method called first).
In case it's useful, I'm using Chef 12 which I realize is EOL but I have other dependencies that require me to stick with this version of Chef for now.
I may very well just be misunderstanding how Chef converges work and the order in which execution occurs, but it's causing me a lot of grief so I'd really appreciate some pointers! Does include_recipe always occur before other code within your recipe? Is there any way around this? Am I missing something?
-- EDIT --
I was able to get the desired functionality (install other packages and gems before an include_recipe call triggered installation of a dependency gem) using the following code in my cookbook recipe:
package 'build-essential' do
action :nothing
end.run_action(:install)
chef_gem 'train' do
version "1.4.4"
action :install
end
chef_gem 'signet' do
version "0.11.0"
action :install
end
include_recipe 'audit'
Note that I ended up installing the build-essential package rather than the ruby-dev package from my original code snippet, and also installed two gems for Chef client to use. This all gets installed in the order I expected during the compile phase of the Chef run.
Sources:
https://docs.chef.io/resource_reference.html#run-in-compile-phase
https://docs.chef.io/resource_chef_gem.html
if you would examine the audit::inspec rescipe, you will find that it uses a compile time installation of the inspec rubygem (see the last line)
inspec_gem 'inspec' do
version node['audit']['inspec_version']
source node['audit']['inspec_gem_source']
action :nothing
end.run_action(:install)
from chef documentation:
run_action
Use .run_action(:some_action) at the end of a resource block to run the specified action during the compile phase.

How to use a class from a module immediately after installing it with Puppet?

I am experimenting with Puppet using Vagrant. I'm new to Puppet.
I'm installing modules in my Puppet manifest using the approach suggested at: Can I install puppet modules through puppet manifest?
My default.pp contains something like:
$dsesterojava = 'dsestero-java'
exec { 'dsestero-java':
command => "puppet module install ${dsesterojava}",
unless => "puppet module list | grep ${dsesterojava}",
path => ['/usr/bin', '/bin']
}
include java::java_7
I'm trying to import a module and then immediately use the classes defined in it.
Currently, I get:
Error: Could not find class java::java_7
If I comment out the include line and re-run it. The module installs. If I then removed the comment and run the provisioning again then it works.
There is some kind of "chicken and egg" situation here. Can I use a module in the same Puppet manifest that installs it?
How should I solve it?
No, you cannot do this. When your catalog is compiled, Puppet will search in the appropriate directories for all of the required code and data. Since the java module does not exist until catalog application, the compilation of a catalog (occurs prior to application) depending upon it will fail. You are absolutely dealing with a "chicken and egg" situation here. I highly recommend against using Puppet code to install Puppet code.
Alternatively, the recommended approach to install and manage your Puppet modules is to use one of these solutions:
librarian-puppet: http://librarian-puppet.com/
r10k: https://github.com/puppetlabs/r10k
code-manager (PE only): https://puppet.com/docs/pe/2017.3/code_management/code_mgr.html
These will also solve the problem for you within the Vagrant if you are using the agent provisioner and subscribing the Vagrant instance to a Puppet Master.
If you are using the apply provisioner inside of Vagrant, then you will need to go a different route. The simplest solution is to use the shell provisioner to install Puppet modules via module install after the Puppet installation (unless you are using a Vagrant box with Puppet baked in, in which case you are probably not installing Puppet on it). Alternatively, you could share a directory with the host where your modules are installed, or install the librarian-puppet or r10k gems onto the Vagrant box and then use them to install into the appropriate path. I can go into more detail on these upon request.

windows_zipfile Chef resource failing due to 'rubyzip' gem file download

We are facing a situation, where end users Windows VM does not have internet connectivity, but only have access to file store.
We are using windows_zipfile resource in one of our recipe. So cookbook execution failed in Windows cookbook, due to the reason that, it is not able to download rubyzip from "rubygems.org" site.
We are thinking of solving the issue in either of these two ways,
Replace the windows_zipfile code with powershell_script and implement the code using Powershell commands
Load the rubyzip gem and its dependency in file store and install the gems before calling windows_zipfile resource.
Please provide suggestions to handle the scenario. Also let me know, is there any other way to solve the issue.
You should be able to install a chef_gem from a local path, after downloading it from a source inside your network (just replace the URL of https://rubygems.org):
{"httpclient" => "2.7.1", "rubyzip" => "1.1.7"}.each do |gem,version|
filename = "#{gem}-#{version}.gem"
remote_file File.join(Chef::Config[:file_cache_path], filename) do
source "https://rubygems.org/downloads/#{filename}"
end
chef_gem gem do
source File.join(Chef::Config[:file_cache_path], filename)
version version
end
end
As the Gem is used by Chef's ruby, make sure to use the chef_gem resource.

how to get started with Chef cookbook?

I have followed below mentioned article to configure apache, mysql, php using chef cookbook.
http://gettingstartedwithchef.com/first-steps-with-chef.html
My purpose was to write a standalone application, which will do all needed setup for mysql, apache, php etc.
By following the article, I observed they are executing the chef commands to install stuffs & getting modifying the configuration files manually.
for example for the following command I wrote respective ruby equivalent:
rvm install ruby-2.1.2
ruby equivalent : `rvm install ruby-2.1.2`
Similar way, I found alternatives for the shell commands & prepared my ruby code.
I am not sure whether that's the right approach.
2nd: how we can do custom functionality, let's say bundling a gemfile inside a project repo or running rails migrations.
How to perform those operations, please guide.
There're many community created cookbooks for many applications.
You can find many cookbooks on chef supermarket.
In general, you can install packet by use your packet manager this way:
For another supported install options you can look to cookbook.
package "#{res_name} :create nginx" do
package_name 'nginx'
action :install
end
If you want install ruby rvm package, you can use gem_package statement:
gem_package 'name' do
clear_sources TrueClass, FalseClass
gem_binary String
notifies # see description
options String
package_name String, Array # defaults to 'name' if not specified
provider Chef::Provider::Package::Rubygems
source String
subscribes # see description
timeout String, Integer
version String, Array
action Symbol # defaults to :install if not specified
end

In a Chef recipe, how do you specify a particular patch level for a Ruby?

I'm working with someone else's chef recipe and it consists of these references to the process of installing 1.9.3p0 on my server:
package 'ruby1.9.3'
package 'ruby1.9.1-dev'
# set ruby 1.9 to be default
execute 'update-alternatives --set ruby /usr/bin/ruby1.9.1'
execute 'update-alternatives --set gem /usr/bin/gem1.9.1'
ohai "reload" do
action :reload
end
I'm new to chef so I'm not sure where these packages reside, but seeing no other reference them to them in the repo of recipes, I'm guessing it's referring to a central repo. In that case, how could I modify this recipe to get chef (solo) to prepare my servers with a different patch level?
The documentation may clear things up a little here:
package tells the chef-client to use one of sixteen different
providers during the chef-client run, where the provider that is used
by chef-client depends on the platform of the machine on which the
chef-client run is taking place
So on Debian-based systems like the one that recipe was written for, Chef will automatically resolve the package resource to an apt_package resource, which will call apt-get to install ruby1.9.3.
Now, given none of the mainstream Linux distros or FreeBSD package up multiple patchlevels of Ruby (and, in some cases, stated patchlevels are not what they seem), you probably don't want to use package to get Ruby. Most likely you'll end up wanting to build it from source using something like the bash resource.
package will still be useful for installing the Ruby prerequisites, which you can use from your vendor's package repository without issue.

Resources