Skip to content

Commit

Permalink
implemented backend migration of hostkeys to deployment_info
Browse files Browse the repository at this point in the history
  • Loading branch information
adrblo committed Jan 9, 2025
1 parent 9c13e92 commit 4e1c8ba
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 294 deletions.
55 changes: 0 additions & 55 deletions controller/tests/routes/api/test_hostkey_interfaces.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
Create Date: 2024-12-18 02:05:04.287386
"""
import uuid

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import sqlite
Expand All @@ -18,12 +20,12 @@

def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
deployment_info = op.create_table(
"deployment_info",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("ssh_public_key", sa.String(), nullable=False),
sa.Column("deployed_config_commit", sa.String(), nullable=False),
sa.Column("deployed_config_id", sa.String(), nullable=False),
sa.Column("deployed_config_commit", sa.String(), nullable=True),
sa.Column("deployed_config_id", sa.String(), nullable=True),
sa.Column("reachable_deployed_host", sa.String(), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("ssh_public_key"),
Expand All @@ -49,8 +51,55 @@ def upgrade():
op.drop_table("images")
# ### end Alembic commands ###

conn = op.get_bind()
res = conn.execute(
sa.text("SELECT identifier, public_key, device_host FROM hostkeys")
)
results = res.fetchall()
hostkey_data = [
{
"id": uuid.uuid4(),
"deployed_config_id": identifier,
"ssh_public_key": public_key,
"reachable_deployed_host": device_host,
}
for identifier, public_key, device_host in results
]
op.bulk_insert(deployment_info, hostkey_data)
op.drop_index("ix_hostkeys_identifier", table_name="hostkeys")
op.drop_table("hostkeys")


def downgrade():
hostkeys = op.create_table(
"hostkeys",
sa.Column("identifier", sa.String(), nullable=False),
sa.Column("build_hash", sa.String(), nullable=True),
sa.Column("public_key", sa.String(), nullable=True),
sa.Column("device_host", sa.String(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("identifier"),
)
op.create_index(
op.f("ix_hostkeys_identifier"), "hostkeys", ["identifier"], unique=False
)

conn = op.get_bind()
res = conn.execute(
sa.text(
"SELECT ssh_public_key, deployed_config_id, reachable_deployed_host FROM deployment_info"
)
)
results = res.fetchall()
deployment_info_data = [
{
"identifier": deployed_config_id,
"public_key": ssh_public_key,
"device_host": reachable_deployed_host,
}
for ssh_public_key, deployed_config_id, reachable_deployed_host in results
]
op.bulk_insert(hostkeys, deployment_info_data)
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"images",
Expand Down
2 changes: 1 addition & 1 deletion controller/thymis_controller/crud/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import deployment_info, hardware_device, hostkey, task, web_session
from . import deployment_info, hardware_device, task, web_session
23 changes: 23 additions & 0 deletions controller/thymis_controller/crud/deployment_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ def check_if_ssh_public_key_exists(session: Session, ssh_public_key: str) -> boo
.first()
is not None
)


def get_all(session: Session):
return session.query(db_models.DeploymentInfo).all()


def get_device_host_by_config_id(session: Session, config_id: str) -> str | None:
di = (
session.query(db_models.DeploymentInfo)
.filter(db_models.DeploymentInfo.deployed_config_id == config_id)
.first()
)
return di.reachable_deployed_host if di else None


def get_by_config_id(
session: Session, config_id: str
) -> db_models.DeploymentInfo | None:
return (
session.query(db_models.DeploymentInfo)
.filter(db_models.DeploymentInfo.deployed_config_id == config_id)
.first()
)
135 changes: 0 additions & 135 deletions controller/thymis_controller/crud/hostkey.py

This file was deleted.

1 change: 0 additions & 1 deletion controller/thymis_controller/db_models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from .deployment_info import DeploymentInfo
from .hardware_device import HardwareDevice
from .hostkey import HostKey
from .task import Task
from .web_session import WebSession
4 changes: 2 additions & 2 deletions controller/thymis_controller/db_models/deployment_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class DeploymentInfo(Base):
)

ssh_public_key: Mapped[str] = mapped_column(nullable=False, unique=True)
deployed_config_commit: Mapped[str]
deployed_config_id: Mapped[str]
deployed_config_commit: Mapped[str] = mapped_column(nullable=True)
deployed_config_id: Mapped[str] = mapped_column(nullable=True)

reachable_deployed_host: Mapped[str | None]

Expand Down
14 changes: 0 additions & 14 deletions controller/thymis_controller/db_models/hostkey.py

This file was deleted.

11 changes: 10 additions & 1 deletion controller/thymis_controller/models/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from git import List
from pydantic import BaseModel, Field
from thymis_controller import db_models


class DeviceNotifyRequest(BaseModel):
commit_hash: str | None
config_id: str
config_id: str | None
hardware_ids: Dict[str, str | None]
public_key: str
ip_addresses: List[str]
Expand All @@ -22,6 +23,14 @@ class Hostkey(BaseModel):
public_key: str
device_host: str

@staticmethod
def from_deployment_info(deployment_info: db_models.DeploymentInfo) -> "Hostkey":
return Hostkey(
identifier=deployment_info.deployed_config_id,
public_key=deployment_info.ssh_public_key,
device_host=deployment_info.reachable_deployed_host,
)


class CreateHostkeyRequest(BaseModel):
public_key: str
Expand Down
7 changes: 4 additions & 3 deletions controller/thymis_controller/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,11 @@ def update_known_hosts(self, db_session: sqlalchemy.orm.Session):
tempfile.NamedTemporaryFile(delete=False).name
)

hostkeys = crud.hostkey.get_all(db_session)
deployment_infos = crud.deployment_info.get_all(db_session)
with open(self.known_hosts_path, "w", encoding="utf-8") as f:
for hostkey in hostkeys:
f.write(f"{hostkey.device_host} {hostkey.public_key}\n")
for di in deployment_infos:
if di.reachable_deployed_host and di.ssh_public_key:
f.write(f"{di.reachable_deployed_host} {di.ssh_public_key}\n")

logger.debug("Updated known_hosts file at %s", self.known_hosts_path)

Expand Down
Loading

0 comments on commit 4e1c8ba

Please sign in to comment.