Skip to content

Instantly share code, notes, and snippets.

@hayleyxyz
Created December 9, 2015 22:04
Show Gist options
  • Save hayleyxyz/f4e393f40b4af1ecf3a3 to your computer and use it in GitHub Desktop.
Save hayleyxyz/f4e393f40b4af1ecf3a3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
/*
* This script will transform file names for templates downloaded for OpenVZ[1],
* to match the format used in Proxmox[2].
*
* [1] https://download.openvz.org/template/precreated/
* [2] http://pve.proxmox.com/wiki/Template_naming_convention
*
* Example formats:
* debian-8.0-x86_64-minimal.tar.gz => debian-8.0-minimal_8.0_amd64.tar.gz
* debian-8.0-x86_64.tar.gz => debian-8.0-standard_8.0_amd64.tar.gz
* debian-7.0-x86-minimal.tar.gz => debian-7.0-minimal_7.0_i386.tar.gz
* centos-6-x86.tar.gz => centos-6-standard_6.0_i386.tar.gz
* debian-6.0-x86-minimal.tar.gz => debian-6.0-minimal_6.0_i386.tar.gz
*/
$inputs = array_slice($argv, 1);
if(count($inputs) === 0) {
fprintf(STDOUT, "Usage: %s file1 [file2...]\n", $argv[0]);
exit(1);
}
foreach($inputs as $input) {
$basename = basename($input);
$file = basename($input, '.tar.gz');
$bits = explode('-', $file);
if(count($bits) !== 3 && count($bits) !== 4) {
throw new Exception('Invalid input file: '.$input);
}
$os = $bits[0];
$version = $bits[1];
$arch = transformArch($bits[2]);
if(array_key_exists(3, $bits)) {
$name = $bits[3];
}
else {
$name = 'standard';
}
if($arch !== 'i386' && $arch !== 'amd64') {
throw new Exception('Invalid arch: '.$arch);
}
$formatted = sprintf('%s-%s-%s_%.1f_%s.tar.gz', $os, $version, $name, $version, $arch);
printf("%-35s => %s\n", $basename, $formatted);
}
function transformArch($arch) {
$archLookup = array(
'x86' => 'i386',
'i486' => 'i386',
'i586' => 'i386',
'i686' => 'i386',
'x86_64' => 'amd64',
);
if(array_key_exists($arch, $archLookup)) {
return $archLookup[$arch];
}
else {
return $arch;
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment