3 import sys,json,datetime,time,types,httplib,re
6 DEFAULT_HOST = "127.0.0.1"
7 OPENECOMP_BE = "127.0.0.1"
11 DEFAULT_USERNAME = "cs0008"
12 DEFAULT_PASSWORD = "cs0008"
14 ONBOARD_BASE_PATH = "/onboarding-api/v1.0"
15 VSP_LIST_PATH = "{0}/vendor-software-products".format(ONBOARD_BASE_PATH)
16 VSP_ACTIONS_PATH = "{0}/vendor-software-products/{{vspId}}/actions".format(ONBOARD_BASE_PATH)
17 VSP_UPLOAD_PATH = "{0}/vendor-software-products/{{vspId}}/upload".format(ONBOARD_BASE_PATH)
18 VSP_DOWNLOAD_PATH = "{0}/vendor-software-products/{{vspId}}/downloadHeat".format(ONBOARD_BASE_PATH)
19 VSP_GET_URL = "{0}/vendor-software-products/{{vspId}}".format(ONBOARD_BASE_PATH)
22 username=DEFAULT_USERNAME
23 password=DEFAULT_PASSWORD
27 print "Going to use default values"
29 if argv[0].lower() == 'h' or argv[0].lower() == '-h':
36 if argv[0].lower() == '-a' and '/' not in argv[1]:
37 print '\n>>> Error: Credentials required (username/password)\n'
42 creds = argv[1].split('/')
44 password = creds[1] # not used
51 print "Going to use user defined values"
54 webHandler=WebHandler(host=host, port=DEFAULT_PORT)
55 response, headers = webHandler.rest(url=VSP_LIST_PATH, method='GET', data=None, userId=username)
56 jResult = json.loads(response)
57 jSrvices = jResult["results"]
58 reportFileName = 'upgradereport.csv' #datetime.now()
59 reportFile = open(reportFileName, 'w')
60 reportFile.write(Service.header())
62 for jService in jSrvices:
63 serviceName = jService["name"]
64 vendorName = jService["vendorName"]
65 vspId = jService["id"]
66 status = jService["status"]
67 if status != "Locked":
70 lockingUser = jService["lockingUser"]
72 service = Service(serviceName=serviceName, vspId=vspId, vendorName=vendorName, lockingUser=lockingUser )
74 # Will try to GET the service
77 serviceMigration(service, status, username)
79 print "Service {0} was tested and does not need a migration".format(serviceName)
81 reportFile.write(service.line())
85 def serviceMigration(service, serviceStatus, username):
86 print "Service {0} was tested and it needs a migration".format(service.serviceName)
87 print "Service {0} - Migration start"
88 if serviceStatus == "Locked":
89 print "Service {0} is locked - forcing checkin".format(service.serviceName)
91 print "Doing new checkout"
92 service.Checkout(username)
94 zipName = service.DownloadHeat()
97 service.uploadStatus = "no heat found"
99 uploadResponse = service.UploadHeat(zipName)
100 uploadResults = json.loads(uploadResponse)
101 if uploadResults['status'] == 'Success' and uploadResults['errors'].__len__() == 0:
102 service.uploadStatus = "Heat uploaded successfully"
104 service.uploadStatus = "Heat uploaded with errors"
105 print "Doing new checkin"
108 print "Service {0} - Migration end"
112 print("Upgrade script Help:")
113 print("==================================")
114 print("1607_to_1610 -h --> get help")
115 print("1607_to_1610 -a <username>/<password> [-ip {ip}]")
116 print("Example: 1607_to_1610 -a root/secret")
118 class Service(object):
119 def __init__(self, serviceName, vspId ,vendorName, lockingUser):
120 self.serviceName = serviceName
122 self.vendorName = vendorName
123 self.lockingUser = lockingUser
124 self.webHandler = WebHandler(host=Service.serveraddress, port=DEFAULT_PORT) # Schema?
125 self.uploadStatus = "not started"
128 return 'Name: {0}, Id: {1}, Vendor: {2}, locked by: {3}, status {4}'.format(self.serviceName, self.vspId ,self.vendorName, self.lockingUser, self.uploadStatus)
131 return 'Name,Id,Vendor,locked-by,status\n'
134 def server(cls, address):
135 cls.serveraddress=address
138 return '{0},{1},{2},{3},{4}\n'.format(self.serviceName, self.vspId ,self.vendorName, self.lockingUser, self.uploadStatus)
140 def Checkout(self, userId):
141 # /v1.0/vendor-software-products/{vspId}/actions
142 urlpath=VSP_ACTIONS_PATH.format(vspId=self.vspId)
143 response, headers = self.webHandler.rest( url=urlpath, method='PUT', data={"action": "Checkout"}, userId=userId)
144 self.lockingUser=userId #we will later use this user to checkin
148 # /v1.0/vendor-software-products/{vspId}/actions
149 urlpath = VSP_ACTIONS_PATH.format(vspId=self.vspId)
150 response, headers = self.webHandler.rest(url=urlpath, method='PUT', data={"action": "Checkin"}, userId=self.lockingUser)
154 # /v1.0/vendor-software-products/{vspId}
155 urlpath = VSP_GET_URL.format(vspId=self.vspId)
157 response, headers = self.webHandler.rest(url=urlpath, method='GET', data=None, userId=self.lockingUser)
158 except HttpError as e:
163 def UploadHeat(self, zipName):
164 #/v1.0/vendor-software-products/{vspId}/upload
165 urlpath = VSP_UPLOAD_PATH.format(vspId=self.vspId)
168 with open(zipName, 'rb') as fin:
171 files = [('upload', 'heatfile.zip', buffer)]
172 response = self.webHandler.post_multipart('HTTP', urlpath, fields, files, self.lockingUser)
178 def DownloadHeat(self):
179 urlpath=VSP_DOWNLOAD_PATH.format(vspId=self.vspId)
181 response, headers = self.webHandler.rest(url=urlpath, method='Get', data=None, userId=self.lockingUser, accept='application/octet-stream')
182 except HttpError as e:
186 for (key, value) in headers:
187 if key.lower() == "content-disposition":
188 file_name = value[value.index('=')+1:]
190 heatsDir= os.path.join(os.path.dirname(__file__), 'heats')
191 if not os.path.exists(heatsDir):
192 os.makedirs(heatsDir)
193 file_name = os.path.join(heatsDir, file_name)
194 with open(file_name, "wb") as fout:
202 class WebHandler(object):
203 def __init__(self, host, port):
207 def rest(self, url, method, data, userId, accept='application/json', content_type='application/json'):
208 connection = httplib.HTTPConnection(host=self.host, port=self.port)
211 headers = {'Content-Type':content_type ,'Accept':accept}
212 headers['USER_ID'] = userId
214 connection.request(method=method, headers=headers, body=json.dumps(data), url=url)
215 response = connection.getresponse()
216 if response.status not in range(200, 300):
217 raise HttpError(status= response.status, message=response.reason)
219 return response.read(), response.getheaders()
223 def post_multipart(self, scheme, selector, fields, files, userId):
225 Post fields and files to an http host as multipart/form-data.
226 fields is a sequence of (name, value) elements for regular form fields.
227 files is a sequence of (name, filename, value) elements for data to be uploaded as files
228 Return the server's response page.
230 content_type, body = self.encode_multipart_form_data(fields, files)
231 if scheme and scheme.lower() == "http":
232 h = httplib.HTTP(self.host, self.port)
234 h = httplib.HTTPS(self.host, self.port)
235 h.putrequest('POST', selector)
236 h.putheader('content-type', content_type)
237 h.putheader('content-length', str(len(body)))
238 h.putheader('Accept', 'application/json')
239 h.putheader('USER_ID', userId)
243 errcode, errmsg, headers = h.getreply()
244 print errcode, errmsg, headers
247 def encode_multipart_form_data(self, fields, files):
248 LIMIT = '----------lImIt_of_THE_fIle_eW_$'
251 for (key, value) in fields:
252 L.append('--' + LIMIT)
253 L.append('Content-Disposition: form-data; name="%s"' % key)
256 for (key, filename, value) in files:
257 L.append('--' + LIMIT)
258 L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
259 L.append('Content-Type: %s' % self.get_content_type(filename))
262 L.append('--' + LIMIT + '--')
265 content_type = 'multipart/form-data; boundary=%s' % LIMIT
266 return content_type, body
268 def get_content_type(self, filename):
269 return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
271 class HttpError(Exception):
272 def __init__(self, status, message):
276 return repr(self.value, self.message)
278 if __name__ == "__main__":