-
Notifications
You must be signed in to change notification settings - Fork 4
/
object_prep.py
306 lines (249 loc) · 12.6 KB
/
object_prep.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python
# Object Prep script create by Magdy Salem of the EMC {code} Team and Licensed under MIT.
# Please visit us at emccode.github.io
import os,json
import subprocess
import shutil
import getopt
import sys,re
import time
AuthToken=None
def getAuthToken(ECSNode, User, Password):
curlCommand = "curl -i -k https://%s:4443/login -u %s:%s" % (ECSNode, User, Password)
print ("Executing getAuthToken: %s " % curlCommand)
res=subprocess.check_output(curlCommand, shell=True)
authTokenPattern = "X-SDS-AUTH-TOKEN:(.*)\r\n"
searchObject=re.search(authTokenPattern,res)
assert searchObject, "Get Auth Token failed"
print("Auth Token %s" % searchObject.group(1))
return searchObject.group(1)
def executeRestAPI(url, method, filter, data, ECSNode,contentType='json',checkOutput=0):
if data:
subprocess.call("echo %s > request_body.tmp" % data, shell=True)
data="-d @request_body.tmp"
if "license" in url:
data="-T license.xml"
curlCommand = "curl -s -k -X %s -H 'Content-Type:application/%s' \
-H 'X-SDS-AUTH-TOKEN:%s' \
-H 'ACCEPT:application/%s' \
%s https://%s:9011%s" %(method, contentType, AuthToken, contentType,data, ECSNode, url)
print ("Executing REST API command: %s " % curlCommand)
#print jsonResult
if checkOutput:
subprocess.call(curlCommand, shell=True)
jsonResult = subprocess.check_output(curlCommand, shell=True)
RestOutputDict = {}
RestOutputDict = json.loads(jsonResult)
return RestOutputDict
assert "code" not in jsonResult, "%s %s failed" % (method, url)
else:
res=subprocess.call(curlCommand, shell=True)
print res
def retry( numberOfRetries, timeToWaitBetweenTriesInSeconds, functionToRetry, argumentList, keywordArgs = {}):
for i in range(numberOfRetries):
try:
return apply(functionToRetry, argumentList, keywordArgs)
except Exception, e:
print("Method %s threw error %s" % (functionToRetry, e))
print("Sleep for %s seconds before retry" % timeToWaitBetweenTriesInSeconds)
time.sleep(timeToWaitBetweenTriesInSeconds)
raise e
def getVDCID(ECSNode,VDCName):
url ='/object/vdcs/vdc/%s' %VDCName
return executeRestAPI(url, 'GET','.id', "", ECSNode,checkOutput=1)["id"]
def getVarrayID(ECSNode):
return executeRestAPI('/vdc/data-services/varrays', 'GET','.id', "", ECSNode, checkOutput=1)['varray'][0]["id"]
def getVpoolID(ECSNode):
return executeRestAPI('/vdc/data-service/vpools', 'GET','.id', "", ECSNode, checkOutput=1)['data_service_vpool'][0]["id"]
def getNamespaces(ECSNode):
return executeRestAPI('/object/namespaces', 'GET','.id', "", ECSNode, checkOutput=1)['namespace'][0]["id"]
def DeleteNamespace(ECSNode, Namespace):
url ='/object/namespaces/namespace/%s/deactivate' %Namespace
return executeRestAPI(url, 'POST','', "", ECSNode)
def DeleteUser(ECSNode,userName,Namespace):
print("\nDelete User %s" % userName)
DeleteUserPayload ='{\\"user\\":\\"%s\\",\
\\"namespace\\":\\"%s\\"\
}' % (userName, Namespace)
executeRestAPI("/object/users/deactivate", 'POST','.id', DeleteUserPayload, ECSNode)
def getVDCSecretKey(self):
Log.info(LoggingInfra.logger, "Fetch VDC secret key")
secretKeyDict = self.executeRestAPI("/vdc/secret-key", 'GET', '.secret_key', "")
return secretKeyDict['secret_key']
def UploadLicense(ECSNode):
executeRestAPI("/license", 'POST','', '', ECSNode, contentType='xml')
def UploadLicenseWithRetry(ECSNode):
retry(5, 60, UploadLicense, [ECSNode])
def CreateObjectVArray(ECSNode, objectVArrayName):
print("\nCreate Object Varray %s" % objectVArrayName)
objectVArrayPayload ='{\\"name\\":\\"%s\\",\
\\"description\\":\\"%s\\",\
\\"isProtected\\":\\"%s\\"\
}' % (objectVArrayName, objectVArrayName, "false")
executeRestAPI("/vdc/data-services/varrays", 'POST','.id', objectVArrayPayload, ECSNode, checkOutput=1)
print("Object Varray %s is created" % objectVArrayName)
def CreateObjectVarrayWithRetry(ECSNode, objectVArrayName):
retry(5, 60, CreateObjectVArray, [ECSNode, objectVArrayName])
def createDataStoreOnCommodityNodes(ECSNode, dataStoreName, varray):
createDataStorePayLoad ='{ \\"nodes\\":[\
{\
\\"nodeId\\":\\"%s\\",\\"name\\":\\"%s\\",\
\\"virtual_array\\":\\"%s\\",\\"description\\":\\"%s\\"\
}]}' % (ECSNode, dataStoreName, varray, dataStoreName)
return executeRestAPI('/vdc/data-stores/commodity', 'POST','.id', createDataStorePayLoad, ECSNode)
def CreateDataStoreOnCommodityNodesWithRetry(ECSNode, dataStoreName, varray):
retry(5, 60, createDataStoreOnCommodityNodes, [ECSNode, dataStoreName, varray])
def InsertVDC(ECSNode, VDCName):
secretKey="secret12345"
#secretKey=getVDCSecretKey()
InsertVDCPayload ='{\\"vdcName\\":\\"%s\\",\
\\"interVdcEndPoints\\":\\"%s\\", \
\\"secretKeys\\":\\"%s\\"\
}' % (VDCName, ECSNode, secretKey)
executeRestAPI('/object/vdcs/vdc/%s' % VDCName, 'PUT','',InsertVDCPayload, ECSNode)
return getVDCID(ECSNode,VDCName)
def InsertVDCWithRetry(ECSNode, objectVpoolName):
retry(5, 60, InsertVDC, [ECSNode, objectVpoolName])
def CreateObjectVpool(ECSNode, objectVpoolName, VDCName):
vdcID = getVDCID(ECSNode,VDCName)
print("\nVDC ID is %s" % vdcID)
vArrayID = getVarrayID(ECSNode)
print("\nVArray ID is %s" % vArrayID)
objectVpoolPayload ='{\\"description\\":\\"%s\\",\
\\"name\\":\\"%s\\", \
\\"zone_mappings\\":[\
{\
\\"name\\":\\"%s\\",\\"value\\":\\"%s\\"\
}]}' % (objectVpoolName, objectVpoolName, vdcID, vArrayID)
print("\nCreate Object VPool %s" % objectVpoolName)
executeRestAPI("/vdc/data-service/vpools", 'POST','.id', objectVpoolPayload, ECSNode, checkOutput=1)
print("Object Vpool %s is created" % objectVpoolName)
def CreateObjectVpoolWithRetry(ECSNode, objectVpoolName, VDCName):
retry(5, 60, CreateObjectVpool, [ECSNode, objectVpoolName, VDCName])
def CreateNamespace(ECSNode, Namespace, objectVpoolName):
print("\nCreate Namespace %s" % Namespace)
NamespacePayload='{\\"namespace\\": \\"%s\\", \\"default_data_services_vpool\\": \\"%s\\"}'%(Namespace, objectVpoolName)
executeRestAPI("/object/namespaces/namespace", 'POST','.id', NamespacePayload, ECSNode, checkOutput=1)
print("Namespace %s is created" % Namespace)
def CreateNamespaceWithRetry(ECSNode, Namespace):
retry(5, 60, CreateNamespace, [ECSNode, Namespace])
def addUser(ECSNode,userName,Namespace):
print("\nCreate User %s" % userName)
createUserPayload ='{\\"user\\":\\"%s\\",\
\\"namespace\\":\\"%s\\"\
}' % (userName, Namespace)
executeRestAPI("/object/users", 'POST','.id', createUserPayload, ECSNode)
def addUserSecretKey(ECSNode, username):
secretKeyPayload='{\\"existing_key_expiry_time_mins\\":20000}'
secretKeyDict = executeRestAPI("/object/user-secret-keys/%s" % username, 'POST', '.secret_key', secretKeyPayload, ECSNode)
print("\nAdd secret key for user %s" % username)
def getUserSecretKey(ECSNode, username):
secretKeyDict = executeRestAPI("/object/user-secret-keys/%s" % username, 'GET', '.secret_key', "", ECSNode, checkOutput=1)
print("\n\nUser %s SecretKey is %s" % (username,secretKeyDict['secret_key_1']))
def main(argv):
try:
opts, argv = getopt.getopt(argv, '', ["ECSNodes=","Namespace=","ObjectVArray=","ObjectVPool=","UserName=","DataStoreName=","VDCName=","MethodName="])
except getopt.GetoptError, e:
print e
print 'ObjectProvsioning.py --ECSNodes=<Coma seperated list of datanodes> --Namespace=<namespace> --ObjectVArray=<Object vArray Name> --ObjectVPool=<Object VPool name> --UserName=<user name to be created> --DataStoreName=<Name of the datastore to be created> --VDCName=<Name of the VDC> --MethodName=<Operation to be performed>\n --MethodName is required only when you need to run a particular step in Object Provisioning.If this option is not provided all the Object Provisioning steps will be run.\n Supported options for --MethodName are:\n UploadLicense \n CreateObjectVarray \n GetVarrayID \n CreateDataStore \n InsertVDC \n CreateObjectVpool \n CreateNamespace \n CreateUserAndSecretKey \n'
sys.exit(2)
ECSNodes=""
MethodName=""
for opt, arg in opts:
if opt == '-h':
print 'ObjectProvsioning.py --ECSNodes=<Coma seperated list of datanodes> --Namespace=<namespace> --ObjectVArray=<Object vArray Name> --ObjectVPool=<Object VPool name> --UserName=<user name to be created> --DataStoreName=<Name of the datastore to be created> --VDCName=<Name of the VDC> --MethodName=<Operation to be performed>\n --MethodName is required only when you need to run a particular step in Object Provisioning.If this option is not provided all the Object Provisioning steps will be run.\n Supported options for --MethodName are:\n UploadLicense \n CreateObjectVarray \n GetVarrayID \n CreateDataStore \n InsertVDC \n CreateObjectVpool \n CreateNamespace \n CreateUserAndSecretKey \n'
sys.exit()
elif opt in ("-ECSNodes", "--ECSNodes"):
ECSNodes = arg
ECSNodeList = ECSNodes.split(",")
ECSNode = ECSNodeList[0]
elif opt in ("-Namespace", "--Namespace"):
Namespace = arg
elif opt in ("-ObjectVArray", "--ObjectVArray"):
ObjectVArray = arg
elif opt in ("-ObjectVPool", "--ObjectVPool"):
ObjectVPool = arg
elif opt in ("-UserName", "--UserName"):
UserName = arg
elif opt in ("-DataStoreName", "--DataStoreName"):
DataStoreName = arg
elif opt in ("-VDCName", "--VDCName"):
VDCName = arg
elif opt in ("-MethodName", "--MethodName"):
MethodName = arg
global AuthToken
AuthToken=getAuthToken(ECSNode, "root", "ChangeMe")
print("ECSNodes: %s" %ECSNode)
print("Namespace: %s" %Namespace)
print("ObjectVArray: %s" %ObjectVArray)
print("ObjectVPool: %s" %ObjectVPool)
print("UserName: %s" %UserName)
print("DataStoreName: %s" %DataStoreName)
print("VDCName: %s" %VDCName)
print("MethodName: %s" %MethodName)
if MethodName == "UploadLicense":
UploadLicense(ECSNode)
sys.exit()
elif MethodName == "CreateObjectVarray":
CreateObjectVarrayWithRetry(ECSNode, ObjectVArray)
print("Virtual Array: %s" %getVarrayID(ECSNode))
sys.exit()
elif MethodName == "GetVarrayID":
ObjectVArrayID = getVarrayID(ECSNode)
sys.exit()
elif MethodName == "CreateDataStore":
ObjectVArrayID = getVarrayID(ECSNode)
for node in ECSNodeList:
CreateDataStoreOnCommodityNodesWithRetry(node, DataStoreName, ObjectVArrayID)
time.sleep(20 * 60)
sys.exit()
elif MethodName == "InsertVDC":
InsertVDC(ECSNode, VDCName)
print("VDCID: %s" %getVDCID(ECSNode, VDCName))
sys.exit()
elif MethodName == "CreateObjectVpool":
CreateObjectVpoolWithRetry(ECSNode, ObjectVPool, VDCName)
print("Data service vPool ID:%s" %getVpoolID(ECSNode))
sys.exit()
elif MethodName == "CreateNamespace":
ObjectVPoolID = getVpoolID(ECSNode)
CreateNamespace(ECSNode, Namespace, ObjectVPoolID)
print("Namespace: %s" %getNamespaces(ECSNode))
sys.exit()
elif MethodName == "CreateUser":
addUser(ECSNode, UserName, Namespace)
sys.exit()
elif MethodName == "CreateSecretKey":
addUserSecretKey(ECSNode, UserName)
getUserSecretKey(ECSNode, UserName)
sys.exit()
elif MethodName == "DeleteUser":
DeleteUser(ECSNode, UserName, Namespace)
sys.exit()
elif MethodName == "getUserSecretKey":
getUserSecretKey(ECSNode, UserName)
sys.exit()
else:
UploadLicense(ECSNode)
CreateObjectVarrayWithRetry(ECSNode, ObjectVArray)
print("Virtual Array: %s" %getVarrayID(ECSNode))
ObjectVArrayID = getVarrayID(ECSNode)
for node in ECSNodeList:
CreateDataStoreOnCommodityNodesWithRetry(node, DataStoreName, ObjectVArrayID)
time.sleep(20 * 60)
InsertVDC(ECSNode, VDCName)
print("VDCID: %s" %getVDCID(ECSNode, VDCName))
CreateObjectVpoolWithRetry(ECSNode, ObjectVPool, VDCName)
print("Data service vPool ID:%s" %getVpoolID(ECSNode))
ObjectVPoolID = getVpoolID(ECSNode)
CreateNamespace(ECSNode, Namespace, ObjectVPoolID)
print("Namespace: %s" %getNamespaces(ECSNode))
addUser(ECSNode, UserName, Namespace)
addUserSecretKey(ECSNode, UserName)
getUserSecretKey(ECSNode, UserName)
sys.exit()
#DeleteUser(ECSNode,UserName,Namespace)
#DeleteNamespace(ECSNode, Namespace)
#print (getNamespaces(ECSNode))
if __name__ == "__main__":
main(sys.argv[1:])