|
| 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 | +} |
0 commit comments