4 * FusionForge Installation Dependency Setup
6 * Copyright 2006 GForge, LLC
7 * http://fusionforge.org/
11 * This file is part of GInstaller, it is called by install.sh.
13 * FusionForge is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 define ('GREEN', "\033[01;32m" );
29 define ('NORMAL', "\033[00m" );
30 define ('RED', "\033[01;31m" );
32 $STDOUT = fopen('php://stdout','w');
33 $STDIN = fopen('php://stdin','r');
35 show("\n-=# Welcome to FusionForge DB-Installer #=-");
37 //TO DO: add dependency check
38 //if (!run("php check-deps.php", true)) {
39 // echo RED.'Not all the necessary dependencies were found. aborting.'.NORMAL."\n";
43 // Make sure the DB is initialized by starting postgresql service
44 if (is_file('/etc/init.d/postgresql'))
46 $pgservice='/etc/init.d/postgresql';
48 elseif (is_file('/etc/init.d/postgresql-8.2'))
50 $pgservice='/etc/init.d/postgresql-8.2';
52 elseif (is_file('/etc/init.d/postgresql-8.3'))
54 $pgservice='/etc/init.d/postgresql-8.3';
56 elseif (is_file('/etc/init.d/postgresql-8.4'))
58 $pgservice='/etc/init.d/postgresql-8.4';
60 elseif (is_file('/etc/init.d/cswpostgres'))
62 $pgservice='/etc/init.d/cswpostgres';
66 die("ERROR: Could not find Postgresql init script\n");
69 # Fedora9 (an maybe newer) requires running initdb
70 if ($pgservice == '/etc/init.d/postgresql') {
71 if (!is_dir("/var/lib/pgsql/data/base")) {
72 run("service postgresql initdb &>/dev/null", true);
76 // Might fail if it's already running, so we'll ingnore the result
77 run("$pgservice start", true);
79 require_once 'install-common.inc' ;
81 if (!is_dir($fusionforge_src_dir))
83 die("Error: GForge folder doesn't exist. Run fusionforge-install-2.php first.");
86 // Where the PGHBA config file is
87 if (is_file("/var/lib/pgsql/data/pg_hba.conf"))
90 $PGHBA='/var/lib/pgsql/data/pg_hba.conf';
92 elseif (is_file('/etc/postgresql/8.2/main/pg_hba.conf'))
94 $PGHBA='/etc/postgresql/8.2/main/pg_hba.conf';
96 elseif (is_file('/etc/postgresql/8.4/main/pg_hba.conf'))
98 $PGHBA='/etc/postgresql/8.4/main/pg_hba.conf';
100 elseif (is_file('/opt/csw/var/pgdata/pg_hba.conf'))
102 $PGHBA='/opt/csw/var/pgdata/pg_hba.conf';
106 die("ERROR: Could not find pg_hba.conf file\n");
110 if (is_file('/usr/share/pgsql/contrib/tsearch2.sql'))
113 $tsearch2_sql='/usr/share/pgsql/contrib/tsearch2.sql';
115 elseif (is_file('/usr/share/postgresql/contrib/tsearch2.sql'))
118 $tsearch2_sql='/usr/share/postgresql/contrib/tsearch2.sql';
120 elseif (is_file('/usr/share/postgresql/8.2/contrib/tsearch2.sql'))
122 $tsearch2_sql='/usr/share/postgresql/8.2/contrib/tsearch2.sql';
124 elseif (is_file('/usr/share/postgresql/8.3/contrib/tsearch2.sql'))
126 $tsearch2_sql='/usr/share/postgresql/8.3/contrib/tsearch2.sql';
128 elseif (is_file('/usr/share/postgresql/8.4/contrib/tsearch2.sql'))
130 $tsearch2_sql='/usr/share/postgresql/8.4/contrib/tsearch2.sql';
132 elseif (is_file('/opt/csw/postgresql/share/contrib/tsearch2.sql'))
135 $tsearch2_sql='/opt/csw/postgresql/share/contrib/tsearch2.sql';
139 die("ERROR: Could not find tsearch2.sql file\n");
145 global $PGHBA, $fusionforge_src_dir, $fusionforge_etc_dir, $tsearch2_sql, $pgservice, $STDIN, $STDOUT;
147 show("\n * Enter the Database Name (gforge): ");
149 if (getenv('FFORGE_DB')) {
150 $gforge_db = getenv('FFORGE_DB');
152 $gforge_db = trim(fgets($STDIN));
153 if (strlen($gforge_db) == 0) {
154 $gforge_db = 'gforge';
157 show(" ...using '$gforge_db'");
159 show(' * Enter the Database Username (gforge): ');
161 if (getenv('FFORGE_USER')) {
162 $gforge_user = getenv('FFORGE_USER');
164 $gforge_user = trim(fgets($STDIN));
165 if (strlen($gforge_user) == 0) {
166 $gforge_user = 'gforge';
169 show(" ...using '$gforge_user'");
171 show(' * Saving database configuration in FForge config file');
172 if (!is_file("$fusionforge_etc_dir/config.ini.d/rpm-install.ini")){
173 if ($fp = fopen("$fusionforge_etc_dir/config.ini.d/rpm-install.ini", "w")) {
174 $config='; This file was generated by the RPM installation system.
175 ; You can edit the values here or move them to other files.
177 forge_name=FusionForge
178 web_host='.system("hostname -f").'
180 fwrite ($fp, $config);
184 if (!is_file("$fusionforge_etc_dir/config.ini.d/rpm-install-secrets.ini")){
185 if ($fp = fopen("$fusionforge_etc_dir/config.ini.d/rpm-install-secrets.ini", "w")) {
186 $config='; This file was generated by the RPM installation system.
187 ; You can edit the values here or move them to other files.
188 ; These values should not go into world-readable files!
192 database_name='.$gforge_db.'
193 database_user='.$gforge_user.'
195 ; database_password=$pw
197 fwrite ($fp, $config);
202 show(" * Modifying DB Access Permissions...");
203 if (!file_exists("$PGHBA.fforge.backup")) {
204 run("cp $PGHBA $PGHBA.fforge.backup", true);
206 run("echo \"# GFORGE\nlocal all all trust\" > $PGHBA");
207 show(' * Restarting PostgreSQL...');
208 run("$pgservice stop", true);
209 run("$pgservice start");
212 show(" * Creating '$gforge_user' Group...");
213 run("/usr/sbin/groupadd $gforge_user", true);
215 show(" * Creating '$gforge_user' User...");
216 run("/usr/sbin/useradd -g $gforge_user $gforge_user", true);
218 // Let's give some time for PostgreSQL to start
221 show(" * Creating Database User '$gforge_user'...");
222 run("su - postgres -c \"createuser -A -R -d -E $gforge_user\"", true);
224 show(' * Creating Language...');
225 run("su - postgres -c \"createlang plpgsql template1\"", true);
227 if (!is_dir("/home/$gforge_user")) {
233 show(" * Creating '$gforge_db' Database...");
234 run("su $susufix $gforge_user -c \"createdb --encoding UNICODE $gforge_db\"", true);
236 # Detect postgresql version, load tsearch2 for pg < 8.3
237 $pg_version = explode(' ', shell_exec("postgres --version"));
238 $pgv = $pg_version[2];
240 if (preg_match('/^(7\.|8\.1|8\.2)/', $pgv)) {
241 show(" * Dumping tsearch2 Database Into '$gforge_db' DB");
242 run("su - postgres -c \"psql $gforge_db < $tsearch2_sql\" >> /tmp/gforge-import.log");
244 $tables = array('pg_ts_cfg', 'pg_ts_cfgmap', 'pg_ts_dict', 'pg_ts_parser');
245 foreach ($tables as $table) {
246 run('su - postgres -c "psql '.$gforge_db.' -c \\"GRANT ALL on '.$table.' TO '.$gforge_user.';\\""');
248 run("su - postgres -c \"psql $gforge_db -c \\\"UPDATE pg_ts_cfg SET locale='en_US.UTF-8' WHERE ts_name='default'\\\"\"");
250 // show(" * Creating FTS default configuation (Full Text Search)");
251 // run("su - postgres -c \"psql $gforge_db < $fusionforge_src_dir/db/FTS-20081108.sql\" >> /tmp/gforge-import.log");
255 show(' * Dumping FusionForge DB');
256 run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/gforge.sql\" >> /tmp/gforge-import.log");
258 // show(' * Dumping FusionForge FTI DB');
259 // run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/FTI.sql\" >> /tmp/gforge-import.log");
260 // run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/FTI-20050315.sql\" >> /tmp/gforge-import.log");
261 // run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/FTI-20050401.sql\" >> /tmp/gforge-import.log");
262 // run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/FTI-20050530.sql\" >> /tmp/gforge-import.log");
263 // run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/FTI-20060130.sql\" >> /tmp/gforge-import.log");
264 // run("su $susufix $gforge_user -c \"psql $gforge_db < $fusionforge_src_dir/db/FTI-20061025.sql\" >> /tmp/gforge-import.log");
266 if (!is_dir($fusionforge_etc_dir)) {
267 mkdir($fusionforge_etc_dir);
269 if (!is_dir($fusionforge_etc_dir.'/config.ini.d')) {
270 mkdir($fusionforge_etc_dir.'/config.ini.d');
273 #$data = file_get_contents("$fusionforge_etc_dir/local.inc");
274 #$lines = explode("\n",$data);
276 #foreach ($lines as $l) {
277 # $l = preg_replace("/^.sys_dbname\s*=\s*'(.*)'/", "\$sys_dbname='$gforge_db'", $l);
278 # $l = preg_replace("/^.sys_dbuser\s*=\s*'(.*)'/", "\$sys_dbuser='$gforge_user'", $l);
279 # $config .= $l."\n";
282 #if ($fp = fopen("$fusionforge_etc_dir/local.inc", "w")) {
283 # fwrite ($fp, $config);
287 show(' * Saving installation log in /tmp/gforge-import.log');
290 function uninstall() {
291 global $PGHBA, $fusionforge_src_dir, $gforge_var_dir, $fusionforge_etc_dir, $gforge_db, $gforge_user, $tsearch2_sql;
293 show(" * Removing DATABASE \n";
294 system("su - $gforge_user -c \"dropdb $gforge_db\"", $ret );
295 show(" done . ($ret)\n";
297 show(" * Removing Language \n";
298 system("su - postgres -c \"droplang plpgsql template1\"", $ret );
299 show(" done. ($ret)\n";
301 show(" * Removing GForge DATABASE User: \n";
302 system("su - postgres -c \"dropuser $gforge_user\"", $ret );
303 show(" done.($ret)");
305 show(" * Removing GForge User: \n";
306 system("userdel $gforge_user");
309 show(" * Restoring $PGHBA file: ... ";
310 system("cp $PGHBA.gforge.backup $PGHBA");
313 show(" * Restarting PostgreSQL: ...\n";
314 system("/etc/init.d/postgresql restart");
320 show(NORMAL."Done.\nYou are ready to run install-gforge-3.php");