#!/usr/bin/env python import sys, os, getpass #import sys, os, getpass, multiprocessing try: from fabric.api import * from fabric.colors import * except: print('Fabric module must be installed !') sys.exit(1) sys.tracebacklimit = 0 output['running'] = False output['stdout'] = False output['aborts'] = False output['status'] = False output['warnings'] = False #env.user = 'root' #env.password = None #env.passwords = { # 'user@host':'pass', # 'gwuser@gwhost':'gwpass' #} #env.shell = "/bin/sh -c" #env.gateway = None env.disable_known_hosts = True #env.parralel = True #env.pool_size = multiprocessing.cpu_count() #Sets the number of concurrent processes to use when executing tasks in parallel. #env.keepalive = 300 #env.timeout = 10 #Network ssh connection timeout, in seconds. #env.command_timeout = 60 env.linewise = True env.warn_only = True env.role_default = 'adm' env.roledefs = { 'eth': ['molx1321', 'molx1322', 'molx1323', 'molx1324', 'molx1325', 'molx1326', 'molx1327', 'molx1328', 'molx1329', 'molx1330', 'molx1331', 'molx1332', 'molx1333', 'molx1334', 'molx1335', 'molx1336' ], 'adm' : ['molx1321-adm', 'molx1322-adm', 'molx1323-adm', 'molx1324-adm', 'molx1325-adm', 'molx1326-adm', 'molx1327-adm', 'molx1328-adm', 'molx1329-adm', 'molx1330-adm', 'molx1331-adm', 'molx1332-adm', 'molx1333-adm', 'molx1334-adm', 'molx1335-adm', 'molx1336-adm' ], 'ib' : ['molx1321-ib', 'molx1322-ib', 'molx1323-ib', 'molx1324-ib', 'molx1325-ib', 'molx1326-ib', 'molx1327-ib', 'molx1328-ib', 'molx1329-ib', 'molx1330-ib', 'molx1331-ib', 'molx1332-ib', 'molx1333-ib', 'molx1334-ib', 'molx1335-ib', 'molx1336-ib' ], 'imm' : ['molx1321-imm', 'molx1322-imm', 'molx1323-imm', 'molx1324-imm', 'molx1325-imm', 'molx1326-imm', 'molx1327-imm', 'molx1328-imm', 'molx1329-imm', 'molx1330-imm', 'molx1331-imm', 'molx1332-imm', 'molx1333-imm', 'molx1334-imm', 'molx1335-imm', 'molx1336-imm' ], 'web_adm' : ['sw-eth','sw-ib'], } env.roledefs['web_adm'] += env.roledefs['imm'] if env.hosts == [] and env.roles == []: env.hosts = env.roledefs[env.role_default] #list_all = [] #for server in env.roledefs.values(): # list_all += server #env.roledefs['all'] = list(set(list_all)) def VERIF_IPMI_PASSWORD(): if 'IPMI_PASSWORD' in os.environ: globals()['IPMI_PASSWORD'] = os.environ['IPMI_PASSWORD'] if 'IPMI_PASSWORD' not in globals(): IPMI_PASSWORD = getpass.getpass(prompt='IMM password : ') globals()['IPMI_PASSWORD'] = IPMI_PASSWORD @task @serial def lr(): """ : Liste les roles utilisables avec l'option '-R'""" for role in env.roledefs: if role == env.role_default: print("%s (default) : %s" % (role, ' ,'.join(env.roledefs[role]))) else: print("%s : %s" % (role, ' ,'.join(env.roledefs[role]))) sys.exit() #@parallel(pool_size = multiprocessing.cpu_count() + 1) @task def cmd(cmd, show = None): """ : Run command on a host (show='details' for details, show='status' for see if command is successful or not)""" #with show('stdout','running','warnings'): try: out = run(cmd) except Exception as err: sys.stdout.write(yellow("%(host)s (%(user)s) : " % env, bold = True)) print(red(err)) return if show == None: for line in out.splitlines(): sys.stdout.write(yellow("%(host)s (%(user)s) : " % env, bold = True)) print(line) if out.failed: sys.stdout.write(yellow("%(host)s (%(user)s) : " % env, bold = True)) print(red(' KO', bold = True)) + " (return %s)" % out.return_code elif show == 'details' or show == 'status': sys.stdout.write(yellow("Executing on %(host)s as %(user)s : " % env, bold = True) + blue(cmd, bold = True)) if out.failed: print(red(' KO', bold = True)) else: print(green(' OK', bold = True)) if show == 'details': print(yellow('OUT : (return code = %s)' % out.return_code)) print('%s\n' % out) else: print("Option %s unknow" % show) sys.exit(1) @task def send_file(src, dst = None, mode = None): """ : Send file on a host (dst=, mode=)""" if dst == None: dst = src if mode == None: put(src, dst) else: try: int(mode) except: print("'mode' value must be an integer !") sys.exit(1) put(src, dst, mode=mode) @task def reboot(wait = 0): """ : Reboot a host (wait=)""" if os.getlogin() != 'root': print('Only root must be reboot a host') sys.exit(1) try: int(wait) except: print("'wait' value must be an integer !") sys.exit(1) reboot(wait = wait) @serial @task def power(state): """ : IMM power manager (state=)""" if not env.host in env.roledefs['imm']: print('Use "-R imm" or "-H -imm"') print('%s not in this table : %s' % (env.host, ','.join(env.roledefs['imm']))) sys.exit(1) if state not in ['status','on','off','cycle','reset','diag','soft']: print("%s state is not supported. Please use state !" % state) sys.exit(1) VERIF_IPMI_PASSWORD() out = local("ipmitool -I lanplus -C 2 -U system -P '%s' -E -H %s chassis power %s" % (globals()['IPMI_PASSWORD'], env.host, state), capture=True) if out.failed: sys.stdout.write(yellow("%(host)s : " % env, bold = True)) print(red('Failed ! (return code : %s)' % out.return_code)) print(out.stderr) sys.exit(1) else: for line in out.splitlines(): sys.stdout.write(yellow("%(host)s : " % env, bold = True)) print(line) @serial @task def console(action='activate'): """ : IMM SOL (action=)""" if len(env.hosts) != 1: print('Only on one host. Use option -H to precise the host !') sys.exit(1) if not env.host in env.roledefs['imm']: print('Use "-H -imm"') print('%s not in this table : %s' % (env.host, ','.join(env.roledefs['imm']))) sys.exit(1) VERIF_IPMI_PASSWORD() os.system("ipmitool -I lanplus -C 2 -U system -P '%s' -E -H %s sol %s" % (globals()['IPMI_PASSWORD'], env.host, action)) @serial @task def web_adm(): """ : Open Firefox to administrate (use -H option)""" if len(env.hosts) != 1: print('Only on one host. Use option -H to precise the host (%s)!' % ', '.join(env.roledefs['web_adm'])) sys.exit(1) if os.getenv('DISPLAY') == None: print('You must have a valid display !') sys.exit(1) if env.host not in env.roledefs['web_adm']: print("%s is not a valid host ! Valid host are %s" % (env.host, ', '.join(env.roledefs['web_adm']))) sys.exit(1) os.system('firefox https://%s/' % env.host)