From 106e4a90f0c6f2493a3c1013ec29a6bad4f8589e Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Tue, 10 Nov 2026 09:53:35 -0600 Subject: [PATCH] Capone: send the image's OS family as an eighth field The Capone image line now ends with Image::getOSFamily(), so FOS can place an osid it has no table entry for. Fields 1-7 keep their order; an old FOS ignores the eighth. The field is empty on a core without getOSFamily() (method_exists guard), so the plugin still works on any fogproject 1.6. The line building moves to a static imageLine() so a test can drive it. No plugin version bump. Refs FOGProject/fog-plugins#43 Co-Authored-By: Claude Opus 5.5 --- capone/src/Util/CaponeTasking.php | 63 +++++++---- tests/capone-image-line.test.php | 179 ++++++++++++++++++++++++++++++ 2 files changed, 223 insertions(+), 19 deletions(-) create mode 100644 tests/capone-image-line.test.php diff --git a/capone/src/Util/CaponeTasking.php b/capone/src/Util/CaponeTasking.php index 9e298bd..a900686 100644 --- a/capone/src/Util/CaponeTasking.php +++ b/capone/src/Util/CaponeTasking.php @@ -44,6 +44,46 @@ class CaponeTasking extends \FOG\Base\FOGBase 3 => 'mpa', 4 => 'dd' ]; + /** + * Builds the unencoded line FOS reads for one Capone image. + * + * Fields: path|osid|type|parttype|format|storage|storageip|osfamily. + * Fields 1-7 keep their order because an old FOS reads them by position + * and ignores a field it does not know. The eighth is the OS family, so + * FOS can place an osid it has no table entry for. It is empty when core + * has no Image::getOSFamily() (before fogproject#2085), which FOS reads + * as "not sent". This lives in its own static method because the + * constructor reads filter_input() and builds its own Image, so a test + * cannot drive it (FOGProject/fog-plugins#43). + * + * @param object $Image the image + * @param object $StorageNode the storage node the image comes from + * @param array $imgTypes image type id => FOS type name + * + * @return string + */ + public static function imageLine($Image, $StorageNode, array $imgTypes) + { + $strSetup = '%s|%s|%s|%s|%s|%s|%s|%s'; + $family = method_exists($Image, 'getOSFamily') ? + $Image->getOSFamily() : + ''; + return sprintf( + $strSetup, + $Image->get('path'), + $Image->get('osID'), + $imgTypes[$Image->get('imageTypeID')], + $Image->getPartitionType(), + (int)$Image->get('format'), + sprintf( + '%s:%s', + $StorageNode->get('ip'), + $StorageNode->get('path') + ), + $StorageNode->get('ip'), + $family + ); + } /** * Initializes the Capone tasking class. * @@ -63,7 +103,6 @@ public function __construct() break; } try { - $strSetup = "%s|%s|%s|%s|%s|%s|%s"; ob_start(); $capones = \FOG\Router\Route::getList( 'capone', @@ -81,27 +120,13 @@ public function __construct() ) { continue; } - $path = $Image->get('path'); - $osid = $Image->get('osID'); - $itid = $Image->get('imageTypeID'); - $ptid = $Image->getPartitionType(); - $format = $Image->get('format'); printf( "%s\n", base64_encode( - sprintf( - $strSetup, - $path, - $osid, - $this->imgTypes[$itid], - $ptid, - (int)$format, - sprintf( - '%s:%s', - $StorageNode->get('ip'), - $StorageNode->get('path') - ), - $StorageNode->get('ip') + self::imageLine( + $Image, + $StorageNode, + $this->imgTypes ) ) ); diff --git a/tests/capone-image-line.test.php b/tests/capone-image-line.test.php new file mode 100644 index 0000000..56232f6 --- /dev/null +++ b/tests/capone-image-line.test.php @@ -0,0 +1,179 @@ + 'win11', + 'osID' => 10, + 'imageTypeID' => 1, + 'format' => 1, + ]; + return $data[$key]; + } + /** + * The partition type id. + * + * @return int + */ + public function getPartitionType() + { + return 7; + } + /** + * The OS family, as core has had it since fogproject#2085. + * + * @return string + */ + public function getOSFamily() + { + return 'windows'; + } +} + +/** + * Image on an older core: identical data, and no getOSFamily() at all. + * + * Not a subclass of the class above: method_exists() would find the + * inherited method. + */ +class CaponeLineImageOldCore +{ + /** + * Read a field. + * + * @param string $key the field + * + * @return mixed + */ + public function get($key) + { + $data = [ + 'path' => 'win11', + 'osID' => 10, + 'imageTypeID' => 1, + 'format' => 1, + ]; + return $data[$key]; + } + /** + * The partition type id. + * + * @return int + */ + public function getPartitionType() + { + return 7; + } +} + +/** + * Storage node with the two fields Capone reads. + */ +class CaponeLineNode +{ + /** + * Read a field. + * + * @param string $key the field + * + * @return string + */ + public function get($key) + { + $data = ['ip' => '10.0.0.5', 'path' => '/images']; + return $data[$key]; + } +} + +$types = [1 => 'n', 2 => 'mps', 3 => 'mpa', 4 => 'dd']; +$node = new CaponeLineNode(); +$failures = []; +$checks = 0; + +/** + * Records one assertion. + * + * @param bool $ok whether it held + * @param string $what what was asserted + * + * @return void + */ +function check($ok, $what) +{ + global $failures, $checks; + $checks++; + if (!$ok) { + $failures[] = $what; + } +} + +$with = \FOG\Plugins\Capone\Util\CaponeTasking::imageLine( + new CaponeLineImageWithFamily(), + $node, + $types +); +check( + $with === 'win11|10|n|7|1|10.0.0.5:/images|10.0.0.5|windows', + 'core with getOSFamily(): got ' . var_export($with, true) +); + +$without = \FOG\Plugins\Capone\Util\CaponeTasking::imageLine( + new CaponeLineImageOldCore(), + $node, + $types +); +check( + $without === 'win11|10|n|7|1|10.0.0.5:/images|10.0.0.5|', + 'core without getOSFamily(): got ' . var_export($without, true) +); +check( + count(explode('|', $without)) === 8, + 'the line has eight fields even when the family is empty' +); + +if ($failures) { + foreach ($failures as $f) { + fwrite(STDERR, "FAIL: $f\n"); + } + exit(1); +} +echo "capone-image-line: $checks checks passed\n"; +exit(0);