Example #1
0
def psql_import(sql_file):
    temp_file = tempfile.mkstemp()[1]
    shutil.copy(sql_file, temp_file)
    # Make sure file is readable by postgres user
    os.chmod(temp_file, stat.S_IROTH)
    process.check_output(["su", "-s", "/bin/sh", "-c", "psql -f %s" % temp_file, installation.system.username])
    os.unlink(temp_file)
Example #2
0
    def install(*packages):
        global aptget, aptget_approved, aptget_updated, need_blankline, all_ok
        if aptget and not aptget_approved:
            all_ok = False
            print """\
Found 'apt-get' executable in your $PATH.  This script can attempt to install
missing software using it.
"""
            aptget_approved = installation.input.yes_or_no(
                prompt="Do you want to use 'apt-get' to install missing packages?",
                default=True)
            if not aptget_approved: aptget = None
        if aptget:
            installed_anything = False
            aptget_env = os.environ.copy()
            if arguments.headless:
                aptget_env["DEBIAN_FRONTEND"] = "noninteractive"
            if not aptget_updated:
                process.check_output(
                    [aptget, "-qq", "update"],
                    env=aptget_env)
                aptget_updated = True
            aptget_output = process.check_output(
                [aptget, "-qq", "-y", "install"] + list(packages),
                env=aptget_env)
            for line in aptget_output.splitlines():
                match = re.search(r"([^ ]+) \(.* \.\.\./([^)]+\.deb)\) \.\.\.", line)
                if match:
                    need_blankline = True
                    installed_anything = True
                    print "Installed: %s (%s)" % (match.group(1), match.group(2))
            return installed_anything
        else:
            return False
Example #3
0
 def check_bcrypt():
     global bcrypt_available
     try:
         process.check_output(
             [sys.executable, "-c", "import bcrypt"],
             stderr=process.STDOUT)
         bcrypt_available = True
     except process.CalledProcessError:
         pass
Example #4
0
def psql_import(sql_file):
    temp_file = tempfile.mkstemp()[1]
    shutil.copy(sql_file, temp_file)
    # Make sure file is readable by postgres user
    os.chmod(temp_file, stat.S_IROTH)
    process.check_output([
        "su", "-s", "/bin/sh", "-c",
        "psql -f %s" % temp_file, installation.system.username
    ])
    os.unlink(temp_file)
Example #5
0
def install(data):
    global user_created, database_created, language_created

    print "Creating database ..."

    # Several subsequent commands will run as Critic system user or "postgres" user,
    # and these users typically don't have read access to the installation 'root_dir'
    root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
    original_dir = os.getcwd()
    try:
        # Set cwd to something that Critic system / "postgres" users has access to.
        os.chdir(tempfile.gettempdir())

        process.check_output([
            "su", "-c",
            "psql -c 'CREATE USER \"%s\";'" % installation.system.username,
            "postgres"
        ])
        user_created = True

        process.check_output(
            ["su", "-c", "psql -c 'CREATE DATABASE \"critic\";'", "postgres"])
        database_created = True

        try:
            process.check_output(
                ["su", "-c", "createlang plpgsql critic", "postgres"],
                stderr=process.STDOUT)
            language_created = True
        except process.CalledProcessError, error:
            if re.search(r"\blanguage\b.*\balready installed\b", error.output):
                pass
            else:
                raise

        process.check_output([
            "su", "-c",
            "psql -c 'GRANT ALL ON DATABASE \"critic\" TO \"%s\";'" %
            installation.system.username, "postgres"
        ])
        psql_import(os.path.join(root_dir, "dbschema.sql"))
        psql_import(os.path.join(root_dir, "dbschema.comments.sql"))
        psql_import(os.path.join(root_dir, "path.pgsql"))
        psql_import(os.path.join(root_dir, "comments.pgsql"))
        psql_import(os.path.join(root_dir, "roles.sql"))

        import psycopg2

        def adapt(value):
            return psycopg2.extensions.adapt(value).getquoted()

        process.check_input(
            [
                "su", "-s", "/bin/sh", "-c", "psql -q -f -",
                installation.system.username
            ],
            stdin=
            ("""INSERT INTO systemidentities (key, name, url_prefix, description)
                                          VALUES ('main', 'main', %s, 'Main');"""
             % adapt("http://%s" % installation.system.hostname)))
Example #6
0
def install(data):
    global site_enabled, default_site_disabled

    source_path = os.path.join(installation.root_dir, "installation", "templates", "site")
    target_path = os.path.join("/etc", "apache2", "sites-available", "critic-main")

    with open(target_path, "w") as target:
        created_file.append(target_path)

        os.chmod(target_path, 0640)

        with open(source_path, "r") as source:
            target.write((source.read().decode("utf-8") % data).encode("utf-8"))

    if installation.prereqs.a2enmod:
        process.check_call([installation.prereqs.a2enmod, "expires"])
        process.check_call([installation.prereqs.a2enmod, "rewrite"])
        process.check_call([installation.prereqs.a2enmod, "wsgi"])

    if installation.prereqs.a2ensite:
        process.check_call([installation.prereqs.a2ensite, "critic-main"])
        site_enabled = True
    if installation.prereqs.a2dissite:
        output = process.check_output([installation.prereqs.a2dissite, "default"], env={ "LANG": "C" })
        if "Site default disabled." in output:
            default_site_disabled = True

    process.check_call(["service", "apache2", "restart"])

    return True
Example #7
0
    def install(*packages):
        global aptget, aptget_approved, need_blankline, all_ok
        if aptget and not aptget_approved:
            all_ok = False
            print """\
Found 'apt-get' executable in your $PATH.  This script can attempt to install
missing software using it.
"""
            aptget_approved = installation.input.yes_or_no(
                prompt=
                "Do you want to use 'apt-get' to install missing packages?",
                default=True)
            if not aptget_approved: aptget = None
        if aptget:
            installed_anything = False
            for line in process.check_output([aptget, "-qq", "-y", "install"] +
                                             list(packages)).splitlines():
                match = re.search(r"([^ ]+) \(.* \.\.\./([^)]+\.deb)\) \.\.\.",
                                  line)
                if match:
                    need_blankline = True
                    installed_anything = True
                    print "Installed: %s (%s)" % (match.group(1),
                                                  match.group(2))
            return installed_anything
        else:
            return False
Example #8
0
def install(data):
    global user_created, database_created, language_created

    print "Creating database ..."

    # Several subsequent commands will run as Critic system user or "postgres" user,
    # and these users typically don't have read access to the installation 'root_dir'
    root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
    original_dir = os.getcwd()
    try:
        # Set cwd to something that Critic system / "postgres" users has access to.
        os.chdir(tempfile.gettempdir())

        process.check_output(["su", "-c", "psql -v ON_ERROR_STOP=1 -c 'CREATE USER \"%s\";'" % installation.system.username, "postgres"])
        user_created = True

        process.check_output(["su", "-c", "psql -v ON_ERROR_STOP=1 -c 'CREATE DATABASE \"critic\";'", "postgres"])
        database_created = True

        try:
            process.check_output(["su", "-c", "createlang plpgsql critic", "postgres"], stderr=process.STDOUT)
            language_created = True
        except process.CalledProcessError:
            # The 'createlang' command fails if the language is already enabled
            # in the database, and we want to ignore such failures.  It might
            # also fail for other reasons, that we really don't mean to ignore,
            # but in that case importing the *.pgsql files below would fail,
            # since they define PL/pgSQL functions.
            pass

        process.check_output(["su", "-c", "psql -v ON_ERROR_STOP=1 -c 'GRANT ALL ON DATABASE \"critic\" TO \"%s\";'" % installation.system.username, "postgres"])
        psql_import(os.path.join(root_dir, "dbschema.sql"))
        psql_import(os.path.join(root_dir, "dbschema.comments.sql"))
        psql_import(os.path.join(root_dir, "comments.pgsql"))
        psql_import(os.path.join(root_dir, "roles.sql"))

        import psycopg2

        def adapt(value): return psycopg2.extensions.adapt(value).getquoted()

        quoted_urlprefix = adapt("http://%s" % installation.system.hostname)
        quoted_installed_sha1 = adapt(data["sha1"])
        add_systemidentity_query = """INSERT INTO systemidentities (key, name, url_prefix, description, installed_sha1)
                                          VALUES ('main', 'main', %s, 'Main', %s);""" \
                                   % (quoted_urlprefix, quoted_installed_sha1)
        process.check_input(["su", "-s", "/bin/sh", "-c", "psql -q -v ON_ERROR_STOP=1 -f -", installation.system.username],
                            stdin=add_systemidentity_query)

    finally:
        os.chdir(original_dir)

    return True
Example #9
0
def undo():
    if language_created:
        process.check_output(["su", "-c", "droplang plpgsql critic", "postgres"])
    if database_created:
        process.check_output(["su", "-c", "psql -c 'DROP DATABASE \"critic\";'", "postgres"])
    if user_created:
        process.check_output(["su", "-c", "psql -c 'DROP USER \"%s\";'" % installation.system.username, "postgres"])
Example #10
0
def undo():
    if language_created:
        process.check_output(
            ["su", "-c", "droplang plpgsql critic", "postgres"])
    if database_created:
        process.check_output(
            ["su", "-c", "psql -c 'DROP DATABASE \"critic\";'", "postgres"])
    if user_created:
        process.check_output([
            "su", "-c",
            "psql -c 'DROP USER \"%s\";'" % installation.system.username,
            "postgres"
        ])
Example #11
0
def install(data):
    global user_created, database_created, language_created

    print "Creating database ..."

    # Several subsequent commands will run as Critic system user or "postgres" user,
    # and these users typically don't have read access to the installation 'root_dir'
    root_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
    original_dir = os.getcwd()
    try:
        # Set cwd to something that Critic system / "postgres" users has access to.
        os.chdir(tempfile.gettempdir())

        process.check_output(["su", "-c", "psql -c 'CREATE USER \"%s\";'" % installation.system.username, "postgres"])
        user_created = True

        process.check_output(["su", "-c", "psql -c 'CREATE DATABASE \"critic\";'", "postgres"])
        database_created = True

        try:
            process.check_output(["su", "-c", "createlang plpgsql critic", "postgres"], stderr=process.STDOUT)
            language_created = True
        except process.CalledProcessError, error:
            if re.search(r"\blanguage\b.*\balready installed\b", error.output): pass
            else: raise

        process.check_output(["su", "-c", "psql -c 'GRANT ALL ON DATABASE \"critic\" TO \"%s\";'" % installation.system.username, "postgres"])
        psql_import(os.path.join(root_dir, "dbschema.sql"))
        psql_import(os.path.join(root_dir, "dbschema.comments.sql"))
        psql_import(os.path.join(root_dir, "path.pgsql"))
        psql_import(os.path.join(root_dir, "comments.pgsql"))
        psql_import(os.path.join(root_dir, "roles.sql"))

        import psycopg2

        def adapt(value): return psycopg2.extensions.adapt(value).getquoted()

        process.check_input(["su", "-s", "/bin/sh", "-c", "psql -q -f -", installation.system.username],
                            stdin=("""INSERT INTO systemidentities (key, name, url_prefix, description)
                                          VALUES ('main', 'main', %s, 'Main');"""
                                   % adapt("http://%s" % installation.system.hostname)))
Example #12
0
    def install(*packages):
        global aptget, aptget_approved, need_blankline, all_ok
        if aptget and not aptget_approved:
            all_ok = False
            print """\
Found 'apt-get' executable in your $PATH.  This script can attempt to install
missing software using it.
"""
            aptget_approved = installation.input.yes_or_no(
                prompt="Do you want to use 'apt-get' to install missing packages?",
                default=True)
            if not aptget_approved: aptget = None
        if aptget:
            installed_anything = False
            for line in process.check_output([aptget, "-qq", "-y", "install"] + list(packages)).splitlines():
                match = re.search(r"([^ ]+) \(.* \.\.\./([^)]+\.deb)\) \.\.\.", line)
                if match:
                    need_blankline = True
                    print "Installed: %s (%s)" % (match.group(1), match.group(2))
            return True
        else:
            return False
Example #13
0
def execute():
    global user_created, database_created

    print "Creating database ..."

    process.check_output(["su", "-c", "psql -c 'CREATE USER \"%s\";'" % installation.system.username, "postgres"])
    user_created = True

    process.check_output(["su", "-c", "psql -c 'CREATE DATABASE \"critic\";'", "postgres"])
    database_created = True

    try:
        process.check_output(["su", "-c", "createlang plpgsql critic", "postgres"], stderr=process.STDOUT)
        language_created = True
    except process.CalledProcessError, error:
        if re.search(r"\blanguage\b.*\balready installed\b", error.output): pass
        else: raise
Example #14
0
def prepare(mode, arguments, data):
    global hostname, username, email, create_system_user
    global groupname, create_system_group
    global uid, gid

    if mode == "install":
        print """
Critic Installation: System
===========================
"""

        if arguments.system_hostname: hostname = arguments.system_hostname
        else:
            try:
                hostname = process.check_output(["hostname", "--fqdn"]).strip()
            except:
                pass

            hostname = installation.input.string(
                prompt="What is the machine's FQDN?", default=hostname)

        while True:
            if arguments.system_username: username = arguments.system_username
            else:
                username = installation.input.string(
                    prompt="What system user should Critic run as?",
                    default=username)

            try:
                pwd.getpwnam(username)
                user_exists = True
            except:
                user_exists = False

            if user_exists:
                print """
The system user '%s' already exists.
""" % username

                if installation.input.yes_or_no(
                        prompt="Use the existing system user '%s'?" % username,
                        default=True):
                    create_system_user = False
                    break
            else:
                print """
The system user '%s' doesn't exists.
""" % username

                if installation.input.yes_or_no(
                        prompt="Create a system user named '%s'?" % username,
                        default=True):
                    create_system_user = True
                    break

        while True:
            if arguments.system_groupname:
                groupname = arguments.system_groupname
            else:
                groupname = installation.input.string(
                    prompt="What system group should Critic run as?",
                    default=groupname)

            try:
                grp.getgrnam(groupname)
                group_exists = True
            except:
                group_exists = False

            if group_exists:
                print """
The system group '%s' already exists.
""" % groupname

                if installation.input.yes_or_no(
                        prompt="Use the existing system group '%s'?" %
                        groupname,
                        default=True):
                    create_system_group = False
                    break
            else:
                print """
The system group '%s' doesn't exists.
""" % groupname

                if installation.input.yes_or_no(
                        prompt="Create a system group named '%s'?" % groupname,
                        default=True):
                    create_system_group = True
                    break

        if arguments.system_email: email = arguments.system_email
        else:
            email = installation.input.string(
                prompt=
                "What address should be used as the sender of emails from the system?",
                default=("%s@%s" % (username, hostname)))
    else:
        import configuration

        hostname = configuration.base.HOSTNAME
        username = configuration.base.SYSTEM_USER_NAME
        email = configuration.base.SYSTEM_USER_EMAIL

        try:
            groupname = configuration.base.SYSTEM_GROUP_NAME
        except AttributeError:
            groupname = data["installation.system.groupname"]

        uid = pwd.getpwnam(username).pw_uid
        gid = grp.getgrnam(groupname).gr_gid

    data["installation.system.hostname"] = hostname
    data["installation.system.username"] = username
    data["installation.system.email"] = email
    data["installation.system.groupname"] = groupname

    return True
Example #15
0
def prepare(arguments):
    global hostname, username, email, create_system_user
    global groupname, create_system_group

    all_ok = True

    print """
Critic Installation: System
===========================
"""

    if arguments.system_hostname: hostname = arguments.system_hostname
    else:
        try: hostname = process.check_output(["hostname", "--fqdn"]).strip()
        except: pass

        hostname = installation.input.string(prompt="What is the machine's FQDN?",
                                                    default=hostname)

    while True:
        if arguments.system_username: username = arguments.system_username
        else:
            username = installation.input.string(prompt="What system user should Critic run as?",
                                                        default=username)

        try:
            pwd.getpwnam(username)
            user_exists = True
        except:
            user_exists = False

        if user_exists:
            print """
The system user '%s' already exists.
""" % username

            if installation.input.yes_or_no(prompt="Use the existing system user '%s'?" % username,
                                            default=True):
                create_system_user = False
                break
        else:
            print """
The system user '%s' doesn't exists.
""" % username

            if installation.input.yes_or_no(prompt="Create a system user named '%s'?" % username,
                                            default=True):
                create_system_user = True
                break

    while True:
        if arguments.system_groupname: groupname = arguments.system_groupname
        else:
            groupname = installation.input.string(prompt="What system group should Critic run as?",
                                                        default=groupname)

        try:
            grp.getgrnam(groupname)
            group_exists = True
        except:
            group_exists = False

        if group_exists:
            print """
The system group '%s' already exists.
""" % groupname

            if installation.input.yes_or_no(prompt="Use the existing system group '%s'?" % groupname,
                                            default=True):
                create_system_group = False
                break
        else:
            print """
The system group '%s' doesn't exists.
""" % groupname

            if installation.input.yes_or_no(prompt="Create a system group named '%s'?" % groupname,
                                            default=True):
                create_system_group = True
                break

    if arguments.system_email: email = arguments.system_email
    else:
        email = installation.input.string(prompt="What address should be used as the sender of emails from the system?",
                                          default=("%s@%s" % (username, hostname)))

    return True
Example #16
0
    print "Creating database ..."

    process.check_output(["su", "-c", "psql -c 'CREATE USER \"%s\";'" % installation.system.username, "postgres"])
    user_created = True

    process.check_output(["su", "-c", "psql -c 'CREATE DATABASE \"critic\";'", "postgres"])
    database_created = True

    try:
        process.check_output(["su", "-c", "createlang plpgsql critic", "postgres"], stderr=process.STDOUT)
        language_created = True
    except process.CalledProcessError, error:
        if re.search(r"\blanguage\b.*\balready installed\b", error.output): pass
        else: raise

    process.check_output(["su", "-c", "psql -c 'GRANT ALL ON DATABASE \"critic\" TO \"%s\";'" % installation.system.username, "postgres"])
    process.check_output(["su", "-s", "/bin/sh", "-c", "psql -f dbschema.sql", installation.system.username])
    process.check_output(["su", "-s", "/bin/sh", "-c", "psql -f dbschema.comments.sql", installation.system.username])
    process.check_output(["su", "-s", "/bin/sh", "-c", "psql -f path.pgsql", installation.system.username])
    process.check_output(["su", "-s", "/bin/sh", "-c", "psql -f comments.pgsql", installation.system.username])
    process.check_output(["su", "-s", "/bin/sh", "-c", "psql -f roles.sql", installation.system.username])

    import psycopg2

    def adapt(value): return psycopg2.extensions.adapt(value).getquoted()

    process.check_input(["su", "-s", "/bin/sh", "-c", "psql -q -f -", installation.system.username],
                        stdin=("""INSERT INTO systemidentities (key, name, url_prefix, description)
                                      VALUES ('main', 'main', %s, 'Main');"""
                               % adapt("http://%s" % installation.system.hostname)))
Example #17
0
def prepare(mode, arguments, data):
    global hostname, username, email, create_system_user
    global groupname, create_system_group
    global uid, gid
    global is_development

    if mode == "install":
        print """
Critic Installation: System
===========================
"""

        if arguments.system_hostname:
            hostname = arguments.system_hostname
        else:
            try:
                hostname = process.check_output(["hostname", "--fqdn"]).strip()
            except:
                pass

            hostname = installation.input.string(prompt="What is the machine's FQDN?", default=hostname)

        while True:
            if arguments.system_username:
                username = arguments.system_username
            else:
                username = installation.input.string(prompt="What system user should Critic run as?", default=username)

            try:
                pwd.getpwnam(username)
                user_exists = True
            except:
                user_exists = False

            if user_exists:
                print """
The system user '%s' already exists.
""" % username

                if installation.input.yes_or_no(prompt="Use the existing system user '%s'?" % username, default=True):
                    create_system_user = False
                    break
            else:
                print """
The system user '%s' doesn't exists.
""" % username

                if arguments.force_create_system_user or installation.input.yes_or_no(
                    prompt="Create a system user named '%s'?" % username, default=True
                ):
                    create_system_user = True
                    break

        while True:
            if arguments.system_groupname:
                groupname = arguments.system_groupname
            else:
                groupname = installation.input.string(
                    prompt="What system group should Critic run as?", default=groupname
                )

            try:
                grp.getgrnam(groupname)
                group_exists = True
            except:
                group_exists = False

            if group_exists:
                print """
The system group '%s' already exists.
""" % groupname

                if installation.input.yes_or_no(prompt="Use the existing system group '%s'?" % groupname, default=True):
                    create_system_group = False
                    break
            else:
                print """
The system group '%s' doesn't exists.
""" % groupname

                if arguments.force_create_system_group or installation.input.yes_or_no(
                    prompt="Create a system group named '%s'?" % groupname, default=True
                ):
                    create_system_group = True
                    break

        if arguments.system_email:
            email = arguments.system_email
        else:
            email = installation.input.string(
                prompt="What address should be used as the sender of emails from the system?",
                default=("%s@%s" % (username, hostname)),
            )

        if arguments.is_development:
            is_development = True
    else:
        import configuration

        hostname = configuration.base.HOSTNAME
        username = configuration.base.SYSTEM_USER_NAME
        email = configuration.base.SYSTEM_USER_EMAIL

        try:
            groupname = configuration.base.SYSTEM_GROUP_NAME
        except AttributeError:
            groupname = data["installation.system.groupname"]

        uid = pwd.getpwnam(username).pw_uid
        gid = grp.getgrnam(groupname).gr_gid

        is_development = configuration.base.IS_DEVELOPMENT

    data["installation.system.hostname"] = hostname
    data["installation.system.username"] = username
    data["installation.system.email"] = email
    data["installation.system.groupname"] = groupname
    data["installation.system.is_development"] = is_development

    return True
Example #18
0
def prepare(arguments):
    global git, tar, psql, bcrypt_available, aptget, apache2ctl, a2enmod, a2ensite, a2dissite

    print """
Critic Installation: Prerequisites
==================================
"""

    success = True
    all_ok = True

    aptget = find_executable("apt-get")

    def install(*packages):
        global aptget, aptget_approved, need_blankline, all_ok
        if aptget and not aptget_approved:
            all_ok = False
            print """\
Found 'apt-get' executable in your $PATH.  This script can attempt to install
missing software using it.
"""
            aptget_approved = installation.input.yes_or_no(
                prompt="Do you want to use 'apt-get' to install missing packages?", default=True
            )
            if not aptget_approved:
                aptget = None
        if aptget:
            installed_anything = False
            for line in process.check_output([aptget, "-qq", "-y", "install"] + list(packages)).splitlines():
                match = re.search(r"([^ ]+) \(.* \.\.\./([^)]+\.deb)\) \.\.\.", line)
                if match:
                    need_blankline = True
                    print "Installed: %s (%s)" % (match.group(1), match.group(2))
            return True
        else:
            return False

    git = find_executable("git")
    if not git:
        if aptget_approved and install("git-core"):
            git = find_executable("git")
        if not git:
            blankline()
            all_ok = False
            print """\
No 'git' executable found in $PATH.  Make sure the Git version control system
is installed.  Is Debian/Ubuntu the package you need to install is 'git-core'
(or 'git' in newer versions, but 'git-core' typically still works.)  The source
code can be downloaded here:

  https://github.com/git/git
"""
            if not aptget_approved and install("git-core"):
                git = find_executable("git")
            if not git:
                success = False

    tar = find_executable("tar")
    assert tar, "System has no 'tar'?!?"

    psql = find_executable("psql")
    if not psql:
        if aptget_approved and install("postgresql", "postgresql-client"):
            psql = find_executable("psql")
        if not psql:
            blankline()
            all_ok = False
            print """\
No 'psql' executable found in $PATH.  Make sure the PostgreSQL database server
and its client utilities are installed.  In Debian/Ubuntu, the packages you need
to install are 'postgresql-server' and 'postgresql-client'.
"""
            if not aptget_approved and install("postgresql", "postgresql-client"):
                psql = find_executable("psql")
            if not psql:
                success = False

    if psql:
        postgresql_version = process.check_output([psql, "--version"]).splitlines()[0].split()[-1].split(".")

        postgresql_major = postgresql_version[0]
        postgresql_minor = postgresql_version[1]

        if postgresql_major < 8 or (postgresql_major == 8 and postgresql_minor < 4):
            blankline()
            all_ok = False
            print """\
The installed PostgreSQL version (%s) is quite old, and has never been
been tested with Critic.  You may encounter problems.  If possible, you should
upgrade to at least PostgreSQL 8.4.x.
"""
            abort = installation.input.yes_or_no(
                prompt="Do you want to abort this script now and upgrade PostgreSQL?", default=True
            )
            if abort:
                sys.exit(1)

    apache2ctl = find_executable("apache2ctl")
    if not apache2ctl:
        if aptget_approved and install("apache2"):
            apache2ctl = find_executable("apache2ctl")
        if not apache2ctl:
            blankline()
            all_ok = False
            print """\
No 'apache2ctl' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                apache2ctl = find_executable("apache2ctl")
            if not apache2ctl:
                success = False

    a2enmod = find_executable("a2enmod")
    if not a2enmod:
        if aptget_approved and install("apache2"):
            a2enmod = find_executable("a2enmod")
        if not a2enmod:
            blankline()
            all_ok = False
            print """\
No 'a2enmod' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                a2enmod = find_executable("a2enmod")
            if not a2enmod:
                success = False

    a2ensite = find_executable("a2ensite")
    if not a2ensite:
        if aptget_approved and install("apache2"):
            a2ensite = find_executable("a2ensite")
        if not a2ensite:
            blankline()
            all_ok = False
            print """\
No 'a2ensite' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                a2ensite = find_executable("a2ensite")
            if not a2ensite:
                success = False

    a2dissite = find_executable("a2dissite")
    if not a2dissite:
        if aptget_approved and install("apache2"):
            a2dissite = find_executable("a2dissite")
        if not a2dissite:
            blankline()
            all_ok = False
            print """\
No 'a2dissite' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                a2dissite = find_executable("a2dissite")
            if not a2dissite:
                success = False

    if not os.path.isdir(os.path.join("/etc", "apache2", "mods-available")):
        print """\
There's no /etc/apache2/mods-available/ directory.  This means I don't know how
to determine whether the 'wsgi' Apache module is available, and will just have
to assume it is.  If you know it *isn't* available, you should install it, or
abort this script now.
"""
        abort = installation.input.yes_or_no(prompt="Do you want to abort this script now?", default=False)
        if abort:
            sys.exit(1)
        else:
            mod_wsgi_available = True
    else:
        mod_wsgi_available_path = os.path.join("/etc", "apache2", "mods-available", "wsgi.load")
        mod_wsgi_available = os.path.isfile(mod_wsgi_available_path)
        if not mod_wsgi_available:
            if aptget_approved and install("libapache2-mod-wsgi"):
                mod_wsgi_available = os.path.isfile(mod_wsgi_available_path)
            if not mod_wsgi_available:
                blankline()
                all_ok = False
                print """\
The WSGI Apache module (mod_wsgi) doesn't appear to be installed.  Make sure
it's installed.  In Debian/Ubuntu, the package you need to install is
'libapache2-mod-wsgi'.  The source code can be downloaded here:

  http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2
"""
                if not aptget_approved and install("libapache2-mod-wsgi"):
                    mod_wsgi_available = os.path.isfile(mod_wsgi_available_path)
                if not mod_wsgi_available:
                    success = False

    def check_psycopg2():
        global psycopg2_available
        try:
            import psycopg2

            psycopg2_available = True
        except ImportError:
            pass

    check_psycopg2()
    if not psycopg2_available:
        if aptget_approved and install("python-psycopg2"):
            check_psycopg2()
        if not psycopg2_available:
            blankline()
            all_ok = False
            print """\
Failed to import the 'psycopg2' module, which is used to access the PostgreSQL
database from Python.  In Debian/Ubuntu, the module is provided by the
'python-psycopg2' package.  The source code can be downloaded here:

  http://www.initd.org/psycopg/download/
"""
        if not aptget_approved and install("python-psycopg2"):
            check_psycopg2()
        if not psycopg2_available:
            success = False

    def check_pygments():
        global pygments_available
        try:
            import pygments

            pygments_available = True
        except ImportError:
            pass

    check_pygments()
    if not pygments_available:
        if aptget_approved and install("python-pygments"):
            check_pygments()
        if not pygments_available:
            blankline()
            all_ok = False
            print """\
Failed to import the 'pygments' module, which is used for syntax highlighting.
In Debian/Ubuntu, the module is provided by the 'python-pygments' package.  The
source code can be downloaded here:

  http://pygments.org/download/
"""
        if not aptget_approved and install("python-pygments"):
            check_pygments()
        if not pygments_available:
            success = False

    def check_bcrypt():
        global bcrypt_available
        try:
            import bcrypt

            bcrypt_available = True
        except ImportError:
            pass

    global bcrypt_available

    check_bcrypt()
    if not bcrypt_available:
        blankline()
        all_ok = False
        print """\
Failed to import the 'bcrypt' module, which is required if you want Critic to
handle user authentication itself.  If user authentication is to be handled by
Apache instead there is no need to install the bcrypt module.

In Debian/Ubuntu, the module is provided by the 'python-bcrypt' package.  The
source code can be downloaded here:

  http://code.google.com/p/py-bcrypt/
"""
        install_bcrypt = installation.input.yes_or_no("Do you want to install the 'bcrypt' module?", default=False)
        if install_bcrypt:
            if install("python-bcrypt"):
                check_bcrypt()
                if not bcrypt_available:
                    print """
Failed to import 'bcrypt' module!  Installing it appeared to go fine, though,
so you might just need to restart this script."""
        if install_bcrypt and not bcrypt_available:
            success = False

    if all_ok:
        print "All okay."

    return success
Example #19
0
def check(arguments):
    global git, tar, psql, bcrypt_available, aptget, apache2ctl, a2enmod, a2ensite, a2dissite

    print """
Critic Installation: Prerequisites
==================================
"""

    success = True
    all_ok = True

    aptget = find_executable("apt-get")

    def install(*packages):
        global aptget, aptget_approved, need_blankline, all_ok
        if aptget and not aptget_approved:
            all_ok = False
            print """\
Found 'apt-get' executable in your $PATH.  This script can attempt to install
missing software using it.
"""
            aptget_approved = installation.input.yes_or_no(
                prompt=
                "Do you want to use 'apt-get' to install missing packages?",
                default=True)
            if not aptget_approved: aptget = None
        if aptget:
            installed_anything = False
            for line in process.check_output([aptget, "-qq", "-y", "install"] +
                                             list(packages)).splitlines():
                match = re.search(r"([^ ]+) \(.* \.\.\./([^)]+\.deb)\) \.\.\.",
                                  line)
                if match:
                    need_blankline = True
                    installed_anything = True
                    print "Installed: %s (%s)" % (match.group(1),
                                                  match.group(2))
            return installed_anything
        else:
            return False

    git = find_executable("git")
    if not git:
        if aptget_approved and install("git-core"):
            git = find_executable("git")
        if not git:
            blankline()
            all_ok = False
            print """\
No 'git' executable found in $PATH.  Make sure the Git version control system
is installed.  Is Debian/Ubuntu the package you need to install is 'git-core'
(or 'git' in newer versions, but 'git-core' typically still works.)  The source
code can be downloaded here:

  https://github.com/git/git
"""
            if not aptget_approved and install("git-core"):
                git = find_executable("git")
            if not git: success = False

    tar = find_executable("tar")
    assert tar, "System has no 'tar'?!?"

    psql = find_executable("psql")
    if not psql:
        if aptget_approved and install("postgresql", "postgresql-client"):
            psql = find_executable("psql")
        if not psql:
            blankline()
            all_ok = False
            print """\
No 'psql' executable found in $PATH.  Make sure the PostgreSQL database server
and its client utilities are installed.  In Debian/Ubuntu, the packages you need
to install are 'postgresql-server' and 'postgresql-client'.
"""
            if not aptget_approved and install("postgresql",
                                               "postgresql-client"):
                psql = find_executable("psql")
            if not psql: success = False

    if psql:
        postgresql_version = process.check_output(
            [psql, "--version"]).splitlines()[0].split()[-1].split(".")

        postgresql_major = postgresql_version[0]
        postgresql_minor = postgresql_version[1]

        if postgresql_major < 8 or (postgresql_major == 8
                                    and postgresql_minor < 4):
            blankline()
            all_ok = False
            print """\
The installed PostgreSQL version (%s) is quite old, and has never been
been tested with Critic.  You may encounter problems.  If possible, you should
upgrade to at least PostgreSQL 8.4.x.
"""
            abort = installation.input.yes_or_no(
                prompt=
                "Do you want to abort this script now and upgrade PostgreSQL?",
                default=True)
            if abort: sys.exit(1)

    apache2ctl = find_executable("apache2ctl")
    if not apache2ctl:
        if aptget_approved and install("apache2"):
            apache2ctl = find_executable("apache2ctl")
        if not apache2ctl:
            blankline()
            all_ok = False
            print """\
No 'apache2ctl' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                apache2ctl = find_executable("apache2ctl")
            if not apache2ctl: success = False

    a2enmod = find_executable("a2enmod")
    if not a2enmod:
        if aptget_approved and install("apache2"):
            a2enmod = find_executable("a2enmod")
        if not a2enmod:
            blankline()
            all_ok = False
            print """\
No 'a2enmod' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                a2enmod = find_executable("a2enmod")
            if not a2enmod: success = False

    a2ensite = find_executable("a2ensite")
    if not a2ensite:
        if aptget_approved and install("apache2"):
            a2ensite = find_executable("a2ensite")
        if not a2ensite:
            blankline()
            all_ok = False
            print """\
No 'a2ensite' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                a2ensite = find_executable("a2ensite")
            if not a2ensite: success = False

    a2dissite = find_executable("a2dissite")
    if not a2dissite:
        if aptget_approved and install("apache2"):
            a2dissite = find_executable("a2dissite")
        if not a2dissite:
            blankline()
            all_ok = False
            print """\
No 'a2dissite' executable found in $PATH.  Make sure the Apache web server is
installed.  In Debian/Ubuntu, the package you need to install is 'apache2'.
"""
            if not aptget_approved and install("apache2"):
                a2dissite = find_executable("a2dissite")
            if not a2dissite: success = False

    if not os.path.isdir(os.path.join("/etc", "apache2", "mods-available")):
        print """\
There's no /etc/apache2/mods-available/ directory.  This means I don't know how
to determine whether the 'wsgi' Apache module is available, and will just have
to assume it is.  If you know it *isn't* available, you should install it, or
abort this script now.
"""
        abort = installation.input.yes_or_no(
            prompt="Do you want to abort this script now?", default=False)
        if abort: sys.exit(1)
        else: mod_wsgi_available = True
    else:
        mod_wsgi_available_path = os.path.join("/etc", "apache2",
                                               "mods-available", "wsgi.load")
        mod_wsgi_available = os.path.isfile(mod_wsgi_available_path)
        if not mod_wsgi_available:
            if aptget_approved and install("libapache2-mod-wsgi"):
                mod_wsgi_available = os.path.isfile(mod_wsgi_available_path)
            if not mod_wsgi_available:
                blankline()
                all_ok = False
                print """\
The WSGI Apache module (mod_wsgi) doesn't appear to be installed.  Make sure
it's installed.  In Debian/Ubuntu, the package you need to install is
'libapache2-mod-wsgi'.  The source code can be downloaded here:

  http://code.google.com/p/modwsgi/wiki/DownloadTheSoftware?tm=2
"""
                if not aptget_approved and install("libapache2-mod-wsgi"):
                    mod_wsgi_available = os.path.isfile(
                        mod_wsgi_available_path)
                if not mod_wsgi_available: success = False

    def check_psycopg2():
        global psycopg2_available
        try:
            import psycopg2
            psycopg2_available = True
        except ImportError:
            pass

    check_psycopg2()
    if not psycopg2_available:
        if aptget_approved and install("python-psycopg2"):
            check_psycopg2()
        if not psycopg2_available:
            blankline()
            all_ok = False
            print """\
Failed to import the 'psycopg2' module, which is used to access the PostgreSQL
database from Python.  In Debian/Ubuntu, the module is provided by the
'python-psycopg2' package.  The source code can be downloaded here:

  http://www.initd.org/psycopg/download/
"""
        if not aptget_approved and install("python-psycopg2"):
            check_psycopg2()
        if not psycopg2_available:
            success = False

    def check_pygments():
        global pygments_available
        try:
            import pygments
            pygments_available = True
        except ImportError:
            pass

    check_pygments()
    if not pygments_available:
        if aptget_approved and install("python-pygments"):
            check_pygments()
        if not pygments_available:
            blankline()
            all_ok = False
            print """\
Failed to import the 'pygments' module, which is used for syntax highlighting.
In Debian/Ubuntu, the module is provided by the 'python-pygments' package.  The
source code can be downloaded here:

  http://pygments.org/download/
"""
        if not aptget_approved and install("python-pygments"):
            check_pygments()
        if not pygments_available:
            success = False

    def check_bcrypt():
        global bcrypt_available
        try:
            import bcrypt
            bcrypt_available = True
        except ImportError:
            pass

    global bcrypt_available

    check_bcrypt()
    if not bcrypt_available:
        blankline()
        all_ok = False
        print """\
Failed to import the 'bcrypt' module, which is required if you want Critic to
handle user authentication itself.  If user authentication is to be handled by
Apache instead there is no need to install the bcrypt module.

In Debian/Ubuntu, the module is provided by the 'python-bcrypt' package.  The
source code can be downloaded here:

  http://code.google.com/p/py-bcrypt/
"""
        install_bcrypt = installation.input.yes_or_no(
            "Do you want to install the 'bcrypt' module?", default=False)
        if install_bcrypt:
            if install("python-bcrypt"):
                check_bcrypt()
                if not bcrypt_available:
                    print """
Failed to import 'bcrypt' module!  Installing it appeared to go fine, though,
so you might just need to restart this script."""
        if install_bcrypt and not bcrypt_available:
            success = False

    if all_ok: print "All okay."

    return success