From 857a30d980071d236aefc9880d5634543d6ad331 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 4 Dec 2024 18:01:48 -0800 Subject: [PATCH] Pull image version from metadata file (#1599) Closes #1554 ![image](https://github.com/user-attachments/assets/fa51c0a3-a25e-4112-8ef2-990404c746d6) --- .../common/hardware/OsImageVersion.java | 55 +++++++++++++++++++ .../src/main/java/org/photonvision/Main.java | 7 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 photon-core/src/main/java/org/photonvision/common/hardware/OsImageVersion.java diff --git a/photon-core/src/main/java/org/photonvision/common/hardware/OsImageVersion.java b/photon-core/src/main/java/org/photonvision/common/hardware/OsImageVersion.java new file mode 100644 index 0000000000..211e7b6d61 --- /dev/null +++ b/photon-core/src/main/java/org/photonvision/common/hardware/OsImageVersion.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) Photon Vision. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.photonvision.common.hardware; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; +import org.photonvision.common.logging.LogGroup; +import org.photonvision.common.logging.Logger; + +/** + * Our blessed images inject the current version via this build workflow: + * https://github.com/PhotonVision/photon-image-modifier/blob/2e5ddb6b599df0be921c12c8dbe7b939ecd7f615/.github/workflows/main.yml#L67 + * + *

This class provides a convienent abstraction around this + */ +public class OsImageVersion { + private static final Logger logger = new Logger(OsImageVersion.class, LogGroup.General); + + private static Path imageVersionFile = Path.of("/opt/photonvision/image-version"); + + public static final Optional IMAGE_VERSION = getImageVersion(); + + private static Optional getImageVersion() { + if (!imageVersionFile.toFile().exists()) { + logger.warn( + "Photon cannot locate base OS image version metadata at " + imageVersionFile.toString()); + return Optional.empty(); + } + + try { + return Optional.of(Files.readString(imageVersionFile).strip()); + } catch (IOException e) { + logger.error("Couldn't read image-version file", e); + } + + return Optional.empty(); + } +} diff --git a/photon-server/src/main/java/org/photonvision/Main.java b/photon-server/src/main/java/org/photonvision/Main.java index a3834abe5e..5ac8d31560 100644 --- a/photon-server/src/main/java/org/photonvision/Main.java +++ b/photon-server/src/main/java/org/photonvision/Main.java @@ -31,6 +31,7 @@ import org.photonvision.common.configuration.NeuralNetworkModelManager; import org.photonvision.common.dataflow.networktables.NetworkTablesManager; import org.photonvision.common.hardware.HardwareManager; +import org.photonvision.common.hardware.OsImageVersion; import org.photonvision.common.hardware.PiVersion; import org.photonvision.common.hardware.Platform; import org.photonvision.common.logging.KernelLogLogger; @@ -353,10 +354,14 @@ public static void main(String[] args) { logger.info( "Starting PhotonVision version " + PhotonVersion.versionString - + " on " + + " on platform " + Platform.getPlatformName() + (Platform.isRaspberryPi() ? (" (Pi " + PiVersion.getPiVersion() + ")") : "")); + if (OsImageVersion.IMAGE_VERSION.isPresent()) { + logger.info("PhotonVision image version: " + OsImageVersion.IMAGE_VERSION.get()); + } + try { if (!handleArgs(args)) { System.exit(1);