Detailed explanation of remote deployment tool Fabric (supporting Python3)

  • 2021-07-09 08:51:06
  • OfStack

Preface

If you search the keyword "Fabric" once, you will find that 90% of the data is outdated, because Fabric now supports Python3, but it is not compatible with the old Fabric. Therefore, if you follow those tutorials, you won't run at all.

If you haven't used Fabric yet, this article is to help you get started with Fabric quickly. Whether you use it now or not, you can use it after you know it first.

Usually, our development process is like this. After several months of hard work, the project was finally developed and the test was no problem. We submitted the code to the hosting platform like GitHub and prepared to deploy it to the formal environment. You carefully log in to the official server, enter the project directory, pull the code down from the remote repository, and start the program. Every time a new feature is released or even if only a small Bug is modified, you have to perform repeated operations, log in to the server, switch to the specified directory, pull the code and restart the service.

In fact, this kind of operation is very cumbersome, has no technical content, and is prone to problems, so Fabric appeared. Fabric is a remote deployment artifact that executes commands from a remote server locally.

How? It's very simple, just a few steps.

Installing Fabric


$ pip install fabric --upgrade

Note that if you install the old version of Fabric, the new version of Fabric is incompatible with the old version. At present, there are three versions of Fabric, Fabric1 is the previous Fabric, which only supports Python2 and is no longer recommended, while Fabric2 is the current Fabric, which supports Python2 and Python3, which is also officially strongly recommended. There is also one Fabric3, which is an unofficial version cloned by netizens from the old version of Fabric1, but it is compatible with Fabric1 and also supports Python2 and Python3.

The latest Fabric does not need fabfile. py file, also does not need fab command, and now on the network almost all tutorials, information is based on fabric1 written, when you look at those tutorials, pay attention to screening. The API provided by the new Fabric is very simple.

Run command

Let's look at an example first. Here is a deployment script


# deploy.py
# 1.  Create 1 Remote connections 
# 2.  Enter the specified directory 
# 3.  Execute the restart command under the specified directory 

from fabric import Connection

def main():
  # ip  I filled it out casually 
  #  If your computer is equipped with ssh Password-free login, you don't need  connect_kwargs  To specify the password. 
  c = Connection("root@232.231.231.22", 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()

Execute


python deploy.py

After the execution is completed, the latest code has been deployed to the formal environment and restarted the service. Is it very convenient? Mom should no longer worry that I will knock the wrong command to delete the database and run away in the formal environment.

Fabric not only supports Linux, but also runs well on Windows platform. In small and medium-sized projects, it is a very good operation and maintenance tool. With Frabic, it is not a problem to manage hundreds of servers.

Build a connection


class Connection(Context):
  host = None
  user = None
  port = None
  ssh_config = None
  connect_timeout = None
  connect_kwargs = None
  ...

There are different ways to build an Connection object. For example, you can write host as "root @ 192.168. 101.1: 22", or you can write it separately as three parameters. connect_kwargs is a dictionary object, which usually fills in the login password or key of the server.

Upload a file
The run method is used to execute commands, the cd method is used to enter the specified directory, and the put method is used to upload files, such as:


from fabric import Connection
c = Connection('web1')
c.put('myfiles.tgz', '/opt/mydata')
c.run('tar -C /opt/mydata -xzvf /opt/mydata/myfiles.tgz')

Multiple servers

If you want to run commands on multiple servers, the easy way is to use iterations to execute commands on a server-by-server basis:


# web1,web2,mac1  They are all the names of the servers. You can also use ip Substitute 
>>> 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

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')

Group (* hosts, **kwargs) Parameter Description:

* hosts: Multiple host names or IP can be passed in **kwargs can receive parameters like Connection1 and can specify a password

After this article, have you get?


Related articles: