edesz
edesz

Reputation: 12406

Python Fabric deploy script structure and semantics

I am trying to write a basic Python Fabric deploy script and I have some beginner level questions.

Here is the fabfile.py I have at the moment (modified from this example):

#!/usr/bin/env python

from fabric.api import *
import socket
import paramiko

env.roledefs = {'dev':['server.domain.tld']}

@task
@roles("dev")
def print_contents():
    echo("Printing working directory contents:\n")
    run("ls")

@task
@roles("dev")
def update_upgrade():
    """ Update the default OS installation's basic default tools. """
    sudo("sudo apt-get update")
    sudo("apt-get -y upgrade")

@task
@roles("dev")
def install_memcached():
    """ Download and install memcached. """
    sudo("apt-get install -y memcached")

@task
@roles("dev")
def deploy():
    print_contents()
    # Update
    update_upgrade()
    # Install
    install_memcached()

Questions:

  1. Do I need import socket and import paramiko?
  2. I do not plan to call functions print_contents(), update_upgrade() and install_memcached() directly - they will only be called within the deploy() function. Does every function need to be a task with the @task decorator?
  3. Since this is a Fabric deploy script, does the final task need to be called deploy()? Or can we use any other name for that function, eg. update_all()?

Upvotes: 1

Views: 87

Answers (1)

YellowShark
YellowShark

Reputation: 2269

  1. You do not need to import socket and paramiko for this script, since you are not calling them directly from your fabfile.
  2. The @task decorator is only used for functions that you would like to be available as fabric tasks on the command line. In the example you've provided, you would only need to decorate the deploy function.

When this decorator is used, it signals to Fabric that only functions wrapped in the decorator are to be loaded up as valid tasks.

  1. You can use whatever name you like! Fabric will use the name of your function as the task name, unless you use the alias argument.

More info on all of this can be found in the docs for the task decorator.

Upvotes: 1

Related Questions