Skip to content

Commit d006728

Browse files
committed
chore: migration from half-baked python-sdk to supported pysdk provided by nebius
1 parent 565d6de commit d006728

File tree

7 files changed

+239
-249
lines changed

7 files changed

+239
-249
lines changed

.github/actions/nebius_runner_create/action.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ runs:
9494
- name: install dependencies
9595
shell: bash
9696
run: |
97-
pip install https://github.com/librarian/python-sdk/releases/download/v0.1.1/nebiusai-0.1.1-py3-none-any.whl
98-
pip install PyGithub==2.2.0
97+
pip install PyGithub==2.2.0 nebius
9998
- name: create vm
10099
id: create-vm
101100
shell: bash

.github/actions/nebius_runner_remove/action.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ runs:
3434
- name: install dependencies
3535
shell: bash
3636
run: |
37-
pip install https://github.com/librarian/python-sdk/releases/download/v0.1.1/nebiusai-0.1.1-py3-none-any.whl
38-
pip install PyGithub==2.2.0
37+
pip install PyGithub==2.2.0 nebius
3938
- name: remove vm
4039
id: remove-vm
4140
shell: bash

.github/actions/prepare/action.yaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ runs:
1111
tzdata python3-dev python3-pip antlr3 libidn11-dev file distcc strace qemu-kvm \
1212
qemu-utils dpkg-dev atop pigz pbzip2 xz-utils pixz gdb unzip
1313
sudo apt-get remove -y unattended-upgrades
14-
sudo pip install https://github.com/librarian/python-sdk/releases/download/v0.1.1/nebiusai-0.1.1-py3-none-any.whl
1514
# shellcheck disable=SC2102
1615
sudo pip3 install pytest pytest-timeout pytest-xdist setproctitle grpcio grpcio-tools \
17-
PyHamcrest tornado xmltodict pyarrow boto3 psutil yandexcloud==0.330.0 PyGithub==2.5.0 pyinstaller \
18-
cryptography packaging six pyyaml rapidgzip pyOpenSSL typing-extensions==4.10.0
16+
PyHamcrest tornado xmltodict pyarrow boto3 psutil PyGithub==2.5.0 pyinstaller \
17+
cryptography packaging six pyyaml rapidgzip pyOpenSSL typing-extensions==4.10.0 nebius
1918
- name: add user github to kvm group if exists
2019
shell: bash
2120
run: |

.github/scripts/github-runner.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ pyOpenSSL==24.2.1
7676
packaging
7777
rapidgzip
7878
typing-extensions==4.10.0
79+
nebius
7980
EOF
80-
sudo pip3 install https://github.com/librarian/python-sdk/releases/download/v0.1.1/nebiusai-0.1.1-py3-none-any.whl
8181
sudo pip3 install -r /tmp/requirements.txt
8282
curl -L "https://github.com/ccache/ccache/releases/download/v${CCACHE_VERSION}/ccache-${CCACHE_VERSION}-linux-${OS_ARCH}.tar.xz" | sudo tar -xJ -C /usr/local/bin/ --strip-components=1 --no-same-owner "ccache-${CCACHE_VERSION}-linux-${OS_ARCH}/ccache"
8383
sudo apt-get remove -y unattended-upgrades

.github/scripts/nebius-manage-images.py

+51-43
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
import os
2-
import json
3-
import grpc
2+
import asyncio
43
import math
54
import logging
65
import argparse
76
from github import Github
8-
from datetime import datetime, timezone
9-
from nebius.compute.v1.image_pb2 import Image, ImageStatus
10-
from nebius.compute.v1.image_service_pb2 import ListImagesRequest
7+
from nebius.sdk import SDK
8+
from nebius.aio.service_error import RequestError
9+
from nebius.api.nebius.compute.v1 import (
10+
ListImagesRequest,
11+
ImageServiceClient,
12+
)
1113

12-
from nebius.compute.v1.image_service_pb2_grpc import ImageServiceStub
13-
import nebius.compute.v1.image_service_pb2 as image_service_pb2
14-
from nebiusai import SDK, RetryInterceptor, backoff_linear_with_jitter
14+
SENSITIVE_DATA_VALUES = {}
15+
if os.environ.get("GITHUB_TOKEN"):
16+
SENSITIVE_DATA_VALUES["github_token"] = os.environ.get("GITHUB_TOKEN")
1517

16-
logging.basicConfig(
17-
level=logging.INFO, format="%(asctime)s: %(levelname)s: %(message)s"
18-
)
19-
logger = logging.getLogger(__name__)
18+
19+
class MaskingFormatter(logging.Formatter):
20+
@staticmethod
21+
def mask_sensitive_data(msg):
22+
# Iterate over the patterns and replace sensitive data with '***'
23+
for pattern_name, pattern in SENSITIVE_DATA_VALUES.items():
24+
msg = msg.replace(pattern, f"[{pattern_name}=***]")
25+
return msg
26+
27+
def format(self, record):
28+
original = logging.Formatter.format(self, record)
29+
return self.mask_sensitive_data(original)
30+
31+
32+
formatter = MaskingFormatter("%(asctime)s: %(levelname)s: %(message)s")
33+
console_handler = logging.StreamHandler()
34+
console_handler.setLevel(logging.INFO)
35+
console_handler.setFormatter(formatter)
36+
37+
logger = logging.getLogger()
38+
logger.addHandler(console_handler)
39+
logger.setLevel(logging.INFO)
2040

2141
# they won't appear in the list of images
2242
# but they will be protected from deletion anyway
@@ -25,10 +45,6 @@
2545
]
2646

2747

28-
def status_to_string(image: Image) -> str:
29-
return ImageStatus.State.Name(image.status.state)
30-
31-
3248
def convert_size(size_bytes):
3349
if size_bytes == 0:
3450
return "0 B"
@@ -39,7 +55,7 @@ def convert_size(size_bytes):
3955
return f"{s} {size_name[i]}"
4056

4157

42-
def main(
58+
async def main(
4359
sdk: SDK,
4460
github_token: str,
4561
github_repository: str,
@@ -68,19 +84,21 @@ def main(
6884
else:
6985
logger.info("Would set %s (new) = %s", image_variable_name, new_image_id)
7086

87+
service = ImageServiceClient(sdk)
7188
request = ListImagesRequest(
7289
parent_id=parent_id,
7390
filter=f"family IN ('{image_family_name}') AND status = 'READY'",
7491
)
92+
try:
93+
response = await service.list(request)
94+
except RequestError as e:
95+
logger.error("Failed to list images: %s", e)
96+
return
7597

76-
client = sdk.client(image_service_pb2, ImageServiceStub)
77-
response = client.List(request)
7898
candidate_images = []
7999
for image in response.items:
80-
status = status_to_string(image)
81-
created_at = datetime.fromtimestamp(
82-
image.metadata.created_at.seconds, tz=timezone.utc
83-
)
100+
status = image.status.state.name
101+
created_at = image.metadata.created_at
84102
storage_size = convert_size(image.status.storage_size_bytes)
85103
min_disk_size = convert_size(image.status.min_disk_size_bytes)
86104
prefix = " (PROTECTED)"
@@ -158,25 +176,15 @@ def main(
158176
args = parser.parse_args()
159177
logger.info(args)
160178

161-
interceptor = RetryInterceptor(
162-
max_retry_count=30,
163-
retriable_codes=[grpc.StatusCode.UNAVAILABLE],
164-
back_off_func=backoff_linear_with_jitter(5, 0),
165-
)
166-
167-
with open(args.service_account_key, "r") as fp:
168-
sdk = SDK(
169-
service_account_key=json.load(fp),
170-
endpoint=args.api_endpoint,
171-
interceptor=interceptor,
179+
sdk = SDK(credentials_file_name=args.service_account_key)
180+
asyncio.run(
181+
main(
182+
sdk=sdk,
183+
github_token=args.github_token,
184+
github_repository=args.github_repo,
185+
new_image_id=args.new_image_id,
186+
image_variable_name=args.image_variable_name,
187+
update_image_id=args.update_image_id,
188+
parent_id=args.parent_id,
172189
)
173-
174-
main(
175-
sdk=sdk,
176-
github_token=args.github_token,
177-
github_repository=args.github_repo,
178-
new_image_id=args.new_image_id,
179-
image_variable_name=args.image_variable_name,
180-
update_image_id=args.update_image_id,
181-
parent_id=args.parent_id,
182190
)

0 commit comments

Comments
 (0)