Skip to content

Commit

Permalink
Merge pull request #3 from NethServer/feat-6835
Browse files Browse the repository at this point in the history
Serve app index with legacy tier policy
  • Loading branch information
DavidePrincipi authored Feb 1, 2024
2 parents 667a5fa + 51b6ef9 commit 651bf79
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
6 changes: 6 additions & 0 deletions imageroot/bin/take-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#

set -e
shopt -s nullglob

# shellcheck disable=SC1091
source /etc/nethserver/core.env
Expand All @@ -14,6 +15,7 @@ snapshot_dir=tmp.$(date +d%Y%m%dt%H%I%S%2N)
mirror_base_url=${PORTHOS_SOURCE:?}
repo_path_list=(/rocky/9/BaseOS/x86_64/os/ /rocky/9/AppStream/x86_64/os/)
rsync_opts=(-aiSH --no-motd --no-super --no-perms --chmod=ugo=rwX --no-g --no-o --delete-after)
distfeed_url=https://raw.githubusercontent.com/NethServer/ns8-repomd/repomd/repodata.json

cd /srv/porthos/webroot

Expand All @@ -24,6 +26,10 @@ if [[ ${#snapshot_list[@]} -gt 0 ]]; then
fi
trap 'rm -rf "${snapshot_dir}"' EXIT

printf "Fetching distfeed from %s\n" "${distfeed_url}"
mkdir -vp "${snapshot_dir}/distfeed"
curl --fail -L -O --output-dir "${snapshot_dir}/distfeed" "${distfeed_url}"

for repo_path in "${repo_path_list[@]}"; do
printf "Synchronizing %s\n" "${mirror_base_url}${repo_path}"
mkdir -vp "${snapshot_dir}${repo_path}"
Expand Down
73 changes: 64 additions & 9 deletions imageroot/script/snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,73 @@

ini_set('date.timezone', 'UTC');

$uri = $_SERVER['DOCUMENT_URI']; // XXX parse URI

// Disable the Content-Type header in PHP, so that nginx x-accel can add its own
ini_set('default_mimetype', FALSE);

$snapshots = array_map('basename', glob('/srv/porthos/webroot/d20*'));
// Return the 0-based tier id for the given $system_id
function system_tier($system_id) {
$idx = abs(crc32($system_id)) % 100;
if($idx < 10) {
$tier_id = 0;
} elseif($idx < 30) {
$tier_id = 1;
} else {
$tier_id = 2;
}
return $tier_id;
}

// Return the age (days) of the given $snapshot
function snapshot_age($snapshot) {
$year = substr($snapshot, 1, 4);
$month = substr($snapshot, 5, 2);
$day = substr($snapshot, 7, 2);
$hour = substr($snapshot, 10, 2);
$minute = substr($snapshot, 12, 2);
$second = substr($snapshot, 14, 2);
$frac = substr($snapshot, 16, 2);

$dt_current = new DateTime();
$dt_snapshot = new DateTime("$year-$month-$day $hour:$minute:$second.$frac");

// Calculate the difference in days between the two dates
$dt_interval = $dt_current->diff($dt_snapshot);

$days_age = $dt_interval->format('%a');
return $days_age;
}

function main() {
// Minimum age (days) expected by tiers
$tier_age = [3, 4, 5];

$lsnapshots = array_map('basename', glob('/srv/porthos/webroot/d20*'));

if (count($lsnapshots) == 0) {
http_response_code(404);
echo "Not found\n";
}

$username = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : "";
$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : "";

sort($lsnapshots);

$snapshot = array_pop($lsnapshots);
if($username && $password) {
// Authenticated clients gain access to older tiers
while($snapshot != NULL) {
$tier_id = system_tier($username);
if (snapshot_age($snapshot) > $tier_age[$tier_id]) {
break;
}
$snapshot = array_pop($lsnapshots);
}
}

if (count($snapshots) > 0) {
sort($snapshots);
header('Cache-Control: private');
header('X-Accel-Redirect: /' . end($snapshots) . $uri);
} else {
http_response_code(404);
echo "Not found\n";
header('X-Accel-Redirect: /' . $snapshot . $_SERVER['DOCUMENT_URI']);
}

// Run
main();
6 changes: 6 additions & 0 deletions imageroot/templates/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,11 @@ http {
fastcgi_pass unix:/srv/porthos/run/fpm.sock;
fastcgi_param SCRIPT_FILENAME /srv/porthos/script/snapshot.php;
}

location = /distfeed/repodata.json {
include fastcgi.conf;
fastcgi_pass unix:/srv/porthos/run/fpm.sock;
fastcgi_param SCRIPT_FILENAME /srv/porthos/script/snapshot.php;
}
}
}

0 comments on commit 651bf79

Please sign in to comment.