How to setup a Chef Workstation using custom bash script

How to setup a Chef Workstation using custom bash script

Purpose – To prepare a bash script which can be used to setup a workstation in one go with all the required tools for working on the Chef development .

Pre-requisites

  • CentOS 7 machine

Every package or software that we install follows a particular directory structure. Lets say when we install a Apache httpd package after the installation you will see the following common directories getting created like bin,etc,lib,conf. In similar way i am going to prepare a directory structure and put it in github repository which we can later on at any time pull and setup our workstation with ease So, here i will show you the steps required to setup a workstation for chef usage using bash script which i have placed in github repository under a particular directory

Procedure

Step1: Clone the below git repository by logging in as root user on CentOS machine under root user home directory


[root@desktop1 ~]# pwd
/root
[root@desktop1 ~]# git clone https://github.com/novicejava1/stack.git
Cloning into 'stack'...
remote: Counting objects: 50, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 50 (delta 3), reused 50 (delta 3), pack-reused 0
Unpacking objects: 100% (50/50), done.   
[root@desktop1 ~]# tree stack
stack
├── middleware
│   ├── extract
│   │   └── test.txt
│   ├── scripts
│   │   ├── bin
│   │   │   └── chefWorkstationSetup.sh
│   │   ├── etc
│   │   │   └── software_source.txt
│   │   └── logs
│   │       └── log.txt
│   └── software
│       ├── chefdk.rpm
│       ├── docker_rpm.rpm
│       ├── docker_selinux_rpm.rpm
│       └── vagrant.rpm
└── README.md

Once you have cloned the above git repository you will see the below directory structure getting created under /root home directory

/root/stack/middleware - This will be the root directory for all our middleware stuff
/root/stack/middleware/extract - This directory could be used to extract any tar or zipped
files for later use
/root/stack/middleware/software - This will be the directory to store our software packages
/root/stack/middleware/scripts  - This will be the root directory for all our bash scripts 
/root/stack/middleware/scripts/bin - This directory will contain the the main bash scripts
which will source the files from etc and lib directory for usage
/root/stack/middleware/scripts/etc - This directory will contain all the configuration
variables
/root/stack/middleware/scripts/lib - This directory can be used for making reusable
functions which can be source in the bin directory scripts

Step2: Run the below script to setup up the workstation for Chef usage


[root@desktop1 ~]# /root/stack/middleware/scripts/bin/chefWorkstationSetup.sh

The above script is a very basic (I can say a very gross) one to get your workstation
setup with the below required packages. I will just give a brief details about each
function that is carried out in the above script and for what purpose

- First and foremost it is setting up some variables for directories
so we can use them later on in the scripts to reference the them. 
This practice will be very helpful in case you want to change the directory
structure without touching the file 
at every place where that directory is referenced.

HOME_DIR=/root/stack/middleware
SCRIPTS_DIR=$HOME_DIR/scripts
SOFTWARE_DIR=$HOME_DIR/software
EXTRACT_DIR=$HOME_DIR/extract

- Second i am sourcing a file from etc directory which contains packages urls
which are hardcoded and exported as environment variables

### List of Softwares with their RPM location

export chefdk_rpm=https://packages.chef.io/files/stable/chefdk/2.0.28/el/7/
chefdk-2.0.28-1.el7.x86_64.rpm
export vagrant_rpm=https://releases.hashicorp.com/vagrant/1.9.6/
vagrant_1.9.6_x86_64.rpm
export docker_selinux_rpm=https://download.docker.com/linux/centos/7/
x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm
export docker_rpm=https://download.docker.com/linux/centos/7/
x86_64/stable/Packages/docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

- Third is preparing the logs and software directory which i have ignored
in the gitignore file while pushing the repository

- Fourth is a set of functions which validate if a particular software is
installed or not and validate it. 
Here are the below functions that i have implemented which are really very basic

 installChefDk() - This will check if ChefDk is already installed or not and
validate its version
 installVagrant() - This will check if vagrant is already installed or not and
validate its status. 
Vagrant is basically used for managing the Virtual machines
 installGit() - This will check if git is already installed or not and
validate its version. 
We will use git to push our chef code into version control repositories
 installDocker() - This will check if docker is already installed or not and
validate its version. 
We will used docker to download and run containers which will be
carried out by Test kitchen.

Once the script execution is completed your workstation is setup with basic tools for working on chef code and testing them with Test Kitchen.
This article is basically consolidating the individual articles that i have posted previously which are as below

setting-up-workstation-with-chefdk
installing-and-validating-docker-ce
how-to-manage-docker-images

Also the github repository contains a test helloworld cookbook which you can test using Test kichen on a Ubuntu machine. Looks at images details in the .kitchen.yml file within the helloworld directory.
Good Luck with Chef!!

Hope you enjoyed reading this article. Thank you.