Merge "docs: Mitaka Build req: Add p7zip-full."
[fuel.git] / deploy / dha_adapters / ipmi_adapter.py
1 ###############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
3 # szilard.cserey@ericsson.com
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
11 import time
12 from hardware_adapter import HardwareAdapter
13
14 from common import (
15     log,
16     exec_cmd,
17     err,
18 )
19
20
21 class IpmiAdapter(HardwareAdapter):
22
23     def __init__(self, yaml_path):
24         super(IpmiAdapter, self).__init__(yaml_path)
25
26     def get_access_info(self, node_id):
27         ip = self.get_node_property(node_id, 'ipmiIp')
28         username = self.get_node_property(node_id, 'ipmiUser')
29         password = self.get_node_property(node_id, 'ipmiPass')
30         return ip, username, password
31
32     def ipmi_cmd(self, node_id):
33         ip, username, password = self.get_access_info(node_id)
34         cmd = 'ipmitool -I lanplus -A password'
35         cmd += ' -H %s -U %s -P %s' % (ip, username, password)
36         return cmd
37
38     def get_node_pxe_mac(self, node_id):
39         mac_list = []
40         mac_list.append(self.get_node_property(node_id, 'pxeMac').lower())
41         return mac_list
42
43     def node_power_on(self, node_id):
44         WAIT_LOOP = 200
45         SLEEP_TIME = 3
46         log('Power ON Node %s' % node_id)
47         cmd_prefix = self.ipmi_cmd(node_id)
48         state = exec_cmd('%s chassis power status' % cmd_prefix)
49         if state == 'Chassis Power is off':
50             exec_cmd('%s chassis power on' % cmd_prefix)
51             done = False
52             for i in range(WAIT_LOOP):
53                 state, _ = exec_cmd('%s chassis power status' % cmd_prefix,
54                                     False)
55                 if state == 'Chassis Power is on':
56                     done = True
57                     break
58                 else:
59                     time.sleep(SLEEP_TIME)
60             if not done:
61                 err('Could Not Power ON Node %s' % node_id)
62
63     def node_power_off(self, node_id):
64         WAIT_LOOP = 200
65         SLEEP_TIME = 3
66         log('Power OFF Node %s' % node_id)
67         cmd_prefix = self.ipmi_cmd(node_id)
68         state = exec_cmd('%s chassis power status' % cmd_prefix)
69         if state == 'Chassis Power is on':
70             done = False
71             exec_cmd('%s chassis power off' % cmd_prefix)
72             for i in range(WAIT_LOOP):
73                 state, _ = exec_cmd('%s chassis power status' % cmd_prefix,
74                                     False)
75                 if state == 'Chassis Power is off':
76                     done = True
77                     break
78                 else:
79                     time.sleep(SLEEP_TIME)
80             if not done:
81                 err('Could Not Power OFF Node %s' % node_id)
82
83     def node_reset(self, node_id):
84         WAIT_LOOP = 600
85         log('RESET Node %s' % node_id)
86         cmd_prefix = self.ipmi_cmd(node_id)
87         state = exec_cmd('%s chassis power status' % cmd_prefix)
88         if state == 'Chassis Power is on':
89             was_shut_off = False
90             done = False
91             exec_cmd('%s chassis power reset' % cmd_prefix)
92             for i in range(WAIT_LOOP):
93                 state, _ = exec_cmd('%s chassis power status' % cmd_prefix,
94                                     False)
95                 if state == 'Chassis Power is off':
96                     was_shut_off = True
97                 elif state == 'Chassis Power is on' and was_shut_off:
98                     done = True
99                     break
100                 time.sleep(1)
101             if not done:
102                 err('Could Not RESET Node %s' % node_id)
103         else:
104             err('Cannot RESET Node %s because it\'s not Active, state: %s'
105                 % (node_id, state))
106
107     def node_set_boot_order(self, node_id, boot_order_list):
108         log('Set boot order %s on Node %s' % (boot_order_list, node_id))
109         boot_order_list.reverse()
110         cmd_prefix = self.ipmi_cmd(node_id)
111         for dev in boot_order_list:
112             if dev == 'pxe':
113                 exec_cmd('%s chassis bootdev pxe options=persistent'
114                          % cmd_prefix)
115             elif dev == 'iso':
116                 exec_cmd('%s chassis bootdev cdrom' % cmd_prefix)
117             elif dev == 'disk':
118                 exec_cmd('%s chassis bootdev disk options=persistent'
119                          % cmd_prefix)