im体育赛事客户端

Running the World with Fabric app

Fabric is a library and command-line application implemented in Python, designed to automate systems management tasks and

Fabric is a library and command-line application implemented in Python , designed to automate systems management tasks and deployment of applications. In addition to performing these tasks locally, Fabric also allows them to be carried out remotely, via SSH.

Fabric app helps the user complete a variety of tasks, which can be condensed to these three:

  • Execute remote commands through ssh
  • Download and upload files to and from remote machines
  • Ask the user data

Cases of Use

  • Systematize the ´deploy´ process in an application (it may also not be a Python application)
  • Automate common systems management tasks:
    • installing applications
    • configuration changes
    • creating users
    • etc , etc , etc.

Installation

If we have pip installed on our system, we can install Fabric running:

												pip install fabric
											

Another option is to use the package administrator that corresponds to our system. For example in ubuntu:

												sudo apt-get install fabric
											

Note: In some systems the package ´fabric´ can appear with the name python -fabric

Components

The two main components of Fabric are the fabfile.py and the utility of the line of command fab .

The fabfile.py file is a Python module that contains the definition of the tasks that we will run. The fab command will be used to perform these tasks. For example, a simple ¨hello world¨

												def greeting(name="World"): print "Hello {name}!".format(name=name)
											

We can do this as follows:

												$ fab greeting: name=Santiago Hello Santiago! Done.
											

In this case we are running the task greeting passing in the parameter name the value Santiago.

Remote Command Execution

What makes Fabric app so powerful is its excellent integration with SSH, which allows us to execute remote commands simply and clearly.

Fabric provides us with a set of basic operations described below for this. To test these transactions start a server configuration using Vagrant that is enclosed.

run :
Is used to run a remote command. In the following example we can see how information from one system
From fabric.api from import run, task

												@task def system_info(): current_remote_directory = run("pwd") print "Current remote directory: " + current_remote_directory result = run("uname -a") if result.succeeded: print "Succeeded" else: print "Error conecting to hosts"
											

We can perform this task on a remote server, specifying the ip and user who will run the commands

												fab system_info -H 192.168.0.2 -u vagrant
											

Note : When you run the above command, we will be asked to enter the password of the user vagrant . As shown, the execution of the command allows interaction between the user who launched it and the remote server.

sudo:

Similar to run but can run the command with Super User permission.

												@task def create_app_dir(appname="my_app"): with settings(warn_only=True): result = sudo("mkdir /var/www/" + appname) if result.failed: print "mkdir exit code: " + str(result.return_code) sudo("ls -l /var/www")
											

The above command will create the directory with the name that you specify in the path / var / www /, giving a warning if it already exists and then list the files. As in the previous case can run with :

												Note: If the previous script we used run instead of the command sudo, it would fail to create the directory and do not have permissions to write to / var / www . local: It's used to run a command on the local machine.
											

Note: If the previous script we used run instead of the command sudo, it would fail to create the directory and do not have permissions to write to / var / www .

local:

It’s used to run a command on the local machine.

												@task def compress_current_local_dir(): local('ls -al') with(lcd("../")): local("tar --exclude='.*' -cvf fabric.tar fabric ")
											

In the example we are listing the files in the current directory and later we’re compressing it. The lcd command allows us to move between directories locally.

put:
This command lets you copy a file from your local machine to the remote machine

												@task def upload_compressed_file(): with(lcd("../")): put('fabric.tar', "https://v6i7z7i8.rocketcdn.me/var/www/fabric.tar", use_sudo=True) sudo("ls -al /var/www/ | grep fabric.tar" )
											

In this case, we create a task that allows us to upload the file to compress in the previous step to our remote server.

get:

It’s used to download a file from the remote machine.

												@task def download_apache_logs(logs_folder_name="apache_logs"): if not os.path.exists(logs_folder_name): os.mkdir(logs_folder_name) get(remote_path="https://v6i7z7i8.rocketcdn.me/var/log/apache2/access.log", local_path=logs_folder_name, use_sudo=True) get(remote_path="https://v6i7z7i8.rocketcdn.me/var/log/apache2/error.log", local_path=logs_folder_name, use_sudo=True)
											

This task allows you to download apache logs and save them to a local folder.

Warning
If file with that name already exists in the local folder, the file will be overwritten.

prompt:
Consult the user (the one running the Fabric script), to enter information required by the script

												@task def download_apache_logs(logs_folder_name="apache_logs"): if not os.path.exists(logs_folder_name): os.mkdir(logs_folder_name) get(remote_path="https://v6i7z7i8.rocketcdn.me/var/log/apache2/access.log", local_path=logs_folder_name, use_sudo=True) get(remote_path="https://v6i7z7i8.rocketcdn.me/var/log/apache2/error.log", local_path=logs_folder_name, use_sudo=True)
											

The previous task allows us to enter the name of the folder where you install our application and the port on which it will run.

reboot:
It’s used to restart the remote system

												@task YES_ANSWER = "yes" def reboot_system(): reboot_answer = prompt("You have to reboot your system. Do you want to reboot now?", default=YES_ANSWER) if reboot_answer.tolower() == YES_ANSWER: reboot(wait=30) else: print "Remember to reboot manually"
											

Other Functions and Utilities

Some more things that Fabric provides:
Context Managers:
To define certain contexts in which a command is executed. For example, we can define environment variables, move to a directory, avoid execution errors, etc.
File management :
Provides a set of functions for managing remote files. Among other things it allows:

  • Checking whether a file exists
  • Comment or uncomment part thereof
  • Add information at the end or replace it
  • Upload a file from a template

How we utilize it

Fabric was used in the FOP – ARSAT project to create installation scripts and update our application. Using this library, we developed a script that allowed the customer to have the application installed and configured on his own servers in a matter of minutes with minimal configuration.

Additionally, we also use fabric to define common and recurring tasks within the project, for example:

  • Packaging the application
  • Deploying on continuous integration servers
  • Deploying on Test servers

Useful Links