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