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 [2013/12/03 22:00] – [Chemin des modules] root | python:les_modules [2024/12/20 12:41] (Version actuelle) – [Fabric] admin | ||
|---|---|---|---|
| Ligne 198: | Ligne 198: | ||
| # | # | ||
| - | from fabric.api import * | + | import sys, os, getpass | 
| - | from fabric.colors import * | + | #import sys, os, getpass, multiprocessing | 
| + | |||
| + | try: | ||
| + |  | ||
| + | from fabric.colors import * | ||
| + | except: | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | sys.tracebacklimit = 0 | ||
| + | |||
| + | output[' | ||
| + | output[' | ||
| + | output[' | ||
| + | output[' | ||
| + | output[' | ||
| + | |||
| + | #env.user = ' | ||
| + | # | ||
| - | env.hosts = [' | ||
| - | env.user = ' | ||
| - | env.password = ' | ||
| - | # | ||
| # | # | ||
| - |  | + | # | 
| - | ' | + | #    ' | 
| - | } | + | #} | 
| #env.shell = "/ | #env.shell = "/ | ||
| + | # | ||
| + | env.disable_known_hosts = True | ||
| + | # | ||
| + | # | ||
| # | # | ||
| - | # | + | #env.timeout = 10 #Network ssh connection timeout, in seconds. | 
| - | 'web': ['www1', 'www2', 'www3'], | + | # | 
| - | 'dns': ['ns1', 'ns2'] | + | env.linewise = True | 
| + | env.warn_only = True | ||
| + | |||
| + | env.role_default = ' | ||
| + | |||
| + | env.roledefs = { | ||
| + | 'eth': ['svr1', 'svr2', 'svr3', ' | ||
| + | 'adm': ['svr1-adm', 'svr2-adm', | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| } | } | ||
| - | env.disable_known_hosts = True | ||
| - | env.parralel = True | ||
| - | env.pool_size = 0 #Sets the number of concurrent processes to use when executing tasks in parallel. | ||
| - | env.timeout = 10 #Network ssh connection timeout, in seconds. | ||
| - | env.command_timeout = 10 | ||
| - | # | ||
| - | hide(' | ||
| - | def remote_info(): | + | env.roledefs['web_adm'] += env.roledefs['imm'] | 
| - | with hide('running', ' | + | |
| - | print(yellow(" | + | |
| - | a = run('uname -a') | + | |
| - | fastprint(a, | + | |
| - | def local_info(): | + | if env.hosts == [] and env.roles == []: | 
| - | local(' | + | env.hosts = env.roledefs[env.role_default] | 
| + | |||
| + | #list_all = [] | ||
| + | #for server in env.roledefs.values(): | ||
| + | # | ||
| + | # | ||
| + | |||
| + | def VERIF_IPMI_PASSWORD(): | ||
| + |  | ||
| + | globals()[' | ||
| + | |||
| + | if ' | ||
| + | IPMI_PASSWORD = getpass.getpass(prompt=' | ||
| + | globals()[' | ||
| + | |||
| + | @task | ||
| + | @serial | ||
| + | def lr(): | ||
| + | """ | ||
| + | for role in env.roledefs: | ||
| + | if role == env.role_default: | ||
| + | print(" | ||
| + | else: | ||
| + | print(" | ||
| + | sys.exit() | ||
| + | |||
| + | # | ||
| + | @task | ||
| + | def cmd(cmd, show = None): | ||
| + | """ | ||
| + | #with show(' | ||
| + | try: | ||
| + | out = run(cmd) | ||
| + | except Exception as err: | ||
| + | sys.stdout.write(yellow(" | ||
| + | print(red(err)) | ||
| + | return | ||
| + | |||
| + | if show == None: | ||
| + | for line in out.splitlines(): | ||
| + | sys.stdout.write(yellow(" | ||
| + | print(line) | ||
| + | |||
| + | if out.failed: | ||
| + | sys.stdout.write(yellow(" | ||
| + | print(red(' | ||
| + | elif show == ' | ||
| + | sys.stdout.write(yellow(" | ||
| + | if out.failed: | ||
| + | print(red(' | ||
| + | else: | ||
| + | print(green(' | ||
| + | |||
| + | if show ==  ' | ||
| + | print(yellow(' | ||
| + | print(' | ||
| + | else: | ||
| + | print(" | ||
| + | sys.exit(1) | ||
| + | |||
| + | @task | ||
| + | def send_file(src, | ||
| + | """ | ||
| + | if dst == None: | ||
| + | dst = src | ||
| + | |||
| + | |||
| + | if mode == None: | ||
| + | put(src, dst) | ||
| + | else: | ||
| + | try: | ||
| + | int(mode) | ||
| + | except: | ||
| + | print("' | ||
| + | sys.exit(1) | ||
| + | |||
| + | put(src, dst, mode=mode) | ||
| + | @task | ||
| + | def reboot(wait = 0): | ||
| + | """ | ||
| + | if os.getlogin() != ' | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | try: | ||
| + | int(wait) | ||
| + | except: | ||
| + | print("' | ||
| + | sys.exit(1) | ||
| + | |||
| + | reboot(wait = wait) | ||
| + | |||
| + | @serial | ||
| + | @task | ||
| + | def power(state): | ||
| + | """ | ||
| + | if not env.host in env.roledefs[' | ||
| + | print(' | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | if state not in [' | ||
| + | print(" | ||
| + | sys.exit(1) | ||
| + | |||
| + | VERIF_IPMI_PASSWORD() | ||
| + | |||
| + | out = local(" | ||
| + | |||
| + | if out.failed: | ||
| + | sys.stdout.write(yellow(" | ||
| + | print(red(' | ||
| + | print(out.stderr) | ||
| + | sys.exit(1) | ||
| + | else: | ||
| + | for line in out.splitlines(): | ||
| + | sys.stdout.write(yellow(" | ||
| + | print(line) | ||
| + | |||
| + | @serial | ||
| + | @task | ||
| + | def console(action=' | ||
| + | """ | ||
| + | if len(env.hosts) != 1: | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | if not env.host in env.roledefs[' | ||
| + | print(' | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | VERIF_IPMI_PASSWORD() | ||
| + | |||
| + | os.system(" | ||
| + | |||
| + | @serial | ||
| + | @task | ||
| + | def web_adm(): | ||
| + | """ | ||
| + | if len(env.hosts) != 1: | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | if os.getenv(' | ||
| + | print(' | ||
| + | sys.exit(1) | ||
| + | |||
| + | if env.host not in env.roledefs[' | ||
| + | print(" | ||
| + | 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 259: | 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.1386108008.txt.gz · Dernière modification :  de root
                
                