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