Skip to content

Commit 47baabc

Browse files
Autoupdate policy implementation
1 parent 151b7f7 commit 47baabc

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

porthos/root/srv/porthos/script/auth.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
if($is_tier_request && $valid_credentials) {
7979
// Seeking a snapshot is a time-consuming op. Ensure we have valid
8080
// credentials before running it!
81-
$snapshot = lookup_snapshot($uri['full_path'], $tier_id, $config['week_size']);
81+
$snapshot = lookup_snapshot($uri['full_path'], $uri['version'], $tier_id);
8282
} else {
8383
$snapshot = 'head';
8484
}
@@ -108,4 +108,8 @@
108108
}
109109

110110
header('Cache-Control: private');
111-
return_file('/' . $snapshot . $uri['full_path']);
111+
if($snapshot == 'empty') {
112+
return_file('/empty/repodata/' . basename($uri['full_path']));
113+
} else {
114+
return_file('/' . $snapshot . $uri['full_path']);
115+
}

porthos/root/srv/porthos/script/config-porthos.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
// the PHP timezone for this application
5757
$config['timezone'] = 'UTC';
5858

59+
// autoupdate_policy (array)
60+
// Decide the content returned for a given version/tier_id combination.
61+
// Valid keys are, for instance "7.6.1810/2" "6.10/0" "7.7.1908/*" ...
62+
// Valid values are "head" "empty" "d20191104" (a snapshot name). If the
63+
// snapshot dir does not exist, "empty" is assumed.
64+
$config['autoupdate_policy'] = array();
65+
66+
// snapshots_dir (string)
67+
// Filesystem directory path where snapshot directories are stored with
68+
// trailing slash.
69+
$config['snapshots_dir'] = '/srv/porthos/webroot/';
70+
5971
// tier_seed (int)
6072
// The system_id value is reduced to the integer range 0-9 plus the seed.
6173
// The resulting value is mapped to a tier_id

porthos/root/srv/porthos/script/lib.php

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,49 @@ function get_snapshot_timestamp($snapshot_name) {
7979
return mktime(0, 0, 0, $parts['month'], $parts['day'], $parts['year']);
8080
}
8181

82-
function lookup_snapshot($path, $tier_id = 0, $week_size = 5) {
83-
$root_path = "/srv/porthos/webroot/";
82+
function lookup_policy($version, $tier_id) {
83+
global $config;
84+
85+
$policy_map = $config['autoupdate_policy'] ?: array();
86+
$policy_key = $version . '/' . $tier_id;
87+
$poldef_key = $version . '/*';
88+
89+
if(isset($policy_map[$policy_key])) {
90+
$value = $policy_map[$policy_key];
91+
} elseif(isset($policy_map[$poldef_key])) {
92+
$value = $policy_map[$poldef_key];
93+
} else {
94+
$value = 'lookup_monday';
95+
}
96+
97+
return $value;
98+
}
99+
100+
function lookup_fixed($path, $snapshot_start) {
101+
global $config;
102+
103+
$root_path = $config['snapshots_dir'] ?: "/srv/porthos/webroot/";
104+
$snapshots = array_map('basename', glob($root_path . "d20*"));
105+
106+
$start_key = array_search($snapshot_start, $snapshots);
107+
if( ! $start_key) {
108+
// the fixed snapshot does not exist: fall back to the empty repository.
109+
return 'empty';
110+
}
111+
$snapshots = array_slice($snapshots, $start_key);
112+
foreach($snapshots as $snapshot) {
113+
if(is_file($root_path . $snapshot . '/' . $path)) {
114+
return $snapshot;
115+
}
116+
}
117+
return 'head';
118+
}
119+
120+
function lookup_monday($path, $tier_id) {
121+
global $config;
122+
123+
$week_size = $config['week_size'] ?: 5;
124+
$root_path = $config['snapshots_dir'] ?: "/srv/porthos/webroot/";
84125
$snapshots = array_reverse(array_map('basename', glob($root_path . "d20*")));
85126
$last_snapshot_day_id = date('w', get_snapshot_timestamp($snapshots[0]));
86127
// $monday_offset formula:
@@ -93,4 +134,19 @@ function lookup_snapshot($path, $tier_id = 0, $week_size = 5) {
93134
}
94135
}
95136
return $i < 0 ? 'head' : $snapshots[$i];
96-
}
137+
}
138+
139+
function lookup_snapshot($path, $version, $tier_id) {
140+
global $config;
141+
142+
$policy_name = lookup_policy($version, $tier_id);
143+
if($policy_name == 'lookup_monday') {
144+
return lookup_monday($path, $tier_id);
145+
} elseif(substr($policy_name, 0, 6) == 'fixed-') {
146+
return lookup_fixed($path, substr($policy_name, 6));
147+
} elseif($policy_name == 'empty') {
148+
return 'empty';
149+
} else {
150+
return 'head';
151+
}
152+
}

0 commit comments

Comments
 (0)