3 ############################################################################
4 # Copyright 2016 RIFT.io Inc #
6 # Licensed under the Apache License, Version 2.0 (the "License"); #
7 # you may not use this file except in compliance with the License. #
8 # You may obtain a copy of the License at #
10 # http://www.apache.org/licenses/LICENSE-2.0 #
12 # Unless required by applicable law or agreed to in writing, software #
13 # distributed under the License is distributed on an "AS IS" BASIS, #
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15 # See the License for the specific language governing permissions and #
16 # limitations under the License. #
17 ############################################################################
20 from contextlib import closing
31 class OnboardPkgError(Exception):
35 class OnboardPkgInputError(OnboardPkgError):
39 class OnboardPkgMissingPkg(OnboardPkgError):
43 class OnboardPkgFileError(OnboardPkgError):
47 class OnboardPkgMissingDescId(OnboardPkgError):
51 class OnboardPkgInvalidDescId(OnboardPkgError):
55 class OnboardPkgMissingAcct(OnboardPkgError):
59 class OnboardPkgSoConnError(OnboardPkgError):
63 class OnboardPkgCmdError(OnboardPkgError):
67 class OnboardPkgUploadError(OnboardPkgError):
71 class OnboardPkgRcConnError(OnboardPkgError):
75 class OnboardPkgDcError(OnboardPkgError):
79 class OnboardPkgAcctError(OnboardPkgError):
83 class OnboardPkgNsdError(OnboardPkgError):
87 class OnboardPkgInstError(OnboardPkgError):
91 class OnboardPkgInvalidPort(OnboardPkgError):
105 self._service_name = None
110 self._ip = args.so_ip
111 self._api_server_ip = "localhost"
113 self._uport = args.upload_port
114 self._onboard_port = args.onboard_port
115 self._rport = args.restconf_port
116 self._user = args.restconf_user
117 self._password = args.restconf_password
118 self._onboard_url = "curl -k --user \"{user}:{passwd}\" \"https://{ip}:{port}/composer/upload?api_server=https://{api_server_ip}&upload_server=https://{ip}\"". \
120 port=self._onboard_port,
122 passwd=self._password,
123 api_server_ip=self._api_server_ip)
124 self._upload_url = "curl -k https://{ip}:{port}/api/upload". \
128 self._headers = '-H "accept: application/json"' + \
129 ' -H "content-type: application/json"'
130 self._conf_url = "curl -k {header} --user \"{user}:{passwd}\" https://{ip}:{port}/api/config". \
131 format(header=self._headers,
133 passwd=self._password,
136 self._oper_url = "curl -k {header} --user \"{user}:{passwd}\" https://{ip}:{port}/api/operational". \
137 format(header=self._headers,
139 passwd=self._password,
147 def validate_args(self):
148 if args.upload_pkg is not None:
149 self._pkgs = args.upload_pkg
150 self.log.debug("Packages to upload: {}".format(self._pkgs))
151 if len(self._pkgs) == 0:
152 raise OnboardPkgMissingPkg('Need to specify atleast one package to upload')
154 for pkg in self._pkgs:
155 self.log.debug("Check pkg: {}".format(pkg))
156 if os.path.isfile(pkg) is False:
157 raise OnboardPkgFileError("Unable to access file: {}".format(pkg))
160 if args.nsd_id is None:
161 raise OnboardPkgMissingDescId("NS Descriptor ID required for instantiation")
165 uuid.UUID(args.datacenter)
166 self._dc = args.datacenter
167 except ValueError as e:
168 raise OnboardPkgInvalidDescId("Invalid UUID for datacenter: {}".
169 format(args.datacenter))
171 elif args.vim_account:
172 self._account = args.vim_account
175 raise OnboardPkgMissingAcct("Datacenter or VIM account required for instantiation")
177 self._service_name = args.instantiate
178 self._nsd_id = args.nsd_id
180 self.log.debug("Instantiate NSD {} as {} on {}".format(self._nsd_id,
184 if (self._pkgs is None) and (self._nsd_id is None):
185 raise OnboardPkgInputError("Need to specify either upload-pkg or instantiate options")
187 # Validate the port numbers are correct
188 def valid_port(port):
189 if 1 <= port <= 65535:
193 if not valid_port(self._uport):
194 raise OnboardPkgInvalidPort("Invalid upload port: {}".format(self._uport))
196 if not valid_port(self._rport):
197 raise OnboardPkgInvalidPort("Invalid Restconf port: {}".format(self._rport))
199 def _exec_cmd(self, cmd):
200 self.log.debug("Execute command: {}".format(cmd))
201 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE,
202 stderr=subprocess.PIPE, shell=True)
203 (output, err) = proc.communicate()
205 self.log.debug("Command exec status: {}\nSTDOUT: {}\nSTDERR: {}".
206 format(rc, output, err))
208 raise OnboardPkgCmdError("Command {} failed ({}): {}".
209 format(cmd, rc, err))
210 return output.decode("utf-8")
212 def validate_connectivity(self):
214 self.log.debug("Check connectivity to SO at {}:{}".
215 format(self._ip, self._uport))
217 with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock:
218 if sock.connect_ex((self._ip, self._uport)) != 0:
219 raise OnboardPkgSoConnError("Connection error to SO for upload at {}:{}".
220 format(self._ip, self._uport))
221 self.log.debug("Connection to SO upload port succeeded")
224 self.log.debug("Check connectivity to SO at {}:{}, with credentials {}:{}".
225 format(self._ip, self._rport, self._user, self._password))
227 rest_url = self._conf_url+"/resource-orchestrator"
229 output = self._exec_cmd(rest_url)
230 self.log.debug("Output of restconf validation: {}".
233 js = json.loads(output)
235 raise OnboardPkgRcConnError("SO Restconf connect error: {}".
238 self.log.debug("Connection to SO restconf port succeeded")
240 except OnboardPkgCmdError as e:
241 self.log.error("SO restconf connect failed: {}".format(e))
242 raise OnboardPkgRcConnError("SO Restconf connect error: {}".
246 def _upload_package(self, pkg):
247 upload_cmd = "{url} -F \"package=@{pkg}\" ". \
248 format(url=self._onboard_url,
250 self.log.debug("Upload pkg {} cmd: {}".format(pkg, upload_cmd))
252 output = self._exec_cmd(upload_cmd)
254 # Get the transaction id and wait for upload to complete
255 tx_id = json.loads(output)['transaction_id']
257 upload_status_url = "{url}/{id}/state". \
258 format(url=self._upload_url,
261 while status not in ['success', 'failure']:
262 output = self._exec_cmd(upload_status_url)
263 js = json.loads(output)
264 self.log.debug("Upload status of pkg {}: {}".format(pkg, js))
265 status = js['status']
267 if status != 'success':
268 raise OnboardPkgUploadError("Package {} upload failed: {}".
269 format(pkg, js['errors']))
271 self.log.info("Upload of package {} succeeded".format(pkg))
273 def upload_packages(self):
274 if self._pkgs is None:
275 self.log.debug("Upload packages not provided")
278 for pkg in self._pkgs:
279 self._upload_package(pkg)
281 def instantiate(self):
282 if self._nsd_id is None:
283 self.log.debug("No NSD ID provided for instantiation")
286 # Check to see if datacenter is valid
288 dc_url = "{url}/datacenters". format(url=self._oper_url)
289 output = self._exec_cmd(dc_url)
290 if (output is None) or (len(output) == 0):
292 raise OnboardPkgDcError("Datacenter {} provided is not valid".
295 js = json.loads(output)
296 if "ro-accounts" in js["rw-launchpad:datacenters"]:
297 for ro in js["rw-launchpad:datacenters"]["ro-accounts"]:
298 if "datacenters" in ro:
299 for dc in ro["datacenters"]:
300 if dc["uuid"] == self._dc:
301 self.log.debug("Found datacenter {}".format(dc))
308 raise OnboardPkgDcError("Datacenter {} provided is not valid".
312 # Check cloud account is valid, if provided
314 acct_url = "{url}/cloud/account/{acct}". \
315 format(url=self._conf_url, acct=self._account)
316 output = self._exec_cmd(acct_url)
317 if (output is None) or (len(output) == 0):
319 raise OnboardPkgAcctError("VIM/Cloud account {} provided is not valid".
320 format(self._account))
322 # Check id NSD ID is valid
323 nsd_url = "{url}/nsd-catalog/nsd/{nsd_id}". \
324 format(url=self._conf_url, nsd_id=self._nsd_id)
325 output = self._exec_cmd(nsd_url)
326 if (output is None) or (len(output) == 0):
328 raise OnboardPkgNsdError("NSD ID {} provided is not valid".
329 format(self._nsd_id))
331 js = json.loads(output)
333 raise OnboardPkgNsdError("NSD ID {} error: {}".
338 self.log.debug("NSD to instantiate: {}".format(nsd))
340 # Generate a UUID for NS
341 ns_id = str(uuid.uuid4())
342 self.log.debug("NS instance uuid: {}".format(ns_id))
344 # Build the nsr post data
346 'name': self._service_name,
349 nsr['om-datacenter'] = self._dc
351 nsr['cloud-account'] = self._account
353 data = {'nsr': [nsr]}
355 data_str = json.dumps(data)
356 self.log.debug("NSR post data: {}".format(data_str))
358 inst_url = "{url}/ns-instance-config -X POST -d '{data}'". \
359 format(url=self._conf_url, data=data_str)
360 output = self._exec_cmd(inst_url)
361 self.log.debug("Instantiate output: {}".format(output))
363 js = json.loads(output)
365 if "last-error" in js:
366 msg = "Error instantiating NS as {} with NSD {}: ". \
367 format(self._service_name, self._nsd_id,
370 raise OnboardPkgInstError(msg)
372 elif "rpc-reply" in js:
373 reply = js["rpc-reply"]
374 if "rpc-error" in reply:
375 msg = "Error instantiating NS as {} with NSD {}: ". \
376 format(self._service_name, self._nsd_id,
378 # self.log.error(msg)
379 raise OnboardPkgInstError(msg)
381 self.log.info("Successfully initiated instantiation of NS as {} ({})".
382 format(self._service_name, ns_id))
387 except Exception as e:
396 self.validate_connectivity()
397 self.upload_packages()
401 if __name__ == "__main__":
402 parser = argparse.ArgumentParser(description='Upload and instantiate NS')
403 parser.add_argument("-s", "--so-ip", default='localhost',
404 help="SO Launchpad IP")
406 parser.add_argument("-u", "--upload-pkg", action='append',
407 help="Descriptor packages to upload. " + \
408 "If multiple descriptors are provided, they are uploaded in the same sequence.")
410 parser.add_argument("-i", "--instantiate",
411 help="Instantiate a network service with the name")
412 parser.add_argument("-d", "--nsd-id",
413 help="Network descriptor ID to instantiate")
414 parser.add_argument("-D", "--datacenter",
415 help="OpenMano datacenter to instantiate on")
416 parser.add_argument("-c", "--vim-account",
417 help="Cloud/VIM account to instantiate on")
419 parser.add_argument("-o", "--onboard-port", default=8443, type=int,
420 help="Onboarding port number - node port number, default 8443")
421 parser.add_argument("-p", "--upload-port", default=4567, type=int,
422 help="Upload port number, default 4567")
423 parser.add_argument("-P", "--restconf-port", default=8008, type=int,
424 help="RESTconf port number, default 8008")
425 parser.add_argument("--restconf-user", default='admin',
426 help="RESTconf user name, default admin")
427 parser.add_argument("--restconf-password", default='admin',
428 help="RESTconf password, default admin")
430 parser.add_argument("-v", "--verbose", action='store_true',
431 help="Show more logs")
433 args = parser.parse_args()
435 fmt = logging.Formatter(
436 '%(asctime)-23s %(levelname)-5s (%(name)s@%(process)d:' \
437 '%(filename)s:%(lineno)d) - %(message)s')
438 log = logging.getLogger('onboard-pkg')
439 log.setLevel(logging.INFO)
441 log.setLevel(logging.DEBUG)
442 ch = logging.StreamHandler()
443 ch.setLevel(logging.DEBUG)
447 log.debug("Input arguments: {}".format(args))
450 ob = OnboardPackage(log, args)
452 except Exception as e: