Making coder’s life easier to setup environments

We’ve all experimented to setup your new laptop when you move to a new company. Depending on the projects, but it could be very painful (and very long as well). You lost maybe 2, 3 days or maybe 1 week just to make everything working correctly. And during this time, you are not very productive for the company.

The solution should be to automatized everything. Just running a script and install all the software you need, configuring stuff, and running your webserver? Would be great isn’t it? Well, linux has the solution for a very long time: make in your terminal.

Use Makefile to make your life easier (and faster)

The idea is quite clear, just like a recipe to cook a cake, you just have to create your own recipe that your laptop/computer will follow to setup things very quickly. And it’s very easy.

First step : create your file

We need to create a single file : touch Makefile .

Second step: create your recipe

Just like Dockerfile for docker, you will put all the steps to setup actions in your Makefile . It’s not very complicated, just have to follow some basic rules. Makefile has a very strict syntax. You just have to follow them carefully:

name_for_my_action:
my_ssh_cmd; #be carefull, tabulation, not spaces
#my second action needs to run the first action before
name_second_action: name_for_my_action
my_ssh_cmd2 \
&& my_ssh_cmd_other;

Well, this is the main format of a Makefile : create simple actions, adding dependencies after : and just after, adding the recipes (ssh commands) to execute. Be carful, you need to begin each line of the recipe by a tabulation (and not spaces).

let’s try an example :

Some explanations. Just like any languages, you can use variables (the first line). you can access to variables just using : $(NAME_VAR) in the script. You can as well use variables passed by command lines. the Syntax is exactly the same, and we will see later what is the command line in that case.

So that simple Makefile will help me to run my local server (I’m using docker-compose) and will create vhosts automatically.

Third step : use the commandline.

In your terminal, just go the directory where you created the Makefile and just type:

make runserver

Well, if you have docker-compose installed and your docker-compose.yml file in the same directory, your server will run automatically.

Now, let’s create a simple vhost in our laptop. My directories are like this (for that example) :

-rw-rw-r--  1 raphael raphael   1195 mai    6 17:09 Makefile
drwxrwxr-x 10 999 docker 4096 mai 6 17:09 mysql/
drwxrwxr-x 4 raphael raphael 4096 févr. 11 13:01 php/
drwxrwxr-x 3 raphael raphael 4096 févr. 11 12:39 php71/
-rwxrwxr-x 1 raphael raphael 522370 févr. 12 20:41 phpcs.phar*
-rw-rw-r-- 1 raphael raphael 234 déc. 5 22:14 php.ini
-rw-rw-r-- 1 raphael raphael 1897 nov. 24 19:30 README.md
drwxrwxr-x 2 raphael raphael 4096 mai 6 17:09 sites/
drwxrwxr-x 20 raphael raphael 4096 mai 6 15:45 sources/

So, just have to run that command line :

make PROJECT_ID=test2 PROJECT_DIR=myprojectdir vhost

And automatically, the output should be :

cd ./apache-ssl/ \
&& openssl \
req \
-nodes \
-newkey rsa:2048 \
-keyout test2.dev.key \
-out test2.dev.csr \
-subj "/C=DE/ST=NRW/L=Berlin/O=Raphael/OU=DevOps/CN=test2.dev/emailAddress=dev@dev";
Generating a 2048 bit RSA private key
...........................+++
.........................................................+++
writing new private key to 'test2.dev.key'
-----
sudo sh -c "echo '::1 test2.dev www.test2.dev' >> /etc/hosts" \
&& sudo sh -c "echo '127.0.0.1 test2.dev www.test2.dev' >> /etc/hosts";
cd ./sites \
&& cp site.conf.example test2.dev.conf \
&& sed -i 's/FQDN/test2.dev/' test2.dev.conf \
&& sed -i 's/DIR/magic/' test2.dev.conf;
docker-compose restart;
Restarting php7_db_1
Restarting php7_elasticsearch_1
Restarting php7_maildev_1
Restarting php7_web_1

And all the entries are set correctly in your /etc/hosts file and the configuration files set correctly, and certificates generated. And your server ready because it’s already restarted!

Infinite use cases

This is a very simple case. And I’m using it locally just to make easy some actions I have to do very often (yeah I’m lazy).

But why don’t use that file to install all your software you need, clone all the projects you need on your laptop, and make your server runs with only one command line. I’m trying to work on that kind of Makefile . I have to admit, it’s quite long to create the first time. But it would save a lot of time if you need to reinstall a new laptop from scratch (for new colleagues for example).

My draft to install my softwares I need :

DOCKER_COMPOSE_VERSION = 1.12.0

all: install setup run

# Install all necessary software to make running properly all projects
install: install_basics install_ngrok install_docker install_docker_compose

install_basics:
sudo apt-get update && sudo apt-get install -y \
git \
curl \
ca-certificates \
apt-transport-https \
software-properties-common \
openssl \
unzip;

install_docker: install_basics
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -;
sudo echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" | sudo tee -a /etc/apt/sources.list.d/docker.list;
sudo apt-get update && sudo apt-cache policy docker-ce;
sudo apt-get -y install docker-ce;
usermod -aG docker $USER;
docker run hello-world;

install_docker_compose: install_docker
curl -L https://github.com/docker/compose/releases/download/$(DOCKER_COMPOSE_VERSION)/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose;
sudo chmod +x /usr/local/bin/docker-compose;

install_ngrok: install_basics
curl -o ngrok-stable-linux-amd64.zip https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip \
&& unzip ngrok-stable-linux-amd64.zip \
&& mv ngrok /usr/local/bin/ngrok \
&& rm -rf ngrok-stable-linux-amd64.zip;

In that case, just running make install will install all I need from scratch. I prepare a command make all which will install, setup all my projects (clone, create vhosts, get dependencies) and run my docker environment.

With that, you can save hours to setup your environment or the future employee’s laptop, and just focus on your projects.