• Ruby on Rails
  • 4 MINUTES READ

Using Chef Repository Directory to Set Up Roles for Rails Environment

  • POSTED ON
  • May 12, 2024
  • POSTED BY
  • Aayan Arif
  • POSTED ON May 12, 2024
  • POSTED BY Aayan Arif

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 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:

{
  "id": "deploy",
  "ssh_keys": [""],
  "groups": [ "sysadmin"],
  "shell": "/bin/bash",
  "password": "",
  "system_user": true
}

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:

     

    $: openssl passwd -1 "yourpasswordinplaintext".

    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:

{
  "name": "users",
  "description": "Setting up users",
  "default_attributes": {
    "authorization": {
      "sudo": {
        "groups": ["sysadmin"],
        "users": ["deploy"],
        "passwordless": "false"
      }
    }
  },
  "json_class": "Chef::Role",
  "run_list": [
    "openssl",
    "build-essential",
    "chef-solo-search",
    "users::sysadmins",
    "sudo"
  ],
  "chef_type": "role"
}

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:

{
  "name": "rvm-ruby",
  "description": "installation of rvm and ruby",
  "json_class": "Chef::Role",
  "run_list": ["rvm::system"],
  "chef_type": "role",
  "default_attributes": {
    "default_ruby": "ruby-2.2.1",
    "global_gems": [
      {"name": "bundler"},
      {"name": "rails"},
      {"name": "pg"},
      {"name": "redis"},
      {"name": "redis_object"},
      {"name": "compass"},
	#more required gems goes here		
    ]
  }
}

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:

{
  "name": "database",
  "description": "database installation",
   "json_class": "Chef::Role",
  "run_list": [
    "recipe[postgresql::server]",
    "recipe[postgresql::client]",
    "recipe[redisio::install]",
    "recipe[redisio::enable]"
  ],
  "chef_type": "role",
  "default_attributes": {
    "postgresql": {
      "password": {"postgres": ""},
      "config": {"listen_addresses": "localhost"}
    }
  }
}

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:
    $: openssl passwd -1 "yourpasswordinplaintext"

    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:

{
  "name": "nodejs",
  "description": "node installation",
  "json_class": "Chef::Role",
  "run_list": ["nodejs"],
  "chef_type": "role"
}

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:

{
  "name": "nginx",
  "description": "nginx installation",
  "json_class": "Chef::Role",
  "run_list": [
    "recipe[nginx]",
    "recipe[nginx::authorized_ips]"
  ],
  "chef_type": "role",
  "default_attributes": {
    "nginx" :{
      "default_root": "/home/deploy/apps",
      "authorized_ips": ["127.0.0.1:3000"]
    }
  }
}

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:

{
  "name": "imagemagick",
  "description": "installs imagemagick",
  "json_class": "Chef::Role",
  "run_list": ["recipe[imagemagick]"],
  "chef_type": "role"
}

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:

$: knife solo prepare root@remote_machine_ip

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:

{
  "run_list":
  [
    "role[database]",
    "role[rvm-ruby]",
    "role[nodejs]",
    "role[nginx]",
    "role[users]",
    "role[imagemagick]"
  ]
}
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:

$: knife solo cook root@your_remote_machine_ip

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.

ABOUT THE AUTHOR

Aayan Arif

Content Strategist at vteams - Aayan has over 8 years of experience of working with multiple industries.

0 Comments

Leave a Reply

More Related Article
We provide tips and advice on delivering excellent customer service, engaging your customers, and building a customer-centric business.