If you search for the keyword “Fabric “, you will find that 90% of the information is outdated because Fabric currently supports Python3, but it is not compatible with older versions of Fabric. So, if you follow those tutorials, it won’t work. This article is for those who use Python3.
If you haven’t used Fabric yet, this article is a quick way to get started with Fabric, whether you use it now or later.
Our usual development process is this: after a few months of hard work, the project is finished, testing is good, and we submit the code to a hosted platform like GitHub, ready to deploy to a formal environment. You carefully log into the official server, go into the project directory, pull the code from the remote repository, and start the program. Every time a new feature is released or even a small Bug is fixed, you have to do the same thing, log in to the server, switch to the specified directory, pull the code, and restart the service.
This is cumbersome, technical, and prone to problems, and Fabric is where it comes in. Fabric is a remote deployment artifact that can execute remote server commands locally.
How to do? It’s easy. Just a few steps.
Install the Fabric
$ pip install fabric --upgrade
Copy the code
Note that if you have installed an older version of Fabric, the new version of Fabric is not compatible with the older version. There are currently three versions of Fabric. Fabric1 is the previous Fabric, which only supports Python2 and is no longer recommended. Fabric2 is the current version of Fabric and supports both Python2 and Python3. It is also an official and highly recommended version. There is also Fabric3, which is an unofficial version of Fabric that is cloned from the previous version, but is compatible with Fabric1. Python2 and Python3 are also supported.
The latest fabrics do not require fabfile.py, nor do they require fab commands, and almost all of the tutorials and materials on the web are still written in Fabric1. Be careful when you are looking at those tutorials. The new Fabric API is very simple.
Run the command
For an example, here is a deployment script
# deploy.py
Create a remote connection
# 2. Go to the specified directory
# 3. Run the restart command in the specified directory
from fabric import Connection
def main(a):
# IP
If your computer is configured with SSH password-free login, you do not need connect_kwargs to specify the password.
c = Connection("[email protected]", connect_kwargs={"password": "youpassword"})
with c.cd('/var/www/youproject'):
c.run("git pull origin master")
c.run("/usr/bin/supervisorctl -c .. /supervisor/supervisord.conf restart youproject")
if __name__ == '__main__':
main()
Copy the code
perform
python deploy.py
Copy the code
After the completion of the execution, the latest code has been deployed to the official environment and restart the service, is not very convenient, my mother never worry about me in the official environment to type the wrong command delete database run.
Fabric supports Not only Linux, but also Windows. It is a great operation tool for small to medium sized projects. With Frabic, you can manage hundreds of servers.
Build connection
class Connection(Context):
host = None
user = None
port = None
ssh_config = None
connect_timeout = None
connect_kwargs = None.Copy the code
There are different ways to build a Connection object. For example, you can write host as “[email protected]:22” or as three separate arguments. Connect_kwargs is a dictionary object, usually filled with the server login password or key.
Upload a file
The run method is used to run a command. The CD method is used to go to a specified directory. The put method is used to upload a file.
from fabric import Connection
c = Connection('web1')
c.put('myfiles.tgz'.'/opt/mydata')
c.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')
Copy the code
Multiple servers
If you want to run a command on multiple servers, an easy way to do this is to use iterations and run the command on each server:
# web1,web2,mac1 are all server names, you can also use IP instead
>>> from fabric import Connection
>>> for host in ('web1'.'web2'.'mac1') :>>> result = Connection(host).run('uname -s')
. print("{}: {}".format(host, result.stdout.strip()))
...
web1: Linux
web2: Linux
mac1: Darwin
Copy the code
Or use SerialGroup
from fabric import SerialGroup as Group
pool = Group('web1'.'web2'.'web3', connect_kwargs={"password": "youpassword"} )
pool.put('myfiles.tgz'.'/opt/mydata')
pool.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')
Copy the code
Group(*hosts, **kwargs) Parameter description:
- *hosts: Multiple host names or IP addresses can be passed in
- ** Kwargs can receive the same parameters as Connection, which can specify a password
Did you get it? For more articles, follow the official account “Zen of Python”
For more information, please click on the official document: www.fabfile.org/
First published in this article: foofish.net/fabric.html