Skip to content

Commit

Permalink
feat: upload scan to new server
Browse files Browse the repository at this point in the history
  • Loading branch information
tymees committed Oct 30, 2024
1 parent 1f88f44 commit 0e9206d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 65 deletions.
1 change: 1 addition & 0 deletions agent/humitifier/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config:
inventory: list[str]
pssh: dict
tasks: dict[str, str]
upload_endpoint = str

@classmethod
def load(cls) -> "Config":
Expand Down
25 changes: 25 additions & 0 deletions agent/humitifier/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
from sys import stdout

import asyncpg
import json
import requests
import time
from datetime import datetime
from rocketry import Rocketry
Expand Down Expand Up @@ -111,6 +113,29 @@ async def parse_facts():
conn = await asyncpg.connect(CONFIG.db)
rows = await conn.fetch("""SELECT name, host, scan, stdout, stderr, exception, exit_code FROM host_outputs""")
parsed_rows = [(row["name"], row["host"], row["scan"], parse_row_data(row)) for row in rows]

hosts = {row["host"] for row in rows}
host_data = []

for host in hosts:
host_data.append(
{
"host": host,
"data": {
name: parsed.to_sql()
for name, this_host, scan, parsed in parsed_rows
if this_host == host
},
}
)

await asyncio.to_thread(
requests.post,
CONFIG.upload_endpoint,
data=json.dumps(host_data),
headers={'Content-Type': 'application/json'}
)

await conn.executemany(
"""INSERT INTO facts(name, host, scan, data) VALUES($1, $2, $3, $4)""",
[(name, host, scan, json.dumps(parsed.to_sql())) for name, host, scan, parsed in parsed_rows],
Expand Down
101 changes: 36 additions & 65 deletions agent/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions agent/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ parallel-ssh = "^2.12.0"
rocketry = "^2.5.1"
pydantic = "<2.0.0" # Needed to keep compatibility with rocketry
asyncpg = "^0.29.0"
requests = "^2.32.3"


[tool.poetry.group.dev.dependencies]
Expand Down

0 comments on commit 0e9206d

Please sign in to comment.