-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprotectFlashblade.py
216 lines (197 loc) · 7.71 KB
/
protectFlashblade.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env python
"""Protect FlashBlade Volumes"""
# usage:
# ./protectFlashblade.py -v mycluster \
# -u myuser \
# -d mydomain.net \
# -p 'My Policy' \
# -j 'My New Job' \
# -f flashblad01 \
# -vol volume1 \
# -t 'America/New_York' \
# -ei \
# -s '00:00' \
# -is 180 \
# -c
# import pyhesity wrapper module
from pyhesity import *
### command line arguments
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--vip', type=str, required=True) # Cohesity cluster name or IP
parser.add_argument('-u', '--username', type=str, required=True) # Cohesity Username
parser.add_argument('-d', '--domain', type=str, default='local') # Cohesity User Domain
parser.add_argument('-j', '--jobname', type=str, required=True) # name of protection job
parser.add_argument('-p', '--policyname', type=str) # name of protection policy
parser.add_argument('-s', '--starttime', type=str, default='20:00') # job start time
parser.add_argument('-i', '--include', action='append', type=str) # include path
parser.add_argument('-n', '--includefile', type=str) # include path file
parser.add_argument('-e', '--exclude', action='append', type=str) # exclude path
parser.add_argument('-x', '--excludefile', type=str) # exclude path file
parser.add_argument('-t', '--timezone', type=str, default='America/New_York') # timezone for job
parser.add_argument('-is', '--incrementalsla', type=int, default=60) # incremental SLA minutes
parser.add_argument('-fs', '--fullsla', type=int, default=120) # full SLA minutes
parser.add_argument('-ei', '--enableindexing', action='store_true') # enable indexing
parser.add_argument('-f', '--flashbladesource', type=str, required=True)
parser.add_argument('-vol', '--volumename', action='append', type=str)
parser.add_argument('-l', '--volumelist', type=str)
parser.add_argument('-c', '--cloudarchivedirect', action='store_true') # cloud archive direct
parser.add_argument('-sd', '--storagedomain', type=str, default='DefaultStorageDomain') # storage domain
args = parser.parse_args()
vip = args.vip
username = args.username
domain = args.domain
jobname = args.jobname
policyname = args.policyname
starttime = args.starttime
timezone = args.timezone
includes = args.include
includefile = args.includefile
excludes = args.exclude
excludefile = args.excludefile
incrementalsla = args.incrementalsla
fullsla = args.fullsla
enableindexing = args.enableindexing
volumes = args.volumename
volumelist = args.volumelist
cloudarchivedirect = args.cloudarchivedirect
storagedomain = args.storagedomain
flashbladesource = args.flashbladesource
# cloud archive direct storage domain
if cloudarchivedirect:
storagedomain = 'Direct_Archive_Viewbox'
# indexing
disableindexing = True
if enableindexing is True:
disableindexing = False
# parse starttime
try:
(hour, minute) = starttime.split(':')
except Exception:
print('starttime is invalid!')
exit(1)
# gather includes
if includes is None:
includes = []
if includefile is not None:
f = open(includefile, 'r')
includes += [e.strip() for e in f.readlines() if e.strip() != '']
f.close()
if len(includes) == 0:
includes += '/'
# gather excludes
if excludes is None:
excludes = []
if excludefile is not None:
f = open(excludefile, 'r')
excludes += [e.strip() for e in f.readlines() if e.strip() != '']
f.close()
# gather volumes
if volumes is None:
volumes = []
if volumelist is not None:
f = open(volumelist, 'r')
volumes += [e.strip() for e in f.readlines() if e.strip() != '']
f.close()
if len(volumes) == 0:
print('No volumes specified!')
exit(1)
if cloudarchivedirect and len(volumes) > 1:
print('Cloud Archive Direct jobs are limited to a single volume')
exit(1)
# authenticate
apiauth(vip, username, domain)
# find storage domain
sd = [sd for sd in api('get', 'viewBoxes') if sd['name'].lower() == storagedomain.lower()]
if len(sd) < 1:
print("Storage domain %s not found!" % storagedomain)
exit(1)
sdid = sd[0]['id']
# get flashblade source
sources = api('get', 'protectionSources?environments=kFlashBlade')
flashblade = [s for s in sources if s['protectionSource']['name'].lower() == flashbladesource.lower()]
if len(flashblade) < 1:
print('FlashBlade %s not registered in Cohesity' % flashbladesource)
exit(1)
else:
flashblade = flashblade[0]
parentId = flashblade['protectionSource']['id']
# gather source ids for volumes
sourceids = []
if len(volumes) > 0:
if cloudarchivedirect and len(volumes) > 1:
print("Cloud Archive Direct jobs are limited to a single volume")
exit(1)
sourceVolumes = [n for n in flashblade['nodes'] if n['protectionSource']['name'].lower() in [v.lower() for v in volumes]]
sourceVolumeNames = [n['protectionSource']['name'] for n in sourceVolumes]
sourceIds = [n['protectionSource']['id'] for n in sourceVolumes]
missingVolumes = [v for v in volumes if v not in sourceVolumeNames]
if len(missingVolumes) > 0:
print("Volumes %s not found" % ', '.join(missingVolumes))
exit(1)
elif cloudarchivedirect:
print("Cloud Archive Direct jobs are limited to a single volume")
exit(1)
else:
sourceIds.append(parentId)
# new or existing job
job = [j for j in api('get', 'protectionJobs?environments=kFlashBlade&isActive=true&isDeleted=false') if j['name'].lower() == jobname.lower()]
if len(job) < 1:
# find protectionPolicy
if policyname is None:
print('Policy name required for new job')
exit(1)
policy = [p for p in api('get', 'protectionPolicies') if p['name'].lower() == policyname.lower()]
if len(policy) < 1:
print("Policy '%s' not found!" % policyname)
exit(1)
policyid = policy[0]['id']
jobparams = {
'name': jobname,
'description': '',
'environment': 'kFlashBlade',
'policyId': policyid,
'viewBoxId': sdid,
'parentSourceId': parentId,
'sourceIds': sourceIds,
'startTime': {
'hour': int(hour),
'minute': int(minute)
},
'timezone': timezone,
'incrementalProtectionSlaTimeMins': incrementalsla,
'fullProtectionSlaTimeMins': fullsla,
'priority': 'kMedium',
'alertingPolicy': [
'kFailure'
],
'indexingPolicy': {
'disableIndexing': disableindexing,
'allowPrefixes': [
'/'
]
},
'abortInBlackoutPeriod': False,
'qosType': 'kBackupHDD',
'environmentParameters': {
'nasParameters': {
'nasProtocol': 'kNfs3',
'continueOnError': True,
'filePathFilters': {
'protectFilters': includes,
'excludeFilters': excludes
}
}
},
'isDirectArchiveEnabled': cloudarchivedirect,
}
print('Creating protection job %s...' % jobname)
result = api('post', 'protectionJobs', jobparams)
else:
if cloudarchivedirect:
print('Cloud Archive Direct jobs are limited to a single volume')
exit(1)
print('Updating protection job %s...' % jobname)
job[0]['sourceIds'] += sourceids
job[0]['sourceIds'] = list(set(job[0]['sourceIds']))
result = api('put', 'protectionJobs/%s' % job[0]['id'], job[0])