Skip to content

Commit f2c2319

Browse files
Porthos start RPM release on Tuesdays (#21)
nethesis/dev#5641
1 parent 3bb5195 commit f2c2319

File tree

13 files changed

+78
-135
lines changed

13 files changed

+78
-135
lines changed

porthos/README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ For instance to create a key on athos
122122
The `tier_id` value should be a number. If the value is negative, the tier
123123
number is calculated by an hash function, based on the system identifier.
124124

125-
If `tier_id` is not a number, both `auth.php` and `subscription.php` reply with
126-
403 - forbidden.
125+
If `tier_id` is not a number, `stable/` is served instead.
127126

128127
### `icat` field
129128

@@ -150,19 +149,16 @@ from `/etc/porthos/repos.conf`. Upstream YUM rsync URLs are defined there.
150149
The following commands are executed automatically, as defined in `porthos.cron`:
151150

152151
- `repo-bulk-pull` creates a snapshot date-dir (e.g. `d20180301`) under
153-
`dest_dir` with differences from upstream repositories. It sets `t0` to point at
154-
it.
155-
- `repo-bulk-shift [N]` updates `t1` ... `tN` links by shifting tiers of one position
156-
the optional `N` parameter creates missing links up to N - 1.
152+
`dest_dir` with differences from upstream repositories.
157153
- `repo-bulk-cleanup` erases stale snapshots directories
158154

159155
The following commands are designed for Porthos initialization, to recover from errors, or implement low-level actions:
160156

161157
- `repo-bulk-hinit` runs initial synchronization from upstream repositories (-f disables the check for already existing directories)
162-
- `repo-head-init` initial/override synchronization of head from a specific upstream repo
163-
- `repo-head-rollback` roll back head to a previous snapshot for a specific repo
164-
- `repo-tier-pull` create a new upstream snapshot for a specific repo
165-
- `repo-tier-delete` delete repomd.xml from a given tier or snapshot
158+
- `repo-head-init` synchronization of head from a specific upstream repo
159+
- `repo-head-rollback` roll back a repository head state to a previous snapshot state
160+
- `repo-snapshot-create` create a new repository snapshot
161+
- `repo-snapshot-delete` delete repomd.xml from a given repository snapshot
166162
- `repo-rpm-lookup` seek the given RPM in every snapshot for a given repository
167163
- `xrsync` run rsync safely, trying to repeat the operation if it fails
168164

@@ -179,8 +175,9 @@ it fails as soon as no snapshot is found, or if an invalid repository identifier
179175
is issued.
180176

181177
Some times it is desirable to re-sync the head repository, without generating a
182-
new snapshot, like `repo-tier-pull` does. That happens if an upstream repo was
183-
fixed before being shifted. In that case run `repo-head-init` as follow:
178+
new snapshot, like `repo-snapshot-create` does. That happens if an upstream repo
179+
was fixed quickly and the bogus RPM never entered any snapshot. In that case run
180+
`repo-head-init` as follow:
184181

185182
repo-head-init -n -f 7.6.1810/nethserver-updates/x86_64
186183

@@ -190,19 +187,19 @@ command to run even if the repository was already initialized.
190187
If one or more snapshots contain a bogus RPM it is possible to delete the whole
191188
repository metadata (repomd.xml) file with the following command:
192189

193-
repo-tier-delete d20190702/7.6.1810/nethserver-updates/x86_64 d20190630/7.6.1810/nethserver-updates/x86_64
190+
repo-snapshot-delete d20190702/7.6.1810/nethserver-updates/x86_64 d20190630/7.6.1810/nethserver-updates/x86_64
194191

195-
The correct snapshot (or tier) name can be found starting from the RPM name with:
192+
The correct snapshot name can be found starting from the RPM name with:
196193

197194
repo-rpm-lookup bogus-rpm-1.2.3-1.ns7.noarch.rpm
198195
d20190702/7.6.1810/nethserver-updates/x86_64
199196
d20190630/7.6.1810/nethserver-updates/x86_64
200197

201198
The two commands can be combined together with `xargs`:
202199

203-
repo-rpm-lookup bogus-rpm-1.2.3-1.ns7.noarch.rpm | xargs -- repo-tier-delete
200+
repo-rpm-lookup bogus-rpm-1.2.3-1.ns7.noarch.rpm | xargs -- repo-snapshot-delete
204201

205-
If the RPM is found under `head/`, `repo-tier-delete` safely ignores it.
202+
If the RPM is found under `head/`, `repo-snapshot-delete` safely ignores it.
206203

207204
## Automated schedule
208205

porthos/root/etc/cron.d/porthos.cron

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
RANDOM=20
66
CRON_TZ=UTC
77

8-
0 21 * * 1 root /usr/local/bin/repo-bulk-pull
9-
0 21 * * 2-4 root /usr/local/bin/repo-bulk-shift
10-
0 22 * * 1-4 root /usr/local/bin/repo-bulk-cleanup
8+
0 21 * * 0-4 root /usr/local/bin/repo-bulk-pull
9+
0 22 * * 0-4 root /usr/local/bin/repo-bulk-cleanup >/dev/null
1110

1211
# end
1312

porthos/root/etc/nginx/conf.d/porthos.conf

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,9 @@ server {
3838
return 200 "pong\n";
3939
}
4040

41-
location ~ "^/T0/(.*)" {
42-
internal;
43-
try_files /t0/$1 /head/$1;
44-
}
45-
46-
location ~ "^/T1/(.*)" {
47-
internal;
48-
try_files /t1/$1 /t0/$1 /head/$1;
49-
}
50-
51-
location ~ "^/T2/(.*)" {
52-
internal;
53-
try_files /t2/$1 /t1/$1 /t0/$1 /head/$1;
54-
}
55-
56-
location ~ "^/T3/(.*)" {
57-
internal;
58-
try_files /t3/$1 /t2/$1 /t1/$1 /t0/$1 /head/$1;
59-
}
60-
6141
# add another location for tier "Tx" here...
6242

63-
location ~ "^/(t[0-9]|d[0-9]{8}|head)(/.*)" {
43+
location ~ "^/(d[0-9]{8}|head)(/.*)" {
6444
internal;
6545
}
6646

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
require_once("lib.php");
2424
require_once("config-" . $_SERVER['PORTHOS_SITE'] . ".php");
25+
ini_set('date.timezone', $config['timezone']);
2526

2627
$uri = parse_uri($_SERVER['DOCUMENT_URI']);
2728

@@ -54,30 +55,36 @@
5455
if($config['legacy_auth']) {
5556
$valid_credentials = $valid_credentials || $_SERVER['PHP_AUTH_USER'] === $_SERVER['PHP_AUTH_PW'];
5657
}
57-
$has_access_disabled = ! is_numeric($access['tier_id']);
5858

5959
if($access['tier_id'] < 0) {
6060
$hash = 0;
6161
foreach(str_split($_SERVER['PHP_AUTH_USER']) as $c) {
6262
$hash += ord($c);
6363
}
6464
$hash = $hash % 256;
65-
if($hash < 13) { // 5%
65+
if($hash < 26) { // 10%
6666
$tier_id = 0;
67-
} elseif($hash < 51) { // +15% = 20%
67+
} elseif($hash < 77) { // +20% = 30%
6868
$tier_id = 1;
69-
} elseif($hash < 128) { // +30% = 50%
69+
} else { // +70% = 100%
7070
$tier_id = 2;
71-
} else { // +50% = 100%
72-
$tier_id = 3;
7371
}
74-
$tier_id += $config['tier_id_base'];
7572
} else {
76-
$tier_id = intval($access['tier_id']);
73+
$tier_id = $access['tier_id'];
74+
}
75+
76+
$is_tier_request = is_numeric($tier_id) && $uri['prefix'] == 'autoupdate';
77+
if($is_tier_request && $valid_credentials) {
78+
// Seeking a snapshot is a time-consuming op. Ensure we have valid
79+
// credentials before running it!
80+
$snapshot = lookup_snapshot($uri['full_path'], $tier_id, $config['week_size']);
81+
} else {
82+
$snapshot = 'head';
7783
}
7884

7985
if(basename($uri['rest']) == 'repomd.xml') {
80-
header('Cache-Control: private');
86+
// repomd.xml is the entry point of repository (meta)data: let's keep track
87+
// of every client access to repositories:
8188
application_log(json_encode(array(
8289
'porthos_site' => $_SERVER['PORTHOS_SITE'],
8390
'connection' => $_SERVER['CONNECTION'] ?: '',
@@ -86,19 +93,18 @@
8693
'repo' => $uri['repo'],
8794
'version' => $uri['version'],
8895
'arch' => $uri['arch'],
89-
'tier_id' => $uri['prefix'] == 'autoupdate' ? $tier_id : NULL,
96+
'tier_id' => $is_tier_request ? $tier_id : FALSE,
9097
'tier_auto' => isset($hash),
9198
'tls' => isset($_SERVER['HTTPS']),
92-
'auth_response' => ! $valid_credentials ? 'bad_credentials' : $has_access_disabled ? 'bad_access' : 'pass',
99+
'auth_response' => ! $valid_credentials ? 'bad_credentials' : 'pass',
100+
'snapshot' => $snapshot,
93101
)));
94102
}
95103

96-
if ($has_access_disabled || ! $valid_credentials) {
104+
if (! $valid_credentials) {
105+
// Exit here, after sending the application_log record for repomd.xml requests.
97106
exit_http(403);
98107
}
99108

100-
if($uri['prefix'] == 'autoupdate') {
101-
return_file('/T' . $tier_id . $uri['full_path']);
102-
} else {
103-
return_file('/head' . $uri['full_path']);
104-
}
109+
header('Cache-Control: private');
110+
return_file('/' . $snapshot . $uri['full_path']);

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@
4747
// if FALSE, check the user name and the password separately
4848
$config['legacy_auth'] = FALSE;
4949

50-
// tier_id_base (int)
51-
// this is the base/minimum tier_id value, when automatic tier_id (-1) is set.
52-
// - be sure that (tier_id_base + 3) < "number of tiers"
53-
// - run repo-bulk-shift N (with N = "number of tiers") to add more tier links
54-
$config['tier_id_base'] = 0;
50+
// week_size (int)
51+
// number of days/snapshots per week. Ensure this value is consistent with
52+
// your crontab settings
53+
$config['week_size'] = 5;
54+
55+
// timezone (string)
56+
// the PHP timezone for this application
57+
$config['timezone'] = 'UTC';

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,27 @@ function parse_uri($uri) {
7070
$parts = array_merge($parts, $matches);
7171
return $parts;
7272
}
73+
74+
function get_snapshot_timestamp($snapshot_name) {
75+
$parts = array();
76+
if(!$snapshot_name || !preg_match('/^d(?<year>\d\d\d\d)(?<month>\d\d)(?<day>\d\d)$/', $snapshot_name, $parts)) {
77+
return time();
78+
}
79+
return mktime(0, 0, 0, $parts['month'], $parts['day'], $parts['year']);
80+
}
81+
82+
function lookup_snapshot($path, $tier_id = 0, $week_size = 5) {
83+
$root_path = "/srv/porthos/webroot/";
84+
$snapshots = array_reverse(array_map('basename', glob($root_path . "d20*")));
85+
$last_snapshot_day_id = date('w', get_snapshot_timestamp($snapshots[0]));
86+
// $monday_offset formula:
87+
// ($last_snapshot_day_id-1): rebase on Mondays
88+
// ($last_snapshot_day_id > $tier_id ? 0 : $week_size): select current week Monday or previous one
89+
$monday_offset = ($last_snapshot_day_id-1) + ($last_snapshot_day_id > $tier_id ? 0 : $week_size);
90+
for($i = min($monday_offset, count($snapshots) - 1); $i >= 0; $i--) {
91+
if(is_file($root_path . $snapshots[$i] . '/' . $path)) {
92+
break;
93+
}
94+
}
95+
return $i < 0 ? 'head' : $snapshots[$i];
96+
}

porthos/root/srv/porthos/script/subscription.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
if($config['legacy_auth']) {
4848
$valid_credentials = $valid_credentials || $_SERVER['PHP_AUTH_USER'] === $_SERVER['PHP_AUTH_PW'];
4949
}
50-
$has_access_disabled = ! is_numeric($access['tier_id']) || $access['icat'] === FALSE;
51-
if ($has_access_disabled || ! $valid_credentials) {
50+
if (! $valid_credentials) {
5251
exit_http(403);
5352
}
5453

porthos/root/usr/local/bin/repo-bulk-cleanup

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ shopt -s nullglob extglob
2626
. /etc/porthos/repos.conf
2727
cd ${dest_dir}
2828

29-
# 4 weeks before last repo-tier-pull:
30-
stale_ts=$[ $(stat -c %Y head) - (86400*28) ]
29+
# 2 weeks before last repo-bulk-pull:
30+
stale_ts=$[ $(stat -c %Y head) - (86400*14) ]
3131

3232
declare -A targets
3333

@@ -38,11 +38,6 @@ for D in d+([0-9]); do
3838
fi
3939
done
4040

41-
# Always preserve a symlinked tier
42-
for T in t[0-9]; do
43-
unset "targets[$(readlink ${T})]"
44-
done
45-
4641
if [[ -n ${!targets[@]} ]]; then
4742
echo "[NOTICE] removing stale tier(s) ${!targets[@]}"
4843
rm -rf ${!targets[@]}

porthos/root/usr/local/bin/repo-bulk-pull

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ date_dir=d$(date +%Y%m%d)
2727
trap 'kill $(jobs -p)' INT HUP TERM
2828

2929
for repo in ${!repos[@]}; do
30-
/usr/local/bin/repo-tier-pull $* ${repo} ${date_dir} &
30+
/usr/local/bin/repo-snapshot-create $* ${repo} ${date_dir} &
3131
done
3232

3333
wait
34-
35-
cd ${dest_dir}
36-
ln -sfT ${date_dir} t0

porthos/root/usr/local/bin/repo-bulk-shift

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)