-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprotectIsilon.py
274 lines (246 loc) · 10.3 KB
/
protectIsilon.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
"""Add Isilon volumes to a Protection Job Using Python"""
### import pyhesity wrapper module
from pyhesity import *
### command line arguments
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--vip', type=str, required=True)
parser.add_argument('-u', '--username', type=str, required=True)
parser.add_argument('-d', '--domain', type=str, default='local')
parser.add_argument('-k', '--useApiKey', action='store_true')
parser.add_argument('-pwd', '--password', type=str, default=None)
parser.add_argument('-s', '--sourcename', type=str, required=True)
parser.add_argument('-z', '--zonename', action='append', type=str)
parser.add_argument('-n', '--volumename', action='append', type=str)
parser.add_argument('-l', '--volumelist', type=str)
parser.add_argument('-j', '--jobname', type=str, required=True)
parser.add_argument('-i', '--include', action='append', type=str)
parser.add_argument('-f', '--includefile', type=str)
parser.add_argument('-e', '--exclude', action='append', type=str)
parser.add_argument('-x', '--excludefile', type=str)
parser.add_argument('-sd', '--storagedomain', type=str, default='DefaultStorageDomain')
parser.add_argument('-p', '--policyname', type=str, default=None)
parser.add_argument('-tz', '--timezone', type=str, default='US/Eastern')
parser.add_argument('-st', '--starttime', type=str, default='21:00')
parser.add_argument('-is', '--incrementalsla', type=int, default=60)
parser.add_argument('-fs', '--fullsla', type=int, default=120)
parser.add_argument('-ei', '--enableindexing', action='store_true')
parser.add_argument('-a', '--pause', action='store_true')
parser.add_argument('-c', '--cloudarchivedirect', action='store_true')
parser.add_argument('-ip', '--incrementalsnapshotprefix', type=str, default=None)
parser.add_argument('-fp', '--fullsnapshotprefix', type=str, default=None)
parser.add_argument('-enc', '--encryptionenabled', action='store_true')
args = parser.parse_args()
vip = args.vip # cluster name/ip
username = args.username # username to connect to cluster
domain = args.domain # domain of username (e.g. local, or AD domain)
password = args.password # password or API key
useApiKey = args.useApiKey # use API key for authentication
sourcename = args.sourcename # name of registered isilon
zonenames = args.zonename # names of zones to protect
volumenames = args.volumename # namea of volumes to protect
volumelist = args.volumelist # file with volume names
jobname = args.jobname # name of protection job to add server to
includes = args.include # include path
includefile = args.includefile # file with include paths
excludes = args.exclude # exclude path
excludefile = args.excludefile # file with exclude paths
storagedomain = args.storagedomain # storage domain for new job
policyname = args.policyname # policy name for new job
starttime = args.starttime # start time for new job
timezone = args.timezone # time zone for new job
incrementalsla = args.incrementalsla # incremental SLA for new job
fullsla = args.fullsla # full SLA for new job
enableindexing = args.enableindexing # enable indexing on new job
pause = args.pause # pause new job
cloudarchivedirect = args.cloudarchivedirect # enable cloud archive direct
incrementalsnapshotprefix = args.incrementalsnapshotprefix # incremental snapshot prefix
fullsnapshotprefix = args.fullsnapshotprefix # full snapshot prefux
encryptionenabled = args.encryptionenabled # encryption enabled
if pause:
isPaused = True
else:
isPaused = False
if cloudarchivedirect:
isCAD = True
else:
isCAD = False
if encryptionenabled:
encrypt = True
else:
encrypt = False
# zone names
if zonenames is None:
zonenames = []
else:
zonenames = [z.lower() for z in zonenames]
# read server file
if volumenames is None:
volumenames = []
if volumelist is not None:
f = open(volumelist, 'r')
volumenames += [s.strip() for s in f.readlines() if s.strip() != '']
f.close()
volumenames = [v.lower() for v in volumenames]
# read include file
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()
# read exclude file
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()
# authenticate to Cohesity
apiauth(vip=vip, username=username, domain=domain, password=password, useApiKey=useApiKey)
# get isilon source
sources = api('get', 'protectionSources?environment=kIsilon')
source = [s for s in sources if s['protectionSource']['name'].lower() == sourcename.lower()]
if source is None or len(source) == 0:
print('Isilion %s not found!' % sourcename)
exit(1)
else:
source = source[0]
# get object IDs to protect
objectids = []
foundVolumes = []
for zone in source['nodes']:
if len(zonenames) == 0 or zone['protectionSource']['name'].lower() in zonenames:
for volume in zone['nodes']:
if len(volumenames) == 0 or volume['protectionSource']['name'].lower() in volumenames:
objectids.append(volume['protectionSource']['id'])
foundVolumes.append(volume['protectionSource']['name'].lower())
# warn on missing volumes
for volume in volumenames:
if volume not in foundVolumes:
print('volume %s not found!' % volume)
exit(1)
# get job info
newJob = False
protectionGroups = api('get', 'data-protect/protection-groups?isDeleted=false&isActive=true', v=2)
jobs = protectionGroups['protectionGroups']
job = [job for job in jobs if job['name'].lower() == jobname.lower()]
if not job or len(job) < 1:
newJob = True
print("Job '%s' not found. Creating new job" % jobname)
# 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']
# 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']
# parse starttime
try:
(hour, minute) = starttime.split(':')
hour = int(hour)
minute = int(minute)
if hour < 0 or hour > 23 or minute < 0 or minute > 59:
print('starttime is invalid!')
exit(1)
except Exception:
print('starttime is invalid!')
exit(1)
job = {
"name": jobname,
"environment": "kIsilon",
"isPaused": isPaused,
"policyId": policyid,
"priority": "kMedium",
"description": "",
"startTime": {
"hour": hour,
"minute": minute,
"timeZone": timezone
},
"abortInBlackouts": False,
"alertPolicy": {
"backupRunStatus": [
"kFailure"
],
"alertTargets": []
},
"sla": [
{
"backupRunType": "kFull",
"slaMinutes": fullsla
},
{
"backupRunType": "kIncremental",
"slaMinutes": incrementalsla
}
],
"qosPolicy": "kBackupHDD",
"isilonParams": {
"objects": [],
"excludeObjectIds": [],
"directCloudArchive": isCAD,
"nativeFormat": True,
"indexingPolicy": {
"enableIndexing": enableindexing,
"includePaths": [
"/"
],
"excludePaths": []
},
"protocol": "kNfs3",
"continueOnError": True,
"useChangelist": False,
"encryptionEnabled": encrypt
}
}
if not cloudarchivedirect:
job['storageDomainId'] = sdid
if incrementalsnapshotprefix is not None or fullsnapshotprefix is not None:
if incrementalsnapshotprefix is not None and fullsnapshotprefix is None:
fullsnapshotprefix = incrementalsnapshotprefix
if fullsnapshotprefix is not None and incrementalsnapshotprefix is None:
incrementalsnapshotprefix = fullsnapshotprefix
job['isilonParams']['snapshotLabel'] = {
"incrementalLabel": incrementalsnapshotprefix,
"fullLabel": fullsnapshotprefix
}
else:
print('Updating job %s' % jobname)
job = job[0]
# add objects to job
existingObjects = [o['id'] for o in job['isilonParams']['objects']]
for objectid in objectids:
if objectid not in existingObjects:
job['isilonParams']['objects'].append({"id": objectid})
existingObjects.append(objectid)
# add includes and excludes
if len(includes) > 0 or len(excludes) > 0:
if 'fileFilters' not in job['isilonParams']:
job['isilonParams']['fileFilters'] = {}
if 'includeList' not in job['isilonParams']['fileFilters']:
job['isilonParams']['fileFilters']['includeList'] = []
if len(includes) == 0:
job['isilonParams']['fileFilters']['includeList'].append('/')
if 'excludeList' not in job['isilonParams']['fileFilters']:
job['isilonParams']['fileFilters']['excludeList'] = []
for include in includes:
if include not in job['isilonParams']['fileFilters']['includeList']:
job['isilonParams']['fileFilters']['includeList'].append(include)
for exclude in excludes:
if exclude not in job['isilonParams']['fileFilters']['excludeList']:
job['isilonParams']['fileFilters']['excludeList'].append(exclude)
# update job
if newJob is True:
result = api('post', 'data-protect/protection-groups', job, v=2)
else:
result = api('put', 'data-protect/protection-groups/%s' % job['id'], job, v=2)