Hello everyone !

Today I’ll share what I have learned something new in world-deployment like how to quickly set up a local development environment. Yess using vagrant, hearing something weird?

What’s vagrant?

Actually, vagrant is tool that makes easy to build and share virtual environment that in normally its take time and effort to configure it one by one. With vagrant makes everything looks like automate by describing the virtual environment setting in the configuration file called Vagrantfile.

So many benefits using vagrant, one the most benefits is we can easily build the same environment in multiple times with same setting in different environment. Other than, we can sharing a Vagrantfile to other people to provide the same virtual environment.

Lets Play

Oke, first of all we need virtual box and I assume you already install the virtual box to simulation production our application. Lets install vagrant with:

sudo apt-get -y install vagrant

And than we need add the vagrant box with the command:

vagrant box add <your main root project directory>/focal64

After that you can running this command in root project directory:

vagrant init ubuntu/focal64

This will generate a Vagrantfile with a VM with image ubuntu 20.04 (Focal Fossa). And now you can vagrant up to create the VM also vagrant ssh if use ssh to access the VM.

You can start the VM with vagrant up to stop just with vagrant halt also you can destroy the VM if you no need longer a VM with vagrant destroy --force

This time to explore Vagrantfile, lets change the ip network with private network:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
  config.vm.network "private_network", ip: "192.168.20.21"
  
  config.vm.provider "virtualbox" do |vb|  
  end
end

After saving the Vagrantfile start the VM with vagrant up. In this case we just setup with ip private with ubuntu server 20.04.

Deployment

In normally, in the manual step of the deployment is:

  1. Install dependencies
  2. Install application
  3. Configure application
  4. Run application

When we run our application directly using main.rb it will run the current user and attached to current terminal. What happen if we want to close terminal? or what happent if we want to restart the applicaton?

So we better using servis manager called systemd

Systemd

Basically, systemd is a service manager that will managed our application like starting application after reboot, restart application etc. In this case, we use Ruby and Sinatra ya. So what we need to use systemd is :

  • Create a new file called sinatra.service inside /etc/systemd/system.
    cd /etc/systemd/system
    sudo nano sinatra.service
  • Create the code and save the previous file to /etc/systemd/system as sinatra.service
    [Unit]
    Description=Sinatra application

    [Service]
    User=root
    WorkingDirectory=/home/vagrant/app
    ExecStart=/usr/bin/ruby web.rb -o 192.168.20.21
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target
  • Reload systemd unit list using
    sudo systemctl daemon-reload
  • Start service using
    sudo systemctl start sinatra.service
  • Stop service using
    sudo systemctl stop sinatral.service
  • To check the status
    sudo systemctl status sinatral.service

That’s it what can i share. And i think Vagrant is a good and simple tool when we are able to quickly set up a local development environment.

Cheers and happy reading! Thank You!