forked from elacerda/t80s-ibootpdu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t80s-ibootpdu.py
90 lines (76 loc) · 3.07 KB
/
t80s-ibootpdu.py
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
import os
import sys
import argparse as ap
from os.path import basename
from iboot_pdu import iboot, print_level
__script_name__ = basename(sys.argv[0])
class readFileArgumentParser(ap.ArgumentParser):
def __init__(self, *args, **kwargs):
super(readFileArgumentParser, self).__init__(*args, **kwargs)
def convert_arg_line_to_args(self, line):
for arg in line.split():
if not arg.strip():
continue
if arg[0] == '#':
break
yield arg
def parse_arguments(default_args_file=None):
parser = readFileArgumentParser(fromfile_prefix_chars='@')
parser.add_argument('--iboot_ip', '-I', type=str, default=None, help='T80-South iBoot PDU I.P. number')
parser.add_argument('--fanwest', '-W', action='store_true', default=False, help='Control FanWest outlet')
parser.add_argument('--faneast', '-E', action='store_true', default=False, help='Control FanEast outlet')
parser.add_argument('--filterwheel', '-F', action='store_true', default=False, help='Control FilterWheel outlet')
parser.add_argument('--token', '-T', type=str, default=None, help='Access token: XXXX-XXXX-XXXX-XXXX')
parser.add_argument('--on', action='store_true', default=False, help='Turn on requested outlet')
parser.add_argument('--off', action='store_true', default=False, help='Turn off requested outlet')
parser.add_argument('--cycle', action='store_true', default=False, help='Cycle requested outlet')
parser.add_argument('--verbose', '-v', action='count', default=0)
args_list = sys.argv[1:]
args = parser.parse_args(args=args_list)
# IBOOT PDU I.P.
if args.iboot_ip is None:
print_level('missing --iboot_ip. Trying to read from enviroment...')
env_ip = os.environ.get('T80S_IBOOT_IP', None)
if env_ip is None:
parser.error('missing T80S_IBOOT_IP enviroment variable...')
else:
print_level('Done', 1, args.verbose)
args.iboot_ip = env_ip
# ACTION
err_msg = 'You must choose --on, --off or --cycle'
if (args.on or args.off or args.cycle):
if ((not args.on or (args.off or args.cycle))
and (not args.off or (args.cycle or args.on))
and (not args.cycle or (args.on or args.off))):
parser.error(err_msg)
# OUTLETS
args.outlets = []
if args.fanwest:
args.outlets.append('fanwest')
if args.faneast:
args.outlets.append('faneast')
if args.filterwheel:
args.outlets.append('filterwheel')
if len(args.outlets) == 0:
args.outlets = None
args.action = None
if args.on:
args.action = 'on'
if args.off:
args.action = 'off'
if args.cycle:
args.action = 'cycle'
return args
if __name__ == '__main__':
args = parse_arguments()
control = iboot(
ip=args.iboot_ip,
outlets=args.outlets,
action=args.action,
token=args.token,
verbose=args.verbose,
)
if control.action is None:
control.get_status()
else:
control.control_outlets()