CLI-Script zum Entfernen alter git-configs
Bernd Wurst

Bernd Wurst commited on 2025-11-13 19:30:37
Zeige 1 geänderte Dateien mit 98 Einfügungen und 0 Löschungen.

... ...
@@ -0,0 +1,98 @@
1
+<?php
2
+
3
+if (php_sapi_name() != 'cli') {
4
+    echo 'command line script';
5
+    die();
6
+}
7
+error_reporting(E_ALL);
8
+ini_set('display_errors', 1);
9
+ini_set('display_startup_errors', 1);
10
+
11
+$script_dir = __DIR__;
12
+$module_dir = dirname($script_dir, 1); 
13
+$base_dir = dirname($script_dir, 3); 
14
+
15
+chdir($base_dir);
16
+set_include_path($module_dir . '/include' . PATH_SEPARATOR . get_include_path());
17
+
18
+require_once('config.php');
19
+require_once('session/checkuser.php');
20
+require_once("class/database.php");
21
+
22
+$result = db_query("SELECT username FROM system.useraccounts");
23
+$users = [];
24
+foreach ($result as $u) {
25
+    $users[] = $u["username"];
26
+}
27
+
28
+if (count($users) < 50) {
29
+    echo "STOP, zu uwenig Useracocunts definiert!";
30
+    die();
31
+}
32
+
33
+
34
+function git($command) 
35
+{
36
+    $command = './modules/gitolite/scripts/git-wrapper.sh ' . $command;
37
+    $output = [];
38
+    $retval = 0;
39
+    DEBUG($command);
40
+    exec($command, $output, $retval);
41
+}
42
+
43
+git('pull');
44
+
45
+
46
+function remove_from_config($u) 
47
+{
48
+    $config = "../gitolite-data/gitolite-admin/conf/webinterface.conf";
49
+    $content = file_get_contents($config);
50
+    file_put_contents($config, str_replace('include  "webinterface/'.$u.'.conf"'."\n", "", $content));
51
+}
52
+
53
+function list_ssh_keys($conf) 
54
+{
55
+    $open_file = fopen($conf, "r");
56
+    $users = [];
57
+    while (($line = fgets($open_file)) !== false) {
58
+        if (str_starts_with($line, "# user ")) {
59
+            $users[] = str_replace("# user ", "", trim($line));
60
+        }
61
+    }
62
+    return $users;
63
+}
64
+
65
+function remove_foreign_key($keyname) {
66
+    foreach (glob("../gitolite-data/gitolite-admin/conf/webinterface/*.conf") as $f) {
67
+        $content = file_get_contents($f);
68
+        file_put_contents($f, str_replace('# foreign user '.$keyname."\n", "", $content));
69
+    }
70
+}
71
+
72
+foreach (glob("../gitolite-data/gitolite-admin/conf/webinterface/*.conf") as $f) {
73
+    $username = str_replace('.conf', '', basename($f));
74
+    if (!in_array($username, $users)) {
75
+        // User gibt es nicht mehr!
76
+        echo "$username gibt es nicht mehr, lösche $f!\n";
77
+        // Zeile aus webinterface.conf entfernen
78
+        remove_from_config($username);
79
+
80
+        // ssh-keys aus config extrahieren
81
+        $ssh_keys = list_ssh_keys($f);
82
+        foreach ($ssh_keys as $key) {
83
+            // prüfen, ob der betreffende ssh-key bei einem anderen User als foreign-key drin steht
84
+            remove_foreign_key($key);
85
+            // ssh-keys löschen
86
+            unlink("../gitolite-data/gitolite-admin/keydir/$key.pub");
87
+        }
88
+
89
+        // Include-Datei löschen
90
+        unlink($f);
91
+
92
+        git("add -u");
93
+        git("commit -m 'delete nonexisting user »{$username}« from config'");
94
+        echo "erledigt!\n";
95
+    }
96
+}
97
+
98
+git("push");
0 99