FOSSology  4.4.0
Open Source License Compliance by Open Source Software
cleanup_test_databases.php
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 /*
10  cleanup_test_databases.php
11 
12  Clean up FOSSology test databases and associated configuration
13  created by create_test_database.php
14 
15  If no parameters are provided and no environment variable is set,
16  then this script will delete all the test databases it can find.
17 
18  If the FOSSOLOGY_TESTCONFIG environment variable is set, then this
19  script will delete only the database and associated files for that test.
20 
21  Optionally, supply a single command-line parameter of the SYSCONFDIR
22  for a specific test database to be cleaned up.
23 
24  Examples:
25 
26  ./cleanup_test_database.php
27  Clean up all existing test databases
28 
29  export FOSSOLOGY_TESTCONFIG=/tmp/fossologytest_20120904_174749; ./cleanup_test_database.php
30 
31  ./cleanup_test_database.php /tmp/fossologytest_20120904_174749
32  Clean up only the database and files associated with the test DB
33  referred to in sysconfdir /tmp/fossologytest_20120904_174749
34 
35 */
36 
37 /* This will be the specific SYSCONFDIR to delete, if one is specified */
38 $sysconfdir;
39 
40 /* the name of the environment variable to look for as the SYSCONFDIR */
41 $test_environment_variable = 'FOSSOLOGY_TESTCONFIG';
42 
43 /* very first step - check for the FOSSOLOGY_TESTCONFIG environment variable.
44  If this exists, then it is the database to be deleted. */
45 $fossology_testconfig = getenv($test_environment_variable);
46 
47 if ($fossology_testconfig && strlen($fossology_testconfig) > 1) {
48  $sysconfdir = $fossology_testconfig;
49 }
50 
51 /* optionally, if a command-line parameter is supplied, then it is the
52  sysconfdir (and associated database) to be deleted (overriding the
53  FOSSOLOGY_TESTCONFIG environment variable, if present). */
54 if ($argc > 1) {
55  $sysconfdir = $argv[1];
56 }
57 
58 /* Check to see if a specific database was provided; if so make
59  sure that its Db.conf file can be read */
60 if (isset($sysconfdir)) {
61  if (is_readable("$sysconfdir/Db.conf")) {
62  print "Found a readable '$sysconfdir/Db.conf' file\n";
63 
64  /* now get the database name from Db.conf */
65  $db_conf_contents = file_get_contents("$sysconfdir/Db.conf");
66  /* the database name will be specified as
67  dbname = something; */
68  $db_match = preg_match('/dbname\s*\=\s*(.*?)[; ]/i', $db_conf_contents, $matches);
69 
70  if ($db_match > 0) {
71  $database_name = $matches[1];
72  print "Found database name '$database_name'\n";
73  }
74  else {
75  print "Did not find a dbname= parameter in '$sysconfdir/Db.conf'\n";
76  exit(1);
77  }
78  }
79  else {
80  print "A SYSCONFDIR was specified as '$sysconfdir' but no Db.conf file was found there!\n";
81  exit(1);
82  }
83 }
84 else {
85  print "No SYSCONFDIR was specified, so will attempt to delete all test DBs\n";
86 }
87 
88 
89 /* our database parameters. Connect to template1, since we are going to
90  attempt to delete one or more of the extant test databases */
91 $postgres_params = "dbname=template1 ";
92 $postgres_params .= "host=localhost ";
93 /* we assume the fossologytest/fossologytest user exists */
94 $postgres_params .= "user=fossologytest ";
95 $postgres_params .= "password=fossologytest ";
96 
97 $PG_CONN = pg_connect($postgres_params)
98  or die("FAIL: Could not connect to postgres server\n");
99 
100 /* if a sysconfdir / database name was not provided then delete everything */
101 if ( empty($database_name) ) {
102 
103  /* query postgres for all the test databases */
104  $sql = "SELECT datname from pg_database where datname like 'fossologytest%'";
105  $result = pg_query($PG_CONN, $sql)
106  or die("FAIL: Could not query postgres database\n");
107 
108  /* drop each test database found */
109  while ($row = pg_fetch_row($result)) {
110  $dbname = $row[0];
111  echo "Dropping test database $dbname\n";
112  $drop_sql = "DROP DATABASE $dbname";
113  pg_query($PG_CONN, $drop_sql)
114  or die("FAIL: Could not drop database '$dbname'\n");
115  }
116 }
117 /* otherwise just delete the specified test database */
118 else {
119  echo "Dropping test database $database_name ONLY.\n";
120  $drop_sql = "DROP DATABASE $database_name";
121  pg_query($PG_CONN, $drop_sql)
122  or die("FAIL: Could not drop drop database '$dbname'\n");
123 }
124 pg_close($PG_CONN);
125 
126 
127 /* now delete all of the test directories */
128 $system_temp_dir = sys_get_temp_dir();
129 $temp_dirs = glob($system_temp_dir . '/*');
130 
131 /* if a sysconfdir was provided then only delete it */
132 if (!empty($sysconfdir)) { // delete specified test directory
133  $temp_dirs = array($sysconfdir);
134 }
135 
136 foreach ($temp_dirs as $temp_dir) {
137  /* try to match the directory name for the expected test directory name
138  fossologytest_YYYYMMDD_HHmmSS/' */
139  if (preg_match('/\/fossologytest_\d{8}_\d{6}\/?$/', $temp_dir)) {
140  echo "Deleting $temp_dir\n";
141  $escaped_temp_dir = escapeshellarg($temp_dir);
142  `rm -rf $escaped_temp_dir`;
143 
144  }
145 }
146 
147 
148 /* also try and cleanup old style test databases
149  but ONLY if no specific database / sysconfdir was provided */
150 if (empty($sysconfdir)) {
151  print "Attempting to clean up old-style FOSSology test databases\n";
152 
153  /* our database parameters. Connect to template1, since we are going to
154  attempt to delete all of the extant test databases */
155  $postgres_params = "dbname=template1 ";
156  $postgres_params .= "host=localhost ";
157  $postgres_params .= "user=fossy ";
158  $postgres_params .= "password=fossy ";
159 
160  $PG_CONN = pg_connect($postgres_params)
161  or die("FAIL: Could not connect to postgres server\n");
162 
163  /* query postgres for all of the OLD style test databases */
164  $sql = "SELECT datname from pg_database where datname like 'fosstest%'";
165  $result = pg_query($PG_CONN, $sql)
166  or die("FAIL: Could not query postgres database\n");
167 
168  /* drop each test database found */
169  while ($row = pg_fetch_row($result)) {
170  $dbname = $row[0];
171  echo "Dropping test databases $dbname\n";
172  $drop_sql = "DROP DATABASE $dbname";
173  pg_query($PG_CONN, $drop_sql)
174  or die("FAIL: Could not drop database $dbname\n");
175  }
176 
177  pg_close($PG_CONN);
178 }
179 
180 print "Done cleaning up FOSSology test databases\n";
181 
182 exit(0);
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN