aboutsummaryrefslogtreecommitdiff
blob: 7bf888106685b39c6746993d5fbc01b7f976a4c3 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/sh

GL_PACKAGE_CONF=/tmp/share/gitolite/conf
# must be the same as the value for the same variable in
# $GL_PACKAGE_CONF/example.gitolite.rc.  Sorry about the catch-22 :)

# TODO need to fix for portability to ksh and so on
# TODO need to get the version in there somehow

# This program is meant to be completely non-interactive, suitable for running
# server-side from a "post RPM/DEB install" script, or manually by users.

# usage:
#   $0 [foo.pub]

# The pubkey filename must end with ".pub" and is mandatory when you first run
# this command.  Otherwise it is optional, and can be used to override a
# pubkey file if you happen to have lost all gitolite-access to the repos (but
# do have shell access via some other means)

die() { echo "$@" >&2; exit 1; }

get_rc_val() {
    `dirname $0`/gl-query-rc $1
}

TEMPDIR=`mktemp -d -t tmp.XXXXXXXXXX`
export TEMPDIR
trap "/bin/rm -rf $TEMPDIR" 0

# quiet mode; only used to suppress popping up an editor on a new rc file
if [ "$1" = "-q" ]
then
    shift
    quiet=1
fi

if [ -n "$GITOLITE_HTTP_HOME" ]
then
    HOME=$GITOLITE_HTTP_HOME
    admin_name=$1
else
    pubkey_file=$1
    admin_name=
    if [ -n "$pubkey_file" ]
    then
        echo $pubkey_file | grep '.pub$' >/dev/null || die "$pubkey_file must end in .pub"
        [ -f $pubkey_file ] || die "cant find $pubkey_file"
        admin_name=` basename $pubkey_file .pub`
        echo $admin_name | grep '@' >/dev/null && die "please don't use '@' in the initial admin name"
    fi
fi

export GL_RC
GL_RC=`get_rc_val GL_RC 2>/dev/null`
[ -z "$GL_RC" ] && GL_RC=$HOME/.gitolite.rc

if [ -f $GL_RC ]
then
    print_rc_vars() {
        perl -ne 's/^\s+//; s/[\s=].*//; print if /^\$/;' < $1 | sort
    }
    print_rc_vars $GL_PACKAGE_CONF/example.gitolite.rc > $TEMPDIR/.newvars
    print_rc_vars $GL_RC                               > $TEMPDIR/.oldvars
    comm -23 $TEMPDIR/.newvars $TEMPDIR/.oldvars > $TEMPDIR/.diffvars
    if [ -s $TEMPDIR/.diffvars ]
    then
        cp $GL_PACKAGE_CONF/example.gitolite.rc $HOME/.gitolite.rc.new
        echo new version of the rc file saved in $HOME/.gitolite.rc.new
        echo
        echo please update $GL_RC manually if you need features
        echo controlled by any of the following variables:
        echo ----
        sed -e 's/^/    /' < $TEMPDIR/.diffvars
        echo ----
    fi
else
    [ -n "$GITOLITE_HTTP_HOME" ] || [ -n "$pubkey_file" ] || die "looks like first run -- I need a pubkey file"
    [ -z "$GITOLITE_HTTP_HOME" ] || [ -n "$admin_name"  ] || die "looks like first run -- I need an admin name"

    cp $GL_PACKAGE_CONF/example.gitolite.rc $GL_RC
    if [ -z "$quiet" ]
    then
        printf "The default settings in the "rc" file ($GL_RC) are fine for most\n"
        printf "people but if you wish to make any changes, you can do so now.\n\nhit enter..."
        read i
        ${EDITOR:-vi} $GL_RC
    fi
fi

# setup ssh stuff.  We break our normal rule that we will not fiddle with
# authkeys etc., because in this case it seems appropriate
(
    cd $HOME
    mkdir -p .ssh
    chmod go-rwx .ssh
    touch .ssh/authorized_keys
    chmod go-w . .ssh .ssh/authorized_keys
)

export GL_BINDIR
export REPO_BASE
export GL_ADMINDIR
GL_BINDIR=`  get_rc_val GL_BINDIR  `
REPO_BASE=`  get_rc_val REPO_BASE  `
GL_ADMINDIR=`get_rc_val GL_ADMINDIR`

# now we get to gitolite itself

gl-install -q

[ -f $GL_ADMINDIR/conf/gitolite.conf ] || {
    cat <<EOF | cut -c9- > $GL_ADMINDIR/conf/gitolite.conf
        repo    gitolite-admin
                RW+     =   $admin_name

        repo    testing
                RW+     =   @all
EOF
}
[ -n "$pubkey_file" ] && cp $pubkey_file $GL_ADMINDIR/keydir

touch $HOME/.ssh/authorized_keys
gl-compile-conf -q

# setup push-to-admin
[ -n "$pubkey_file" ] && (
    cd $HOME; cd $REPO_BASE/gitolite-admin.git
    GIT_WORK_TREE=$GL_ADMINDIR; export GIT_WORK_TREE
    git add conf/gitolite.conf keydir
    git config --get user.email >/dev/null || git config user.email $USER@`hostname`
    git config --get user.name  >/dev/null || git config user.name  "$USER on `hostname`"
    git diff --cached --quiet 2>/dev/null || git commit -am start
)

# now that the admin repo is created, you have to set the hooks properly; best
# do it by running install again
gl-install -q

# ----

# the never-ending quest to help with bloody ssh issues...
cd $GL_ADMINDIR/keydir
[ -n "$pubkey_file" ] && $GL_BINDIR/sshkeys-lint -q -a $admin_name < $HOME/.ssh/authorized_keys

exit 0