XAL

un code unique,
une exécution qui s'adapte
https://xal.readthedocs.org

par Benoît Bryon - www.marmelune.net

PyCon FR - Pau - Octobre 2015

Pourquoi xal ?

Python, c'est cool !

  • lisible
  • haut niveau + expressif
  • stdlib + PyPI
  • se connecte partout
  • shell, web, GUI...
  • super communauté

Exercice : migrations

$ tree migrations/
migrations/
├── 1-hello.sh
└── 2-goodbye.py
$ cat migrations/1-hello.sh
#!/bin/bash
echo "Hello bash!"

... et des commandes à exécuter

Des fichiers à manipuler...

Avec la stdlib, en local

>>> import pathlib
>>> import subprocess

>>> for script in pathlib.Path('migrations').iterdir():
...     subprocess.call(str(script))

Shell python :

Avec Fabric, en local

>>> import pathlib
>>> import fabric.api

>>> for script in pathlib.Path('migrations').iterdir():
...     fabric.api.local(str(script))

Shell python :

Avec Fabric, à distance

import fabric.api

def migrate():
    ls_migrations = fabric.api.run('ls migrations')
    migrations = ls_migrations.split()
    for script in migrations:
        fabric.api.run('migrations/{0}'.format(script))

fabfile.py :

Avec Fabric, en sudoer

import fabric.api

def migrate():
    ls_migrations = fabric.api.run('ls migrations')
    migrations = ls_migrations.split()
    for script in migrations:
        fabric.api.sudo('migrations/{0}'.format(script))

fabfile.py :

Salt ?

Réécrire tous les

scripts de déploiement ?

Python est limité

Python
 pour manipuler des ressources,

c'est la misère

Python
 pour manipuler des ressources,

c'est la misère

xal !

Interagir avec une session

>>> import xal

>>> session = xal.LocalSession()

>>> for script in session.path('migrations').iterdir():
...     session.sh.run(script)

Réutiliser du code

def migrate(session):
    for script in session.path('migrations').iterdir():
        session.sh.run(script)
>>> session = xal.LocalSession()
>>> migrate(session)
    

Dans une session locale...

>>> session = xal.FabricSession(host='localhost')
>>> migrate(session)
    

... ou distante :

Fichiers : pathlib

>>> import xal

>>> session = xal.LocalSession()

>>> with session.path.cd('demo'):
...     file_path = session.path('hello.txt')
...     if not file_path.exists():
...         file_path.open('w').write('Hello world!')
...     file_path.chmod(0o664)

sh : proof of concept

>>> import xal

>>> session = xal.LocalSession()

>>> echo = session.sh("echo -n Hello")
>>> echo
ShCommand(echo -n Hello)

>>> result = echo()  # Commands instances are callables.
>>> result.stdout
'Hello'
>>> result.return_code
0

>>> count_words = session.sh('wc -c')
>>> session.sh.run(echo | count_words).stdout  # Pipes!
'5'

Ça marche !

  • proof of concept
  • en local et en SSH (via Fabric/Fabtools)
  • session.path : pathlib !
  • session.sh : API basique
  • plein de choses à améliorer...

 

https://github.com/benoitbryon/xal/issues

Aller plus loin !

Coopération

Développement itératif

Python pour sysadmins

  • forces de Python
  • interactions avec les ressources du système
  • local ou distant
  • en shell ou dans des scripts
  • simplicité de l'API

APIs => PEPs ?

  • fichiers : pathlib
  • commandes : subprocess => PEP ?
  • packages : PEP ?
  • ...

Questions ?

par Benoît Bryon - www.marmelune.net

PyCon FR - Pau - Octobre 2015

Made with Slides.com