Rename pharos-dashboard and pharos-validator
[pharos-tools.git] / validator / src / validation_tool / src / ipmi.py
1 ##############################################################################
2 # Copyright (c) 2015 Todd Gaunt and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import subprocess
12 import logging
13
14 def power_nodes(nodes, action):
15     """ Attempts to power on all nodes specified in a list, then returns a list
16     of the names of all failures. The list will be empty if no failures."""
17     failed_nodes = []
18     logger = logging.getLogger(__name__)
19     if not nodes:
20         logger.info("No nodes, is empty list")
21     for node in nodes:
22         # -I flag must be 'lanplus', 'lan' by itself doesn't work with
23         # the most recent idrac/ipmi version
24         if action == "on":
25             pass
26         elif action == "off":
27             pass
28         else:
29             logger.error("Invalid ipmi command")
30
31         cmd = ["ipmitool", \
32                   "-I", "lanplus", \
33                   "-H ", "'"+node.ipmi_addr+"'", \
34                   "-U ", "'"+node.ipmi_user+"'", \
35                   "-P ", "'"+node.ipmi_pass+"'", \
36                   "power", action]
37
38         logger.debug("Running: \"{}\"".format(' '.join(cmd)))
39         try:
40             with open(os.devnull, 'w') as fn:
41                 status = subprocess.check_call(" ".join(cmd), \
42                         stdout=fn, stderr=fn, shell=True)
43         except subprocess.CalledProcessError as e:
44             status = e.returncode
45             logger.error("{} could not be accessed at {} (exit code {})".format(\
46                     node.name, node.ipmi_addr, status))
47             failed_nodes.append(node.name)
48         if int(status) == 0:
49             logger.info("{} successfully powered {}".format(node.name, action))
50
51     return failed_nodes
52
53 def status(node, ipaddr, username, passwd):
54     # -I flag must be 'lanplus', 'lan' by itself doesn't work with
55     # the most recent idrac/ipmi version
56     chkcmd = ["ipmitool", \
57               "-I", "lanplus", \
58               "-H", ipaddr, \
59               "-U", username, \
60               "-P", passwd, \
61               "chassis", "status"]
62     print(chkcmd)
63     subprocess.Popen(chkcmd)