94 lines
3.6 KiB
Markdown
94 lines
3.6 KiB
Markdown
When focusing on combining the capabilities of Python and Linux for a specific, fine-tuned purpose, `Fabric` emerges as a highly valuable library. Fabric extends Python’s ability to execute local or remote shell commands, thus automating the use of SSH for application deployment or system administration tasks. It's particularly well-suited for streamlining the deployment process, executing server administration tasks, and carrying out remote commands across a fleet of servers. Here’s a concise reference guide for common use cases with `Fabric`:
|
||
|
||
# `Fabric` Reference Guide
|
||
|
||
## Installation
|
||
```
|
||
pip install fabric
|
||
```
|
||
|
||
## Basic Usage
|
||
|
||
### Running Commands Locally
|
||
Fabric allows you to run shell commands on your local machine using the `local` function.
|
||
```python
|
||
from fabric import task
|
||
|
||
@task
|
||
def local_ls(c):
|
||
c.local('ls -lah')
|
||
```
|
||
This function lists the contents of the current directory in a long listing format.
|
||
|
||
### Executing Commands Remotely
|
||
To run commands on remote systems, you use the `run` method. Fabric handles the SSH connection details.
|
||
```python
|
||
@task
|
||
def remote_uname(c):
|
||
c.run('uname -a')
|
||
```
|
||
This command displays system information about the remote machine.
|
||
|
||
### Connecting to Remote Hosts
|
||
You can specify connection parameters such as the hostname, user, and more directly in your tasks.
|
||
```python
|
||
@task
|
||
def deploy(c):
|
||
with c.connection(host="example.com", user="username") as conn:
|
||
conn.run('git pull')
|
||
conn.run('touch tmp/restart.txt')
|
||
```
|
||
This task demonstrates a simple deployment process on a remote server.
|
||
|
||
## Advanced Usage
|
||
|
||
### Serial and Parallel Execution
|
||
Fabric can execute tasks in serial (one after the other) or in parallel (simultaneously on multiple hosts), which is particularly useful for deploying changes across a server cluster.
|
||
|
||
```python
|
||
from fabric import SerialGroup, ParallelGroup
|
||
|
||
@task
|
||
def deploy(c):
|
||
for conn in SerialGroup('host1', 'host2', 'host3'):
|
||
conn.run('uname -a')
|
||
|
||
@task
|
||
def parallel_deploy(c):
|
||
for conn in ParallelGroup('host1', 'host2', 'host3'):
|
||
conn.run('uname -a')
|
||
```
|
||
|
||
### File Transfers
|
||
Fabric provides simple mechanisms for uploading and downloading files between local and remote locations.
|
||
```python
|
||
@task
|
||
def upload_project(c):
|
||
c.put('my_project.zip', remote='/opt/projects/')
|
||
|
||
@task
|
||
def download_data(c):
|
||
c.get(remote='/var/log/myapp.log', local='logs/')
|
||
```
|
||
|
||
### Context Managers for Changing Directories and Settings
|
||
Fabric’s context managers can temporarily change the working directory or modify environment settings during command execution.
|
||
```python
|
||
from fabric import Connection
|
||
|
||
@task
|
||
def deploy(c):
|
||
with Connection('example.com') as c:
|
||
with c.cd('/path/to/application'):
|
||
c.run('git pull')
|
||
c.run('./restart.sh')
|
||
```
|
||
|
||
## Security Considerations
|
||
- Use SSH keys for authentication to enhance security.
|
||
- Manage sensitive information, such as host addresses and passwords, securely and outside your script when possible.
|
||
|
||
`Fabric` is optimized for simplicity and flexibility, allowing developers and system administrators to automate complex deployment and system administration tasks efficiently. By writing tasks in Python, Fabric users can take advantage of Python’s readability and extensive library ecosystem to create powerful, maintainable scripts for automating their workflows.
|
||
|
||
|
||
Fabric's combination of straightforward syntax and powerful features like serial and parallel execution, file transfers, and the ability to execute both local and remote commands makes it a standout tool for automating server setup, application deployment, and task execution in Linux environments using Python. |