install
author Paul Crowley <paul@lshift.net>
Mon, 12 Oct 2009 17:04:26 +0100
changeset 109 72100d3ed1bd
parent 96 975fb921c3f3
child 164 32131253c2f1
permissions -rwxr-xr-x
Don't mix exceptions and sys.exit based failures
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
     1
#!/usr/bin/env python
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
     2
84
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
     3
import sys
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
     4
import shutil
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
     5
import os
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
     6
import pwd
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
     7
import subprocess
87
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
     8
import optparse
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
     9
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    10
oparser = optparse.OptionParser()
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    11
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    12
oparser.add_option("--prefix")
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    13
oparser.add_option("--root")
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    14
oparser.set_defaults(root="", prefix="/usr/local")
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    15
(options, args) = oparser.parse_args()
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    16
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    17
if len(args) > 0:
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    18
    oparser.print_help()
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    19
    sys.exit(-1)
27
ec31ba248edd Installer to set things up automatically
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    20
84
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    21
# This must be run as root, because it must create an hg user.
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    22
# Normally the clean thing to do is let it fail with a permission error
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    23
# if a non-root user tries to run it, but I don't want anyone thinking
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    24
# that they can make it work as non-root by changing install paths.
89
30a8be7ce326 reword comment for clarity
Paul Crowley <paul@lshift.net>
parents: 87
diff changeset
    25
# Patches for doing this more cleanly welcome of course.
84
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    26
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    27
if os.getgid() != 0:
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    28
    print >>sys.stderr, "Install must be run as root user"
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    29
    sys.exit(-1)
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    30
964b04126d01 Force install as root and allow path to change.
Paul Crowley <paul@lshift.net>
parents: 80
diff changeset
    31
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    32
def installFiles(d, *sources):
87
535502c18eaa Give install real command line options
Paul Crowley <paul@lshift.net>
parents: 84
diff changeset
    33
    d = options.root + d
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    34
    os.makedirs(d)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    35
    for f in sources:
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    36
        shutil.copy(f, d)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    37
92
647c3e61bc95 use share not lib
Paul Crowley <paul@lshift.net>
parents: 91
diff changeset
    38
installFiles(options.prefix + '/share/mercurial-server',
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    39
    'src/hg-ssh',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    40
    'src/refresh-auth')
92
647c3e61bc95 use share not lib
Paul Crowley <paul@lshift.net>
parents: 91
diff changeset
    41
installFiles(options.prefix + '/share/mercurial-server/mercurialserver',
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    42
    'src/mercurialserver/__init__.py',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    43
    'src/mercurialserver/paths.py',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    44
    'src/mercurialserver/changes.py',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    45
    'src/mercurialserver/access.py',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    46
    'src/mercurialserver/servelog.py',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    47
    'src/mercurialserver/refreshauth.py',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    48
    'src/mercurialserver/ruleset.py')
92
647c3e61bc95 use share not lib
Paul Crowley <paul@lshift.net>
parents: 91
diff changeset
    49
installFiles(options.prefix + '/share/mercurial-server/init',
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    50
    'src/init/hginit',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    51
    'src/init/hgadmin-hgrc')
94
864e1a47234f Install documentation
Paul Crowley <paul@lshift.net>
parents: 93
diff changeset
    52
installFiles(options.prefix + '/share/doc/mercurial-server',
864e1a47234f Install documentation
Paul Crowley <paul@lshift.net>
parents: 93
diff changeset
    53
    'README',
864e1a47234f Install documentation
Paul Crowley <paul@lshift.net>
parents: 93
diff changeset
    54
    'doc/configuring-access',
864e1a47234f Install documentation
Paul Crowley <paul@lshift.net>
parents: 93
diff changeset
    55
    'doc/file-conditions',
864e1a47234f Install documentation
Paul Crowley <paul@lshift.net>
parents: 93
diff changeset
    56
    'doc/how-it-works',
864e1a47234f Install documentation
Paul Crowley <paul@lshift.net>
parents: 93
diff changeset
    57
    'doc/security')
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    58
installFiles('/etc/mercurial-server',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    59
    'src/init/conf/remote-hgrc',
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    60
    'src/init/conf/access.conf')
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    61
installFiles('/etc/mercurial-server/keys/root')
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    62
installFiles('/etc/mercurial-server/keys/users')
27
ec31ba248edd Installer to set things up automatically
Paul Crowley <paul@ciphergoth.org>
parents:
diff changeset
    63
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    64
def becomeFunc(u):
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    65
    p = pwd.getpwnam(u)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    66
    def become():
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    67
        os.setgid(p.pw_gid)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    68
        os.setegid(p.pw_gid)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    69
        os.setuid(p.pw_uid)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    70
        os.seteuid(p.pw_uid)
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    71
    return become
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    72
91
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    73
if options.root == '':
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    74
    try:
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    75
       pwd.getpwnam('hg')
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    76
    except KeyError:
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    77
        subprocess.check_call(
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    78
            "adduser --system --shell /bin/sh --group --disabled-password".split() + 
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    79
            ["--gecos", "Mercurial repositories", "hg"])
96
975fb921c3f3 abolish that one last path
Paul Crowley <paul@lshift.net>
parents: 94
diff changeset
    80
        subprocess.check_call([options.prefix + '/share/mercurial-server/init/hginit',
975fb921c3f3 abolish that one last path
Paul Crowley <paul@lshift.net>
parents: 94
diff changeset
    81
            options.prefix + '/share/mercurial-server'],
91
69dd70e1d844 Don't try to create user if we're installing outside /
Paul Crowley <paul@lshift.net>
parents: 90
diff changeset
    82
            preexec_fn = becomeFunc('hg'))
80
fcb20d7593e6 Rewrite install script in Python
Paul Crowley <paul@lshift.net>
parents: 79
diff changeset
    83