*.cgi: replace hard-coded use of /usr/bin/mail with mailer_pipe
[girocco.git] / shlib.sh
blob79e961f76cf3d86176f348d1d8ed58eb8b339845
1 #!/bin/bash
3 # This is generic shell library for all the scripts used by Girocco;
4 # most importantly, it introduces all the $cfg_* shell variables.
7 # Import all the variables from Girocco::Config to the local environment,
8 # prefixing them with 'cfg_'. E.g. $cfg_admin is admin's mail address now.
9 __girocco_conf="$GIROCCO_CONF"
10 [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config"
11 [ -z "$basedir" ] || __girocco_extrainc="-I$basedir"
12 eval $(perl -I@basedir@ $__girocco_extrainc -M$__girocco_conf -le \
13 'foreach (keys %Girocco::Config::) {
14 my $val = ${$Girocco::Config::{$_}}; $val ||= "";
15 print "cfg_$_=\"$val\"";
16 }')
19 git() { "$cfg_git_bin" "$@"; }
22 # Supports both the bang CMD... and bang_eval CMD... functions
23 # Called when the command fails
24 # Should only be called from bang and bang_eval functions
25 bang_err() {
26 if ! [ -e .banged ]; then
27 bangmailok=true
28 ! [ -f HEAD -a -f config -a -d objects ] ||
29 bangmailok="$(GIT_DIR=. git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
30 bangaddrs=''
31 [ "$bangmailok" = "false" -o -z "$mail" ] || bangaddrs="$mail"
32 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] ||
33 if [ -z "$bangaddrs" ]; then bangaddrs="$cfg_admin"; else bangaddrs="$bangaddrs,$cfg_admin"; fi
34 [ -z "$bangaddrs" ] ||
36 echo "$* failed with error code $errcode"
37 echo ""
38 [ ! -n "$bang_once" ] || echo "you will not receive any more notifications until recovery"
39 echo "this status message may be disabled on the project admin page"
40 echo ""
41 echo "Log follows:"
42 echo ""
43 cat "$bang_log"
44 } | mail -s "[$cfg_name] $proj $bang_action failed" "$bangaddrs"
46 touch .banged
47 bang_trap
48 exit 1
51 # bang CMD... will execute the command with well-defined failure mode;
52 # set bang_action to string of the failed action ('clone', 'update', ...);
53 # pre-set bang_once=1 to make sure jobs banging on a repo repeatedly will
54 # not spam the owner; re-define the bang_trap() function to do custom
55 # cleanup before bailing out
56 bang() {
57 if [ -n "$show_progress" ]; then
58 exec 3>&1
59 errcode=
60 read -r errcode <<-EOT || :
62 exec 4>&3 3>&1 1>&4 4>&-
63 { "$@" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
65 EOT
66 exec 3>&-
67 if [ -z "$errcode" ]; then
68 # All right. Cool.
69 return;
71 else
72 if "$@" >>"$bang_log" 2>&1; then
73 # All right. Cool.
74 return;
75 else
76 errcode="$?"
79 bang_err "$@"
82 # bang_eval CMD... will evaluate the command with well-defined failure mode;
83 # Identical to bang CMD... except the command is eval'd instead of executed.
84 bang_eval() {
85 if [ -n "$show_progress" ]; then
86 exec 3>&1
87 errcode=
88 read -r errcode <<-EOT || :
90 exec 4>&3 3>&1 1>&4 4>&-
91 { eval "$*" 3>&- || echo $? >&3; } 2>&1 | tee -a "$bang_log"
93 EOT
94 exec 3>&-
95 if [ -z "$errcode" ]; then
96 # All right. Cool.
97 return;
99 else
100 if eval "$*" >>"$bang_log" 2>&1; then
101 # All right. Cool.
102 return;
103 else
104 errcode="$?"
107 bang_err "$@"
110 # Default bang settings:
111 bang_setup() {
112 bang_action="lame_programmer"
113 bang_once=
114 bang_trap() { :; }
115 bang_log="$(mktemp -t repomgr-XXXXXX)"
116 trap "rm \"\$bang_log\"" EXIT
120 # Progress report - if show_progress is set, shows the given message.
121 progress() {
122 [ ! -n "$show_progress" ] || echo "$@"
126 # Project config accessors; must be run in project directory
127 config_get() {
128 git config "gitweb.$1"
131 config_set() {
132 git config "gitweb.$1" "$2" && chgrp repo config && chmod g+w config
135 config_set_raw() {
136 git config "$1" "$2" && chgrp repo config && chmod g+w config
140 # Tool for checking whether given number of seconds has not passed yet
141 check_interval() {
142 od="$(config_get "$1" || :)"
143 [ -n "$od" ] || return 1
144 os="$(perl -I@basedir@ -MGirocco::Util -e "print parse_rfc2822_date('$od')")"
145 [ -n "$os" ] || return 1
146 ns="$(date +%s)"
147 [ $ns -lt $(($os+$2)) ]
151 # List all Git repositories, with given prefix if specified, one-per-line
152 get_repo_list() {
153 if [ -n "$1" ]; then
154 cut -d : -f 1,3 "$cfg_chroot"/etc/group | grep "^$1"
155 else
156 cut -d : -f 1,3 "$cfg_chroot"/etc/group
157 fi | while IFS=: read name id; do
158 [ $id -lt 65536 ] || echo "$name"
159 done