-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-wiki-instance
More file actions
executable file
·133 lines (110 loc) · 5.34 KB
/
create-wiki-instance
File metadata and controls
executable file
·133 lines (110 loc) · 5.34 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Configuration variables used in this script,
#+ and in the configuration files' templates.
#
MW_Ver=${5-'1.35.5'}
# Wiki configuration:
Apex=wikido.xyz
MW_Instance_Dir=mw
URL_Prefix="" #with leading slash. Used not in this script, but in templates
Repo_Alias=repo #Also used not in this script, but in templates
# Directories:
MW_Source=/srv/app/mediawiki/$MW_Ver/core
Templates_Dir=/srv/src/wikitools/templates
Web_Root=/srv/www
Repo_Root=/srv/repositorea
Cache_Root=/var/cache
Temp_Root=/var/tmp
Secrets_Dir=/srv/websecrets
# Template file names:
DB_Template=wikido.xyz.mw-$MW_Ver.sql
LocalSettings_Template=LocalSettings.template
Apache_Template=apache.$Apex.conf.template
Robots_Template=robots.txt.template
Secrets_Template=secrets.template
Logo_Template=wikido.xyz-logo.svg.template
###=========
if [[ -z $1 || -z $2 || -z $3 || -z $4 ]]; then
cat <<- USAGE >&2
WikiFamily configuration generator, and database creator.
usage:
$(basename $0) <handle> <title> <username> <password> <mw_ver>
where:
<handle>: A string to be used as the the hostname for the wiki website, as well as the base of both the database name and database username.
<title>: The natural language title of the wikisite.
<username>: Username of the first user to be created; and granted both "sysop" and "bureaucrat" status.
<password>: Password for aforementioned user.
<mw_version>: an optoinal MW version number to construct paths. Currently defaults to $MW_Ver
More configuration variables exist in the code to customise the process.
This scritp needs to be run as root. All arguments are required.
USAGE
exit 85
fi
if ! [ $(id -u) = 0 ]; then
echo "The script needs to be run as root." >&2
exit 1
else
real_user=$SUDO_USER
fi
Wiki_Handle=$1
Wiki_Title=$2
Wiki_Username=$3
Wiki_Password=$4
echo -e "Creating a Mediawiki instance for:\nHandle: $Wiki_Handle\nTitle: $Wiki_Title\nMW version: $MW_Ver" >&2
# Derivative constants:
WIKI_FQDN=$Wiki_Handle.$Apex
WIKI_ROOT=$Web_Root/$Apex/$Wiki_Handle
REPOSITORY_DIR=$Repo_Root/$Apex/$Wiki_Handle
CACHE_DIR=$Cache_Root/$Apex/$Wiki_Handle
TEMP_DIR=$Temp_Root/$Apex/$Wiki_Handle
DB_NAME=${Wiki_Handle}_wiki
DB_USER=wiki_${Wiki_Handle}
conf_from_template() {
# Magically generates configuration files based on templates.
TEMPLATE=$1
shift
eval "`printf 'local %s\n' $@`
cat <<EOF
`sudo -u www-data cat $TEMPLATE`
EOF"
}
# Create directories and most needed symbolic links, acting as the Apache user
echo -n "Setting directories up.."
if [ ! -d $WIKI_ROOT ]; then sudo -u www-data mkdir --parents $WIKI_ROOT; fi
sudo -u www-data ln --symbolic --force --no-target-directory $MW_Source $WIKI_ROOT/$MW_Instance_Dir
sudo -u www-data cp --recursive --no-target-directory $MW_Source/images $REPOSITORY_DIR
sudo -u www-data cp --recursive --no-target-directory $MW_Source/cache $CACHE_DIR
if [ ! -d $TEMP_DIR ]; then sudo -u www-data mkdir --parents $TEMP_DIR; fi
if [ ! -d $Secrets_Dir ]; then mkdir --parents $Secrets_Dir && chgrp www-data $Secrets_Dir; fi
echo "done."
#Generate websecrets files ##This is entropy-expensive, so better not run it unless it's feasible
DB_PASSWORD=$(apg -a1 -n1 -m16 -MNCL -d)
conf_from_template "$Templates_Dir/$Secrets_Template" > "$Secrets_Dir/$Wiki_Handle.$Apex"
chmod u=r,g=r,o=- "$Secrets_Dir/$Wiki_Handle.$Apex"
#Create database
#DEBUG#echo "CREATE USER '$DB_USER'@'localhost' IDENTIFIED BY '$DB_PASSWORD'; CREATE DATABASE $DB_NAME; GRANT ALL PRIVILEGES ON $DB_NAME.* TO '$DB_USER'@'localhost';" | mysql --user=root --password && echo -e "Successfuly created database $DB_NAME; user $DB_USER, and granted permissions.\nDatabase user's password is $DB_PASSWORD." >&2
#mysql --user=$DB_USER --password=$DB_PASSWORD $DB_NAME < $Templates_Dir/$DB_Template && echo "Successfuly created Mediawiki tables in $DB_NAME." >&2
#create 1st wiki user
#sudo -u www-data php "$MW_Source/maintenance/createAndPromote.php" --bureaucrat --sysop --wiki "$DB_NAME" "$Wiki_Username" $Wiki_Password
# Generate LocalSettings.php for the wiki instance
# This instance-specific file is invoked from a dispatcher at the wikifamily-level
conf_from_template "$Templates_Dir/$LocalSettings_Template" > "$WIKI_ROOT/LocalSettings.php"
echo "run:" "php $WIKI_ROOT/$MW_Instance_Dir/maintenance/install.php" --dbuser $DB_USER --dbpass $DB_PASSWORD --confpath /tmp/ --dbname $DB_NAME --installdbuser root --installdbpass \"\" --pass "$Wiki_Password" "$Wiki_Title" "$Wiki_Username"
#create and inject logo
HANDLE_HASH=$(echo -n $Wiki_Handle | md5sum)
COLOUR1=${HANDLE_HASH:0:6}
COLOUR2=$(for o in {0..4..2}; do printf '%02X' $(( (0x${HANDLE_HASH:o:2} + 0x80) % 0xFF )); done)
if [[ 0x$COLOUR1 -lt 0x$COLOUR2 ]]; then
FILL1=#$COLOUR2
FILL2=#$COLOUR1
else
FILL1=#$COLOUR1
FILL2=#$COLOUR2
fi
conf_from_template "$Templates_Dir/$Logo_Template" > $WIKI_ROOT/logo.svg
#DEBUG#sudo -u www-data php $MW_Source/maintenance/importImages.php --wiki $DB_NAME --extensions=svg --skip-dupes --comment='شعار ويكي $WIKI_FQDN بألوان مستنبطة من عنوانها' --license='برخصة_المشاع_الإبداعي_النسبة_4.0' $WIKI_ROOT
sudo -u www-data cp "$Templates_Dir/$Robots_Template" "$WIKI_ROOT/robots.txt"
conf_from_template "$Templates_Dir/$Apache_Template" > "/etc/apache2/sites-available/$WIKI_FQDN.conf"
#DEBUG#a2ensite $WIKI_FQDN.conf
#restart apache if configuration is OK
#DEBUG#apache2ctl configtest && apache2ctl restart