Skip to content

Commit f821657

Browse files
authored
Use updated image metadata (#2209)
1 parent 8c7ca16 commit f821657

File tree

3 files changed

+104
-58
lines changed

3 files changed

+104
-58
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) Photon Vision.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.photonvision.common.hardware;
19+
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.Optional;
26+
import org.photonvision.common.logging.LogGroup;
27+
import org.photonvision.common.logging.Logger;
28+
29+
/**
30+
* Our blessed images inject the current version via the build process in
31+
* https://github.com/PhotonVision/photon-image-modifier
32+
*
33+
* <p>This class provides a convenient abstraction around this
34+
*/
35+
public class OsImageData {
36+
private static final Logger logger = new Logger(OsImageData.class, LogGroup.General);
37+
38+
private static Path imageVersionFile = Path.of("/opt/photonvision/image-version");
39+
private static Path imageMetadataFile = Path.of("/opt/photonvision/image-version.json");
40+
41+
/** The OS image version string, if available. This is legacy, use {@link ImageMetadata}. */
42+
public static final Optional<String> IMAGE_VERSION = getImageVersion();
43+
44+
private static Optional<String> getImageVersion() {
45+
if (!imageVersionFile.toFile().exists()) {
46+
logger.warn("Photon cannot locate base OS image version at " + imageVersionFile.toString());
47+
return Optional.empty();
48+
}
49+
50+
try {
51+
return Optional.of(Files.readString(imageVersionFile).strip());
52+
} catch (IOException e) {
53+
logger.error("Couldn't read image-version file", e);
54+
}
55+
56+
return Optional.empty();
57+
}
58+
59+
public static final Optional<ImageMetadata> IMAGE_METADATA = getImageMetadata();
60+
61+
public static record ImageMetadata(
62+
String buildDate, String commitSha, String commitTag, String imageName, String imageSource) {}
63+
64+
private static Optional<ImageMetadata> getImageMetadata() {
65+
if (!imageMetadataFile.toFile().exists()) {
66+
logger.warn("Photon cannot locate OS image metadata at " + imageMetadataFile.toString());
67+
return Optional.empty();
68+
}
69+
70+
try {
71+
String content = Files.readString(imageMetadataFile).strip();
72+
73+
ObjectMapper mapper =
74+
new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
75+
76+
ImageMetadata md = mapper.readValue(content, ImageMetadata.class);
77+
78+
if (md.buildDate() == null
79+
&& md.commitSha() == null
80+
&& md.commitTag() == null
81+
&& md.imageName() == null
82+
&& md.imageSource() == null) {
83+
logger.warn(
84+
"OS image metadata JSON did not contain recognized fields; preserving legacy behavior");
85+
return Optional.empty();
86+
}
87+
88+
return Optional.of(md);
89+
} catch (IOException e) {
90+
logger.error("Couldn't read image metadata file", e);
91+
} catch (Exception e) {
92+
logger.error("Failed to parse image metadata", e);
93+
}
94+
95+
return Optional.empty();
96+
}
97+
}

photon-core/src/main/java/org/photonvision/common/hardware/OsImageVersion.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

photon-server/src/main/java/org/photonvision/Main.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.photonvision.common.configuration.NeuralNetworkModelManager;
2929
import org.photonvision.common.dataflow.networktables.NetworkTablesManager;
3030
import org.photonvision.common.hardware.HardwareManager;
31-
import org.photonvision.common.hardware.OsImageVersion;
31+
import org.photonvision.common.hardware.OsImageData;
3232
import org.photonvision.common.hardware.PiVersion;
3333
import org.photonvision.common.hardware.Platform;
3434
import org.photonvision.common.logging.KernelLogLogger;
@@ -173,8 +173,12 @@ public static void main(String[] args) {
173173
+ Platform.getPlatformName()
174174
+ (Platform.isRaspberryPi() ? (" (Pi " + PiVersion.getPiVersion() + ")") : ""));
175175

176-
if (OsImageVersion.IMAGE_VERSION.isPresent()) {
177-
logger.info("PhotonVision image version: " + OsImageVersion.IMAGE_VERSION.get());
176+
if (OsImageData.IMAGE_METADATA.isPresent()) {
177+
logger.info("PhotonVision image data: " + OsImageData.IMAGE_METADATA.get());
178+
} else if (OsImageData.IMAGE_VERSION.isPresent()) {
179+
logger.info("PhotonVision image version: " + OsImageData.IMAGE_VERSION.get());
180+
} else {
181+
logger.info("PhotonVision image version: unknown");
178182
}
179183

180184
try {

0 commit comments

Comments
 (0)