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