This file is indexed.

/usr/lib/python3/dist-packages/provisioningserver/drivers/power/hmc.py is in python3-maas-provisioningserver 2.0.0~beta3+bzr4941-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
# Copyright 2015-2016 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""HMC Power Driver.

Support for managing lpars via the IBM Hardware Management Console (HMC).
This module provides support for interacting with IBM's HMC via SSH.
"""

__all__ = []

from socket import error as SOCKETError

from paramiko import (
    AutoAddPolicy,
    SSHClient,
    SSHException,
)
from provisioningserver.drivers.power import (
    PowerActionError,
    PowerConnError,
    PowerDriver,
    PowerFatalError,
)


class HMCState:
    OFF = ('Shutting Down', 'Not Activated')
    ON = ('Starting', 'Running', 'Open Firmware')


class HMCPowerDriver(PowerDriver):

    name = 'hmc'
    description = "IBM Hardware Management Console Power Driver."
    settings = []

    def detect_missing_packages(self):
        # uses pure-python paramiko ssh client - nothing to look for!
        return []

    def run_hmc_command(self, command, power_address=None, power_user=None,
                        power_pass=None, **extra):
        """Run a single command on HMC via SSH and return output."""
        try:
            ssh_client = SSHClient()
            ssh_client.set_missing_host_key_policy(AutoAddPolicy())
            ssh_client.connect(
                power_address, username=power_user, password=power_pass)
            _, stdout, _ = ssh_client.exec_command(command)
            output = stdout.read().decode('utf-8')
        except (SSHException, EOFError, SOCKETError) as e:
            raise PowerConnError(
                "Could not make SSH connection to HMC for "
                "%s on %s - %s" % (power_user, power_address, e))
        finally:
            ssh_client.close()

        return output

    def power_on(self, system_id, context):
        """Power on HMC lpar."""
        if self.power_query(system_id, context) in HMCState.ON:
            self.power_off(system_id, context)
        try:
            # Power lpar on
            self.run_hmc_command(
                "chsysstate -r lpar -m %s -o on -n %s --bootstring network-all"
                % (context['server_name'], context['lpar']))
        except PowerConnError as e:
            raise PowerActionError(
                "HMC Power Driver unable to power on lpar %s: %s"
                % (context['lpar'], e))

    def power_off(self, system_id, context):
        """Power off HMC lpar."""
        try:
            # Power lpar off
            self.run_hmc_command(
                "chsysstate -r lpar -m %s -o shutdown -n %s --immed"
                % (context['server_name'], context['lpar']))
        except PowerConnError as e:
            raise PowerActionError(
                "HMC Power Driver unable to power off lpar %s: %s"
                % (context['lpar'], e))

    def power_query(self, system_id, context):
        """Power query HMC lpar."""
        try:
            # Power query lpar
            power_state = self.run_hmc_command(
                "lssyscfg -m %s -r lpar -F state --filter lpar_names=%s"
                % (context['server_name'], context['lpar']))
        except PowerConnError as e:
            raise PowerActionError(
                "HMC Power Driver unable to power query lpar %s: %s"
                % (context['lpar'], e))
        else:
            if power_state in HMCState.OFF:
                return 'off'
            elif power_state in HMCState.ON:
                return 'on'
            else:
                raise PowerFatalError(
                    "HMC Power Driver retrieved unknown power state %s"
                    " for lpar %s" % (power_state, context['lpar']))