-
Notifications
You must be signed in to change notification settings - Fork 2
/
install-server.sh
133 lines (118 loc) · 3.78 KB
/
install-server.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/bash
msg() {
local mesg="$1"; shift
printf "\033[1;32m***\033[0;0m ${mesg}\n" "$@"
}
submsg() {
local mesg="$1"; shift
printf "\033[1;34m ->\033[0;0m ${mesg}\n" "$@"
}
die() {
local mesg="$1"; shift
printf "\033[1;31merror:\033[0;0m ${mesg}\n" "$@" >&2
exit 1
}
source config.sh || die "failed to read config"
check_config() {
msg "checking for key files..."
for i in "${admin_keys[@]}"; do
if [ -e "${i}" ]; then
submsg "found ssh key: ${i}"
else
die "key not found: ${i}"
fi
done
}
setup_user() {
repo_uid=$(id -u "${repo_user}" 2>/dev/null)
if (( $? != 0 )); then
msg "creating user ${repo_user}"
pw useradd "${repo_user}" -m || die "failed to create user ${repo_user}"
for i in "${repo_addgroups[@]}"; do
pw groupmod "${i}" -m "${repo_user}" || die "failed to add ${repo_user} to group ${i}"
done
repo_uid=$(id -u "${repo_user}")
if (( $? != 0 )); then die "failed to retrieve user's uid"; fi
fi
repo_gid=$(id -g "${repo_user}")
if (( $? != 0 )); then die "failed to retrieve user's gid"; fi
repo_home=$(eval "echo ~${repo_user}")
if (( $? != 0 )); then die "failed to retrieve user's home directory"; fi
[ -d "${repo_home}" ] || die "home does not exist: ${repo_home}"
msg "user: ${repo_user} (${repo_uid}:${repo_gid})"
msg "home: ${repo_home}"
}
setup_home() {
msg "setting up home directory"
for i in \
.ssh \
uploads \
log \
bin \
lib \
admin
do
submsg "${repo_home}/${i}"
install -d -g "${repo_gid}" -m755 -o "${repo_uid}" "${repo_home}/${i}" \
|| die "failed to create home directory structure"
done
}
copy_bin() {
msg "installing scripts"
install -d -g "${repo_gid}" -m755 -o "${repo_uid}" "${repo_home}/bin" \
|| die "failed to create user's bin/ directory"
for i in server-bin/*; do
submsg "${i#server-}"
install -g "${repo_gid}" -m755 -o "${repo_uid}" "${i}" "${repo_home}/bin/" \
|| die "failed to copy scripts"
done
for i in common-lib/*; do
submsg "${i#common-}"
install -g "${repo_gid}" -m755 -o "${repo_uid}" "${i}" "${repo_home}/lib/" \
|| die "failed to copy scripts"
done
for i in server-lib/*; do
submsg "${i#server-}"
install -g "${repo_gid}" -m755 -o "${repo_uid}" "${i}" "${repo_home}/lib/" \
|| die "failed to copy scripts"
done
}
setup_admin_repo() {
msg "setting up admin repository"
pushd "${repo_home}/admin" >/dev/null \
|| die "failed to change directory to ${repo_home}/admin"
[ -d "admin.git" ] || git init --bare admin.git \
|| die "failed to initialize admin git repository"
chown -R "${repo_uid}:${repo_gid}" admin.git
submsg "setting up git push hook"
rm -f "admin.git/hooks/"{post-receive,post-update}
#ln -svf "${repo_home}/bin/admin-push-hook" "admin.git/hooks/post-receive" \
# || die "failed to setup post-receive git hook"
ln -svf "${repo_home}/bin/admin-push-hook" "admin.git/hooks/post-update" \
|| die "failed to setup post-receive git hook"
popd >/dev/null
}
config_home() {
msg "copying key files..."
cat "${admin_keys[@]}" > "${repo_home}/.ssh/admin_keys.pub" \
|| die "failed to create .ssh/admin_keys.pub"
chown -R "${repo_uid}:${repo_gid}" "${repo_home}/.ssh"
msg "running admin push hook to populate the authorized_keys file"
su - "${repo_user}" -c "cd ~/admin/admin.git && ../../bin/admin-push-hook 2>/dev/null"
{
echo '# Automatically generated, do not change!'
echo '# Use the "config" file to overwrite these values!'
echo '#'
grep '^repo_user=' config.sh
grep '^repo_base=' config.sh
grep '^push_hook=' config.sh
grep '^repo_list=' config.sh
} > "${repo_home}/admin/sys_config"
}
check_config
setup_user
setup_home
copy_bin
setup_admin_repo
config_home
# vim: set ts=2 sts=2 sw=2 et: