Installing a Python package remotely

While dealing with the remote host in the previous recipes, you may have noticed that we need to do a lot of stuff related to the connection setup. For efficient execution, it is desirable that they become abstract and only the relevant high-level part is exposed to the programmers. It is cumbersome and slow to always explicitly set up connections to execute commands remotely.

Fabric (http://fabfile.org/), a third-party Python module, solves this problem. It only exposes as many APIs as can be used to efficiently interact with remote machines.

In this recipe, a simple example of using Fabric will be shown.

Getting ready

We need Fabric to be installed first. You can install Fabric using the Python packing tools, pip or easy_install, as shown in the following command. Fabric relies on the paramiko module, which will be installed automatically.

$ pip install fabric

Here, we will connect the remote host using the SSH protocol. So, it's necessary to run the SSH server on the remote end. If you like to test with your local machine (pretending to access as a remote machine), you may install the openssh server package locally. On a Debian/Ubuntu machine, this can be done with the package manager, apt-get, as shown in the following command:

$ sudo apt-get install openssh-server

How to do it...

Here's the code for installing a Python package using Fabric.

Listing 7.4 gives the code for installing a Python package remotely as follows:

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 7
# This program is optimized for Python 2.7.
# It may run on any other version with/without modifications.
 
from getpass import getpass
from fabric.api import settings, run, env, prompt
 
def remote_server():
  env.hosts = ['127.0.0.1']
  env.user = prompt('Enter user name: ')
  env.password = getpass('Enter password: ')
  
def install_package():
  run("pip install yolk")

Fabric scripts are run in a different way as compared to the normal Python scripts. All functions using the fabric library must be referred to a Python script called fabfile.py. There's no traditional __main__ directive in this script. Instead, you can define your method using the Fabric APIs and execute these methods using the command-line tool, fab. So, instead of calling python <script>.py, you can run a Fabric script, which is defined in a fabfile.py script and located under the current directory, by calling fab one_function_name another_function_name.

So, let's create a fabfile.py script as shown in the following command. For the sake of simplicity, you can create a file shortcut or link from any file to a fabfile.py script. First, delete any previously created fabfile.py file and create a shortcut to fabfile:

$ rm -rf fabfile.py
$ ln -s 7_4_install_python_package_remotely.py fabfile.py

If you call the fabfile now, it will produce the following output after installing the Python package, yolk, remotely as follows:

$ ln -sfn 7_4_install_python_package_remotely.py fabfile.py
$ fab remote_server install_package
Enter user name: faruq
Enter password:
[127.0.0.1] Executing task 'install_package'
[127.0.0.1] run: pip install yolk
[127.0.0.1] out: Downloading/unpacking yolk
[127.0.0.1] out:   Downloading yolk-0.4.3.tar.gz (86kB): 
[127.0.0.1] out:   Downloading yolk-0.4.3.tar.gz (86kB): 100%  86kB
[127.0.0.1] out:   Downloading yolk-0.4.3.tar.gz (86kB):           
[127.0.0.1] out:   Downloading yolk-0.4.3.tar.gz (86kB): 86kB 
downloaded
[127.0.0.1] out:   Running setup.py egg_info for package yolk
[127.0.0.1] out:     Installing yolk script to /home/faruq/env/bin
[127.0.0.1] out: Successfully installed yolk
[127.0.0.1] out: Cleaning up...
[127.0.0.1] out: 
 
Done.
Disconnecting from 127.0.0.1... done.

How it works...

This recipe demonstrates how a system administration task can be done remotely using a Python script. There are two functions present in this script. The remote_server() function sets up the Fabric env environment variables, for example, the hostname, user, password, and so on.

The other function, install_package(), calls the run() function. This takes the commands that you usually type in the command line. In this case, the command is pip install yolk. This installs the Python package, yolk, with pip. As compared to the previously described recipes, this method of running a remote command using Fabric is easier and more efficient.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.8.153