Fabric

De Linuxmemo.

http://docs.fabfile.org/en/1.4.2/index.html

Sommaire

Installation

apt-get install fabric

ou bien

apt-get install python-pip
pip install fabric

Commande "fab"

fab -H localhost,linuxbox fonction
fab -R role fonction
fab -H localhost -f fabfile.py fonction
fab -H localhost -f fabfile.py -l

Fichier de commande: "fabfile"

Variables d’environnement

   abort_on_prompts
   all_hosts
   always_use_pty
   combine_stderr
   command
   command_prefixes
   connection_attempts
   cwd
   disable_known_hosts
   exclude_hosts
   fabfile
   host_string
   forward_agent
   host
   hosts
   keepalive
   key_filename
   linewise
   local_user
   no_agent
   no_keys
   parallel
   password
   passwords
   path
   pool_size
   port
   real_fabfile
   rcfile
   reject_unknown_hosts
   roledefs
   roles
   shell
   skip_bad_hosts
   ssh_config_path
   sudo_prefix
   sudo_prompt
   timeout
   use_shell
   use_ssh_config
   user
   version
   warn_only

Modèle d’exécution

Exécution en série

Exécution en parallèle

Importer des modules de "fabric.api"

les modules: abort, cd, env, get, hide, hosts, local, prompt, put, require, roles, run, runs_once, settings, show, sudo, warn

import urllib

ou

from fabric.api import env run

ou

from fabric.api import *

Définir une lite de machines

Globale:

env.hosts = ['host1', 'host2']

Roles:

from fabric.api import env
env.roledefs = {
    'web': ['www1', 'www2', 'www3'],
    'dns': ['ns1', 'ns2']
}

Via la ligne de commande "fab"

 fab -H host1,host2 mytask
 fab -R roles

Ordre de préférence:

-Per-task, command-line host lists (fab mytask:host=host1) override absolutely everything else.
-Per-task, decorator-specified host lists (@hosts('host1')) override the env variables.
-Globally specified host lists set in the fabfile (env.hosts = ['host1']) can override
such lists set on the command-line, but only if you’re not careful (or want them to.)
-Globally specified host lists set on the command-line (--hosts=host1) will initialize the env variables, but that’s it.

Définir une fonction de base

def host_type():
    run('uname -s')

Définir une tache

  • Encapsuler les fonctions de base de travail avec le mot clé "@task".
from fabric.api import task, run
@task
def mytask():
   run("a command")

Lorsque ce mot clé est utilisé, il signale à Fabric que seules les fonctions enveloppés dans "@task" doivent être chargés en tant tâches valables. (Quand il n'est pas présent, le comportement tâche "classique de style" entre en jeu.)

Arguments:

-"task_class": The Task subclass used to wrap the decorated function. Defaults to WrappedCallableTask.

-"aliases": An iterable of string names which will be used as aliases for the wrapped function. See Aliases for details.

-"alias": Like aliases but taking a single string argument instead of an iterable. If both alias and aliases are specified, aliases will take precedence.

from fabric.api import task
@task(alias='dwm')
def deploy_with_migrations():
   pass

-"default": A boolean value determining whether the decorated task also stands in for its containing module as a task name

@task(default=True)
def full_deploy():
   pass
  • classique de style

Faric examinera n'importe quel objet appelable trouvé dans votre "fabfile".

A l'exception de:

- Les fonctions qui commences par un underscore (_)

- Les fonctions propres Fabric, tels que "run" et "sudo" ne sera pas affiché dans votre liste de tâches.

Les modules de Fabric (Core API)

fabric.colors

fabric.colors.blue()
fabric.colors.cyan()
fabric.colors.green()
fabric.colors.magenta()
fabric.colors.red()
fabric.colors.white()
fabric.colors.yellow()

fabric.context_managers

fabric.context_managers.cd(path)
fabric.context_managers.hide(*groups)
fabric.context_managers.lcd(path)
fabric.context_managers.prefix()
fabric.context_managers.settings()
fabric.context_managers.show(*groups)

fabric.decorators

fabric.decorators.hosts()
fabric.decorators.roles()
fabric.decorators.runs_once()
fabric.decorators.serial()
fabric.decorators.parallel()
fabric.decorators.task()
fabric.decorators.with_settings()

fabric.network

fabric.network.disconnect_all()

fabric.operations

fabric.operations.run(command, shell=True, pty=True, combine_stderr=True)
fabric.operations.get(remote_path, local_path=None)
fabric.operations.put(local_path, remote_path, use_sudo=False, mirror_local_mode=False, mode=None)
fabric.operations.open_shell(command=None)
fabric.operations.local() Exécuter une commande sur le système local.
fabric.operations.prompt() Comme imput()
fabric.operations.reboot(wait=120)
fabric.operations.require() Vérifiez les clés indiquées dans le "dict environnement partagé" et abandonner s'il n'est pas trouvé.
fabric.operations.sudo(command, shell=True, pty=True, combine_stderr=True, user=None)

fabric.tasks

fabric.tasks.execute()

fabric.utils

fabric.utils.abort()
fabric.utils.error()
fabric.utils.fastprint()
fabric.utils.indent()
fabric.utils.puts()
fabric.utils.warn()

Les modules des contributeurs (Contrib API)

  • Console Output Utilities

fabric.contrib.console

fabric.contrib.console.confirm()
  • Django Integration

fabric.contrib.django

fabric.contrib.django.project()
fabric.contrib.django.settings_module()
  • File and Directory Management

fabric.contrib.files

fabric.contrib.files.append()
fabric.contrib.files.comment()
fabric.contrib.files.contains()
fabric.contrib.files.exists()
fabric.contrib.files.first()
fabric.contrib.files.sed()
fabric.contrib.files.uncomment()
fabric.contrib.files.upload_template()
  • Project Tools

fabric.contrib.project

fabric.contrib.project.rsync_project()
fabric.contrib.project.upload_project()
Outils personnels