def handle(self, command, *args, **options):
        if not len(command):
            raise ValidationError('ipmitool requires a list of command args.')

        validate_host(options['address'])
        self.validate_privlvl(options['privlvl'])

        call_args = [
            'ipmitool',
            '-H', options['address'],
            '-I', options['interface'],
            '-L', options['privlvl'],
            '-p', str(options['port']),

            '-U', options['username'],
            '-P', options['password'],
        ] + command

        logger.info(' '.join(call_args))

        try:
            return subprocess.check_output(call_args,
                                           stderr=subprocess.STDOUT,
                                           encoding='utf-8',
                                           timeout=options['timeout'])
        except Exception as e:
            logging.exception(e)
            logging.warn('ipmitool may report failure on success. '
                         'So we ignore the exception and check for ping:')
Exemplo n.º 2
0
    def handle(self, hostname, *args, **options):
        validate_host(hostname)

        logger.info("Powercycling %s via HP iLO. %s", hostname, options)

        username = options.get('login', None) or settings.ILO_USERNAME
        password = options.get('password', None) or settings.ILO_PASSWORD

        ilo = hpilo.Ilo(hostname,
                        login=username,
                        password=password,
                        timeout=options['timeout'])

        power_status = ilo.get_host_power_status()
        logger.debug("Got power status %s for ilo server %s.", power_status, hostname)

        try:
            ilo.reset_server()
            logger.debug("Soft reset of ilo server %s complete.", hostname)
        except Exception as error:
            logger.debug("clean powercycle of ilo server %s failed with error: %s", hostname, error)
            ilo.set_host_power(host_power=False)
            logger.debug("hard shutdown of ilo sever %s complete.", hostname)

            logger.debug("Power is off, waiting %d seconds before turning it back on.", options['delay'])
            time.sleep(options['delay'])

            ilo.set_host_power(host_power=True)

        logger.info("Powercycle of %s completed.", hostname)
    def handle(self, hostname, *args, **options):
        validate_host(hostname)

        call_args = [
            'ssh',

            '-o', 'PasswordAuthentication=no',

            # disable host key checks
            '-o', 'StrictHostKeyChecking=no',
            '-o', 'UserKnownHostsFile=/dev/null',

            '-i', options['identity_file'],
            '-l', options['login_name'],
            '-p', str(options['port']),
            hostname,
        ]
        logger.debug('ssh reboot with base args: {}'.format(' '.join(call_args)))

        # By trying a few different reboot commands we don't need to special case
        # different types of hosts. The "shutdown" command is for Windows, but uses
        # hyphens because it gets run through a bash shell. We also delay the
        # shutdown for a few seconds so that we have time to read the exit status
        # of the shutdown command.
        for reboot_cmd in ['reboot', 'shutdown -f -t 3 -r']:
            try:
                return subprocess.check_output(call_args + [reboot_cmd],
                                               stderr=subprocess.STDOUT,
                                               timeout=options['timeout']).decode()
            except subprocess.CalledProcessError as error:
                logger.info('{} ssh reboot with command {} failed: {}'.format(hostname, reboot_cmd, error))

        raise CommandError('{} All ssh reboot commands failed.'.format(hostname))
    def handle(self, host, *args, **options):
        validate_host(host)

        call_args = [
            'set -e; ret=0; out=$(ping',
            '-q',  # only print summary lines
            '-c',
            str(options['count']),
            '-w',
            str(options['timeout']),
            host,
            ' 2>&1) || ret=$?; echo $out|tail -2|tr \'\n\' \'\t\'; exit $ret',
        ]

        return subprocess.check_output(' '.join(call_args),
                                       stderr=subprocess.STDOUT,
                                       encoding='utf-8',
                                       shell=True,
                                       timeout=(2 + options['timeout']))