Idempotent guarantee with deployment tool fabric + cuisine

Introduction

Python's deployment tool fabric makes shell scripts convenient to use in Python, and can easily perform Chef-Solo-like configuration management. In addition, idempotency, which is one of the major features of Chef, can be guaranteed by using cuisine, which is a wrapper for fabric.

It is very simple, the learning cost is low compared to Chef etc., and it can be installed easily because it only needs to be installed on the target machine. This time I wrote a little script to place dotfiles in vagrant using fabric.

Installation

It can be installed with one command below.

$ pip install fabric cuisine

How to use

Specify the host of the target machine in env.hosts, enclose the command in run (), and execute it. If you want to execute it with sudo, enclose it in sudo (). This is the only basic.

sample.py


# -*- encoding:utf-8 -*-

from fabric.api import env
from fabric.decorators import task
from cuisine import run

env.hosts = ['192.168.33.10']

@task
def A():
  run('echo A')

@task
def B():
  sudo('echo B')

@task
def main():
  A()
  B()

If you execute> main, you can see that it can be executed on the target machine as follows.

$ fab main
[192.168.33.10] run: echo A
[192.168.33.10] out: A
[192.168.33.10] run: echo B
[192.168.33.10] out: B

Done.
Disconnecting from 192.168.33.10... done.

Idempotency can be guaranteed by using the _ensure () method such as package_ensure () and the _exisits () method such as dir_exists ().

package_ensure('vim') #Install only if vim is not installed

vagrant integration

To link with vagrant, execute the following command.

$ vagrant plugin install vagrant-fabric 

Then, describe the following settings in the Vagrantfile.

...
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
...
  config.vm.provision :fabric do |fabric|
    fabric.fabfile_path = './fabfile.py'
    fabric.tasks = ['set_env'] #Task you want to perform
  end
end

This time, I wrote a file to place the following dotfile.

fabfile.py


# -*- encoding:utf-8 -*-

from fabric.api import env, settings
from fabric.decorators import task
from cuisine import mode_sudo, select_package, run, package_ensure, dir_exists, cd

select_package('apt')

@task
def set_env():
  setup_packages()
  fetch_dotfiles()
  set_symlinks()

@task
def setup_packages():
  with settings(mode_sudo()):
    package_ensure('unzip')

@task
def fetch_dotfiles():
  if not dir_exists('dotfiles'):
    run('git clone --recursive [email protected]:pika-shi/dotfiles.git')

@task
def set_symlinks():
  path_pair_list = [
    ('dotfiles/_zshrc', '.zshrc'), ('dotfiles/_tmux.conf', '.tmux.conf'),
    ('dotfiles/_vimrc', '.vimrc'), ('dotfiles/_vim', '.vim')
    ]
  with cd('~/'):
    for path_set in path_pair_list:
      run('ln -s {} {}'.format(*path_set))

If you execute the following command with vagrant up, the task will be executed and the appropriate symbolic link will be attached to the dotfile.

$ vagrant provision

in conclusion

In this way, a little configuration management can be done very easily, so please try it and realize its convenience.

Recommended Posts

Idempotent guarantee with deployment tool fabric + cuisine
First Fabric (Python deployment tool)
Giving idempotence to fabric with cuisine
Easy deployment environment with gaffer + fabric
Why we chose Fabric as our deployment tool
Create a deploy script with fabric and cuisine and reuse it
[Linux] Write a deployment tool using rsync with a shell script