<?php
// Higher level helper functions
require_once(__DIR__ . '/util.inc.php');
require_once(__DIR__ . '/state.inc.php');
/*
* Returns the correct URL to the current virtual host
*/
// TODO: we must move this function to hutil.inc.php
function rg_base_url($db, $user, $pass)
{
$hostname = rg_state_get($db, 'hostname');
$http_allow = rg_state_get($db, 'http_allow');
$https_allow = rg_state_get($db, 'https_allow');
//rg_log_debug('from state: ' . $hostname . ' ' . $http_allow
// . ' ' . $https_allow);
return rg_base_url_build($hostname, $http_allow, $https_allow,
$user, $pass);
}
/*
* Enable new distros
*/
function rg_env_enable($db, $list)
{
$ret = array('ok' => 0);
do {
$en = rg_state_get_array($db, 'distros');
if ($en === FALSE) {
$ret['errmsg'] = rg_state_error();
break;
}
$do_update = FALSE;
foreach ($list as $env) {
if (isset($en[$env]) && ($en[$env] == 0)) {
rg_log_debug('admin disabled ' . $env);
continue;
}
if (isset($en[$env]) && ($en[$env] == 1)) {
//rg_log_debug('env ' . $env . ' already enabled');
continue;
}
$en[$env] = 1;
$do_update = TRUE;
}
if (isset($ret['errmsg']))
break;
if ($do_update === TRUE) {
$r = rg_state_set_array($db, 'distros', $en);
if ($r === FALSE) {
$ret['errmsg'] = 'cannot update state: '
. rg_state_error();
break;
}
}
$ret['ok'] = 1;
} while (0);
return $ret;
}
/*
* Returns info based on distro name
*/
function rg_env_distro_info($distro)
{
$ret = array();
if (strcmp($distro, 'fedora') == 0) {
$ret['nice'] = 'Fedora Linux';
$ret['pkg_type'] = 'rpm';
} else if (strcmp($distro, 'centos') == 0) {
$ret['nice'] = 'CentOS';
$ret['pkg_type'] = 'rpm';
} else if (strcmp($distro, 'debian') == 0) {
$ret['nice'] = 'Debian';
$ret['pkg_type'] = 'deb';
} else if (strcmp($distro, 'rocky') == 0) {
$ret['nice'] = 'Rocky Linux';
$ret['pkg_type'] = 'rpm';
} else if (strcmp($distro, 'archlinux') == 0) {
$ret['nice'] = 'ArchLinux';
$ret['pkg_type'] = 'apk?';
} else if (strcmp($distro, 'opensuse') == 0) {
$ret['nice'] = 'OpenSuse';
$ret['pkg_type'] = 'rpm';
} else if (strcmp($distro, 'ubuntu') == 0) {
$ret['nice'] = 'Ubuntu';
$ret['pkg_type'] = 'deb';
} else {
$ret['nice'] = $distro;
$ret['pkg_type'] = '?';
}
return $ret;
}
/*
* Returns a codename based on major
*/
function rg_env_major2codename($distro, $major)
{
if (strcmp($distro, 'debian') == 0) {
switch ($major) {
case 13: return 'trixie';
case 12: return 'bookworm';
case 11: return 'bullseye';
case 10: return 'buster';
case 9: return 'stretch';
case 8: return 'jessie';
}
} else if (strcmp($distro, 'ubuntu') == 0) {
if (strcmp($major, '20.04') == 0) {
return 'focal'; // fossa
} else if (strcmp($major, '20.10') == 0) {
return 'groovy'; // gorilla
} else if (strcmp($major, '21.04') == 0) {
return 'hirsute'; // hippo
} else if (strcmp($major, '21.10') == 0) {
return 'impish'; // indri
} else if (strcmp($major, '22.04') == 0) {
return 'jammy'; // jellyfish
}
}
return sprintf('%u', $major);
}
/*
* Returns a major based on codename
*/
function rg_env_codename2major($distro, $codename)
{
if (strcmp($distro, 'debian') == 0) {
switch ($codename) {
case 'trixie': return '13'; break;
case 'bookworm': return '12'; break;
case 'bullseye': return '11'; break;
case 'buster': return '10'; break;
case 'stretch': return '9'; break;
case 'jessie': return '8'; break;
}
} else if (strcmp($distro, 'ubuntu') == 0) {
switch ($codename) {
case 'focal': return '20.04'; break;
case 'groovy': return '20.10'; break;
case 'hirsute': return '21.04'; break;
case 'impish': return '21.10'; break;
case 'jammy': return '22.04'; break;
}
}
return '-';
}
/*
* Returns some info about an 'env' (distro-major-arch)
*/
function rg_env_info($env)
{
$ret = array();
$p = explode('-', $env, 3);
$ret['distro'] = $p[0];
if (isset($p[1]))
$ret['major'] = $p[1];
if (isset($p[2]))
$ret['arch'] = $p[2];
$r = rg_env_distro_info($ret['distro']);
foreach ($r as $k => $v)
$ret[$k] = $v;
$ret['codename'] = rg_env_major2codename($ret['distro'], $ret['major']);
return $ret;
}
/*
* Transforms a distro_info coming from pkg_subrepo into a tree
*/
function rg_distro_info2tree($list)
{
$ret = array();
foreach ($list as $env => $i) {
$di = rg_env_info($env);
$distro = $di['distro']; $major = $di['major']; $arch = $di['arch'];
unset($di['distro']); unset($di['major']); unset($di['arch']);
foreach ($di as $k => $v)
$i[$k] = $v;
if (!isset($ret[$distro]))
$ret[$distro] = array('versions' => array());
if (!isset($ret[$distro]['versions'][$major]))
$ret[$distro]['versions'][$major] = array();
if (!isset($ret[$distro]['versions'][$major]['archs'][$arch]))
$ret[$distro]['versions'][$major]['archs'][$arch] = $i;
}
return $ret;
}
function rg_distro_sort_func($a, $b)
{
$x = strcmp($a['nice'], $b['nice']);
if ($x != 0)
return $x;
// TODO: Ubuntu is using xx.yy, but we are fine for now
if ($a['major'] > $b['major'])
return 1;
else if ($a['major'] < $b['major'])
return -1;
$x = strcmp($a['arch'], $b['arch']);
if ($x != 0)
return $x;
return 0;
}
function rg_distro_sort($list)
{
uasort($list, 'rg_distro_sort_func');
return $list;
}
/*
* Returns a tree from the current list of envs
*/
function rg_envs_tree($db, $only_enabled)
{
$ret = array('ok' => 0);
do {
$envs = rg_state_get_array($db, 'distros');
if ($envs === FALSE) {
$ret['errmsg'] = rg_state_error();
break;
}
$ret['tree'] = array();
foreach ($envs as $env => $enabled) {
if ($only_enabled && ($enabled != 1))
continue;
$ei = rg_env_info($env);
$distro = $ei['distro'];
if (!isset($ret['tree'][$distro]))
$ret['tree'][$distro] = array(
'pkg_type' => $ei['pkg_type'],
'nice' => $ei['nice'],
'versions' => array());
$major = $ei['major'];
if (!isset($ret['tree'][$distro]['versions'][$major]))
$ret['tree'][$distro]['versions'][$major] = array(
'archs' => array());
$arch = $ei['arch'];
if (!isset($ret['tree'][$distro]['versions'][$major]['archs'][$arch]))
$ret['tree'][$distro]['versions'][$major]['archs'][$arch] = array();
$ret['tree'][$distro]['versions'][$major]['archs'][$arch]['enabled'] = $enabled;
}
$ret['ok'] = 1;
} while (0);
return $ret;
}
/*
* Test if a env is enabled
* @env - environment (examples: fedora-34-x86_64)
* @tree - output of rg_envs_tree
*/
function rg_envs_tree_is_enabled($tree, $env)
{
$p = explode('-', $env);
if (!isset($tree[$p[0]]['versions'][$p[1]]['archs'][$p[2]]['enabled']))
return FALSE;
return $tree[$p[0]]['versions'][$p[1]]['archs'][$p[2]]['enabled'] == 1;
}
/*
* Returns info about an environment, in a flat array
* @env - environment (examples: fedora-34-x86_64)
* @tree - output of rg_envs_tree
*/
function rg_envs_tree_info($tree, $env)
{
$p = explode('-', $env);
if (!isset($tree[$p[0]]['versions'][$p[1]]['archs'][$p[2]]))
return FALSE;
$ret = array();
foreach ($tree[$p[0]] as $k => $v) {
if (is_array($v))
continue;
$ret[$k] = $v;
}
foreach ($tree[$p[0]]['versions'][$p[1]] as $k => $v) {
if (is_array($v))
continue;
$ret[$k] = $v;
}
foreach ($tree[$p[0]]['versions'][$p[1]]['archs'][$p[2]] as $k => $v) {
if (is_array($v))
continue;
$ret[$k] = $v;
}
return $ret;
}