-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfailover.py
153 lines (124 loc) · 4.94 KB
/
failover.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python
"""
Author: Kapil Arora
Github: @kapilarora
"""
from solidfire_client import SFClient
from k8s_client import K8SClient
from kubernetes.client.rest import ApiException
import os
import time
from time import strftime
import logging
def main():
k8s_config = os.environ['KUBECONFIG']
ip = os.environ['SF_IP']
username = os.environ['SF_USERNAME']
password = os.environ['SF_PASSWORD']
target_portal = os.environ['SF_TARGET_PORTAL']
no_execute_str = os.environ['NO_EXECUTE']
wait_time = os.environ['WAIT_TIME']
if no_execute_str.lower() == 'true':
no_execute = True
else:
no_execute = False
log_level_str = os.environ['LOG_LEVEL']
log_level = _get_Log_level(log_level_str)
log_filename = strftime("sf_failover_k8s_%Y%m%d%H%M%S.log")
logging.basicConfig(filename=log_filename,
level=log_level, format='%(asctime)s - %(levelname)s - %(message)s')
try:
wait_time += 1
except TypeError:
wait_time = 5
logging.info('Starting the failover script with following options:')
logging.info('KUBECONFIG: %s',k8s_config)
logging.info('SF_IP: %s', ip)
logging.info('SF_USERNAME: %s', username)
logging.info('SF_PASSWORD: %s', password)
logging.info('SF_TARGET_PORTAL: %s', target_portal)
logging.info('WAIT_TIME: %s', wait_time)
if no_execute:
logging.info('This is just a dry run!')
sf = SFClient(ip, username, password)
k8s = K8SClient(k8s_config)
pvs = k8s.get_all_pvs()
logging.info('Getting IDs and IQNs for all iSCSI Volumes (PVCs) from SolidFire')
vol_ids = {}
vol_iqns= {}
for pv in pvs.items:
pv_name = pv.metadata.name
logging.info('Processing PV %s', pv_name)
if pv.spec.iscsi is not None:
logging.info('Verified PV %s is an iSCSI volume', pv_name)
vol = sf.get_volume(pv_name)
vol_id = vol.volume_id
vol_ids[pv_name] = vol_id
vol_iqn = vol.iqn
vol_iqns[pv_name] = vol_iqn
logging.info('Step 1: Starting removal of all volume pairs')
for pv_name in vol_ids:
# remove volume pair
vol_id = vol_ids[pv_name]
if no_execute:
logging.info('Not Removing volume pair for volume %s with vol id: %s as this is a dry run.',
pv_name, vol_id)
else:
logging.info('Removing volume pair for volume %s with vol id: %s', pv_name, vol_id)
sf.remove_volume_pair(vol_id)
logging.info('Remove Volume pair started', )
logging.info('Waiting for %s seconds before next step.',wait_time)
time.sleep(wait_time)
logging.info('Step 2: Starting changing all volume access to readWrite')
for pv_name in vol_ids:
vol_id = vol_ids[pv_name]
# change volume access type
if no_execute:
logging.info(
'Not Changing volume access type to readWrite for volume %s with vol id: %s as this is a dry run.',
pv_name, vol_id)
else:
logging.info('Changing volume access type to readWrite for volume %s with vol id: %s .',
pv_name, vol_id)
sf.modify_volume_access(vol_id, 'readWrite')
logging.info('Modified volume accesstype to readWrite', )
logging.info('Waiting for %s seconds before next step.', wait_time)
time.sleep(wait_time)
logging.info('Step 3: Starting Updating PVs with new IQN and target portal')
for pv in pvs.items:
pv_name = pv.metadata.name
logging.info('Processing PV %s', pv_name)
if pv.spec.iscsi is not None:
logging.info('Verified PV %s is an iSCSI volume', pv_name)
vol_iqn = vol_iqns[pv_name]
#update pv
logging.info('Changing iqn for PV from %s to %s', pv.spec.iscsi.iqn, vol_iqn)
pv.spec.iscsi.iqn = vol_iqn
logging.info('Changing target_portal for PV from %s to %s', pv.spec.iscsi.target_portal, target_portal)
pv.spec.iscsi.target_portal = target_portal
try:
if no_execute:
logging.info('Not Updating PV %s as this a dry run', pv_name)
else:
logging.info('Updating PV %s', pv_name)
pv_updated = k8s.update_pv(pv_name, pv)
logging.debug('Returned Updated PV %s', pv_updated)
except ApiException as e:
print("Exception when calling CoreV1Api->patch_persistent_volume: %s\n" % e)
logging.info('Failover script completed.')
def _get_Log_level(log_level):
'''
:param log_level: str info/error/warn/debug
:return: logging.level
'''
if log_level == 'info':
level = logging.INFO
elif log_level == 'error':
level = logging.ERROR
elif log_level == 'warn':
level = logging.WARN
else:
level = logging.DEBUG
return level
if __name__ == '__main__':
main()