Automate Your Linux Deployment Using Azure DevOps
By: Agustinus Theodorus
Tags: Software Engineering, Software Development, Azure, Microsoft, DevOps

Configure your Linux server for automation

Now, this would be the fun part. Microsoft already did all the heavy lifting with the boring installation things. Now it is our turn to automate things.

1. Make a script to automate NGINX configurations

To add new NGINX configurations we will create a shell script, add this add-nginx-conf.sh script on the home directory:

A shell script to add NGINX configurations automatically.

This script will create a search for configuration files named using the associated DNS, it will create a new one if it was not found. Once found, the new configuration routes will be added there. Mind you, the configuration route in this script would be a basic proxy pass, you might want to add more configurations to the script if you want to use it for production.

The shell script accepts three parameters. The DNS, proxy route, and the port being used:

sudo ~/add-nginx-conf.sh <DNS> <proxy route> <app port>

Example:

sudo ~/add-nginx-conf.sh yourdomain.com enrichment_track 14321

In this example, the script will add configurations for the yourdomain.com domain, with a reverse proxy to port 14321 in the enrichment_track.

2. Make a script to automate system service configurations

To add new system service configurations we will create a shell script, add this make-service.sh script on the home directory:

A shell script to automate the making of systemd services

Before you copy and paste the code, notice the <user> and <server environment> tags in the script. Please change it to your corresponding user and server environment beforehand. Server environments in this case would be either Development or Production.

The script will generate new systemd service files that would run .NET Core projects. To generate scripts for other types of applications you would want to change the given template.

The shell script accepts two parameters. The service name, and the project name used:

sudo ~/make-service.sh <service name> <project name>

Example:

sudo ~/make-service.sh CI-Build API-Project

In this example, the script will add a new system service file CI-Build.service, that runs a dll file named API-Project.dll.

3. Setup a user to run systemctl and Nginx commands without password prompts

For automation purposes, the user that you used to install your Azure Agent needs to be able to run systemctl and Nginx commands without a password prompt. To remove the password prompts on sudo run:

sudo visudo

We need to run all the commands in the script using sudo without having a password being prompted. So, we make exceptions for those commands, and the corresponding scripts we are going to use.

Then add this on the last line (change the <user> tag beforehand):

<user> ALL=(ALL) NOPASSWD:/usr/bin/systemctl, /usr/sbin/nginx, /usr/bin/chcon, /usr/bin/chown, /usr/bin/echo,

/usr/bin/touch, /usr/bin/sed, /home/<user>/make-service.sh, /home/<user>/add-nginx-conf.sh

Exit and save the config file, then try running one of these commands using sudo. You should not be prompted for a password.

A full explanation on how to run a sudo command without a password can be seen here.