File: samizdat-create-database

package info (click to toggle)
samizdat 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,576 kB
  • sloc: ruby: 7,776; xml: 899; sql: 897; sh: 67; makefile: 11
file content (83 lines) | stat: -rw-r--r-- 2,153 bytes parent folder | download | duplicates (2)
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
#!/bin/bash
#
# Samizdat database generation script
#
#   Copyright (c) 2002-2011  Dmitry Borodaenko <angdraug@debian.org>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU General Public License version 3 or later.
#

print_usage()
{
	echo "Usage: samizdat-create-database site [ driver ]"
	echo
	echo "Supported drivers:"
	echo "    postgres (default)"
	echo "    sqlite3"
	echo
	echo "Requires root priviledges to run"
}

if [ -z "$1" ]; then
	print_usage
	exit 1
fi

SITE="$1"
DRIVER="$2"
[ -z "$DRIVER" ] && DRIVER="postgres"

PACKAGE=samizdat
SQLDIR=/usr/share/$PACKAGE/database
DATADIR=/var/lib/$PACKAGE
SQL=`cat $SQLDIR/{create,triggers}-${DRIVER}.sql 2>/dev/null`
if [ -r "$SQLDIR/grant-${DRIVER}.sql" ]; then
	SQL=$SQL`sed "s/samizdat/$SITE/g" $SQLDIR/grant-${DRIVER}.sql`
fi

case "$DRIVER" in
	postgres)
		su -c "echo '' | psql -q '$SITE' 2>/dev/null" - postgres
		if [ "$?" = "0" ]; then
			echo "Error: database '$SITE' already exists."
			exit 3
		fi
		echo "Creating database and user..."
		su -c "createdb --encoding UNICODE '$SITE' && createuser -SDR '$SITE' && createlang plpgsql '$SITE'" - postgres
		echo "Generating database schema..."
		echo "$SQL" | su -c "psql -q '$SITE' 2>/dev/null" - postgres
		echo
		echo "Please configure IDENT access as described in install.txt, or provide"
		echo "other means for Samizdat to log in to PostrgreSQL database '$SITE'."
		echo
		echo "adapter: postgres"
		echo "database: ${SITE}"
		echo "user: ${SITE}"
		;;
	sqlite3)
		SQLITE_DIR=$DATADIR/$SITE
		SQLITE_FILE=$SQLITE_DIR/${SITE}-sqlite.db
		if [ -e "$SQLITE_FILE" ]; then
			echo "Error: SQLite3 database file for site '$SITE' already exists."
			exit 3
		fi
		echo "Creating database..."
		mkdir -p $SQLITE_DIR
		sqlite3 $SQLITE_FILE "$SQL"
		echo "Granting write access to the database to group 'www-data'..."
		chgrp -R www-data $SQLITE_DIR
		chmod 775 $SQLITE_DIR
		chmod 664 $SQLITE_FILE
		echo
		echo "adapter: sqlite3"
		echo "database: $SQLITE_FILE"
		;;
	*)
		echo "Error: driver '$DRIVER' is not supported."
		exit 2
		;;
esac

echo "Database generation completed."