git-instaweb.shon commit git-instaweb: Wait for server to start before running web browser (d94775e)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Eric Wong
   4#
   5
   6PERL='@@PERL@@'
   7OPTIONS_KEEPDASHDASH=
   8OPTIONS_SPEC="\
   9git instaweb [options] (--start | --stop | --restart)
  10--
  11l,local        only bind on 127.0.0.1
  12p,port=        the port to bind to
  13d,httpd=       the command to launch
  14b,browser=     the browser to launch
  15m,module-path= the module path (only needed for apache2)
  16 Action
  17stop           stop the web server
  18start          start the web server
  19restart        restart the web server
  20"
  21
  22. git-sh-setup
  23
  24fqgitdir="$GIT_DIR"
  25local="$(git config --bool --get instaweb.local)"
  26httpd="$(git config --get instaweb.httpd)"
  27root="$(git config --get instaweb.gitwebdir)"
  28port=$(git config --get instaweb.port)
  29module_path="$(git config --get instaweb.modulepath)"
  30
  31conf="$GIT_DIR/gitweb/httpd.conf"
  32
  33# Defaults:
  34
  35# if installed, it doesn't need further configuration (module_path)
  36test -z "$httpd" && httpd='lighttpd -f'
  37
  38# Default is @@GITWEBDIR@@
  39test -z "$root" && root='@@GITWEBDIR@@'
  40
  41# any untaken local port will do...
  42test -z "$port" && port=1234
  43
  44resolve_full_httpd () {
  45        case "$httpd" in
  46        *apache2*|*lighttpd*)
  47                # ensure that the apache2/lighttpd command ends with "-f"
  48                if ! echo "$httpd" | sane_grep -- '-f *$' >/dev/null 2>&1
  49                then
  50                        httpd="$httpd -f"
  51                fi
  52                ;;
  53        esac
  54
  55        httpd_only="$(echo $httpd | cut -f1 -d' ')"
  56        if case "$httpd_only" in /*) : ;; *) which $httpd_only >/dev/null 2>&1;; esac
  57        then
  58                full_httpd=$httpd
  59        else
  60                # many httpds are installed in /usr/sbin or /usr/local/sbin
  61                # these days and those are not in most users $PATHs
  62                # in addition, we may have generated a server script
  63                # in $fqgitdir/gitweb.
  64                for i in /usr/local/sbin /usr/sbin "$root" "$fqgitdir/gitweb"
  65                do
  66                        if test -x "$i/$httpd_only"
  67                        then
  68                                full_httpd=$i/$httpd
  69                                return
  70                        fi
  71                done
  72
  73                echo >&2 "$httpd_only not found. Install $httpd_only or use" \
  74                     "--httpd to specify another httpd daemon."
  75                exit 1
  76        fi
  77}
  78
  79start_httpd () {
  80        if test -f "$fqgitdir/pid"; then
  81                say "Instance already running. Restarting..."
  82                stop_httpd
  83        fi
  84
  85        # here $httpd should have a meaningful value
  86        resolve_full_httpd
  87
  88        # don't quote $full_httpd, there can be arguments to it (-f)
  89        case "$httpd" in
  90        *mongoose*)
  91                #The mongoose server doesn't have a daemon mode so we'll have to fork it
  92                $full_httpd "$fqgitdir/gitweb/httpd.conf" &
  93                #Save the pid before doing anything else (we'll print it later)
  94                pid=$!
  95
  96                if test $? != 0; then
  97                        echo "Could not execute http daemon $httpd."
  98                        exit 1
  99                fi
 100
 101                cat > "$fqgitdir/pid" <<EOF
 102$pid
 103EOF
 104                ;;
 105        *)
 106                $full_httpd "$fqgitdir/gitweb/httpd.conf"
 107                if test $? != 0; then
 108                        echo "Could not execute http daemon $httpd."
 109                        exit 1
 110                fi
 111                ;;
 112        esac
 113}
 114
 115stop_httpd () {
 116        test -f "$fqgitdir/pid" && kill $(cat "$fqgitdir/pid")
 117        rm -f "$fqgitdir/pid"
 118}
 119
 120httpd_is_ready () {
 121        "$PERL" -MIO::Socket::INET -e "
 122local \$| = 1; # turn on autoflush
 123exit if (IO::Socket::INET->new('127.0.0.1:$port'));
 124print 'Waiting for \'$httpd\' to start ..';
 125do {
 126        print '.';
 127        sleep(1);
 128} until (IO::Socket::INET->new('127.0.0.1:$port'));
 129print qq! (done)\n!;
 130"
 131}
 132
 133while test $# != 0
 134do
 135        case "$1" in
 136        --stop|stop)
 137                stop_httpd
 138                exit 0
 139                ;;
 140        --start|start)
 141                start_httpd
 142                exit 0
 143                ;;
 144        --restart|restart)
 145                stop_httpd
 146                start_httpd
 147                exit 0
 148                ;;
 149        -l|--local)
 150                local=true
 151                ;;
 152        -d|--httpd)
 153                shift
 154                httpd="$1"
 155                ;;
 156        -b|--browser)
 157                shift
 158                browser="$1"
 159                ;;
 160        -p|--port)
 161                shift
 162                port="$1"
 163                ;;
 164        -m|--module-path)
 165                shift
 166                module_path="$1"
 167                ;;
 168        --)
 169                ;;
 170        *)
 171                usage
 172                ;;
 173        esac
 174        shift
 175done
 176
 177mkdir -p "$GIT_DIR/gitweb/tmp"
 178GIT_EXEC_PATH="$(git --exec-path)"
 179GIT_DIR="$fqgitdir"
 180GITWEB_CONFIG="$fqgitdir/gitweb/gitweb_config.perl"
 181export GIT_EXEC_PATH GIT_DIR GITWEB_CONFIG
 182
 183webrick_conf () {
 184        # generate a standalone server script in $fqgitdir/gitweb.
 185        cat >"$fqgitdir/gitweb/$httpd.rb" <<EOF
 186require 'webrick'
 187require 'yaml'
 188options = YAML::load_file(ARGV[0])
 189options[:StartCallback] = proc do
 190  File.open(options[:PidFile],"w") do |f|
 191    f.puts Process.pid
 192  end
 193end
 194options[:ServerType] = WEBrick::Daemon
 195server = WEBrick::HTTPServer.new(options)
 196['INT', 'TERM'].each do |signal|
 197  trap(signal) {server.shutdown}
 198end
 199server.start
 200EOF
 201        # generate a shell script to invoke the above ruby script,
 202        # which assumes _ruby_ is in the user's $PATH. that's _one_
 203        # portable way to run ruby, which could be installed anywhere,
 204        # really.
 205        cat >"$fqgitdir/gitweb/$httpd" <<EOF
 206#!/bin/sh
 207exec ruby "$fqgitdir/gitweb/$httpd.rb" \$*
 208EOF
 209        chmod +x "$fqgitdir/gitweb/$httpd"
 210
 211        cat >"$conf" <<EOF
 212:Port: $port
 213:DocumentRoot: "$root"
 214:DirectoryIndex: ["gitweb.cgi"]
 215:PidFile: "$fqgitdir/pid"
 216EOF
 217        test "$local" = true && echo ':BindAddress: "127.0.0.1"' >> "$conf"
 218}
 219
 220lighttpd_conf () {
 221        cat > "$conf" <<EOF
 222server.document-root = "$root"
 223server.port = $port
 224server.modules = ( "mod_setenv", "mod_cgi" )
 225server.indexfiles = ( "gitweb.cgi" )
 226server.pid-file = "$fqgitdir/pid"
 227server.errorlog = "$fqgitdir/gitweb/$httpd_only/error.log"
 228
 229# to enable, add "mod_access", "mod_accesslog" to server.modules
 230# variable above and uncomment this
 231#accesslog.filename = "$fqgitdir/gitweb/$httpd_only/access.log"
 232
 233setenv.add-environment = ( "PATH" => env.PATH, "GITWEB_CONFIG" => env.GITWEB_CONFIG )
 234
 235cgi.assign = ( ".cgi" => "" )
 236
 237# mimetype mapping
 238mimetype.assign             = (
 239  ".pdf"          =>      "application/pdf",
 240  ".sig"          =>      "application/pgp-signature",
 241  ".spl"          =>      "application/futuresplash",
 242  ".class"        =>      "application/octet-stream",
 243  ".ps"           =>      "application/postscript",
 244  ".torrent"      =>      "application/x-bittorrent",
 245  ".dvi"          =>      "application/x-dvi",
 246  ".gz"           =>      "application/x-gzip",
 247  ".pac"          =>      "application/x-ns-proxy-autoconfig",
 248  ".swf"          =>      "application/x-shockwave-flash",
 249  ".tar.gz"       =>      "application/x-tgz",
 250  ".tgz"          =>      "application/x-tgz",
 251  ".tar"          =>      "application/x-tar",
 252  ".zip"          =>      "application/zip",
 253  ".mp3"          =>      "audio/mpeg",
 254  ".m3u"          =>      "audio/x-mpegurl",
 255  ".wma"          =>      "audio/x-ms-wma",
 256  ".wax"          =>      "audio/x-ms-wax",
 257  ".ogg"          =>      "application/ogg",
 258  ".wav"          =>      "audio/x-wav",
 259  ".gif"          =>      "image/gif",
 260  ".jpg"          =>      "image/jpeg",
 261  ".jpeg"         =>      "image/jpeg",
 262  ".png"          =>      "image/png",
 263  ".xbm"          =>      "image/x-xbitmap",
 264  ".xpm"          =>      "image/x-xpixmap",
 265  ".xwd"          =>      "image/x-xwindowdump",
 266  ".css"          =>      "text/css",
 267  ".html"         =>      "text/html",
 268  ".htm"          =>      "text/html",
 269  ".js"           =>      "text/javascript",
 270  ".asc"          =>      "text/plain",
 271  ".c"            =>      "text/plain",
 272  ".cpp"          =>      "text/plain",
 273  ".log"          =>      "text/plain",
 274  ".conf"         =>      "text/plain",
 275  ".text"         =>      "text/plain",
 276  ".txt"          =>      "text/plain",
 277  ".dtd"          =>      "text/xml",
 278  ".xml"          =>      "text/xml",
 279  ".mpeg"         =>      "video/mpeg",
 280  ".mpg"          =>      "video/mpeg",
 281  ".mov"          =>      "video/quicktime",
 282  ".qt"           =>      "video/quicktime",
 283  ".avi"          =>      "video/x-msvideo",
 284  ".asf"          =>      "video/x-ms-asf",
 285  ".asx"          =>      "video/x-ms-asf",
 286  ".wmv"          =>      "video/x-ms-wmv",
 287  ".bz2"          =>      "application/x-bzip",
 288  ".tbz"          =>      "application/x-bzip-compressed-tar",
 289  ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
 290  ""              =>      "text/plain"
 291 )
 292EOF
 293        test x"$local" = xtrue && echo 'server.bind = "127.0.0.1"' >> "$conf"
 294}
 295
 296apache2_conf () {
 297        test -z "$module_path" && module_path=/usr/lib/apache2/modules
 298        bind=
 299        test x"$local" = xtrue && bind='127.0.0.1:'
 300        echo 'text/css css' > "$fqgitdir/mime.types"
 301        cat > "$conf" <<EOF
 302ServerName "git-instaweb"
 303ServerRoot "$root"
 304DocumentRoot "$root"
 305ErrorLog "$fqgitdir/gitweb/$httpd_only/error.log"
 306CustomLog "$fqgitdir/gitweb/$httpd_only/access.log" combined
 307PidFile "$fqgitdir/pid"
 308Listen $bind$port
 309EOF
 310
 311        for mod in mime dir; do
 312                if test -e $module_path/mod_${mod}.so; then
 313                        echo "LoadModule ${mod}_module " \
 314                             "$module_path/mod_${mod}.so" >> "$conf"
 315                fi
 316        done
 317        cat >> "$conf" <<EOF
 318TypesConfig "$fqgitdir/mime.types"
 319DirectoryIndex gitweb.cgi
 320EOF
 321
 322        # check to see if Dennis Stosberg's mod_perl compatibility patch
 323        # (<20060621130708.Gcbc6e5c@leonov.stosberg.net>) has been applied
 324        if test -f "$module_path/mod_perl.so" &&
 325           sane_grep 'MOD_PERL' "$root/gitweb.cgi" >/dev/null
 326        then
 327                # favor mod_perl if available
 328                cat >> "$conf" <<EOF
 329LoadModule perl_module $module_path/mod_perl.so
 330PerlPassEnv GIT_DIR
 331PerlPassEnv GIT_EXEC_DIR
 332PerlPassEnv GITWEB_CONFIG
 333<Location /gitweb.cgi>
 334        SetHandler perl-script
 335        PerlResponseHandler ModPerl::Registry
 336        PerlOptions +ParseHeaders
 337        Options +ExecCGI
 338</Location>
 339EOF
 340        else
 341                # plain-old CGI
 342                resolve_full_httpd
 343                list_mods=$(echo "$full_httpd" | sed 's/-f$/-l/')
 344                $list_mods | sane_grep 'mod_cgi\.c' >/dev/null 2>&1 || \
 345                if test -f "$module_path/mod_cgi.so"
 346                then
 347                        echo "LoadModule cgi_module $module_path/mod_cgi.so" >> "$conf"
 348                else
 349                        $list_mods | grep 'mod_cgid\.c' >/dev/null 2>&1 || \
 350                        if test -f "$module_path/mod_cgid.so"
 351                        then
 352                                echo "LoadModule cgid_module $module_path/mod_cgid.so" \
 353                                        >> "$conf"
 354                        else
 355                                echo "You have no CGI support!"
 356                                exit 2
 357                        fi
 358                        echo "ScriptSock logs/gitweb.sock" >> "$conf"
 359                fi
 360                cat >> "$conf" <<EOF
 361AddHandler cgi-script .cgi
 362<Location /gitweb.cgi>
 363        Options +ExecCGI
 364</Location>
 365EOF
 366        fi
 367}
 368
 369mongoose_conf() {
 370        cat > "$conf" <<EOF
 371# Mongoose web server configuration file.
 372# Lines starting with '#' and empty lines are ignored.
 373# For detailed description of every option, visit
 374# http://code.google.com/p/mongoose/wiki/MongooseManual
 375
 376root            $root
 377ports           $port
 378index_files     gitweb.cgi
 379#ssl_cert       $fqgitdir/gitweb/ssl_cert.pem
 380error_log       $fqgitdir/gitweb/$httpd_only/error.log
 381access_log      $fqgitdir/gitweb/$httpd_only/access.log
 382
 383#cgi setup
 384cgi_env         PATH=$PATH,GIT_DIR=$GIT_DIR,GIT_EXEC_PATH=$GIT_EXEC_PATH,GITWEB_CONFIG=$GITWEB_CONFIG
 385cgi_interp      $PERL
 386cgi_ext         cgi,pl
 387
 388# mimetype mapping
 389mime_types      .gz=application/x-gzip,.tar.gz=application/x-tgz,.tgz=application/x-tgz,.tar=application/x-tar,.zip=application/zip,.gif=image/gif,.jpg=image/jpeg,.jpeg=image/jpeg,.png=image/png,.css=text/css,.html=text/html,.htm=text/html,.js=text/javascript,.c=text/plain,.cpp=text/plain,.log=text/plain,.conf=text/plain,.text=text/plain,.txt=text/plain,.dtd=text/xml,.bz2=application/x-bzip,.tbz=application/x-bzip-compressed-tar,.tar.bz2=application/x-bzip-compressed-tar
 390EOF
 391}
 392
 393gitweb_conf() {
 394        cat > "$fqgitdir/gitweb/gitweb_config.perl" <<EOF
 395#!/usr/bin/perl
 396our \$projectroot = "$(dirname "$fqgitdir")";
 397our \$git_temp = "$fqgitdir/gitweb/tmp";
 398our \$projects_list = \$projectroot;
 399EOF
 400}
 401
 402gitweb_conf
 403
 404resolve_full_httpd
 405mkdir -p "$fqgitdir/gitweb/$httpd_only"
 406
 407case "$httpd" in
 408*lighttpd*)
 409        lighttpd_conf
 410        ;;
 411*apache2*)
 412        apache2_conf
 413        ;;
 414webrick)
 415        webrick_conf
 416        ;;
 417*mongoose*)
 418        mongoose_conf
 419        ;;
 420*)
 421        echo "Unknown httpd specified: $httpd"
 422        exit 1
 423        ;;
 424esac
 425
 426start_httpd
 427url=http://127.0.0.1:$port
 428
 429if test -n "$browser"; then
 430        httpd_is_ready && git web--browse -b "$browser" $url || echo $url
 431else
 432        httpd_is_ready && git web--browse -c "instaweb.browser" $url || echo $url
 433fi