# Configuring Apache
-You can use apachectl to start/stop/restart apache server from the command line:
-
- - Starting httpd: `sudo apachectl start`
- - Stopping httpd: `sudo apachectl stop`
- - Restarting httpd: `sudo apachectl restart`
-
-The apache logs are located at `/private/var/log/apache2`.
-
## Instructions if you're using Server.app
- Enable PHP web applications
- In El Capitan and later, comment out the `LockFile` directive in `/private/etc/apache2/extra/httpd-mpm.conf`
since the directive has been superseded by `Mutex` directive.
+## Starting Apache
+
+You can use apachectl to start/stop/restart apache server from the command line:
+
+- Starting httpd: `sudo apachectl start`
+- Stopping httpd: `sudo apachectl stop`
+- Restarting httpd: `sudo apachectl restart`
+
+The apache logs are located at `/private/var/log/apache2`.
+
## Production Configurations
1. Update ServerAdmin to your email address
# Configuring PostgreSQL
-1. Create database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/initdb /Volumes/Data/perf.webkit.org/PostgresSQL`
-2. Start database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/pg_ctl -D /Volumes/Data/perf.webkit.org/PostgresSQL -l logfile -o "-k /Volumes/Data/perf.webkit.org/PostgresSQL" start`
+Run the following command to setup a Postgres server at `/Volumes/Data/perf.webkit.org/PostgresSQL` (or wherever you'd prefer):
+`python ./setup-database.py /Volumes/Data/perf.webkit.org/PostgresSQL`
+
+It automatically retrieves the database name, the username, and the password from `config.json`.
-## Creating a Database and a User
+## Starting PostgreSQL
-The binaries located in PostgreSQL's directory, or if you're using Server.app in /Applications/Server.app/Contents/ServerRoot/usr/bin/
+The setup script automatically starts the database but you may need to run the following command to manually start the database after reboot.
-1. Create a database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/createdb webkit-perf-db -h localhost`
-2. Create a user: `/Applications/Server.app/Contents/ServerRoot/usr/bin/createuser -P -S -e webkit-perf-db-user -h localhost`
-3. Connect to database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/psql webkit-perf-db -h localhost`
-4. Grant all permissions to the new user: `grant all privileges on database "webkit-perf-db" to "webkit-perf-db-user";`
-5. Update database/config.json.
+- Starting the database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/pg_ctl -D /Volumes/Data/perf.webkit.org/PostgresSQL -l /Volumes/Data/perf.webkit.org/PostgresSQL/logfile -o "-k /Volumes/Data/perf.webkit.org/PostgresSQL" start`
+- Stopping the database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/pg_ctl -D /Volumes/Data/perf.webkit.org/PostgresSQL -l /Volumes/Data/perf.webkit.org/PostgresSQL/logfile -o "-k /Volumes/Data/perf.webkit.org/PostgresSQL" start`
## Initializing the Database
## Making a Backup and Restoring
-Run `/Applications/Server.app/Contents/ServerRoot/usr/bin/pg_dump -h localhost --no-owner -f <filepath> webkit-perf-db | gzip > backup.gz`
-
-To restore, setup a new database and run
-`gunzip backup.gz | /Applications/Server.app/Contents/ServerRoot/usr/bin/psql webkit-perf-db -h localhost --username webkit-perf-db-user`
+- Backing up the database: `/Applications/Server.app/Contents/ServerRoot/usr/bin/pg_dump -h localhost --no-owner -f <filepath> webkit-perf-db | gzip > backup.gz`
+- Restoring the database: `gunzip -c backup.gz | /Applications/Server.app/Contents/ServerRoot/usr/bin/psql webkit-perf-db -h localhost --username webkit-perf-db-user`
--- /dev/null
+#!/usr/bin/python
+
+import json
+import os
+import shutil
+import subprocess
+import sys
+import time
+
+
+def main():
+ if len(sys.argv) <= 1:
+ sys.exit('Specify the database directory')
+
+ database_dir = os.path.abspath(sys.argv[1])
+ if os.path.exists(database_dir) and not os.path.isdir(database_dir):
+ sys.exit('The specified path is not a directory')
+
+ database_config = load_database_config()
+
+ database_name = database_config['name']
+ username = database_config['username']
+ password = database_config['password']
+
+ psql_dir = determine_psql_dir()
+
+ print "Initializing database at %s" % database_dir
+ execute_psql_command(psql_dir, 'initdb', [database_dir])
+
+ print "Starting postgres at %s" % database_dir
+ start_or_stop_database(psql_dir, database_dir, 'start')
+ postgres_is_running = False
+ for unused in range(0, 5):
+ try:
+ execute_psql_command(psql_dir, 'psql', ['--list', '--host', 'localhost'], suppressStdout=True)
+ postgres_is_running = True
+ except subprocess.CalledProcessError:
+ time.sleep(0.5)
+
+ if not postgres_is_running:
+ sys.exit('Postgres failed to start in time')
+ print 'Postgres is running!'
+
+ done = False
+ try:
+ print "Creating database: %s" % database_name
+ execute_psql_command(psql_dir, 'createdb', [database_name, '--host', 'localhost'])
+
+ print "Creating user: %s" % username
+ execute_psql_command(psql_dir, 'psql', [database_name, '--host', 'localhost', '--command',
+ "create role \"%s\" with nosuperuser login encrypted password '%s';" % (username, password)])
+
+ print "Granting all access on %s to %s" % (database_name, username)
+ execute_psql_command(psql_dir, 'psql', [database_name, '--host', 'localhost', '--command',
+ 'grant all privileges on database "%s" to "%s";' % (database_name, username)])
+
+ print "Successfully configured postgres database %s at %s" % (database_name, database_dir)
+
+ done = True
+ except Exception as exception:
+ start_or_stop_database(psql_dir, database_dir, 'stop')
+ shutil.rmtree(database_dir)
+ raise exception
+
+ sys.exit(0)
+
+
+def load_database_config():
+ config_json_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../config.json'))
+ with open(config_json_path) as config_json_file:
+ database_config = json.load(config_json_file).get('database')
+
+ if database_config.get('host') != 'localhost':
+ sys.exit('Cannot setup a database on a remote machine')
+
+ return database_config
+
+
+def determine_psql_dir():
+ psql_dir = ''
+ try:
+ subprocess.check_output(['initdb', '--version'])
+ except OSError:
+ psql_dir = '/Applications/Server.app/Contents/ServerRoot/usr/bin/'
+ try:
+ subprocess.check_output([psql_dir + 'initdb', '--version'])
+ except OSError:
+ sys.exit('Cannot find psql')
+ return psql_dir
+
+
+def start_or_stop_database(psql_dir, database_dir, command):
+ execute_psql_command(psql_dir, 'pg_ctl', ['-D', database_dir,
+ '-l', os.path.join(database_dir, 'logfile'),
+ '-o', '-k ' + database_dir,
+ command])
+
+
+def execute_psql_command(psql_dir, command, args=[], suppressStdout=False):
+ return subprocess.check_output([os.path.join(psql_dir, command)] + args,
+ stderr=subprocess.STDOUT if suppressStdout else None)
+
+
+if __name__ == "__main__":
+ main()