-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipmi.py
92 lines (78 loc) · 2.5 KB
/
ipmi.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
91
'''
Wrapper around IPMI for simple machine management
'''
import subprocess
from datetime import datetime
from dao import DAO
class lom_ipmi():
'''
Fluent API to build the IPMI command. Only a subset of the commands are used.
'''
def __init__(self):
self.interface = "ipmitools"
self.intf_type = "lanplus"
def _check_power(self, power_arg):
'''
Method to check if a correct power option has been given
'''
if power_arg in ["on", "off", "cycle"]:
return True
def _check_ip(self, ip):
'''
Method to check if an IP quad is valid
'''
try:
if int(ip) > 0 and int(ip) < 254:
return True
except ValueError as ve:
print (ve)
def interface_type (self, intf_type="lanplus"):
'''
Set the interface type
'''
self.intf_type = intf_type
return self
def connection_auth(self, user, passwd):
'''
Sets up the connection parameters.
'''
self.user = user
self.passwd = passwd
return self
def command(self, cmd):
'''
Sets up the commands.
Currently limited to the power command.
'''
if self._check_power(cmd):
self.cmd = "chassis power " + cmd
return self
def host(self, config, ip):
'''
Checks the ip is valid and adds to the line
'''
if self._check_ip(ip):
self.ip = config['base'] + str(ip)
return self
def run_command(self, config):
'''
Runs the command via a subprocess
'''
dao = DAO(config)
try:
dao.put_log(str(datetime.now()) + " " + self.build_command())
p = subprocess.check_output(self.interface + " " + self.build_command(), shell=True, stderr=subprocess.STDOUT)
dao.put_log(str(datetime.now()) + " " + p)
except subprocess.CalledProcessError as cpe:
dao.put_log(str(datetime.now()) + " " + cpe.cmd)
dao.put_log(str(datetime.now()) + " " + cpe.output)
def build_command(self):
'''
Builds the Command line
'''
if not self.ip:
raise Exception("IP not given")
if not self.cmd:
raise Exception("Command not given")
return ' '.join([" -U", self.user, "-P", self.passwd, "-vI", self.intf_type,
"-H", self.ip, self.cmd ])