Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 1 | # Copyright (C) 2019 Garmin Ltd. |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | # |
| 5 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 6 | import logging |
| 7 | import socket |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 8 | import bb.asyncrpc |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 9 | import json |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 10 | from . import create_async_client |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 11 | |
| 12 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 13 | logger = logging.getLogger("hashserv.client") |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 14 | |
| 15 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 16 | class AsyncClient(bb.asyncrpc.AsyncClient): |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 17 | MODE_NORMAL = 0 |
| 18 | MODE_GET_STREAM = 1 |
| 19 | |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 20 | def __init__(self, username=None, password=None): |
| 21 | super().__init__("OEHASHEQUIV", "1.1", logger) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 22 | self.mode = self.MODE_NORMAL |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 23 | self.username = username |
| 24 | self.password = password |
| 25 | self.saved_become_user = None |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 26 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 27 | async def setup_connection(self): |
| 28 | await super().setup_connection() |
| 29 | cur_mode = self.mode |
| 30 | self.mode = self.MODE_NORMAL |
| 31 | await self._set_mode(cur_mode) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 32 | if self.username: |
| 33 | # Save off become user temporarily because auth() resets it |
| 34 | become = self.saved_become_user |
| 35 | await self.auth(self.username, self.password) |
| 36 | |
| 37 | if become: |
| 38 | await self.become_user(become) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 39 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 40 | async def send_stream(self, msg): |
| 41 | async def proc(): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 42 | await self.socket.send(msg) |
| 43 | return await self.socket.recv() |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 44 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 45 | return await self._send_wrapper(proc) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 46 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 47 | async def _set_mode(self, new_mode): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 48 | async def stream_to_normal(): |
| 49 | await self.socket.send("END") |
| 50 | return await self.socket.recv() |
| 51 | |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 52 | if new_mode == self.MODE_NORMAL and self.mode == self.MODE_GET_STREAM: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 53 | r = await self._send_wrapper(stream_to_normal) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 54 | if r != "ok": |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 55 | self.check_invoke_error(r) |
| 56 | raise ConnectionError("Unable to transition to normal mode: Bad response from server %r" % r) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 57 | elif new_mode == self.MODE_GET_STREAM and self.mode == self.MODE_NORMAL: |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 58 | r = await self.invoke({"get-stream": None}) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 59 | if r != "ok": |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 60 | raise ConnectionError("Unable to transition to stream mode: Bad response from server %r" % r) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 61 | elif new_mode != self.mode: |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 62 | raise Exception( |
| 63 | "Undefined mode transition %r -> %r" % (self.mode, new_mode) |
| 64 | ) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 65 | |
| 66 | self.mode = new_mode |
| 67 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 68 | async def get_unihash(self, method, taskhash): |
| 69 | await self._set_mode(self.MODE_GET_STREAM) |
| 70 | r = await self.send_stream("%s %s" % (method, taskhash)) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 71 | if not r: |
| 72 | return None |
| 73 | return r |
| 74 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 75 | async def report_unihash(self, taskhash, method, outhash, unihash, extra={}): |
| 76 | await self._set_mode(self.MODE_NORMAL) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 77 | m = extra.copy() |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 78 | m["taskhash"] = taskhash |
| 79 | m["method"] = method |
| 80 | m["outhash"] = outhash |
| 81 | m["unihash"] = unihash |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 82 | return await self.invoke({"report": m}) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 83 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 84 | async def report_unihash_equiv(self, taskhash, method, unihash, extra={}): |
| 85 | await self._set_mode(self.MODE_NORMAL) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 86 | m = extra.copy() |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 87 | m["taskhash"] = taskhash |
| 88 | m["method"] = method |
| 89 | m["unihash"] = unihash |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 90 | return await self.invoke({"report-equiv": m}) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 91 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 92 | async def get_taskhash(self, method, taskhash, all_properties=False): |
| 93 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 94 | return await self.invoke( |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 95 | {"get": {"taskhash": taskhash, "method": method, "all": all_properties}} |
| 96 | ) |
Andrew Geissler | 475cb72 | 2020-07-10 16:00:51 -0500 | [diff] [blame] | 97 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 98 | async def get_outhash(self, method, outhash, taskhash, with_unihash=True): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 99 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 100 | return await self.invoke( |
| 101 | { |
| 102 | "get-outhash": { |
| 103 | "outhash": outhash, |
| 104 | "taskhash": taskhash, |
| 105 | "method": method, |
| 106 | "with_unihash": with_unihash, |
| 107 | } |
| 108 | } |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 109 | ) |
| 110 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 111 | async def get_stats(self): |
| 112 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 113 | return await self.invoke({"get-stats": None}) |
Brad Bishop | a34c030 | 2019-09-23 22:34:48 -0400 | [diff] [blame] | 114 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 115 | async def reset_stats(self): |
| 116 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 117 | return await self.invoke({"reset-stats": None}) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 118 | |
| 119 | async def backfill_wait(self): |
| 120 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 121 | return (await self.invoke({"backfill-wait": None}))["tasks"] |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 122 | |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 123 | async def remove(self, where): |
| 124 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 125 | return await self.invoke({"remove": {"where": where}}) |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 126 | |
| 127 | async def clean_unused(self, max_age): |
| 128 | await self._set_mode(self.MODE_NORMAL) |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 129 | return await self.invoke({"clean-unused": {"max_age_seconds": max_age}}) |
| 130 | |
| 131 | async def auth(self, username, token): |
| 132 | await self._set_mode(self.MODE_NORMAL) |
| 133 | result = await self.invoke({"auth": {"username": username, "token": token}}) |
| 134 | self.username = username |
| 135 | self.password = token |
| 136 | self.saved_become_user = None |
| 137 | return result |
| 138 | |
| 139 | async def refresh_token(self, username=None): |
| 140 | await self._set_mode(self.MODE_NORMAL) |
| 141 | m = {} |
| 142 | if username: |
| 143 | m["username"] = username |
| 144 | result = await self.invoke({"refresh-token": m}) |
| 145 | if ( |
| 146 | self.username |
| 147 | and not self.saved_become_user |
| 148 | and result["username"] == self.username |
| 149 | ): |
| 150 | self.password = result["token"] |
| 151 | return result |
| 152 | |
| 153 | async def set_user_perms(self, username, permissions): |
| 154 | await self._set_mode(self.MODE_NORMAL) |
| 155 | return await self.invoke( |
| 156 | {"set-user-perms": {"username": username, "permissions": permissions}} |
| 157 | ) |
| 158 | |
| 159 | async def get_user(self, username=None): |
| 160 | await self._set_mode(self.MODE_NORMAL) |
| 161 | m = {} |
| 162 | if username: |
| 163 | m["username"] = username |
| 164 | return await self.invoke({"get-user": m}) |
| 165 | |
| 166 | async def get_all_users(self): |
| 167 | await self._set_mode(self.MODE_NORMAL) |
| 168 | return (await self.invoke({"get-all-users": {}}))["users"] |
| 169 | |
| 170 | async def new_user(self, username, permissions): |
| 171 | await self._set_mode(self.MODE_NORMAL) |
| 172 | return await self.invoke( |
| 173 | {"new-user": {"username": username, "permissions": permissions}} |
| 174 | ) |
| 175 | |
| 176 | async def delete_user(self, username): |
| 177 | await self._set_mode(self.MODE_NORMAL) |
| 178 | return await self.invoke({"delete-user": {"username": username}}) |
| 179 | |
| 180 | async def become_user(self, username): |
| 181 | await self._set_mode(self.MODE_NORMAL) |
| 182 | result = await self.invoke({"become-user": {"username": username}}) |
| 183 | if username == self.username: |
| 184 | self.saved_become_user = None |
| 185 | else: |
| 186 | self.saved_become_user = username |
| 187 | return result |
| 188 | |
| 189 | async def get_db_usage(self): |
| 190 | await self._set_mode(self.MODE_NORMAL) |
| 191 | return (await self.invoke({"get-db-usage": {}}))["usage"] |
| 192 | |
| 193 | async def get_db_query_columns(self): |
| 194 | await self._set_mode(self.MODE_NORMAL) |
| 195 | return (await self.invoke({"get-db-query-columns": {}}))["columns"] |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 196 | |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 197 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 198 | class Client(bb.asyncrpc.Client): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 199 | def __init__(self, username=None, password=None): |
| 200 | self.username = username |
| 201 | self.password = password |
| 202 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 203 | super().__init__() |
| 204 | self._add_methods( |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 205 | "connect_tcp", |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 206 | "connect_websocket", |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 207 | "get_unihash", |
| 208 | "report_unihash", |
| 209 | "report_unihash_equiv", |
| 210 | "get_taskhash", |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 211 | "get_outhash", |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 212 | "get_stats", |
| 213 | "reset_stats", |
| 214 | "backfill_wait", |
Andrew Geissler | 2013739 | 2023-10-12 04:59:14 -0600 | [diff] [blame] | 215 | "remove", |
| 216 | "clean_unused", |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 217 | "auth", |
| 218 | "refresh_token", |
| 219 | "set_user_perms", |
| 220 | "get_user", |
| 221 | "get_all_users", |
| 222 | "new_user", |
| 223 | "delete_user", |
| 224 | "become_user", |
| 225 | "get_db_usage", |
| 226 | "get_db_query_columns", |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 227 | ) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 228 | |
Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 229 | def _get_async_client(self): |
Patrick Williams | ac13d5f | 2023-11-24 18:59:46 -0600 | [diff] [blame] | 230 | return AsyncClient(self.username, self.password) |