Source code for accre.datacenter

"""
Tools for datacenter or physical management of ACCRE hardware
"""
import subprocess
from time import sleep
import sys

from accre.dcim.client import DCIMClient

from accre.config import get_config
from accre.util import accre_argparser, RedStr, GreenStr


CONFIG = get_config()





[docs]def griswold(cabinet, blinktime=15, ssh=True): """ Cause all servers in a cabinet (as known in OpenDCIM) to blink in sequence from highest to lowest position and print information about the make, model, and serial number/service tag of each physical server. ..note:: You must be root on a node with passwordless root ssh to all other nodes for this function to work ..todo:: Add support for blades/child servers :param str cabinet: Cabinet location, ex. "H15" :param int blinktime: Time to spend locating each server :param bool ssh: use ssh to connect to servers if true, rsh if false :returns: True if all servers blinked successfully, false if errors :rtype: bool """ with DCIMClient(caching=True) as client: devices = client.get_cabinet_devices(cabinet, nochildren=True) servers = [d for d in devices if d['DeviceType'] == 'Server'] servers.sort(key=lambda s: s['Position'], reverse=True) everything_ok = True for server in servers: hostname = server['Label'] position = server['Position'] info = client.model(hostname) msg = 'U{0} contains {1}, a {2} {3} with SN={4}'.format( position, hostname, info['make'], info['model'], info['serial'] ) print(msg) result = blink_locate_led( hostname, blinktime=blinktime, ssh=ssh, verbose=True ) if not result: everything_ok = False return everything_ok
[docs]def griswold_cli(): """ CLI entry point for the griswold function. Use ``griswold --help`` for usage. """ description = ( 'Check locate LEDs on all servers on a given cabinet,' ' and spread the holiday cheer.' ) parser = accre_argparser('griswold', description=description) parser.add_argument('cabinet', help="Cabinet to light up with holiday cheer" ) parser.add_argument('-s', '--seconds', action='store', default=15, type=int, help="Number of seconds to blink for" ) parser.add_argument('-n', '--nossl', action='store_true', help="Use rsh instead of ssh" ) args = parser.parse_args() ssh = True if args.nossl: ssh = False msg = ( 'This command will spread holiday cheer to rack {0}! Are you sure?' .format(args.cabinet) ) cheer = input(msg) if not cheer.lower().startswith('y'): sys.exit(1) griswold(args.cabinet, blinktime=args.seconds, ssh=ssh)
if __name__ == '__main__': griswold_cli()