1
1
#!/usr/bin/env php
2
2
<?php
3
+
4
+ function die1 ($ msg )
5
+ {
6
+ fwrite (STDERR , $ msg . "\n" );
7
+ exit (1 );
8
+ }
9
+
10
+ function run_system ($ cmd , $ errmsg = "" )
11
+ {
12
+ system ($ cmd , $ exit );
13
+
14
+ if ($ exit !== 0 ) {
15
+ die1 ("failed: "
16
+ . $ errmsg
17
+ . ' ( '
18
+ . $ cmd
19
+ . ') ' );
20
+ }
21
+ }
22
+
3
23
/* Fetch SSH Keys from the master db and create [email protected]
4
24
* files and push them to the gitolite administration repository
5
25
*/
6
26
define ('SSH_KEY_REPO ' , '/local/gitolite-admin/keydir ' );
7
27
8
28
mysql_connect ('localhost ' , 'nobody ' )
9
- or die ('unable to connect to server ' );
29
+ or die1 ('unable to connect to server ' );
10
30
mysql_select_db ('phpmasterdb ' )
11
- or die ('unable to select database ' );
31
+ or die1 ('unable to select database ' );
12
32
13
33
$ query = 'SELECT DISTINCT username, ssh_keys FROM users ' ;
14
- $ res = mysql_query ($ query ) or die ('cannot query ssh keys ' );
34
+ $ res = mysql_query ($ query ) or die1 ('cannot query ssh keys ' );
15
35
16
36
chdir (SSH_KEY_REPO )
17
- or die ('cannot change working directory ' );
37
+ or die1 ('cannot change working directory ' );
18
38
19
- false !== system ('git pull origin master ' )
20
- or die ('cannot update repository ' );
39
+ run_system ('git pull origin master ' , 'pull failed ' );
21
40
22
41
#TODO: remove all keys first in order to remove unused keys!
23
42
@@ -32,8 +51,7 @@ while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
32
51
}
33
52
}
34
53
35
- false !== system ('git add -u :/ ' )
36
- or die ('cannot update git information ' );
54
+ run_system ('git add -u :/ ' , 'cannot update git information ' );
37
55
38
56
foreach ($ ssh_keys as $ keyinfo ) {
39
57
$ filename = sprintf ('%s@%s.pub ' ,
@@ -43,16 +61,13 @@ while($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
43
61
$ fp = fopen ($ filename , 'w+ ' );
44
62
fwrite ($ fp , trim ($ keyinfo ['key ' ]) . "\n" );
45
63
fclose ($ fp );
46
- false !== system ('git add ' . $ filename )
47
- or die ('cannot execute git ' );
64
+ run_system ('git add ' . $ filename , 'cannot execute git ' );
48
65
}
49
66
}
50
67
51
68
if (system ('git status --porcelain -uno ' )) {
52
- false !== system ('git commit -m"Automatic update" ' )
53
- or die ('cannot commit ' );
54
- false !== system ('git push origin master ' )
55
- or die ('cannot push ' );
69
+ run_system ('git commit -m"Automatic update" ' , 'cannot commit ' );
70
+ run_system ('git push origin master ' , 'cannot push ' );
56
71
}
57
72
58
73
function get_ssh_keys ($ string ) {
0 commit comments