R&D

Using Chef-Repo Directory to Set Up Roles for Rails Environment

In the previous article, setting up the Rails environment using Chef was explained. Picking up from where we left off, we’ll begin this tutorial by setting up roles.

Adding roles is an important step but a bit tricky as well.

Users

This cookbook adds users to the server, granting them appropriate privileges and so on. You will need to use it together with the Sudo cookbook in order to grant sudo privileges to the user. Users should be defined in the databags_ directory. Now, let’s add the first user and set up the first role that will be used later by your server.

User Definition

Firstly, create a users directory in the databags directory. In the users directory, create a deploy.json_ file and open it in your editor. Paste the following lines into that file:

Now, let’s quickly go through the most important options:

  • id: is the name of the user
  • ssh_keys: from here, you can pass an array of strings. Each string should represent one ssh key
  • password: encrypted password
    Note: It is not recommended that you store the password in plain text. Instead of this, go to your console and type the following:
    This command will generate an encrypted password which can safely be inserted into your recipe. Copy the output of the command as given above, and paste it between the quotes in the “passwordoption.
Users Role

Go to the roles directory and create an empty file. Name it as users.json; this is the file where the user’s role will be specified. Each role is defined in the form of a JSON object. Copy and paste the definitions as given below into your users.json file:

You’re done as the first role has been specified. Let’s set up another.

RVM, Ruby, Rails and other Gems

Setting up this role is easy as compared to the previous one. You’ve already added the cookbook for rvm to the Berksfile, and this is enough to install rvm and a ruby version of our choice. Now, you need to create a gemset and install Rails as well as other useful gems.

Once again, go to the roles directory and create a new file. Name it as rvm-ruby.json. Copy and paste the lines given below:

Note: It is also possible to create a gemset by adding “@” after the Ruby version, for example 2.2.1@myrailsapp

Postgresql and Redis

Time to set up the next role, i.e. the database. Go to the roles directory and create a new file. Name it as database.json and then paste the following lines into it:

This role is also pretty easy to understand. There is only one option which you have to change:

  • run_list: This role will use two recipes from the postgresql cookbook; server recipe and client recipe. You will also need two recipes from the redisio cookbook (which installs Redis); install recipe and enable recipe
  • default_attributes: Inside the postgresql object, you will find a password key. You can generate it with the same command you used to generate password for the user role:
    Paste the output between the quotes, next to the password key.

Node.js

To install node.js, go to the roles directory and create a new file. Name it as nodes.json and paste the following lines into it:

NGINX

Similarly when it comes to other roles, start by creating an empty file in the roles directory. Name it as nginx.json and paste the following lines of code into that file:

ImageMagick

The imagemagick role same as the nodejs role, is quite simple. Just go to the roles directory and create a new file. Name it as imagemagick.json and paste the following lines of code into it:

Installation on Remote Machine

After the addition of few cookbooks and a user in a data bag and defining a few roles that will help you install all the things that were needed to start the development work, now its the time to install all of it on the remote machine.

Setting Up Remote Server with Knife Solo Prepare

At first, you will use the knife gem which is not only able to initialize a Chef repository but also prepares the remote server. Go to your console, make sure that you are in the chefrepo_ directory and run the following command:

This will perform a couple of tasks. Firstly, it will log in to your remote server and install Chef. It is smart enough to recognize the remote server OS and adapt the installation process accordingly.

Secondly, it will create a new file namedyourremotemachineip.json in the nodes_ directory of the chef repository on your local machine. This is where we need to specify which roles should be used to notify the server. Open the newly created file and paste the following lines of code into it:

Installing Cookbooks on a Remote Machine with Knife Solo Cook

Now it’s time to run the installation. Go to your console, make sure that you are in the chefrepo_ directory and run the command given below:

That’s it! This will run the scripts and install everything on the server. Once it finishes, try SSH using newly created user “deploy” to log into your server. If everything goes OK, you will successfully log into the server and all the modules will be installed by then.