Thumbnail

mod/infra.git

Clone URL: https://git.buni.party/mod/infra.git

Viewing file on branch master

1#!/bin/sh
2
3ADMIN_REPO="/home/mod/gitolite-admin"
4CONF_FILE="$ADMIN_REPO/conf/gitolite.conf"
5KEYS_DIR="$ADMIN_REPO/keydir"
6
7# $1: user
8# $2...: command to run
9as() {
10 user="$1"
11 shift
12 su -s /bin/sh "$user" -c "$*"
13}
14
15# $*: message
16fail() {
17 printf '%s\n' "$*" >&2
18 exit 1
19}
20
21# $1: username
22# $2: key itself
23get_keyname() {
24 sum="$(printf '%s' "$2" | sha1sum - | cut -d' ' -f1)"
25 keyname="$KEYS_DIR/${1}@${sum}.pub"
26 [ -e "$keyname" ] && return 1
27 printf '%s' "$keyname"
28}
29
30[ "$(id -u)" -ne 0 ] && fail "Must be run as root"
31cd "$ADMIN_REPO" || fail "Cannot enter admin repo"
32
33import() {
34 echo "Importing keys"
35 # default stuff
36 as mod git pull
37 cat <<EOF >"$CONF_FILE"
38repo gitolite-admin
39RW+ = mod
40EOF
41 rm -f "$KEYS_DIR"/*.pub
42 as mod git rm -r --cached .
43
44 # generate user config & keys
45 for homedir in /home/*; do
46 user="${homedir##*/}"
47
48 cat <<EOF >>"$CONF_FILE"
49repo $user/.*
50 C = $user
51 RW+ = $user
52 R = @all
53EOF
54
55 keysfile="$homedir/.ssh/git_keys"
56 if [ -e "$keysfile" ]; then
57 while IFS= read -r line || [ -n "$line" ]; do
58 keyname="$(get_keyname "$user" "$line")"
59 if [ $? -gt 0 ]; then
60 echo "Duplicate key found: $line"
61 continue
62 fi
63 printf '%s' "$line" >"$keyname"
64 done <"$keysfile"
65 fi
66 done
67
68 chown -R mod:mod .
69
70 export GIT_AUTHOR_NAME="mod"
71 export GIT_AUTHOR_EMAIL="mod@groupnix"
72 export GIT_COMMITTER_NAME="mod"
73 export GIT_COMMITTER_EMAIL="mod@groupnix"
74 as mod 'git add -A && git commit -m "Update using import" && git push'
75 echo "Keys imported"
76}
77
78trap import USR1
79
80while :; do
81 import
82 # import twice a day
83 sleep $(( 60 * 60 * 12 )) &
84 wait $!
85done
86