-
Notifications
You must be signed in to change notification settings - Fork 1
/
snapomatic.credential
executable file
·296 lines (253 loc) · 11.3 KB
/
snapomatic.credential
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#! /usr/bin/python3
import sys
pythonversion=sys.version_info
if pythonversion[0] != 3 or (pythonversion[1] < 6 or pythonversion[1] > 11):
print("This script requires Python 3.6 through 3.11")
sys.exit(1)
sys.path.append(sys.path[0] + "/NTAPlib")
import os
import userio
import fileio
import doREST
import json
import requests
snapomaticversion='dev'
if os.getenv("SNAPOMATIC_CREDENTIAL_PATH") is not None:
configFile=os.getenv("SNAPOMATIC_CREDENTIAL_PATH")
elif os.name=='posix':
configFile='/etc/snapomatic/config.json'
elif os.name=='nt':
configFile='c:\snapomatic\config.json'
### main
if len(sys.argv) > 1:
if sys.argv[1] == "INIT":
if not fileio.forcemkdir(os.path.dirname(configFile),mode=700,quiet=True):
userio.fail("Unable to create directory " + os.path.dirname(configFile))
try:
open(configFile,'w').write("{\n}\n")
except:
userio.fail("Unable to initialize " + configFile)
userio.message("Credential cache initialized")
sys.exit(0)
else:
userio.fail("The only argument accepted is INIT")
svmgrid=[['SVM name','Mgmt Interface','Username','Password','Data Interface']]
dbgrid=[['Database','Username','Password']]
svmlist=[]
sidlist=[]
mgmtlist=[]
datalist=[]
if not os.path.isfile(configFile):
userio.fail("Unable to access " + configFile)
else:
try:
lines=open(configFile).readlines()
configjson=json.loads('\n'.join(lines))
except:
userio.fail("Unable to parse json data in " + configFile)
if 'ontap' in configjson.keys():
for svm in configjson['ontap']['svm']:
svmname=svm['name']
svmuuid=svm['uuid']
svmlist.append(svmname)
mgmt=svm['managementLIF']
mgmtlist.append(mgmt)
username=svm['username']
password=svm['password'][0] + ('X' * (len(svm['password'])-2)) + svm['password'][-1]
if 'dataLIF' in svm.keys():
datalifs=svm['dataLIF']
for lif in datalifs.split(','):
datalist.append(lif)
else:
datalif=''
svmgrid.append([svmname,mgmt,username,password,datalifs])
else:
configjson['ontap']={'svm':[]}
if 'oracle' in configjson.keys():
for sid in configjson['oracle']['sid']:
name=sid['name']
sidlist.append(name)
password=sid['password'][0] + ('X' * (len(svm['password'])-2)) + svm['password'][-1]
dbgrid.append([name,username])
else:
configjson['oracle']={'sid':[]}
menuoptions=['Manage ONTAP credentials','Manage Oracle databases']
credmode=userio.basicmenu(choices=menuoptions,nowait=True,returnnames=True)[0]
if credmode == 'Manage ONTAP credentials':
menuoptions=['List credentials','Add credential','Edit credential','Delete credential']
elif credmode == 'Manage Oracle databases':
menuoptions=['List databases','Register database','Edit database registration','Unregister database']
mode=userio.basicmenu(choices=menuoptions,nowait=True,returnnames=True)[0]
if mode=='List databases':
userio.grid(dbgrid)
sys.exit(0)
if mode=='List credentials':
userio.grid(svmgrid)
sys.exit(0)
elif mode == 'Unregister database':
sidlist.sort()
choice=userio.basicmenu(choices=sidlist,nowait=True,returnnames=True)[0]
certain=userio.yesno("Are you sure you want to deregister database " + choice)
if certain:
for x in range(len(configjson['oracle']['sid'])):
if configjson['oracle']['sid'][x]['name'] == choice:
del configjson['oracle']['sid'][x]
break
try:
open(configFile,'w').write(json.dumps(configjson,indent=3))
userio.message("Database " + choice + " unregistered")
except:
userio.fail("Unable to write new data to " + configFile)
elif mode == 'Delete credential':
svmlist.sort()
choice=userio.basicmenu(choices=svmlist,nowait=True,returnnames=True)[0]
certain=userio.yesno("Are you sure you want to delete the ONTAP credential for " + choice)
if certain:
for x in range(len(configjson['ontap']['svm'])):
if configjson['ontap']['svm'][x]['name'] == choice:
del configjson['ontap']['svm'][x]
break
try:
open(configFile,'w').write(json.dumps(configjson,indent=3))
userio.message("SVM " + choice + " deleted")
except:
userio.fail("Unable to write new data to " + configFile)
elif mode == "Register database":
newsid=''
while newsid == '':
newsid=userio.ask("Enter SID or TNS alias")
if newsid in sidlist:
userio.message("SID " + name + " is already defined")
newsid=''
newuser=userio.ask("Enter new username")
newpasswd=userio.ask("Enter new password",hide=True)
# proceed=False
# while not proceed:
# newdbfsvm=userio.ask("Enter SVM to host datafile backups")
# if newdbfsvm not in svmlist:
# userio.message("SVM " + newdbfsvm + " is not registered")
# newdbfsvm=''
# else:
# proceed=True
# newdbfaggr=userio.ask("Enter aggregate on SVM " + newdbfsvm + " to host datafile backups [Leave blank to autoselect]")
# proceed=False
# while not proceed:
# newlogsvm=userio.ask("Enter SVM to host archive log backups")
# if newlogsvm not in svmlist:
# userio.message("SVM " + newlogsvm + " is not registered")
# newlogsvm=''
# else:
# proceed=True
# newlogaggr=userio.ask("Enter aggregate on SVM " + newlogsvm + " to host archive log backups [Leave blank to autoselect]")
# configjson['nrmd']['sid'].append({'name':newsid,'datafileSVM':newdbfsvm,'archivelogSVM':newlogsvm,'datafileAggr':str(newdbfaggr),'archivelogAggr':newlogaggr})
# try:
# open(configFile,'w').write(json.dumps(configjson,indent=4))
# userio.message(configFile + " updated")
# except:
# userio.fail("Unable to write new data to " + configFile)
configjson['oracle']['sid'].append({'name':newsid,'username':str(newuser),'password':newpasswd})
try:
open(configFile,'w').write(json.dumps(configjson,indent=4))
userio.message(configFile + " updated")
except:
userio.fail("Unable to write new data to " + configFile)
elif mode == "Add credential":
newname=''
while newname == '':
newname=userio.ask("Enter SVM name")
if newname in svmlist:
userio.warn("SVM " + newname + " is already defined")
newname=''
proceed=False
while not proceed:
newmgmt=userio.ask("Enter new management interface")
if newmgmt in mgmtlist:
userio.warn("Management interface " + newmgmt + " is already known")
proceed=True
newuser=userio.ask("Enter new username")
newpasswd=userio.ask("Enter new password",hide=True)
proceed=False
while not proceed:
newdatalif=userio.ask("Enter new data interface(s), if any")
proceed=True
out=doREST.doREST(newmgmt,'get','/svm/svms',restargs={'name=':newname,'fields':'name,uuid'},username=newuser,password=newpasswd)
if not out.result == 0:
faildata=['Unable to validate credentials for SVM ' + newname]
faildata.append(out.reason)
userio.fail(faildata)
elif not out.response['num_records']:
userio.fail("Succesfully authenticated to management address, but could not find SVM " + newname)
else:
configjson['ontap']['svm'].append({'name':newname,
'managementLIF':newmgmt,
'username':newuser,
'password':newpasswd,
'uuid':out.response['records'][0]['uuid'],
'dataLIF':newdatalif})
try:
open(configFile,'w').write(json.dumps(configjson,indent=4))
userio.message(configFile + " updated")
except:
userio.fail("Unable to write new data to " + configFile)
elif mode == "Edit credential":
svmlist.sort()
choice=userio.basicmenu(choices=svmlist,nowait=True,returnnames=True)[0]
svm={}
for x in range(len(configjson['ontap']['svm'])):
if configjson['ontap']['svm'][x]['name'] == choice:
svm=configjson['ontap']['svm'][x].copy()
del configjson['ontap']['svm'][x]
break
newname=userio.ask("Enter new SVM name",default=svm['name'])
newmgmt=userio.ask("Enter new management interface",default=svm['managementLIF'])
newuser=userio.ask("Enter new username",default=svm['username'])
newpasswd=userio.ask("Enter new password",hide=True,default=svm['password'][0] + ('X' * (len(svm['password'])-2)) + svm['password'][-1])
if newpasswd == svm['password'][0] + ('X' * (len(svm['password'])-2)) + svm['password'][-1]:
newpasswd = svm['password']
if 'dataLIF' in svm.keys():
datalifdefault=svm['dataLIF']
else:
datalifdefault=''
newdatalif=userio.ask("Enter new data interface(s), if any",default=datalifdefault)
for x in range(len(configjson['ontap']['svm'])):
if configjson['ontap']['svm'][x]['name'] == choice:
del configjson['ontap']['svm'][x]
break
out=doREST.doREST(newmgmt,'get','/svm/svms',['name=' + newname,'fields=name,uuid'],username=newuser,password=newpasswd)
if not out.result == 0:
userio.fail([['Unable to validate credentials for SVM ' + newname] + [out.reason] + [out.stdout] + [out.stderr]])
elif not out.response['num_records']:
userio.fail("Succesfully authenticated to management address, but could not find SVM " + newname)
else:
configjson['ontap']['svm'].append({'name':newname,
'managementLIF':newmgmt,
'username':newuser,
'password':newpasswd,
'uuid':out.response['records'][0]['uuid'],
'dataLIF':newdatalif})
try:
open(configFile,'w').write(json.dumps(configjson,indent=3))
userio.message(configFile + " updated")
except:
userio.fail("Unable to write new data to " + configFile)
elif mode == "Edit database registration":
sidlist.sort()
choice=userio.basicmenu(choices=sidlist,nowait=True,returnnames=True)[0]
for x in range(len(configjson['oracle']['sid'])):
if configjson['oracle']['sid'][x]['name'] == choice:
sid=configjson['oracle']['sid'][x].copy()
del configjson['oracle']['sid'][x]
break
newname=userio.ask("Enter new database name",default=sid['name'])
newuser=userio.ask("Enter new username",default=sid['username'])
newpasswd=userio.ask("Enter new password",hide=True,default=sid['password'][0] + ('X' * (len(sid['password'])-2)) + sid['password'][-1])
for x in range(len(configjson['oracle']['sid'])):
if configjson['oracle']['sid'][x]['name'] == choice:
del configjson['ontap']['oracle'][x]
break
configjson['oracle']['sid'].append({'name':newname,'username':str(newuser),'password':newpasswd})
try:
open(configFile,'w').write(json.dumps(configjson,indent=4))
userio.message(configFile + " updated")
except:
userio.fail("Unable to write new data to " + configFile)