Skip to content

Commit 5821982

Browse files
committed
Implement detect-engine-arch command
1 parent ee7bbad commit 5821982

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

tools/ddct-src/App.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private function runCommand(int $argc, array $argv): int
4949
'build-selection' => $this->buildSelection ($argc, $argv),
5050
'can-build' => $this->canBuild ($argc, $argv),
5151
'detect-engine' => $this->detectEngine ($argc, $argv),
52+
'detect-engine-arch' => $this->detectEngineArch ($argc, $argv),
5253
'generate' => $this->generate ($argc, $argv),
5354
'generate-all' => $this->generateAll ($argc, $argv),
5455
'has-built' => $this->hasBuilt ($argc, $argv),
@@ -261,6 +262,21 @@ private function detectEngine(int $argc, array $argv): int
261262
return 0;
262263
}
263264

265+
private function detectEngineArch(int $argc, array $argv): int
266+
{
267+
$containerEngine = new ContainerEngine();
268+
269+
try {
270+
$arch = $containerEngine->getArch();
271+
} catch (Exception $ex) {
272+
errorln($ex->getMessage());
273+
return 1;
274+
}
275+
276+
outputln($arch);
277+
return 0;
278+
}
279+
264280
private function namespaceRemoveAll(int $argc, array $argv): int
265281
{
266282
if ($argc !== 2) {

tools/ddct-src/Util/ContainerEngine.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,51 @@ public function pushImage(string $name): void
169169
$this->passthruCommand('push', $name);
170170
}
171171

172+
private function getArchDocker(): string
173+
{
174+
$json = $this->executeCommand('version', '--format={{ json .Server.Arch }}');
175+
176+
if (!isset($json[0])) {
177+
throw new Exception('No data received.');
178+
}
179+
180+
return json_decode($json[0], true, JSON_THROW_ON_ERROR);
181+
}
182+
183+
private function getArchPodman(): string
184+
{
185+
$json = $this->executeCommand('version', '--format={{ json .OsArch }}');
186+
187+
if (!isset($json[0])) {
188+
throw new Exception('No data received.');
189+
}
190+
191+
$osArch = json_decode($json[0], true, JSON_THROW_ON_ERROR);
192+
$osArchSplit = explode('/', $osArch);
193+
return end($osArchSplit);
194+
}
195+
196+
public function getArch(): string
197+
{
198+
if (stripos($this->containerEngine, 'docker') !== false) {
199+
return $this->getArchDocker();
200+
}
201+
if (stripos($this->containerEngine, 'podman') !== false) {
202+
return $this->getArchPodman();
203+
}
204+
205+
try {
206+
return $this->getArchDocker();
207+
} catch (Exception) {
208+
}
209+
try {
210+
return $this->getArchPodman();
211+
} catch (Exception) {
212+
}
213+
214+
throw new Exception('Unable to determine CPU architecture of container engine.');
215+
}
216+
172217
public static function detectContainerEngine(): ?string
173218
{
174219
if (isset($_SERVER['CONTAINER_ENGINE'])) {

0 commit comments

Comments
 (0)