Executing Cookbooks with Chef-Solo

Chef-Solo is an open source tool that runs locally and allows us to provision the guest machine using chef cookbooks without the complication of any chef client and server configuration. In this post, we will learn to execute a simple recipe which will create a file with some content inside /tmp directory using chef cookbook recipe.

To start with, we will create a directory where all our chef related content resides and for me it’s chef-repository, as follows:

$ mkdir chef-repository

Next, we will configure Knife inside our chef-repository to create cookbooks and recipes, as follows:

$ cd chef-repository
$ mkdir .chef
$ echo "cookbook_path [ '/vagrant/chef-repository/cookbooks' ]" > .chef/knife.rb

/vagrant/chef-repository/cookbooks specified above refers the path to chef cookbooks.

Now, we will create our first cookbook using knife which will also create recipe with name default.rb for us with default content, as follows:

$ knife cookbook create mycookbook

Edit the content of cookbooks/mycookbook/recipes/default.rb to create the file with name default-file with given content inside /tmp directory as below:

file "/tmp/default-file" do
  content "It's file created by Chef Recipe"
  owner "root"
  group "root"
  mode 00600
end

Move to directory ‘cookbooks’ and create chef configuration file solo.rb:

$ echo "cookbook_path ['$PWD']" > solo.rb

Next, run the recipe we created using chef-solo., as follows:

$ chef-solo -c solo.rb -o mycookbook::default;

Running multiple recipes inside cookbook?

We can run multiple recipes using chef-solo –override-runlist and –json-attributes command line options as follows:

a). Using chef-solo –override-runlist

chef-solo -c solo.rb -o recipe[mycookbook::default],mycookbook::first

mycookbook::first refers to the recipe with name first.rb inside directory cookbooks/mycookbook/recipes/

b). Using chef-solo –json-attributes

Create a .json file with any name for me it’s recipes.json with content as:

{
  "run_list": [
    "recipe[mycookbook::default]",
    "recipe[mycookbook::first]"
  ]
}

Then execute below command from directory ‘cookbooks’:

sudo chef-solo -c solo.rb -j recipes.json
Advertisements

Application Server as a Service in Unix

Running the Application Server as a service or daemon on the machine ensures that service restart automatically when the machine reboots and also provides the capability to start the server with a UNIX shell. In this post, we will create Weblogic  server as a service making it to start at machine reboot in few steps as follows:

Step 1: Create a file to configure the init script with name weblogic inside directory /etc/default/ with content, as follows:

# /etc/default/weblogic script used to configure the init script

# Weblogic Server installation home directory
WLSHOME="/opt/weblogic/wlserver_10.3"

# Weblogic Server sample server
SAMPLESERVER="/samples/domains/wl_server"

# Username password for stopping
USER="weblogic"
PASSWORD="welcome1"

Step 2: Create a file with name weblogic inside directory /etc/init.d/ with content, as follows:

#!/bin/sh

. /etc/default/weblogic

start() {
  ${WLSHOME}${SAMPLESERVER}/bin/startWebLogic.sh &
}

stop() {
  ${WLSHOME}${SAMPLESERVER}/bin/stopWebLogic.sh -username ${USER} -password ${PASSWORD} &
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac

 

Step 3: Make weblogic file inside directory /etc/init.d/ as executable

$ cd /etc/init.d/
$ chmod +x weblogic

Step 4: Add the service to the automatic startup system.

$ sudo update-rc.d weblogic defaults