python:les_modules
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
python:les_modules [2014/01/18 10:28] – [Fabric] root | python:les_modules [2024/12/20 12:41] (Version actuelle) – [Fabric] admin | ||
---|---|---|---|
Ligne 202: | Ligne 202: | ||
try: | try: | ||
- | from fabric.api import * | + | |
- | from fabric.colors import * | + | from fabric.colors import * |
except: | except: | ||
- | print(' | + | |
- | sys.exit(1) | + | sys.exit(1) |
sys.tracebacklimit = 0 | sys.tracebacklimit = 0 | ||
Ligne 233: | Ligne 233: | ||
env.linewise = True | env.linewise = True | ||
env.warn_only = True | env.warn_only = True | ||
- | |||
env.role_default = ' | env.role_default = ' | ||
env.roledefs = { | env.roledefs = { | ||
- | ' | + | |
- | ' | + | ' |
- | ' | + | ' |
- | ' | + | ' |
- | 'sw' : [' | + | 'web_adm': [' |
} | } | ||
+ | env.roledefs[' | ||
if env.hosts == [] and env.roles == []: | if env.hosts == [] and env.roles == []: | ||
- | env.hosts = env.roledefs[env.role_default] | + | |
#list_all = [] | #list_all = [] | ||
#for server in env.roledefs.values(): | #for server in env.roledefs.values(): | ||
- | # list_all += server | + | # |
# | # | ||
+ | |||
+ | def VERIF_IPMI_PASSWORD(): | ||
+ | if ' | ||
+ | globals()[' | ||
+ | |||
+ | if ' | ||
+ | IPMI_PASSWORD = getpass.getpass(prompt=' | ||
+ | globals()[' | ||
@task | @task | ||
@serial | @serial | ||
def lr(): | def lr(): | ||
- | """ | + | |
- | for role in env.roledefs: | + | for role in env.roledefs: |
- | if role == env.role_default: | + | if role == env.role_default: |
- | print(" | + | print(" |
- | else: | + | else: |
- | print(" | + | print(" |
- | sys.exit() | + | sys.exit() |
# | # | ||
@task | @task | ||
def cmd(cmd, show = None): | def cmd(cmd, show = None): | ||
- | """ | + | |
- | #with show(' | + | #with show(' |
- | try: | + | try: |
- | out = run(cmd) | + | out = run(cmd) |
- | except Exception as err: | + | except Exception as err: |
- | sys.stdout.write(yellow(" | + | sys.stdout.write(yellow(" |
- | print(red(err)) | + | print(red(err)) |
- | return | + | return |
- | if show == None: | + | |
- | for line in out.splitlines(): | + | for line in out.splitlines(): |
- | sys.stdout.write(yellow(" | + | sys.stdout.write(yellow(" |
- | print(line) | + | print(line) |
- | if out.failed: | + | |
- | sys.stdout.write(yellow(" | + | sys.stdout.write(yellow(" |
- | print(red(' | + | print(red(' |
- | elif show == ' | + | elif show == ' |
- | sys.stdout.write(yellow(" | + | sys.stdout.write(yellow(" |
- | if out.failed: | + | if out.failed: |
- | print(red(' | + | print(red(' |
- | else: | + | else: |
- | print(green(' | + | print(green(' |
- | if show == ' | + | |
- | print(yellow(' | + | print(yellow(' |
- | print(' | + | print(' |
- | else: | + | else: |
- | print(" | + | print(" |
- | sys.exit(1) | + | sys.exit(1) |
@task | @task | ||
def send_file(src, | def send_file(src, | ||
- | """ | + | |
- | if dst == None: | + | if dst == None: |
- | dst = src | + | dst = src |
- | + | ||
- | if mode == None: | + | |
- | put(src, dst) | + | |
- | else: | + | put(src, dst) |
- | try: | + | else: |
- | int(mode) | + | try: |
- | except: | + | int(mode) |
- | print("' | + | except: |
- | sys.exit(1) | + | print("' |
- | + | sys.exit(1) | |
- | put(src, dst, mode=mode) | + | |
- | @task | + | put(src, dst, mode=mode) |
+ | @task | ||
def reboot(wait = 0): | def reboot(wait = 0): | ||
- | """ | + | |
- | if os.getlogin() != ' | + | if os.getlogin() != ' |
- | print(' | + | print(' |
- | sys.exit(1) | + | sys.exit(1) |
- | try: | + | |
- | int(wait) | + | int(wait) |
- | except: | + | except: |
- | print("' | + | print("' |
- | sys.exit(1) | + | sys.exit(1) |
- | reboot(wait = wait) | + | |
@serial | @serial | ||
@task | @task | ||
def power(state): | def power(state): | ||
- | """ | + | |
- | if not env.host.endswith(' | + | if not env.host |
- | env.host | + | print('Use "-R imm" or "-H < |
+ | | ||
+ | sys.exit(1) | ||
- | if state not in [' | + | |
- | print(" | + | print(" |
- | sys.exit(1) | + | sys.exit(1) |
- | if ' | + | VERIF_IPMI_PASSWORD() |
- | IPMI_PASSWORD = getpass.getpass(prompt=' | + | |
- | globals()[' | + | |
- | + | ||
- | out = local(" | + | |
- | if out.failed: | + | out = local(" |
- | sys.stdout.write(yellow(" | + | |
- | print(red(' | + | |
- | print(out.stderr) | + | sys.stdout.write(yellow(" |
- | sys.exit(1) | + | print(red(' |
- | else: | + | print(out.stderr) |
- | for line in out.splitlines(): | + | sys.exit(1) |
- | sys.stdout.write(yellow(" | + | else: |
- | print(line) | + | for line in out.splitlines(): |
+ | sys.stdout.write(yellow(" | ||
+ | print(line) | ||
@serial | @serial | ||
@task | @task | ||
def console(action=' | def console(action=' | ||
- | """ | + | |
- | if len(env.hosts) != 1: | + | if len(env.hosts) != 1: |
- | print(' | + | print(' |
- | sys.exit() | + | sys.exit(1) |
- | if not env.host.endswith(' | + | |
- | env.host | + | print('Use "-H < |
+ | | ||
+ | sys.exit(1) | ||
- | | + | |
- | IPMI_PASSWORD = getpass.getpass(prompt=' | + | |
- | globals()[' | + | |
- | os.system(" | + | |
@serial | @serial | ||
@task | @task | ||
- | def switch(): | + | def web_adm(): |
- | """ | + | """ |
if len(env.hosts) != 1: | if len(env.hosts) != 1: | ||
- | print(' | + | print(' |
- | sys.exit() | + | sys.exit(1) |
- | if os.getenv(' | + | |
- | print(' | + | print(' |
- | sys.exit(1) | + | sys.exit(1) |
- | if env.host not in env.roledefs[' | + | |
- | print(" | + | print(" |
- | sys.exit(1) | + | sys.exit(1) |
- | os.system(' | + | |
</ | </ | ||
+ | |||
+ | ===== readline ===== | ||
+ | |||
+ | <code python readline.py> | ||
+ | import readline, signal | ||
+ | prompt = 'GigiX > ' | ||
+ | |||
+ | def signal_handler(signal, | ||
+ | |||
+ | input(prompt) | ||
+ | return | ||
+ | |||
+ | signal.signal(signal.SIGINT, | ||
+ | readline.parse_and_bind(" | ||
+ | |||
+ | class completer: | ||
+ | def __init__(self, | ||
+ | self.tree = tree | ||
+ | |||
+ | def _parser(self, | ||
+ | if (search is None) or (callable(search) == True): | ||
+ | return [] | ||
+ | elif len(line) == 0: | ||
+ | return [x for x in search] | ||
+ | elif len(line) == 1 and readline.get_line_buffer()[-1] != ' ': | ||
+ | return ['%s ' % x for x in search if x.startswith(line[0])] | ||
+ | else: | ||
+ | return self._parser(line[1: | ||
+ | return [] | ||
+ | |||
+ | def complete(self, | ||
+ | line = readline.get_line_buffer().split() | ||
+ | results = self._parser(line, | ||
+ | return results[state] | ||
+ | |||
+ | def linux_sles_12(): | ||
+ | print(' | ||
+ | |||
+ | tree = { | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }, | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | } | ||
+ | |||
+ | readline.set_completer(completer(tree).complete) | ||
+ | l = '' | ||
+ | while l != (' | ||
+ | try: | ||
+ | l = input(prompt) | ||
+ | eval(" | ||
+ | except EOFError: | ||
+ | print(' | ||
+ | break | ||
+ | except: | ||
+ | continue | ||
+ | </ | ||
+ | |||
===== Twisted ===== | ===== Twisted ===== | ||
Ligne 421: | Ligne 527: | ||
import ctypes, ctypes.util | import ctypes, ctypes.util | ||
- | ctypes.cdll.LoadLibrary(ctypes.util.find_library(' | + | libc=ctypes.cdll.LoadLibrary(ctypes.util.find_library(' |
libc.printf(" | libc.printf(" | ||
</ | </ |
python/les_modules.1390040932.txt.gz · Dernière modification : 2014/01/18 10:28 de root