Skip to content

Commit

Permalink
implemented extraction of config id and commit hash on agent
Browse files Browse the repository at this point in the history
  • Loading branch information
adrblo committed Jan 9, 2025
1 parent f88ec56 commit 9c13e92
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
4 changes: 4 additions & 0 deletions agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__/
venv/
state.json
.env
35 changes: 24 additions & 11 deletions agent/thymis_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import psutil
import requests
from git import Tuple

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -38,13 +39,25 @@ def retry_if_fails(self, interval, action, actionargs=()):

class Agent:
controller_host: str
config_id: str
commit_hash: str

def __init__(self, path: pathlib.Path, controller_host, config_id, commit_hash):
def __init__(self, path: pathlib.Path, controller_host):
self.controller_host = controller_host
self.config_id = config_id
self.commit_hash = commit_hash

def detect_system_config(self) -> Tuple[str, str]:
config_id = None
commit_hash = None
with open("/etc/os-release", "r") as f:
for line in f:
if line.startswith("IMAGE_ID="):
config_id = line.split("=")[1].strip().replace('"', "")
if config_id == "":
config_id = None
if line.startswith("IMAGE_VERSION="):
commit_hash = line.split("=")[1].strip().replace('"', "")
if commit_hash == "":
commit_hash = None

return config_id, commit_hash

def detect_hostname(self):
return socket.gethostname()
Expand Down Expand Up @@ -103,14 +116,16 @@ def notify(self) -> bool:
logger.error("Controller host not set")
raise ValueError("Controller host not set")

config_id, commit_hash = self.detect_system_config()

json_data = {
"commit_hash": self.commit_hash,
"config_id": self.config_id,
"commit_hash": commit_hash,
"config_id": config_id,
"hardware_ids": self.detect_hardware_id(),
"public_key": self.detect_public_key(),
"ip_addresses": self.detect_ip_addresses(),
}
print(json_data)
logger.info("Sending notify request: %s", json_data)
response = requests.post(f"{self.controller_host}/agent/notify", json=json_data)

if response.status_code != 200:
Expand All @@ -130,15 +145,13 @@ def main():
logging.basicConfig(level=logging.DEBUG)

controller_host = os.getenv("CONTROLLER_HOST")
config_id = os.getenv("CONFIG_ID", None)
commit_hash = os.getenv("COMMIT_HASH", None)

if not controller_host:
raise ValueError("CONTROLLER_HOST environment variable is required")

path = pathlib.Path("/var") / "lib" / "thymis" / "agent.json"

agent = Agent(path, controller_host, config_id, commit_hash)
agent = Agent(path, controller_host)
scheduler = AgentScheduler()

scheduler.retry_if_fails(10, agent.notify)
Expand Down
4 changes: 4 additions & 0 deletions controller/thymis_controller/routers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def device_notify(
db_session: SessionAD,
project: ProjectAD,
):
logger.debug(
f"Device with public key {device_notify_request.public_key} notifies controller with commit hash {device_notify_request.commit_hash} and config id {device_notify_request.config_id}"
)

# TODO verify authenticity of device in general (only permitted devices) tag libguestfs

# TODO verify authenticity of device of its key pair
Expand Down

0 comments on commit 9c13e92

Please sign in to comment.