summaryrefslogtreecommitdiffstats
path: root/cli-tools/cura-power.py
blob: 4a22d38592ce35d999a1023abe6043344fd8ecf9 (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
73
74
#!/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 sys

from cura.cura_options import CuraBasicOptions
from cura.cura_address import CuraIpv4Addr, CuraIpv4AddrGenerator
from cura.cura_client_power import CuraPowerClient

class CuraPowerOptions(CuraBasicOptions):
    def __init__(self):
        super(self.__class__, self).__init__(
            "Available actions:\n"
            "  poweroff, reboot, suspend, hibernate\n"
            "  force-poweroff, force-reboot\n")
        self.m_parser.set_usage("Usage %prog [options] action")

    @property
    def good(self):
        return len(self.m_pos_options) == 1

    @property
    def action(self):
        return self.m_pos_options[0] if self.good and self.m_pos_options[0] else ""

if __name__ == "__main__":
    options = CuraPowerOptions()
    options.parse(sys.argv)
    if not options.good:
        sys.stderr.write(
            "Wrong tool usage. Run " + __file__ +
            " --help for detailed help.\n")
        sys.exit(1)

    client_failed = False
    client_hostnames = CuraIpv4AddrGenerator(options.hostname).enumerate()
    client_action = options.action.lower()
    for client_hostname in client_hostnames:
        if not len(client_hostname):
            continue
        client = CuraPowerClient(client_hostname, options.username, options.password)
        actions = {
            "reboot": (client.reboot, "Reboot: "),
            "poweroff": (client.poweroff, "Poweroff: "),
            "suspend": (client.suspend, "Suspend: "),
            "hibernate": (client.hibernate, "Hibernate: ")
        }

        if not client_action in actions:
            sys.stdout.write("No such action to perform!\n")
            sys.exit(1)

        (rval, rparam) = actions[client_action][0]()
        if rval:
            sys.stdout.write("%s: %s\n" % (client_hostname,
                actions[client_action][1] + rparam if rparam else "ok"))
        else:
            sys.stderr.write("%s: %s\n" % (client_hostname, rparam))
            client_failed = True

    sys.exit(client_failed)