Skip to content

Commit 99632a4

Browse files
committed
v1.3.0: versie rapporteren bij opstart via MQTT retain
1 parent 6f20eac commit 99632a4

1 file changed

Lines changed: 31 additions & 2 deletions

File tree

go-deploy

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ from pathlib import Path
1818

1919
import paho.mqtt.client as mqtt
2020

21-
VERSION = "1.2.1"
21+
VERSION = "1.3.0"
2222
CONFIG_PATH = os.environ.get("GO_DEPLOY_CONFIG", "/etc/go-deploy/go-deploy.conf")
23+
APP_STATE_FILE = "/etc/go-deploy/current-app.json"
2324

2425
logging.basicConfig(
2526
level=logging.INFO,
@@ -43,6 +44,23 @@ def load_config(path: str) -> configparser.ConfigParser:
4344
return cfg
4445

4546

47+
def read_app_state() -> dict:
48+
try:
49+
with open(APP_STATE_FILE, "r") as f:
50+
return json.load(f)
51+
except Exception:
52+
return {}
53+
54+
55+
def write_app_state(version: str, app_id: str) -> None:
56+
try:
57+
os.makedirs(os.path.dirname(APP_STATE_FILE), exist_ok=True)
58+
with open(APP_STATE_FILE, "w") as f:
59+
json.dump({"version": version, "app_id": app_id}, f)
60+
except Exception as exc:
61+
log.warning("Kan app state niet opslaan: %s", exc)
62+
63+
4664
def sha256_file(path: str) -> str:
4765
h = hashlib.sha256()
4866
with open(path, "rb") as f:
@@ -149,6 +167,16 @@ class Agent:
149167
log.info("Verbonden met broker %s:%d", self.broker, self.port)
150168
client.subscribe([(self.cmd_topic, 1), (self.cmd_tunnel_topic, 1)])
151169
log.info("Gesubscribed op: %s en %s", self.cmd_topic, self.cmd_tunnel_topic)
170+
state = read_app_state()
171+
if state.get("version"):
172+
pub = json.dumps({
173+
"status": "running",
174+
"version": state["version"],
175+
"app_id": state.get("app_id", self.app_id),
176+
"agent_version": VERSION,
177+
})
178+
client.publish(self.status_topic, pub, qos=1, retain=True)
179+
log.info("Geinstalleerde versie gepubliceerd: %s", state["version"])
152180
else:
153181
log.error("Verbinding geweigerd door broker (rc=%d)", rc)
154182

@@ -228,9 +256,10 @@ class Agent:
228256
"agent_version": VERSION,
229257
**({"error": error} if error else {}),
230258
})
231-
client.publish(self.status_topic, status_payload, qos=1, retain=False)
259+
client.publish(self.status_topic, status_payload, qos=1, retain=True)
232260

233261
if ok:
262+
write_app_state(version, self.app_id)
234263
log.info("Update geslaagd — versie %s geinstalleerd", version)
235264
else:
236265
log.error("Update mislukt — %s", error)

0 commit comments

Comments
 (0)