Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 15 | from __future__ import absolute_import, print_function |
| 16 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 17 | import argparse |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 18 | import base64 |
| 19 | import functools |
Scott Baker | a2717c8 | 2019-06-06 10:55:11 -0700 | [diff] [blame] | 20 | import hashlib |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 21 | import inspect |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 22 | import os |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 23 | import sys |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 24 | |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 25 | from google.protobuf.empty_pb2 import Empty |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 26 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 27 | import grpc |
| 28 | from grpc import (composite_channel_credentials, metadata_call_credentials, |
| 29 | ssl_channel_credentials) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 30 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 31 | from twisted.internet import reactor |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 32 | from xosconfig import Config |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 33 | |
| 34 | from xosapi import orm |
| 35 | import xosapi.chameleon_client.grpc_client as chameleon_client |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 37 | from multistructlog import create_logger |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 38 | log = create_logger(Config().get("logging")) |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 39 | |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 40 | currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) |
| 41 | sys.path = [currentdir] + sys.path |
| 42 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 43 | SERVER_CA = "/usr/local/share/ca-certificates/local_certs.crt" |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 44 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 45 | |
| 46 | class UsernamePasswordCallCredentials(grpc.AuthMetadataPlugin): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | """Metadata wrapper for raw access token credentials.""" |
| 48 | |
| 49 | def __init__(self, username, password): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 50 | self._username = username |
| 51 | self._password = password |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 52 | |
| 53 | def __call__(self, context, callback): |
| 54 | basic_auth = "Basic %s" % base64.b64encode( |
| 55 | "%s:%s" % (self._username, self._password) |
| 56 | ) |
| 57 | metadata = (("authorization", basic_auth),) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 58 | callback(metadata, None) |
| 59 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 60 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 61 | class SessionIdCallCredentials(grpc.AuthMetadataPlugin): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 62 | """Metadata wrapper for raw access token credentials.""" |
| 63 | |
| 64 | def __init__(self, sessionid): |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 65 | self._sessionid = sessionid |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | |
| 67 | def __call__(self, context, callback): |
| 68 | metadata = (("x-xossession", self._sessionid),) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 69 | callback(metadata, None) |
| 70 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 71 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 72 | class XOSClient(chameleon_client.GrpcClient): |
| 73 | # We layer our own reconnect_callback functionality so we can setup the |
| 74 | # ORM before calling reconnect_callback. |
| 75 | |
Scott Baker | a2717c8 | 2019-06-06 10:55:11 -0700 | [diff] [blame] | 76 | def __init__(self, *args, **kwargs): |
| 77 | self.hashes = {} |
| 78 | self.restart_on_protobuf_change = False |
| 79 | super(XOSClient, self).__init__(*args, **kwargs) |
| 80 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 81 | def set_reconnect_callback(self, reconnect_callback): |
| 82 | self.reconnect_callback2 = reconnect_callback |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 83 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 84 | return self |
| 85 | |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 86 | def request_convenience_methods(self): |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 87 | |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 88 | convenience_methods_dir = "/var/run/xosapi/convenience" |
| 89 | if not os.path.exists(convenience_methods_dir): |
| 90 | log.info("Creating convenience methods directory", convenience_methods_dir=convenience_methods_dir) |
| 91 | os.makedirs(convenience_methods_dir) |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 92 | |
| 93 | try: |
Matteo Scandolo | 5dda1a3 | 2018-05-14 14:03:10 -0700 | [diff] [blame] | 94 | response = self.dynamicload.GetConvenienceMethods(Empty()) |
| 95 | |
| 96 | if response: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 97 | log.info( |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 98 | "Saving convenience methods", |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 99 | methods=[m.filename for m in response.convenience_methods], |
| 100 | ) |
Matteo Scandolo | 5dda1a3 | 2018-05-14 14:03:10 -0700 | [diff] [blame] | 101 | |
| 102 | for cm in response.convenience_methods: |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 103 | log.debug("Saving convenience method", method=cm.filename) |
Matteo Scandolo | 5dda1a3 | 2018-05-14 14:03:10 -0700 | [diff] [blame] | 104 | save_path = os.path.join(convenience_methods_dir, cm.filename) |
Zack Williams | 5c2ea23 | 2019-01-30 15:23:01 -0700 | [diff] [blame] | 105 | open(save_path, "w").write(cm.contents) |
Matteo Scandolo | 5dda1a3 | 2018-05-14 14:03:10 -0700 | [diff] [blame] | 106 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 107 | log.exception( |
| 108 | "Cannot load convenience methods, restarting the synchronzier" |
| 109 | ) |
| 110 | os.execv(sys.executable, ["python"] + sys.argv) |
Matteo Scandolo | 5dda1a3 | 2018-05-14 14:03:10 -0700 | [diff] [blame] | 111 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 112 | except grpc._channel._Rendezvous as e: |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 113 | code = e.code() |
| 114 | if code == grpc.StatusCode.UNAVAILABLE: |
| 115 | # NOTE if the core is not available, restart the synchronizer |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 116 | os.execv(sys.executable, ["python"] + sys.argv) |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 117 | |
Scott Baker | a2717c8 | 2019-06-06 10:55:11 -0700 | [diff] [blame] | 118 | def hash_check(self, pb2_file_name, pb2_grpc_file_name): |
| 119 | # If the protobufs have changed, then it's likely that new models |
| 120 | # have been downloaded. One way we have dealt with this in the past |
| 121 | # is to force a reload() of the affected modules. However, it seems |
| 122 | # safer to force a full synchronizer restart as this will allow |
| 123 | # the synchronizer to perform a version check against the core, and |
| 124 | # it will refresh any data structures that might be affected by the |
| 125 | # new models. |
| 126 | |
| 127 | pb2_hash = hashlib.sha256(open(pb2_file_name).read()) |
| 128 | pb2_grpc_hash = hashlib.sha256(open(pb2_grpc_file_name).read()) |
| 129 | |
| 130 | if (pb2_file_name in self.hashes) or (pb2_grpc_file_name in self.hashes): |
| 131 | if (pb2_hash != self.hashes[pb2_file_name]) or (pb2_grpc_hash != self.hashes[pb2_grpc_file_name]): |
| 132 | log.warning( |
| 133 | "Protobuf change detected, restarting the synchronzier" |
| 134 | ) |
| 135 | os.execv(sys.executable, ["python"] + sys.argv) |
| 136 | |
| 137 | self.hashes[pb2_file_name] = pb2_hash |
| 138 | self.hashes[pb2_grpc_file_name] = pb2_grpc_hash |
| 139 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 140 | def reconnected(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 141 | for api in ["modeldefs", "utility", "xos", "dynamicload"]: |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 142 | pb2_file_name = os.path.join(self.work_dir, api + "_pb2.py") |
| 143 | pb2_grpc_file_name = os.path.join(self.work_dir, api + "_pb2_grpc.py") |
| 144 | |
| 145 | if os.path.exists(pb2_file_name) and os.path.exists(pb2_grpc_file_name): |
Scott Baker | a2717c8 | 2019-06-06 10:55:11 -0700 | [diff] [blame] | 146 | if self.restart_on_protobuf_change: |
| 147 | self.hash_check(pb2_file_name, pb2_grpc_file_name) |
| 148 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 149 | orig_sys_path = sys.path |
| 150 | try: |
| 151 | sys.path.append(self.work_dir) |
| 152 | m_protos = __import__(api + "_pb2") |
| 153 | m_grpc = __import__(api + "_pb2_grpc") |
| 154 | finally: |
| 155 | sys.path = orig_sys_path |
| 156 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 157 | stub_class = getattr(m_grpc, api + "Stub") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 158 | |
| 159 | setattr(self, api, stub_class(self.channel)) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 160 | setattr(self, api + "_pb2", m_protos) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 161 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 162 | print("failed to locate api", api, file=sys.stderr) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 163 | |
| 164 | if hasattr(self, "xos"): |
Scott Baker | b96ba43 | 2018-02-26 09:53:48 -0800 | [diff] [blame] | 165 | self.xos_orm = orm.ORMStub(self.xos, self.xos_pb2, "xos") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 166 | |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 167 | # ask the core for the convenience methods |
Scott Baker | 8203dc6 | 2019-03-14 16:16:19 -0700 | [diff] [blame] | 168 | self.request_convenience_methods() |
Matteo Scandolo | 10a2f3c | 2018-04-20 16:59:38 +0200 | [diff] [blame] | 169 | |
| 170 | # Load convenience methods after reconnect |
| 171 | orm.import_convenience_methods() |
| 172 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 173 | if self.reconnect_callback2: |
| 174 | self.reconnect_callback2() |
| 175 | |
| 176 | |
| 177 | class InsecureClient(XOSClient): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 178 | def __init__( |
| 179 | self, |
| 180 | consul_endpoint=None, |
| 181 | work_dir="/tmp/xos_grpc_protos", |
| 182 | endpoint="localhost:50055", |
| 183 | reconnect_callback=None, |
| 184 | ): |
| 185 | super(InsecureClient, self).__init__( |
| 186 | consul_endpoint, work_dir, endpoint, self.reconnected |
| 187 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 188 | |
| 189 | self.reconnect_callback2 = reconnect_callback |
| 190 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 191 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 192 | class SecureClient(XOSClient): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 193 | def __init__( |
| 194 | self, |
| 195 | consul_endpoint=None, |
| 196 | work_dir="/tmp/xos_grpc_protos", |
| 197 | endpoint="localhost:50055", |
| 198 | reconnect_callback=None, |
| 199 | cacert=SERVER_CA, |
| 200 | username=None, |
| 201 | password=None, |
| 202 | sessionid=None, |
| 203 | ): |
| 204 | server_ca = open(cacert, "r").read() |
| 205 | if sessionid: |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 206 | call_creds = metadata_call_credentials(SessionIdCallCredentials(sessionid)) |
| 207 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 208 | call_creds = metadata_call_credentials( |
| 209 | UsernamePasswordCallCredentials(username, password) |
| 210 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 211 | chan_creds = ssl_channel_credentials(server_ca) |
| 212 | chan_creds = composite_channel_credentials(chan_creds, call_creds) |
| 213 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 214 | super(SecureClient, self).__init__( |
| 215 | consul_endpoint, work_dir, endpoint, self.reconnected, chan_creds |
| 216 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 217 | |
| 218 | self.reconnect_callback2 = reconnect_callback |
| 219 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 220 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 221 | # ----------------------------------------------------------------------------- |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 222 | # Wrappers for easy setup for test cases, etc |
| 223 | # ----------------------------------------------------------------------------- |
| 224 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 225 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 226 | def parse_args(): |
| 227 | parser = argparse.ArgumentParser() |
| 228 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 229 | defs = { |
| 230 | "grpc_insecure_endpoint": "xos-core.cord.lab:50055", |
| 231 | "grpc_secure_endpoint": "xos-core.cord.lab:50051", |
| 232 | "config": "/opt/xos/config.yml", |
| 233 | } |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 234 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 235 | _help = "Path to the config file (default: %s)" % defs["config"] |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 236 | parser.add_argument( |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 237 | "-C", |
| 238 | "--config", |
| 239 | dest="config", |
| 240 | action="store", |
| 241 | default=defs["config"], |
| 242 | help=_help, |
| 243 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 244 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 245 | _help = ( |
| 246 | "gRPC insecure end-point to connect to. It is a direct", |
| 247 | ". (default: %s" % defs["grpc_insecure_endpoint"], |
| 248 | ) |
| 249 | parser.add_argument( |
| 250 | "-G", |
| 251 | "--grpc-insecure-endpoint", |
| 252 | dest="grpc_insecure_endpoint", |
| 253 | action="store", |
| 254 | default=defs["grpc_insecure_endpoint"], |
| 255 | help=_help, |
| 256 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 257 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 258 | _help = ( |
| 259 | "gRPC secure end-point to connect to. It is a direct", |
| 260 | ". (default: %s" % defs["grpc_secure_endpoint"], |
| 261 | ) |
| 262 | parser.add_argument( |
| 263 | "-S", |
| 264 | "--grpc-secure-endpoint", |
| 265 | dest="grpc_secure_endpoint", |
| 266 | action="store", |
| 267 | default=defs["grpc_secure_endpoint"], |
| 268 | help=_help, |
| 269 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 270 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 271 | parser.add_argument( |
| 272 | "-u", "--username", dest="username", action="store", default=None, help=_help |
| 273 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 274 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 275 | parser.add_argument( |
| 276 | "-p", "--password", dest="password", action="store", default=None, help=_help |
| 277 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 278 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 279 | _help = "omit startup banner log lines" |
| 280 | parser.add_argument( |
| 281 | "-n", |
| 282 | "--no-banner", |
| 283 | dest="no_banner", |
| 284 | action="store_true", |
| 285 | default=False, |
| 286 | help=_help, |
| 287 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 288 | |
| 289 | _help = "suppress debug and info logs" |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 290 | parser.add_argument("-q", "--quiet", dest="quiet", action="count", help=_help) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 291 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 292 | _help = "enable verbose logging" |
| 293 | parser.add_argument("-v", "--verbose", dest="verbose", action="count", help=_help) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 294 | |
| 295 | args = parser.parse_args() |
| 296 | |
| 297 | return args |
| 298 | |
Sapan Bhatia | 5188553 | 2017-08-29 01:55:39 -0400 | [diff] [blame] | 299 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 300 | def coreclient_reconnect(client, reconnect_callback, *args, **kwargs): |
| 301 | global coreapi |
| 302 | |
| 303 | coreapi = coreclient.xos_orm |
| 304 | |
| 305 | if reconnect_callback: |
| 306 | reconnect_callback(*args, **kwargs) |
| 307 | |
| 308 | reactor.stop() |
| 309 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 310 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 311 | def start_api(reconnect_callback, *args, **kwargs): |
| 312 | global coreclient |
| 313 | |
| 314 | if kwargs.get("username", None): |
| 315 | coreclient = SecureClient(*args, **kwargs) |
| 316 | else: |
| 317 | coreclient = InsecureClient(*args, **kwargs) |
| 318 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 319 | coreclient.set_reconnect_callback( |
| 320 | functools.partial(coreclient_reconnect, coreclient, reconnect_callback) |
| 321 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 322 | coreclient.start() |
| 323 | |
| 324 | reactor.run() |
| 325 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 326 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 327 | def start_api_parseargs(reconnect_callback): |
Scott Baker | 95f7d95 | 2017-03-09 10:04:26 -0800 | [diff] [blame] | 328 | """ This function is an entrypoint for tests and other simple programs to |
| 329 | setup the API and get a callback when the API is ready. |
| 330 | """ |
| 331 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 332 | args = parse_args() |
| 333 | |
| 334 | if args.username: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 335 | start_api( |
| 336 | reconnect_callback, |
| 337 | endpoint=args.grpc_secure_endpoint, |
| 338 | username=args.username, |
| 339 | password=args.password, |
| 340 | ) |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 341 | else: |
| 342 | start_api(reconnect_callback, endpoint=args.grpc_insecure_endpoint) |
| 343 | |
| 344 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 345 | # ----------------------------------------------------------------------------- |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 346 | # Self test |
| 347 | # ----------------------------------------------------------------------------- |
| 348 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 349 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 350 | def insecure_callback(client): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 351 | print("insecure self_test start") |
| 352 | print(client.xos_orm.User.objects.all()) |
| 353 | print("insecure self_test done") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 354 | |
| 355 | # now start the next test |
| 356 | client.stop() |
| 357 | reactor.callLater(0, start_secure_test) |
| 358 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 359 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 360 | def start_insecure_test(): |
Matteo Scandolo | e3d2f26 | 2018-06-05 17:45:39 -0700 | [diff] [blame] | 361 | client = InsecureClient(endpoint="xos-core:50055") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 362 | client.set_reconnect_callback(functools.partial(insecure_callback, client)) |
| 363 | client.start() |
| 364 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 365 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 366 | def secure_callback(client): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 367 | print("secure self_test start") |
| 368 | print(client.xos_orm.User.objects.all()) |
| 369 | print("secure self_test done") |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 370 | reactor.stop() |
| 371 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 372 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 373 | def start_secure_test(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 374 | client = SecureClient( |
| 375 | endpoint="xos-core:50051", username="admin@opencord.org", password="letmein" |
| 376 | ) |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 377 | client.set_reconnect_callback(functools.partial(secure_callback, client)) |
| 378 | client.start() |
| 379 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 380 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 381 | def main(): |
| 382 | reactor.callLater(0, start_insecure_test) |
| 383 | |
| 384 | reactor.run() |
| 385 | |
Scott Baker | 96b995a | 2017-02-15 16:21:12 -0800 | [diff] [blame] | 386 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 387 | if __name__ == "__main__": |
| 388 | main() |