Develop the DevOps way — Part 1

Abdessamad Bayzi
OCP digital factory
6 min readSep 30, 2019

Welcome to this DevOps series where I share about my experience experimenting some technical practices, that aims to ease some tasks when developing, and make development environment more similar to a production one, so the time spent on debugging environment problems may be used in code and infrastructure improvements.

When starting or joining a project that’s been going on for a while, the first thing to do is to setup a development environment, which by the way might be time expensive, especially when no documentation is provided.

In the past, using a WAMP or MAMP stack was a common approach to setup quickly and easily a local development environment all to yourself. However this scenario is dirty, what if you need a specific version of PHP that differs from what MAMP provides? What if you want to use Nginx instead of Apache? Such environment is way far from a production one, and doesn’t promote standardization practices across development teams. For new joiners it’s difficult to know which version to install, since each member is using a different one, and if you’re solo and trying to innovate, It’s better to have a machine where you’ll find all you need already installed. So you can see how using the old approach quickly become problematic.

Vagrant is here

In the DevOps era, using a sandbox for development is a best practice. One of the implementations is using Vagrant, in addition you will use Docker and Ansible.

What’s Vagrant

Quoting Mitchell Hashimoto its creator: “Vagrant is a tool for managing virtual machines”. Vagrant generalizes virtual machine creation across multiple virtualization tools such as VirtualBox or VMware and even provides remote options for AWS, OpenStack or GCE. Vagrant configures and provisions virtual machines, based on configuration file called Vagrantfile. The syntax of this file is the Ruby programming language and as it is a simple text file we can easily commit it to our project VCS repository.

Implementation

First, here’s an overview of what we’ll be implementing:

Sandbox Setup

Step 1: Download and install required tools

First, Install Virtualbox via this link

  • For Windows users, install git bash from here, and make sure that Hyper-V is not enabled. You can turn off the feature by running this Powershell command:

Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All

Depending on your OS, refer to this link to install Vagrant

After installing vagrant, make sure to install vagrant-vbguest. It’s a Vagrant plugin which automatically installs the host’s VirtualBox Guest Additions on the guest system.

vagrant plugin install vagrant-vbguest

All done? Great! You now have more power than you know.

Step 2:

Using your terminal or Git Bash for windows users,

  • create a directory named dev_env
  • change directory with

cd dev_env

  • Create a directory named data in dev_env directory. This is a shared folder between the VM and the host. This folder is where you’ll be creating your projects, so you can access it directly from your host using your preferred IDE.
  • clone the repo inside dev_env

git clone https://github.com/bayzi/sandbox.git

Go to the repo, you’ll find a bunch of files and directories. We’ll get to know the usage of each one of them soon.

Edit the email field in the provisioning/defaults/main.yml file. I suppose you’re using your email to connect to your VCS. If not, leave it empty and you’ll need then to setup git manually after the provisioning.

Finally take a break and let the magic happen by running:

vagrant up

If you are on Windows, then you may get the following error

==> default: Booting VM... 
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "1fe83aa1-2f23-4ddd-ad77-b7e7e00aabe6", "--type", "headless"]

Stderr: VBoxManage.exe: error: Failed to open/create the internal network 'HostInterfaceNetworking-VirtualBox Host-Only
Ethernet Adapter #3' (VERR_INTNET_FLT_IF_NOT_FOUND).
VBoxManage.exe: error: Failed to attach the network LUN (VERR_INTNET_FLT_IF_NOT_FOUND)
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component ConsoleWrap, interface IConsole

Note the Adapter referred here:

VirtualBox Host-Only Ethernet Adapter #3

Open Control Panel >> Network and Sharing Center. Now click on Change Adapter Settings. Right click on the adapter whose Name or the Device Name matches with VirtualBox Host-Only Ethernet Adapter # 3 and click on Properties. Click on the Configure button.

Now click on the Driver tab. Click on Update Driver. Select Browse my computer for drivers. Now choose Let me pick from a list of available drivers on my computer. Select the choice you get and click on Next. Click Close to finish the update. Now go back to your Terminal/Powershell/Command window and repeat the vagrant up command. It should work fine this time.

Once the provisioning is done, you can access your sandbox using:

vagrant ssh

Bonus: If you’re wondering by chance, why the output indicates that my last login was from 10.0.2.2, refer to this link

BOOOM, now you’re officially in a new virtual world, happy coding :)

What happens under the hood?

The provisioned VM uses Centos 7 as base box and store it locally. Since we’re using IaC (Infrastructure as Code), all resource specifications are defined using code. This is what’s the Vagrantfile is for.

In this file, we give the VM a name, we define the amount of memory to use, we fix the IP address of the VM, and we define the path to the shared folder.

The last part is provisioners. Vagrant offers a number of provisioners by default, including one for Shell, Ansible, Chef or Puppet, I’ve used the Shell provisioner so that the developer doesn’t have to use tools that he doesn’t really need on his machine . This provisioner will first update packages in the VM, install Ansible, git, python3, and finally use Ansible to do the rest.

In this blog post, I tried to show how beneficial is to use vagrant for setting a development environment on the fly. In the next post, we’ll take a closer look on Ansible, and how it’s used to install and configure:

  • Git
  • Java
  • Maven
  • Nodejs
  • Docker
  • Kubectl (kubernetes cli)

Got questions? Loved it? Don’t agree? Talk to me in the comments below.

--

--

Abdessamad Bayzi
OCP digital factory

DevOps & Plateform Engineer: Docker/Kubernetes/Openshift, CI-CD, GitLab-CI/Jenkins, Elastic stack, Ansible, Python/Java