cgcsdk 1.0.6__py3-none-any.whl → 1.0.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. cgc/.env +1 -1
  2. cgc/CHANGELOG.md +30 -0
  3. cgc/cgc.py +7 -1
  4. cgc/commands/auth/__init__.py +2 -2
  5. cgc/commands/auth/auth_cmd.py +14 -2
  6. cgc/commands/auth/auth_responses.py +9 -2
  7. cgc/commands/auth/auth_utils.py +6 -5
  8. cgc/commands/cgc_cmd.py +28 -3
  9. cgc/commands/cgc_cmd_responses.py +8 -0
  10. cgc/commands/cgc_models.py +9 -0
  11. cgc/commands/compute/compute_models.py +3 -23
  12. cgc/commands/compute/compute_utills.py +16 -6
  13. cgc/commands/db/db_cmd.py +7 -1
  14. cgc/commands/db/db_models.py +37 -0
  15. cgc/commands/exceptions.py +8 -0
  16. cgc/commands/jobs/__init__.py +10 -0
  17. cgc/commands/jobs/job_utils.py +180 -0
  18. cgc/commands/jobs/jobs_cmd.py +238 -0
  19. cgc/commands/jobs/jobs_responses.py +52 -0
  20. cgc/commands/keys/__init__.py +5 -0
  21. cgc/commands/keys/keys_cmd.py +176 -0
  22. cgc/commands/keys/keys_models.py +16 -0
  23. cgc/commands/keys/keys_responses.py +47 -0
  24. cgc/commands/keys/keys_utils.py +79 -0
  25. cgc/commands/resource/resource_cmd.py +2 -1
  26. cgc/sdk/__init__.py +1 -2
  27. cgc/sdk/job.py +147 -0
  28. cgc/sdk/resource.py +1 -0
  29. cgc/utils/__init__.py +8 -0
  30. cgc/utils/config_utils.py +5 -1
  31. cgc/utils/consts/env_consts.py +1 -1
  32. cgc/utils/custom_exceptions.py +8 -1
  33. cgc/utils/message_utils.py +1 -1
  34. cgc/utils/prepare_headers.py +22 -13
  35. cgc/utils/requests_helper.py +1 -3
  36. cgcsdk-1.0.9.dist-info/METADATA +66 -0
  37. {cgcsdk-1.0.6.dist-info → cgcsdk-1.0.9.dist-info}/RECORD +41 -32
  38. cgc/sdk/handlers.py +0 -24
  39. cgc/sdk/mongodb.py +0 -204
  40. cgc/sdk/redis.py +0 -91
  41. cgcsdk-1.0.6.dist-info/METADATA +0 -39
  42. {cgcsdk-1.0.6.dist-info → cgcsdk-1.0.9.dist-info}/LICENSE +0 -0
  43. {cgcsdk-1.0.6.dist-info → cgcsdk-1.0.9.dist-info}/WHEEL +0 -0
  44. {cgcsdk-1.0.6.dist-info → cgcsdk-1.0.9.dist-info}/entry_points.txt +0 -0
  45. {cgcsdk-1.0.6.dist-info → cgcsdk-1.0.9.dist-info}/top_level.txt +0 -0
cgc/sdk/mongodb.py DELETED
@@ -1,204 +0,0 @@
1
- from pymongo.errors import ConnectionFailure
2
- from pymongo import MongoClient, DESCENDING, ASCENDING
3
- from cgc.sdk.handlers import exception_handler
4
-
5
-
6
- class MongoConnector:
7
- """
8
- :param database_name: Database name
9
- :type database_name: str
10
- :param hosts: Host address defined as IP or DNS Name with port ex. db.example.com:27017
11
- :type hosts: list
12
- :param username: Username used for connection, not required, defaults to None
13
- :type username: str | None
14
- :param password: Password for Username, used with Username, defaults to None
15
- :type password: str | None
16
- :param authsource: Database to authenticate user with, defaults to admin
17
- :type authsource: str
18
- """
19
-
20
- def __init__(
21
- self,
22
- database_name: str,
23
- hosts: str,
24
- username: str = None,
25
- password: str = None,
26
- authsource: str = "admin",
27
- replica_set: str = None,
28
- ) -> None:
29
- self._database_name = database_name
30
- self._hosts = hosts
31
- assert type(hosts) is str
32
- 'hosts must be a str of host addresses ex. "db.example.com:27017,db2.example.com:27017"'
33
- self._username = username
34
- self._password = password
35
- self._authsource = authsource
36
- _extra_args = []
37
- if replica_set:
38
- _extra_args.append(f"replicaSet={replica_set}")
39
- if authsource:
40
- _extra_args.append(f"authSource={authsource}")
41
- _extra_args.append("readPreference=primaryPreferred")
42
- _extra_args.append("ssl=false")
43
- self._extra_args = "&".join(_extra_args)
44
- print(self._extra_args)
45
- self.connect()
46
-
47
- def disconnect(self):
48
- print(f"Disconnecting from MongoDB: {self._hosts}")
49
- self._mongo_client.close()
50
- print(f"Disconnected from MongoDB")
51
-
52
- def connect(self):
53
- _credentials = (
54
- ":".join([self._username, self._password]) if self._username else ""
55
- )
56
- _host_connection_string = (
57
- "@".join([_credentials, self._hosts]) if _credentials else self._hosts
58
- )
59
- while True:
60
- try:
61
- self._mongo_client = MongoClient(
62
- "mongodb://{}/{}?{}".format(
63
- _host_connection_string, self._database_name, self._extra_args
64
- )
65
- )
66
- self._mongo_client_server_info = self._mongo_client.server_info()
67
- print(f"Connected to MongoDB ({self._database_name}): {self._hosts}")
68
- self._db = self._mongo_client[self._database_name]
69
- break
70
- except (ConnectionFailure,) as e:
71
- print(f"MongoDB connection error: {e}")
72
- print(f"retrying to connect...")
73
-
74
- def _select_collection(self, collection_name: str):
75
- return self._db[collection_name]
76
-
77
- def get_pymongo_client(self):
78
- return self._mongo_client
79
-
80
- @staticmethod
81
- def _ascending():
82
- return ASCENDING
83
-
84
- @staticmethod
85
- def _descending():
86
- return DESCENDING
87
-
88
- @exception_handler
89
- def find(self, collection_name: str, query: dict, session=None):
90
- collection = self._select_collection(collection_name)
91
- return collection.find(query, session=session)
92
-
93
- @exception_handler
94
- def find_one(self, collection_name: str, query: dict, session=None):
95
- collection = self._select_collection(collection_name)
96
- return collection.find_one(query, session=session)
97
-
98
- @exception_handler
99
- def insert_one(self, collection_name: str, query: dict, session=None):
100
- collection = self._select_collection(collection_name)
101
- return collection.insert_one(query, session=session)
102
-
103
- @exception_handler
104
- def delete_one(self, collection_name: str, query: dict, session=None):
105
- collection = self._select_collection(collection_name)
106
- return collection.delete_one(query, session=session)
107
-
108
- @exception_handler
109
- def aggregate(self, collection_name: str, pipeline, session=None):
110
- collection = self._select_collection(collection_name)
111
- return collection.aggregate(pipeline=pipeline, session=session)
112
-
113
- @exception_handler
114
- def count_documents(self, collection_name: str, query: dict, session=None):
115
- collection = self._select_collection(collection_name)
116
- return collection.count_documents(query, session=session)
117
-
118
- @exception_handler
119
- def update_one(
120
- self,
121
- collection_name: str,
122
- query: dict,
123
- update: dict,
124
- upsert: bool = False,
125
- session=None,
126
- ):
127
- collection = self._select_collection(collection_name)
128
- return collection.update_one(query, update, upsert=upsert, session=session)
129
-
130
- @exception_handler
131
- def watch(
132
- self,
133
- collection_name: str,
134
- operation_before_watch,
135
- operation_on_event,
136
- first_operation_kwargs={},
137
- second_operation_kwargs={},
138
- ):
139
- """Function to watch for changes in a collection. Preferred to run over threading as daemon.
140
-
141
- :param collection_name: Name of the collection to watch
142
- :type collection_name: str
143
- :param operation_on_event: Function to run on event
144
- :type operation_on_event: function
145
- """
146
- operation_before_watch(**first_operation_kwargs)
147
- while True:
148
- print("Creating new watch cursor")
149
- watch_cursor = self._select_collection(
150
- collection_name=collection_name
151
- ).watch()
152
- print("Watching: {}.{}\n".format(self._database_name, collection_name))
153
- for d in watch_cursor:
154
- if d["operationType"] == "invalidate":
155
- print(
156
- "Watch cursor invalidated (deleted collection: {}?)".format(
157
- collection_name
158
- )
159
- )
160
- print("Closing watch cursor")
161
- watch_cursor.close()
162
- break
163
- else:
164
- operation_on_event(d, **second_operation_kwargs)
165
- watch_cursor.close()
166
- return d
167
- else:
168
- continue
169
- break
170
-
171
- @staticmethod
172
- def example_operation_for_watch(d):
173
- print(d)
174
- # del d["fullDocument"]["_id"]
175
- # print("local time : {}".format(datetime.utcnow()))
176
- # print("cluster time : {}".format(d["clusterTime"].as_datetime()))
177
- # print("collection : {}.{}".format(d["ns"]["db"], d["ns"]["coll"]))
178
- # try:
179
- # print("doc : {}".format(d["fullDocument"]))
180
- # except KeyError:
181
- # pass
182
-
183
-
184
- def get_mongo_access(
185
- app_name: str, password: str, database: str = "db", restart: bool = False
186
- ):
187
- global _mongo_access
188
-
189
- def init_access():
190
- global _mongo_access
191
- _mongo_access = MongoConnector(
192
- database_name=database,
193
- hosts=f"{app_name}:27017",
194
- username="admin",
195
- password=password,
196
- )
197
-
198
- try:
199
- if not isinstance(_mongo_access, MongoConnector) or restart:
200
- init_access()
201
- except NameError:
202
- init_access()
203
- pass
204
- return _mongo_access
cgc/sdk/redis.py DELETED
@@ -1,91 +0,0 @@
1
- import redis.asyncio as redis_async
2
- import redis
3
-
4
-
5
- class RedisConnector:
6
- redis_client_async = None
7
- redis_client = None
8
-
9
- def __init__(
10
- self, host: str, password: str = None, decode_responses: bool = False
11
- ) -> None:
12
- self._host = host
13
- assert type(host) is str
14
- "host must be a str containing redis app name"
15
- self._password = password
16
- self._decode_responses = decode_responses
17
-
18
- def connect(self, async_client: bool = False):
19
- while True:
20
- try:
21
- if not async_client:
22
- self.redis_client = redis.Redis(
23
- host=self._host,
24
- port=6379,
25
- password=self._password,
26
- decode_responses=self._decode_responses,
27
- )
28
- else:
29
- self.redis_client_async = redis_async.Redis(
30
- host=self._host,
31
- port=6379,
32
- password=self._password,
33
- decode_responses=self._decode_responses,
34
- )
35
- print(f"Connected to Redis: {self._host}")
36
- break
37
- except (redis.ConnectionError,) as e:
38
- print(f"Redis connection error: {e}")
39
- print(f"retrying to connect...")
40
-
41
- def get_redis_client(self):
42
- if self.redis_client is None:
43
- self.connect()
44
- return self.redis_client
45
-
46
- def get_redis_client_async(self):
47
- if self.redis_client_async is None:
48
- self.connect(async_client=True)
49
- return self.redis_client_async
50
-
51
-
52
- def get_redis_access(
53
- app_name: str,
54
- password: str,
55
- decode_responses: bool = False,
56
- restart: bool = False,
57
- async_client=False,
58
- ):
59
- global _redis_access
60
- global _redis_access_async
61
-
62
- def init_access(async_client=False):
63
- global _redis_access
64
- global _redis_access_async
65
-
66
- _redis_access = None
67
- _redis_access_async = None
68
-
69
- if not async_client:
70
- _redis_access = RedisConnector(
71
- host=app_name, password=password, decode_responses=decode_responses
72
- )
73
- else:
74
- _redis_access_async = RedisConnector(
75
- host=app_name, password=password, decode_responses=decode_responses
76
- )
77
-
78
- try:
79
- if not async_client:
80
- if not isinstance(_redis_access, RedisConnector) or restart:
81
- init_access()
82
- else:
83
- if not isinstance(_redis_access_async, RedisConnector) or restart:
84
- init_access(True)
85
- except NameError:
86
- if not async_client:
87
- init_access()
88
- else:
89
- init_access(True)
90
- pass
91
- return _redis_access if not async_client else _redis_access_async
@@ -1,39 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: cgcsdk
3
- Version: 1.0.6
4
- Summary: Comtegra GPU Cloud REST API client
5
- Author: Comtegra AI Team
6
- Author-email: ai@comtegra.pl
7
- License: BSD 2-clause
8
- Project-URL: Documentation, https://example.com/documentation/
9
- Project-URL: GitHub, https://github.com/foobar/foobar/
10
- Project-URL: Changelog, https://github.com/foobar/foobar/blob/master/CHANGELOG.md
11
- Keywords: cloud,sdk,orchestrator,kubernetes,jupyter-notebook
12
- Classifier: Development Status :: 1 - Planning
13
- Classifier: Intended Audience :: Science/Research
14
- Classifier: License :: OSI Approved :: BSD License
15
- Classifier: Operating System :: POSIX :: Linux
16
- Classifier: Programming Language :: Python :: 3
17
- Classifier: Programming Language :: Python :: 3.4
18
- Classifier: Programming Language :: Python :: 3.5
19
- Classifier: Programming Language :: Python :: 3.8
20
- Classifier: Programming Language :: Python :: 3.9
21
- Classifier: Programming Language :: Python :: 3.10
22
- Description-Content-Type: text/markdown
23
- License-File: LICENSE
24
- Requires-Dist: click
25
- Requires-Dist: python-dotenv
26
- Requires-Dist: tabulate
27
- Requires-Dist: pycryptodomex
28
- Requires-Dist: paramiko >=2.11
29
- Requires-Dist: statsd
30
- Requires-Dist: requests
31
- Requires-Dist: setuptools
32
- Requires-Dist: colorama
33
- Requires-Dist: redis
34
- Requires-Dist: pymongo
35
- Requires-Dist: psycopg2-binary
36
-
37
- # CGC Client - K8s Cloud
38
-
39
- cgc command line client for k8s Comtegra cloud environment.
File without changes