
Bash Booster - SCM tool on a clean bash
Professional administrators have been using SCM systems such as Chef, Ansible, SaltStack, etc. for a long time to manage servers . These tools help centrally administer a large fleet of servers. To manage a single server, the labor required to install and configure such a tool often exceeds the gain from its use. In this case, the approach is often used "yes it, I’ll write a script on the bash faster." The approach is quite popular, and therefore I would like to introduce you to the lightweight SCM tool, which requires nothing but the good old bash, and can be quite successfully used to configure one server.
So, Bash Booster is a library that helps you write idempotentbash scripts for setting up servers and deploying applications. It was written under the impression of Chef and for use with Vagrant, although the scope is not limited to this at all. It requires nothing but bash, standard utilities and, in some cases, python (which is also installed on any Linux system out of the box). Those. It is quite suitable to run on a completely bare car without additional training.
Let's look at a living example. I will use Vagrant for demonstration. The source code for the example is on Bitbucket , where all steps are framed 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 Vagantfile in it:
And execute the command:
Vagrant will create and run a virtual machine with a clean system (you may have to wait until it downloads the image). In addition, it will mount the current directory of the host system to the / vagrant point, i.e. we will be able to access the files from our example inside the virtual machine. You can check it out:
Next, download the Bash Booster archive and unpack it in bashbooster-0.3beta (0.3beta is the current version at the time of writing). And also tweak Vagrantfile by specifying a configuration script:
On this, the preparation is completed and you can proceed to the most interesting. Create a provision.sh script:
Now mark the script as executable:
And run the setup of our server:
The following lines will appear in the logs:
What does it mean? In fact, our script ran:
You can go to http: // localhost: 8080 in your browser to see the standard greeting “Welcome to nginx!” Now, if you run the vagrant provision again, the script will work almost instantly, because the bb-apt-install function does nothing if The requested package is already installed.
Let's now create a www directory with the index.html file:
And configure nginx so that it gives files from this directory. To do this, create the nginx-default-site configuration in the conf directory:
And add the configuration synchronization to the provision.sh script:
Now execute the configuration command:
The logs will appear:
By going to http: // localhost: 8080 , you can see the indecently large inscription “Bash Booster Rocks!” From a previously created file instead of the standard nginx greeting.
How it works? The bb-event-on function subscribes the on-nginx-updated function to the nginx-updated event. The bb-file-sync function synchronizes the local copy of the nginx configuration with its current version. If there have been changes, then this function raises the nginx-updated event, according to which its handler will restart nginx. Try the vagrant provision again and it will work without rebooting nginx. If you make changes to nginx-default-site, then nginx will reboot. Thus, we got a compact idempotent script that does exactly what it needs and no more.
Of course, not all the features of Bash Booster are described here, but for the first acquaintance it is quite enough. Full documentation is available at www.bashbooster.net .
So, Bash Booster is a library that helps you write idempotentbash scripts for setting up servers and deploying applications. It was written under the impression of Chef and for use with Vagrant, although the scope is not limited to this at all. It requires nothing but bash, standard utilities and, in some cases, python (which is also installed on any Linux system out of the box). Those. It is quite suitable to run on a completely bare car without additional training.
Let's look at a living example. I will use Vagrant for demonstration. The source code for the example is on Bitbucket , where all steps are framed 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 Vagantfile in it:
# -*- 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 run a virtual machine with a clean system (you may have to wait until it downloads the image). In addition, it will mount the current directory of the host system to the / vagrant point, i.e. we will be able to 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 in bashbooster-0.3beta (0.3beta is the current version at the time of writing). And also tweak Vagrantfile by specifying a 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
On this, the 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 setup 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 ran:
$ apt-get update
$ apt-get install nginx
You can go to http: // localhost: 8080 in your browser to see the standard greeting “Welcome to nginx!” Now, if you run the vagrant provision again, the script will work almost instantly, because the bb-apt-install function does nothing if The requested package is already installed.
Let's now create a www directory with the index.html file:
Bash Booster Rocks!
And configure nginx so that it gives files from this directory. To do this, create the nginx-default-site configuration in the conf directory:
server {
root /vagrant/www;
index index.html;
}
And add the configuration synchronization to the provision.sh script:
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
Now execute the configuration command:
$ vagrant provision
The logs will appear:
* Restarting nginx nginx
...done.
By going to http: // localhost: 8080 , you can see the indecently large inscription “Bash Booster Rocks!” From a previously created file instead of the standard nginx greeting.
How it works? The bb-event-on function subscribes the on-nginx-updated function to the nginx-updated event. The bb-file-sync function synchronizes the local copy of the nginx configuration with its current version. If there have been changes, then this function raises the nginx-updated event, according to which its handler will restart nginx. Try the vagrant provision again and it will work without rebooting nginx. If you make changes to nginx-default-site, then nginx will reboot. Thus, we got a compact idempotent script that does exactly what it needs and no more.
Of course, not all the features of Bash Booster are described here, but for the first acquaintance it is quite enough. Full documentation is available at www.bashbooster.net .