📜 ⬆️ ⬇️

Bash Booster - SCM tool on a clean bash

For management of servers, professional administrators have long used such SCM systems as Chef, Ansible, SaltStack, etc. These tools help centrally administer a large fleet of servers. To manage a single server, the effort to install and configure such a tool often exceeds the gain from its use. In this case, the “yes to him” approach is often used; I will write a script on a bash faster ”. The approach is quite popular, so I would like to introduce you to the lightweight SCM tool, which does not require anything other than the good old bash, and can quite successfully be used to configure one server.

So, Bash Booster is a library that helps you write idempotent bash scripts for setting up servers and scanning applications. It was written under the impression of Chef and for use with Vagrant, although the scope is not limited to this. It requires nothing but a bash, standard utilities and, in some cases, python (which is also installed on any Linux system out of the box). Those. quite suitable for running on a completely naked car without additional training.

Let's look at a live example. I will use Vagrant for the demonstration. The example sources are on Bitbucket , where all steps are designed as separate commits.

So, suppose we have a server with Linux Ubuntu 14.04, on which you need to install nginx and configure it.
')
Create an empty directory, and in it Vagantfile:

# -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "ubuntu/trusty32" config.vm.network :forwarded_port, host: 8080, guest: 80 end 


And execute the command:

 $ vagrant up 


Vagrant will create and start a virtual machine with a clean system (you may have to wait while it downloads the image). In addition, it will mount the current directory of the host system to the / vagrant point, i.e. we can access the files from our example inside the virtual machine. You can check it out:

 $ vagrant ssh $ ls /vagrant Vagrantfile $ exit 


Next, download the Bash Booster archive and unpack it into bashbooster-0.3beta (0.3beta is the current version at the time of this writing). We will also correct the Vagrantfile by specifying the configuration script:

 # -*- mode: ruby -*- # vi: set ft=ruby : VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "ubuntu/trusty32" config.vm.provision :shell, inline: "/vagrant/provision.sh" config.vm.network :forwarded_port, host: 8080, guest: 80 end 


This preparation is completed and you can proceed to the most interesting. Create a provision.sh script:

 #!/usr/bin/env bash #   CDPATH,     #    cd unset CDPATH #      ,    cd "$( dirname "${BASH_SOURCE[0]}" )" ## #  Bash Booster ## BB_LOG_USE_COLOR=true #     source bashbooster-0.3beta/bashbooster.sh #  nginx bb-apt-install nginx 


Now mark the script as executable:

 $ chmod a+x provision.sh 


And run the configuration of our server:

 $ vagrant provision 


The following lines will appear in the logs:

 bb-apt [INFO] Updating apt cache bb-apt [INFO] Installing package 'nginx' 


What does it mean? In fact, our script executed:

 $ apt-get update $ apt-get install nginx 


You can go to http: // localhost: 8080 in the browser to see the standard welcome message “Welcome to nginx!” Now, if you run vagrant provision again, the script will work almost instantly, because the bb-apt-install function does nothing, The requested package is already installed.

Let's now create the www directory with the index.html file:

 <h1>Bash Booster Rocks!</h1> 


And configure nginx, that he would give the files from this directory. To do this, create a nginx-default-site configuration in the conf directory:

 server { root /vagrant/www; index index.html; } 


And we will add configuration synchronization to script provision.sh:

 bb-event-on "nginx-updated" "on-nginx-updated" on-nginx-updated() { service nginx restart } bb-sync-file \ /etc/nginx/sites-available/default \ conf/nginx-default-site \ nginx-updated 


And now we execute the configuration command:

 $ vagrant provision 


The logs will appear:

  * Restarting nginx nginx ...done. 


Turning to http: // localhost: 8080 , instead of the standard nginx greeting, you can see the indecently large “Bash Booster Rocks!” Inscription from the previously created file.

How it works? The bb-event-on function signs the on-nginx-updated function on the nginx-updated event. The bb-file-sync function synchronizes the local copy of the nginx configuration with its current version. If there were changes, then this function raises the nginx-updated event, according to which its handler will restart nginx. Try to perform vagrant provision again, and it will work without rebooting nginx. If you make changes to nginx-default-site, then nginx will be rebooted. Thus, we have a compact idempotent script that does exactly what you need and no more.

Of course, not all features of Bash Booster are described here, but for the first acquaintance it is quite enough. Full documentation is available at www.bashbooster.net .

Source: https://habr.com/ru/post/253209/


All Articles