FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
createTestDB.php
1 #!/usr/bin/php
2 <?php
3 /*
4  Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
39 require_once(__DIR__ . '/../lib/libTestDB.php');
40 
41 $Options = getopt('c:d:esh');
42 $usage = $argv[0] . ": [-h] -c path [-d name] [-s]\n" .
43  "-c path: The path to the fossology system configuration directory\n" .
44  "-d name: Drop the named data base.\n" .
45  "-e: create ONLY an empty db, sysconf dir and repository\n" .
46  "-h: This message (Usage)\n" .
47  "-s: Start the scheduler with the new sysconfig directory\n" .
48  "Examples:\n".
49  " Create a test DB: 'createTestDb.php' \n" .
50  " Drop the database fosstest1537938: 'createTestDb.php -d fosstest1537938'\n" .
51  " Create test DB, start scheduler: 'createTestDb.php -s'\n" .
52  " Create empty DB, sysconf and repo: 'createTestDb.php -e'\n";
53 
54 $pathPrefix = '/srv/fossologyTestRepo';
55 $dbPrefix = 'fosstest';
56 
57 // check if the user is in the fossy group
58 $gid_array = posix_getgroups();
59 $gflag = 0; // 0: in the fossy group, 1: not in the fossy group
60 foreach($gid_array as $gid)
61 {
62  $gid_info = posix_getgrgid ($gid);
63  if ($gid_info['name'] === 'fossy')
64  {
65  $gflag = 1; // the user is in fossy group
66  break;
67  }
68 }
69 $uid = posix_getuid();
70 $uid_info = posix_getpwuid($uid);
71 if ($uid_info['name'] === 'root') $gflag = 1; // user is root
72 
73 if ($gflag == 0)
74 {
75 // echo "FATAL: The user must be in the fossy group.\n";
76 // exit(1);
77 }
78 
79 // create .pgpass file and place in the users home dir who is running this
80 // program. This file will be needed later in the code.
81 $user = getenv('USER');
82 $userHome = getenv('HOME');
83 $ipv4 = gethostbyname(gethostname());
84 $fullHostName = gethostbyaddr(gethostbyname($ipv4));
85 $contents = "$fullHostName:*:*:fossy:fossy\n";
86 $pgpass = "$userHome/.pgpass";
87 
88 // check for an existing ~/.pgpass. If one already exists, and if the
89 // file already contains a :fossy:fossy entry, then do not modify the
90 // file at all
91 $pg_pass_contents = ""; // start with an empty string
92 if (file_exists($pgpass)) {
93  // read the file contents into the string
94  $pg_pass_contents = file_get_contents($pgpass);
95 }
96 
97 // If a fossy:fossy entry does not already exist then add it.
98 // If the .pgpass file already exists do not overwrite, but append
99 if (!preg_match('/\:fossy\:fossy/', $pg_pass_contents)) {
100  $FD = fopen($pgpass,'w');
101  $howmany = fwrite($FD, $contents);
102  if($howmany === FALSE)
103  {
104  echo "FATAL! Could not write .pgpass file to $pgpass\n";
105  exit(1);
106  }
107  fclose($FD);
108 }
109 
110 // chmod so only owner can read/write it. If this is not set
111 // postgres will ignore the .pgpass file.
112 if(!chmod($pgpass, 0600))
113 {
114  echo "Warning! could not set $pgpass to 0600\n";
115 }
116 
117 if(array_key_exists('h',$Options))
118 {
119  print "$usage\n";
120  exit(0);
121 }
122 $sysconfig = NULL;
123 // use the passed in sysconfdir to start with
124 if(array_key_exists('c', $Options))
125 {
126  $sysconfig = $Options['c'];
127  if(empty($sysconfig))
128  {
129  echo $usage;
130  exit(1);
131  }
132 }
133 /*
134  * Drop DataBase and remove conf dir and repo dir.
135  */
136 if(array_key_exists('d', $Options))
137 {
138  $dropName = $Options['d'];
139  if(empty($dropName))
140  {
141  echo $usage;
142  exit(1);
143  }
144  // check that postgresql is running
145  //$ckCmd = "sudo su postgres -c 'echo \\\q | psql'";
146  $ckCmd = "psql -c '\q' postgres -U fossy";
147  $lastCmd = exec($ckCmd, $ckOut, $ckRtn);
148  if($ckRtn != 0)
149  {
150  echo "ERROR: postgresql isn't running, not deleting database $dropName\n";
151  exit(1);
152  }
153  $existCmd = "psql -l postgres -U fossy|grep -q $dropName";
154  $lastExist = exec($existCmd, $existkOut, $existRtn);
155  if($existRtn == 0)
156  {
157  // drop the db
158  # stop all users of the fossology db
159  $pkillCmd ="sudo pkill -f -u postgres fossy || true";
160  $lKill = exec($pkillCmd, $killOut, $killRtn);
161  $dropCmd = "sudo su postgres -c 'echo \"drop database $dropName;\"|psql'";
162  $lastDrop = exec($dropCmd, $dropOut, $dropRtn);
163  if($dropRtn != 0 )
164  {
165  echo "ERROR: failed to delete database $dropName\n";
166  exit(1);
167  }
168  }
169  else
170  {
171  echo "NOTE: database $dropName does not exist, nothing to delete\n";
172  }
173  // remove sysconf and repository
174  // remove name from string
175  $len = strlen($dbPrefix);
176  $uni = substr($dropName,$len);
177  $rmRepo = $pathPrefix . '/testDbRepo' .$uni;
178  $rmConf = $pathPrefix . '/testDbConf' .$uni;
179  $last = system("sudo rm -rf $rmConf $rmRepo", $rmRtn);
180  exit(0);
181 }
182 
183 if(empty($sysconfig))
184 {
185  $sysconfig = getenv('SYSCONFDIR');
186  //echo "DB: ctdb: sysconfdir from env is:$sysconfig\n";
187  if(empty($sysconfig))
188  {
189  echo "FATAL!, no SYSCONFDIR defined\n";
190  echo "either export SYSCONFDIR path and rerun or use -c <sysconfdirpath>\n";
191  flush();
192  exit(1);
193  }
194 }
195 
196 putenv("SYSCONFDIR=$sysconfig");
197 $_ENV['SYSCONFDIR'] = $sysconfig;
198 
199 $unique = mt_rand();
200 $DbName = $dbPrefix . $unique;
201 
202 $createEmpty = array_key_exists('e', $Options);
203 $startSched = array_key_exists('s', $Options);
204 
205 // create the db
206 $newDB = createTestDB($DbName);
207 if($newDB != NULL)
208 {
209  echo "ERROR, could not create database $DbName\n";
210  echo $newDB;
211  exit(1);
212 }
213 
214 $confName = 'testDbConf' . $unique;
215 $confPath = "$pathPrefix/$confName";
216 $repoName = 'testDbRepo' . $unique;
217 $repoPath = "$pathPrefix/$repoName";
218 
219 // sysconf and repo's always go in /srv/fossologyTestRepo to ensure enough room.
220 // perms are 755
221 if(mkdir($confPath,0755,TRUE) === FALSE)
222 {
223  echo "FATAL! Cannot create test sysconf at:$confPath\n" .
224  __FILE__ . " at line " . __LINE__ . "\n";
225  exit(1);
226 }
227 if(chmod($confPath, 0755) === FALSE )
228 {
229  echo "ERROR: Cannot set mode to 755 on $confPath\n" .
230  __FILE__ . " at line " . __LINE__ . "\n";
231 }
232 if(mkdir($repoPath,0755,TRUE) === FALSE)
233 {
234  echo "FATAL! Cannot create test repository at:$repoPath\n" .
235  __FILE__ . " at line " . __LINE__ . "\n";
236  exit(1);
237 }
238 if(chmod($repoPath, 0755) === FALSE )
239 {
240  echo "ERROR: Cannot set mode to 755 on $repoPath\n" .
241  __FILE__ . " at line " . __LINE__ . "\n";
242 }
243 //create Db.conf file
244 // Should the host be what's in fossology.conf?
245 $conf = "dbname=$DbName;\n" .
246  "host=localhost;\n" .
247  "user=fossy;\n" .
248  "password=fossy;\n";
249 
250 if(file_put_contents($confPath . "/Db.conf", $conf) === FALSE)
251 {
252  echo "FATAL! Could not create Db.conf file at:$confPath\n";
253  exit(1);
254 }
255 
256 // copy and modify fossology.conf
257 $fossConf = $sysconfig . '/fossology.conf';
258 $myConf = $confPath . '/fossology.conf';
259 
260 if(file_exists($fossConf))
261 {
262  if(copy($fossConf, $myConf) === FALSE)
263  {
264  echo "FATAL! cannot copy $fossConf to $myConf\n";
265  exit(1);
266  }
267 }
268 
269 if(setRepo($confPath, $repoPath) === FALSE)
270 {
271  echo "ERROR!, could not change $sysconfig/fossology.conf, please change by " .
272  "hand before running tests\n";
273  exit(1);
274 }
275 
276 // copy mods-enabled from real sysconf.
277 $modConf = $sysconfig . '/mods-enabled';
278 $cmd = "cp -RP $modConf $confPath";
279 if(system($cmd) === FALSE)
280 {
281  //echo "DB: Cannot copy directory $modConf to $confPath\n";
282  exit(1);
283 }
284 
285 // copy version file
286 // copy and modify fossology.conf
287 $version = $sysconfig . '/VERSION';
288 $myVersion = $confPath . '/VERSION';
289 if(file_exists($fossConf))
290 {
291  if(copy($version, $myVersion) === FALSE)
292  {
293  echo "FATAL! cannot copy $version to $myVersion\n";
294  exit(1);
295  }
296 }
297 putenv("SYSCONFDIR=$confPath");
298 $_ENV['SYSCONFDIR'] = $confPath;
299 $GLOBALS['SYSCONFDIR'] = $confPath;
300 
301 if($createEmpty)
302 {
303  echo $confPath . "\n";
304  exit(0);
305 }
306 
307 // load the schema
308 $loaded = TestDBInit(NULL, $DbName);
309 //echo "DB: return from TestDBinit is:\n";print_r($loaded) . "\n";
310 if($loaded !== NULL)
311 {
312  echo "ERROR, could not load schema\n";
313  echo $loaded;
314  exit(1);
315 }
316 
317 // export to environment the new sysconf dir
318 // The update has to happen before schema-update gets called or schema-update
319 // will not end up with the correct sysconf
320 
321 putenv("SYSCONFDIR=$confPath");
322 $_ENV['SYSCONFDIR'] = $confPath;
323 $GLOBALS['SYSCONFDIR'] = $confPath;
324 
325 // scheduler should be in $MODDIR/scheduler/agent/fo_scheduler
326 // no need to check if it's running, as a new one is started with a new
327 // SYSCONFDIR.
328 if($startSched)
329 {
330  $skedOut = array();
331  $cmd = "sudo $MODDIR/scheduler/agent/fo_scheduler -d -c $confPath";
332  $skedLast = exec($cmd, $skedOut, $skedRtn);
333  if($skedRtn != 0)
334  {
335  echo "FATAL! could not start scheduler with -d -c $confPath\n";
336  echo implode("\n", $skedOut) . "\n";
337  exit(1);
338  }
339 }
340 echo $confPath . "\n";
341 exit(0);