-
Notifications
You must be signed in to change notification settings - Fork 0
feat: store distribution credentials in database #442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from sqlalchemy import Boolean, Integer, String | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
|
|
||
| from imgtests.database.models.base import Base | ||
|
|
||
|
|
||
| class DistributionConnection(Base): | ||
| __tablename__ = "distribution_connection" | ||
|
|
||
| id: Mapped[int] = mapped_column(primary_key=True) | ||
| name: Mapped[str] = mapped_column(String(100), unique=True) | ||
| host: Mapped[str] = mapped_column(String(100)) | ||
| user: Mapped[str] = mapped_column(String(100)) | ||
| password: Mapped[str] = mapped_column(String(100)) | ||
| port: Mapped[int] = mapped_column(Integer) | ||
| is_active: Mapped[bool] = mapped_column(Boolean, server_default="true") | ||
|
|
||
| def __repr__(self) -> str: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Обычно пишется так, чтобы по этой строке можно было точно воссоздать объект в коде, т.е. не хватает остальных полей. |
||
| return ( | ||
| f"DistributionConnection(name={self.name}, " | ||
| f"host={self.host}, user={self.user}, " | ||
| f"port={self.port}, is_active={self.is_active})" | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,16 +269,16 @@ def download(self, remotepath: Path, localpath: Path) -> ExecResult: | |
|
|
||
|
|
||
| def wait_remote( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ещё бы подтянуть изменения из |
||
| address_env: str, | ||
| user_env: str, | ||
| password_env: str, | ||
| port_env: str, | ||
| hostname: str, | ||
| username: str, | ||
| password: str, | ||
| port: int, | ||
| ) -> SSHClient | None: | ||
| wait_sec = 60 * 60 * 5 | ||
| step_sec = 60 | ||
| while wait_sec > 0: | ||
| try: | ||
| return SSHClient.build_from_env(address_env, user_env, password_env, port_env) | ||
| return SSHClient(hostname, username, password, port) | ||
| except paramiko.ssh_exception.SSHException: | ||
| logger.info("Waiting remote node to build and run image.") | ||
| sleep(step_sec) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
| from django.core.management.base import BaseCommand | ||
| from tests_interface.models import Distribution | ||
|
|
||
| from imgtests.database.database import ImgtestsDatabase | ||
|
|
||
| DEFAULT_DISTROS: list[dict[str, str | int]] = [ | ||
| { | ||
| "name": "yocto", | ||
|
|
@@ -18,6 +20,23 @@ | |
| }, | ||
| ] | ||
|
|
||
| DEFAULT_CONNECTIONS: list[dict[str, str | int | bool]] = [ | ||
| { | ||
| "name": "yocto", | ||
| "host": "10.5.0.10", | ||
| "user": "root", | ||
| "password": "", | ||
| "port": 2222, | ||
| }, | ||
| { | ||
| "name": "opensuse", | ||
| "host": "10.5.0.13", | ||
| "user": "suser", | ||
| "password": "password", | ||
| "port": 1616, | ||
| }, | ||
| ] | ||
|
Comment on lines
+23
to
+38
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Надо бы также из переменных окружения доставать host, user, password, port (с помощью env_var_to_type) и определять данный список только при наличии их (переменных окружения). |
||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| def handle(self, *args: Any, **options: Any) -> None: # noqa: ARG002 | ||
|
|
@@ -29,6 +48,16 @@ def handle(self, *args: Any, **options: Any) -> None: # noqa: ARG002 | |
| created += 1 | ||
| self.stdout.write(f"Created: {distro_data['display_name']}") | ||
|
|
||
| database = ImgtestsDatabase() | ||
| for connection_data in DEFAULT_CONNECTIONS: | ||
| name = str(connection_data["name"]) | ||
| if database.get_connection(name) is not None: | ||
| self.stdout.write(f"Connection for '{name}' already exists, keeping it.") | ||
| continue | ||
| database.upsert_connection(**connection_data) | ||
| self.stdout.write(f"Created connection for: {name}") | ||
| database.session.close_all() | ||
|
|
||
| self.stdout.write( | ||
| self.style.SUCCESS(f"Successfully initialized {created} default distributions"), | ||
| ) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ещё бы добавить путь до приватного ключа, чтобы была возможность безболезненно на них перейти и не хранить пароли в БД.