summaryrefslogtreecommitdiffstats
path: root/cli-tools/cura/cura_client_power.py
blob: c2e7b271918b0bd9978f1548dbf0fa9995daac0c (plain)
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
#!/usr/bin/env python
# Copyright (C) 2012  Peter Hatina <phatina@redhat.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

import pywbem

class CuraPowerClient():
    POWER_CLASS_NAME = "LMI_PowerManagementService"
    POWER_STATE_SUSPEND = 4
    POWER_STATE_FORCE_REBOOT = 5
    POWER_STATE_HIBERNATE = 7
    POWER_STATE_FORCE_POWEROFF = 8
    POWER_STATE_POWEROFF = 12
    POWER_STATE_REBOOT = 15

    def __init__(self, hostname, username="", password=""):
        self.m_hostname = hostname
        self.m_username = username
        self.m_password = password
        self.m_cliconn = pywbem.WBEMConnection("https://" + self.m_hostname,
            (self.m_username, self.m_password))

    def __getPowerInstance(self):
        try:
            inst_name_list = self.m_cliconn.EnumerateInstanceNames(
                    CuraPowerClient.POWER_CLASS_NAME)
        except pywbem.cim_operations.CIMError, e:
            return (None, e.args[1])
        except pywbem.cim_http.AuthError, e:
            return (None, e.args[0])
        inst_list = self.m_cliconn.GetInstance(inst_name_list[0],
            LocalOnly=False)
        return (inst_list, "")

    def __powerCallMethod(self, method, **params):
        (inst, rparam) = self.__getPowerInstance()
        if not inst:
            return (False, rparam)
        try:
            (rval, rparams) = self.m_cliconn.InvokeMethod(method,
                inst.path, **params)
        except pywbem.cim_operations.CIMError, e:
            return (False, e.args[1])
        return (rval in (0, 4096), "")

    def poweroff(self):
        return self.__powerCallMethod("RequestPowerStateChange",
            PowerState=pywbem.Uint16(CuraPowerClient.POWER_STATE_POWEROFF))

    def reboot(self):
        return self.__powerCallMethod("RequestPowerStateChange",
            PowerState=pywbem.Uint16(CuraPowerClient.POWER_STATE_REBOOT))

    def suspend(self):
        return self.__powerCallMethod("RequestPowerStateChange",
            PowerState=pywbem.Uint16(CuraPowerClient.POWER_STATE_SUSPEND))

    def hibernate(self):
        return self.__powerCallMethod("RequestPowerStateChange",
            PowerState=pywbem.Uint16(CuraPowerClient.POWER_STATE_HIBERNATE))