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