What is Fabric?
- Why We Use Fabric?
- How To Use Fabric?
- Example of how to install WordPress application and Deploy custom developed plugin
What is Fabric?
- Fabric is a Python library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.
- It provides a basic suite of operations
- Executing local or remote shell commands
- Uploading/downloading files
- Prompting the running user for input
- Aborting execution.
- It can be used to automate deployments, run single or multiple commands on multiple servers in parallel or pretty much anything else you can think of that involves remotely logging in to a server, copying files, etc.
- User can execute shell commands over SSH, so you only need to have SSH running on the remote machine. It interacts with the remote machines that you specify as if they were local.
Why We Use Fabric?
- It is a better way to mention script for deployment process of application and no matter who has deployed.
- Most system administrators have “cheat sheets”, that they use to remember how to do a task that is frequently repeated. Most of these tasks involve using SSH to log in to a server and run a series of commands. Fabric allows to convert the repetitive task into code that can be run using SSH. So system administrators doesn’t make any typographical error in commands they are running, logging in to the wrong server accidentally or forgetting to do a critical step.
How To Use Fabric?
To install Fabric using pip, run following command,
Also, using Operating system’s package manager,
- Now, we learn about some of the functions in Fabric. Fabric provides a set of commands in fabric.api module:
- run – Fabric run command is used for executing a shell command on one or more remote hosts.
- sudo – It allows the execution of a given set of commands and arguments with sudo privileges on the remote host.
- local – Fabric local command is used for executing shell commands locally.
- get – The get command exists to download (i.e. pull) file(s) from the remote system to the computer where the Fabric is being used.
- put – When a user needs to upload files, put command can be used very similarly to ‘get’ command.
- prompt – This command asks the user (i.e. one that is running the script) to input a certain data to use during the successive execution.
- reboot – It is used to reboot the remote system. Default waiting time is 2 mins before performing the task/job
- After successful installation, it’s time to write your first Fabric file!
The ‘user’ is the user-name used to login remotely to the servers.
To run ‘hello’ function on remote machine use following command:
Example to run this script on ‘devopstech’ :
[devopstech] Executing task ‘hello’
[devopstech] run: echo “hello from `hostname`”
[devopstech] Login password for ‘user’:
[devopstech] out: hello from devopstech.com
Disconnecting from devopstech… done.
Install WordPress application and Deploy custom developed plugin
WordPress plugins allow users to easily modify, customize and enhance any WordPress website. Many developers create plugins to enhance a custom theme or to modularize their code. While developing a plugin, user always use the WordPress SCM repository (e.g git) to host a plugin code. So after the development of plugin user need to deploy it and test on the local environment.
Let’s install WordPress application and deploy plugin using Fabric
The following Fabric Code Performs accomplish steps:
- Install WordPress on remote machine
- Download user’s custom plugin code from git repo
- Zip the deployable files before deploying the plugin
- Copy zip file to remote server and unzip to WordPress Plugin directory
- Start the service
WordPress installation assumes that user has already setup and installed MySql on another host. Also created user, database grant appropriate permission to user for WordPress application in MySql.
Installation of WordPress application and deployment of plugin are not interdependent to each other. User can execute them independently like only install WordPress (e.g. fab wordpress) or install WordPress with custom plugin deployment ( e.g. fab wordpress_deploy).
Application deployments should also be kept as simple as possible to avoid having too much logic and scope for error built into the deployment scripts.
Fabric can be used for multiple purposes, including application deployment, restarting of servers, stopping and restarting processes. Tasks are written in Python after which Fabric executes the code on remote machines. Fabric is simple, powerful and comes with a really good documentation.