Fabric

De Linuxmemo.

(Différences entre les versions)
(Commande "fab")
(fabric.operations)
 
(44 versions intermédiaires masquées)
Ligne 14 : Ligne 14 :
  fab -H localhost -f fabfile.py fonction
  fab -H localhost -f fabfile.py fonction
  fab -H localhost -f fabfile.py -l
  fab -H localhost -f fabfile.py -l
-
 
-
 
-
 
-
-a, --no_agent
 
-
    Sets env.no_agent to True, forcing our SSH layer not to talk to the SSH agent when trying to unlock private key files.
 
-
    New in version 0.9.1.
 
-
 
-
-A, --forward-agent
 
-
    Sets env.forward_agent to True, enabling agent forwarding.
 
-
    New in version 1.4.
 
-
 
-
--abort-on-prompts
 
-
    Sets env.abort_on_prompts to True, forcing Fabric to abort whenever it would prompt for input.
 
-
    New in version 1.1.
 
-
 
-
-c RCFILE, --config=RCFILE
 
-
    Sets env.rcfile to the given file path, which Fabric will try to load on startup and use to update environment variables.
 
-
 
-
-d COMMAND, --display=COMMAND
 
-
    Prints the entire docstring for the given task, if there is one. Does not currently print out the task’s function signature, so descriptive docstrings are a good idea. (They’re always a good idea, of course – just moreso here.)
 
-
 
-
--connection-attempts=M, -n M
 
-
    Set number of times to attempt connections. Sets env.connection_attempts.
 
-
    See also
 
-
    env.connection_attempts, env.timeout
 
-
    New in version 1.4.
 
-
 
-
-D, --disable-known-hosts
 
-
    Sets env.disable_known_hosts to True, preventing Fabric from loading the user’s SSH known_hosts file.
 
-
 
-
-f FABFILE, --fabfile=FABFILE
 
-
    The fabfile name pattern to search for (defaults to fabfile.py), or alternately an explicit file path to load as the fabfile (e.g. /path/to/my/fabfile.py.)
 
-
    See also
 
-
    Fabfile construction and use
 
-
 
-
-F LIST_FORMAT, --list-format=LIST_FORMAT
 
-
    Allows control over the output format of --list. short is equivalent to --shortlist, normal is the same as simply omitting this option entirely (i.e. the default), and nested prints out a nested namespace tree.
 
-
    New in version 1.1.
 
-
    See also
 
-
    --shortlist, --list
 
-
 
-
-h, --help
 
-
    Displays a standard help message, with all possible options and a brief overview of what they do, then exits.
 
-
 
-
--hide=LEVELS
 
-
    A comma-separated list of output levels to hide by default.
 
-
 
-
-H HOSTS, --hosts=HOSTS
 
-
    Sets env.hosts to the given comma-delimited list of host strings.
 
-
 
-
-x HOSTS, --exclude-hosts=HOSTS
 
-
    Sets env.exclude_hosts to the given comma-delimited list of host strings to then keep out of the final host list.
 
-
    New in version 1.1.
 
-
 
-
-i KEY_FILENAME
 
-
    When set to a file path, will load the given file as an SSH identity file (usually a private key.) This option may be repeated multiple times. Sets (or appends to) env.key_filename.
 
-
 
-
-k
 
-
    Sets env.no_keys to True, forcing the SSH layer to not look for SSH private key files in one’s home directory.
 
-
    New in version 0.9.1.
 
-
 
-
--keepalive=KEEPALIVE
 
-
    Sets env.keepalive to the given (integer) value, specifying an SSH keepalive interval.
 
-
    New in version 1.1.
 
-
 
-
--linewise
 
-
    Forces output to be buffered line-by-line instead of byte-by-byte. Often useful or required for parallel execution.
 
-
    New in version 1.3.
 
-
 
-
-l, --list
 
-
    Imports a fabfile as normal, but then prints a list of all discovered tasks and exits. Will also print the first line of each task’s docstring, if it has one, next to it (truncating if necessary.)
 
-
    Changed in version 0.9.1: Added docstring to output.
 
-
    See also
 
-
    --shortlist, --list-format
 
-
 
-
-p PASSWORD, --password=PASSWORD
 
-
    Sets env.password to the given string; it will then be used as the default password when making SSH connections or calling the sudo program.
 
-
 
-
-P, --parallel
 
-
    Sets env.parallel to True, causing tasks to run in parallel.
 
-
    New in version 1.3.
 
-
    See also
 
-
    Parallel execution
 
-
 
-
--no-pty
 
-
    Sets env.always_use_pty to False, causing all run/sudo calls to behave as if one had specified pty=False.
 
-
    New in version 1.0.
 
-
 
-
-r, --reject-unknown-hosts
 
-
    Sets env.reject_unknown_hosts to True, causing Fabric to abort when connecting to hosts not found in the user’s SSH known_hosts file.
 
-
 
-
-R ROLES, --roles=ROLES
 
-
    Sets env.roles to the given comma-separated list of role names.
 
-
 
-
--set KEY=VALUE,...
 
-
    Allows you to set default values for arbitrary Fabric env vars. Values set this way have a low precedence – they will not override more specific env vars which are also specified on the command line. E.g.:
 
-
    fab --set password=foo --password=bar
 
-
    will result in env.password = 'bar', not 'foo'
 
-
    Multiple KEY=VALUE pairs may be comma-separated, e.g. fab --set var1=val1,var2=val2.
 
-
    Other than basic string values, you may also set env vars to True by omitting the =VALUE (e.g. fab --set KEY), and you may set values to the empty string (and thus a False-equivalent value) by keeping the equals sign, but omitting VALUE (e.g. fab --set KEY=.)
 
-
    New in version 1.4.
 
-
 
-
-s SHELL, --shell=SHELL
 
-
    Sets env.shell to the given string, overriding the default shell wrapper used to execute remote commands.
 
-
 
-
--shortlist
 
-
    Similar to --list, but without any embellishment, just task names separated by newlines with no indentation or docstrings.
 
-
    New in version 0.9.2.
 
-
    See also
 
-
    --list
 
-
 
-
--show=LEVELS
 
-
    A comma-separated list of output levels to be added to those that are shown by default.
 
-
    See also
 
-
    run, sudo
 
-
 
-
--ssh-config-path
 
-
    Sets env.ssh_config_path.
 
-
    New in version 1.4.
 
-
    See also
 
-
    Leveraging native SSH config files
 
-
 
-
--skip-bad-hosts
 
-
    Sets env.skip_bad_hosts, causing Fabric to skip unavailable hosts.
 
-
    New in version 1.4.
 
-
 
-
--timeout=N, -t N
 
-
    Set connection timeout in seconds. Sets env.timeout.
 
-
    See also
 
-
    env.timeout, env.connection_attempts
 
-
    New in version 1.4.
 
-
 
-
-u USER, --user=USER
 
-
    Sets env.user to the given string; it will then be used as the default username when making SSH connections.
 
-
 
-
-V, --version
 
-
    Displays Fabric’s version number, then exits.
 
-
 
-
-w, --warn-only
 
-
    Sets env.warn_only to True, causing Fabric to continue execution even when commands encounter error conditions.
 
-
 
-
-z, --pool-size
 
-
    Sets env.pool_size, which specifies how many processes to run concurrently during parallel execution.
 
-
    New in version 1.3.
 
==Fichier de commande: "fabfile"==
==Fichier de commande: "fabfile"==
Ligne 206 : Ligne 62 :
     version
     version
     warn_only
     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() <span style="color: red;">Exécuter une commande sur le système local.</span>
 +
fabric.operations.prompt() <span style="color: red;">Comme imput()</span>
 +
fabric.operations.reboot(wait=120)
 +
fabric.operations.require() <span style="color: red;">Vérifiez les clés indiquées dans le "dict environnement partagé" et abandonner si elle n'est pas trouvée.</span>
 +
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()

Version actuelle en date du 10 octobre 2012 à 11:44

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

Sommaire

[modifier] Installation

apt-get install fabric

ou bien

apt-get install python-pip
pip install fabric

[modifier] 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

[modifier] Fichier de commande: "fabfile"

[modifier] 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

[modifier] Modèle d’exécution

[modifier] Exécution en série

[modifier] Exécution en parallèle

[modifier] 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 *

[modifier] 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.

[modifier] Définir une fonction de base

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

[modifier] 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.

[modifier] Les modules de Fabric (Core API)

[modifier] fabric.colors

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

[modifier] 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)

[modifier] 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()

[modifier] fabric.network

fabric.network.disconnect_all()

[modifier] 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 si elle n'est pas trouvée.
fabric.operations.sudo(command, shell=True, pty=True, combine_stderr=True, user=None)

[modifier] fabric.tasks

fabric.tasks.execute()

[modifier] fabric.utils

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

[modifier] Les modules des contributeurs (Contrib API)

  • Console Output Utilities

[modifier] fabric.contrib.console

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

[modifier] fabric.contrib.django

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

[modifier] 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

[modifier] fabric.contrib.project

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