FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
srcConfig.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 /*
4  Copyright (C) 2013-2014 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  */
26 global $Debian;
27 global $RedHat;
28 
29 $debian = NULL;
30 $redHat = NULL;
31 $fedora = NULL;
32 $ubuntu = NULL;
33 
34 /*
35  * determine what os and version:
36  * configure yum or apt for fossology
37  * install fossology
38  * stop scheduler (if install is good).
39  * do the steps below.
40  * 1. tune kernel
41  * 2. postgres files
42  * 3. php ini files
43  * 4. fossology.org apache file (No needec)
44  * 5. checkout fossology
45  * 6. run fo-installdeps
46  */
47 
48 // Check for Super User
49 $euid = posix_getuid();
50 if($euid != 0) {
51  print "Error, this script must be run as root\n";
52  exit(1);
53 }
54 
55 // determine os flavor
56 $distros = array();
57 $f = exec('cat /etc/issue', $dist, $dRtn);
58 $distros = explode(' ', $dist[0]);
59 
60 //echo "DB: distros[0] is:{$distros[0]}\n";
61 
62 // create this class which can be used by any release/os
63 //$testUtils = new TestRun();
64 // distro can be Debian, Red, Fedora, Ubuntu
65 switch ($distros[0]) {
66  case 'Debian1':
67  $debian = TRUE; // is this needed?
68  $debianVersion = $distros[2];
69  echo "debian version is:$debianVersion\n";
70  try
71  {
72  $Debian = new ConfigSys($distros[0], $debianVersion);
73  }
74  catch (Exception $e)
75  {
76  echo "FATAL! could not process ini file for Debian $debianVersion system\n";
77  exit(1);
78  }
79  echo "*** Configure fossology ***\n";
80  if(!configFossology($Debian))
81  {
82  echo "FATAL! Could not config fossology on {$distros[0]} version $debianVersion\n";
83  exit(1);
84  }
85  break;
86  case 'Red1':
87  $redHat = 'RedHat';
88  $rhVersion = $distros[6];
89  //echo "rh version is:$rhVersion\n";
90  try
91  {
92  $RedHat = new ConfigSys($redHat, $rhVersion);
93  }
94  catch (Exception $e)
95  {
96  echo "FATAL! could not process ini file for RedHat $rhVersion system\n";
97  echo $e;
98  exit(1);
99  }
100  echo "*** Configure fossology ***\n";
101  if(!configFossology($RedHat))
102  {
103  echo "FATAL! Could not config fossology on $redHat version $rhVersion\n";
104  exit(1);
105  }
106 
107  if(!stop('iptables'))
108  {
109  echo "Erorr! Could not stop Firewall, please stop by hand\n";
110  exit(1);
111  }
112 
113  break;
114  case 'CentOS1':
115  $redHat = 'RedHat';
116  $rhVersion = $distros[2];
117  echo "rh version is:$rhVersion\n";
118  try
119  {
120  $RedHat = new ConfigSys($redHat, $rhVersion);
121  }
122  catch (Exception $e)
123  {
124  echo "FATAL! could not process ini file for RedHat $rhVersion system\n";
125  echo $e;
126  exit(1);
127  }
128  echo "*** Configure fossology ***\n";
129  if(!configFossology($RedHat))
130  {
131  echo "FATAL! Could not config fossology on $redHat version $rhVersion\n";
132  exit(1);
133  }
134 
135  if(!stop('iptables'))
136  {
137  echo "Erorr! Could not stop Firewall, please stop by hand\n";
138  exit(1);
139  }
140 
141  break;
142  case 'Fedora1':
143  $fedora = 'Fedora';
144  $fedVersion = $distros[2];
145  try
146  {
147  $Fedora = new ConfigSys($fedora, $fedVersion);
148  }
149  catch (Exception $e)
150  {
151  echo "FATAL! could not process ini file for Fedora $fedVersion system\n";
152  echo $e;
153  exit(1);
154  }
155  echo "*** Configure fossology ***\n";
156  if(!configFossology($Fedora))
157  {
158  echo "FATAL! Could not config fossology on $fedora version $fedVersion\n";
159  exit(1);
160  }
161  $last = exec("systemctl stop iptables.service", $out, $rtn);
162  if($rtn != 0)
163  {
164  echo "Erorr! Could not stop Firewall, please stop by hand\n";
165  exit(1);
166  }
167  break;
168  case 'Ubuntu':
169  $distro = 'Ubuntu';
170  $ubunVersion = $distros[1];
171  echo "Ubuntu version is:$ubunVersion\n";
172  try
173  {
174  $Ubuntu = new ConfigSys($distros[0], $ubunVersion);
175  }
176  catch (Exception $e)
177  {
178  echo "FATAL! could not process ini file for Ubuntu $ubunVersion system\n";
179  echo $e . "\n";
180  exit(1);
181  }
182  echo "*** Configure fossology ***\n";
183  if(!configDebian($distros[0], $ubunVersion))
184  {
185  echo "FATAL! Could not config fossology on {$distros[0]} version $ubunVersion\n";
186  exit(1);
187  }
188 
189  break;
190  default:
191  echo "Fatal! unrecognized distribution! {$distros[0]}\n" ;
192  exit(1);
193  break;
194 }
195 class ConfigSys {
196 
197  public $osFlavor;
198  public $osVersion = 0;
199  private $fossVersion;
200  private $osCodeName;
201  public $deb;
202  public $comment = '';
203  public $yum;
204 
205  function __construct($osFlavor, $osVersion)
206  {
207  if(empty($osFlavor))
208  {
209  throw new Exception("No Os Flavor supplied\n");
210  }
211  if(empty($osVersion))
212  {
213  throw new Exception("No Os Version Supplied\n");
214  }
215 
216  $dataFile = '../dataFiles/pkginstall/' . strtolower($osFlavor) . '.ini';
217  $releases = parse_ini_file($dataFile, 1);
218  //echo "DB: the parsed ini file is:\n";
219  //print_r($releases) . "\n";
220  foreach($releases as $release => $values)
221  {
222  if($values['osversion'] == $osVersion)
223  {
224  // found the correct os, gather attributes
225  $this->osFlavor = $values['osflavor'];
226  $this->osVersion = $values['osversion'];
227  $this->fossVersion = $values['fossversion'];
228  $this->osCodeName = $values['codename'];
229  // code below is needed to avoid php notice
230  switch (strtolower($this->osFlavor)) {
231  case 'ubuntu':
232  case 'debian':
233  $this->deb = $values['deb'];
234  break;
235  case 'fedora':
236  case 'redhat':
237  $this->yum = $values['yum'];
238  break;
239  default:
240  ;
241  break;
242  }
243  $this->comment = $values['comment'];
244  }
245  }
246  if($this->osVersion == 0)
247  {
248  throw new Exception("FATAL! no matching os flavor or version found\n");
249  }
250  return;
251  } // __construct
252 
258  public function printAttr()
259  {
260 
261  echo "Attributes of ConfigSys:\n";
262  echo "\tosFlavor:$this->osFlavor\n";
263  echo "\tosVersion:$this->osVersion\n";
264  echo "\tfossVersion:$this->fossVersion\n";
265  echo "\tosCodeName:$this->osCodeName\n";
266  echo "\tdeb:$this->deb\n";
267  echo "\tcomment:$this->comment\n";
268  echo "\tyum:$this->yum\n";
269 
270  return;
271  } //printAttr
272 } // ConfigSys
273 
274 
282 function configFossology($objRef)
283 {
284  if(!is_object($objRef))
285  {
286  return(FALSE);
287  }
288  $debLog = NULL;
289  $installLog = NULL;
290 
291  //echo "DB: IFOSS: osFlavor is:$objRef->osFlavor\n";
292  switch ($objRef->osFlavor) {
293  case 'Ubuntu':
294  case 'Debian':
295  $debApache = "ln -s /usr/local/etc/fossology/conf/src-install-apache-example.conf /etc/apache2/sites-enabled/fossology.conf";
296  $last = exec($debApache, $out, $rtn);
297  //echo "last is:$last\nresults of update are:\n";print_r($out) . "\n";
298  if($rtn != 0)
299  {
300  echo "Failed to config fossology!\nTranscript is:\n";
301  echo implode("\n",$out) . "\n";
302  return(FALSE);
303  }
304  break;
305  case 'Fedora':
306  case 'RedHat':
307  $initPostgres = "service postgresql initdb";
308  $startPostgres = "service postgresql start";
309  $restartPostgres = "service postgresql restart";
310  $psqlFile = "../dataFiles/pkginstall/redhat/6.x/pg_hba.conf";
311 
312  echo "** Initial postgresql **\n";
313  $last = exec($initPostgres, $out, $rtn);
314  if($rtn != 0)
315  {
316  echo "Failed to initial postgresql!\nTranscript is:\n";
317  echo implode("\n",$out) . "\n";
318  return(FALSE);
319  }
320  echo "** Start postgresql **\n";
321  $last = exec($startPostgres, $out, $rtn);
322  if($rtn != 0)
323  {
324  echo "Failed to start postgresql!\nTranscript is:\n";
325  echo implode("\n",$out) . "\n";
326  return(FALSE);
327  }
328  echo "** Configure pg_hba.conf **\n";
329  try
330  {
331  copyFiles($psqlFile, "/var/lib/pgsql/data/");
332  }
333  catch (Exception $e)
334  {
335  echo "Failure: Could not copy postgres config files\n";
336  }
337  $last = exec($restartPostgres, $out, $rtn);
338  if($rtn != 0)
339  {
340  echo "Failed to restart postgresql!\nTranscript is:\n";
341  return(FALSE);
342  }
343  break;
344 
345  default:
346  echo "FATAL! Unrecongnized OS/Release, not one of Ubuntu, Debian, RedHat" .
347  " or Fedora\n";
348  return(FALSE);
349  break;
350  }
351  return(TRUE);
352 }
353 
367 function copyFiles($files, $dest)
368 {
369  if(empty($files))
370  {
371  throw new Exception('No file to copy', 0);
372  }
373  if(empty($dest))
374  {
375  throw new Exception('No destination for copy', 0);
376  }
377  //echo "DB: copyFiles: we are at:" . getcwd() . "\n";
378  $login = posix_getlogin();
379  //echo "DB: copyFiles: running as:$login\n";
380  //echo "DB: copyFiles: uid is:" . posix_getuid() . "\n";
381  if(is_array($files))
382  {
383  foreach($files as $file)
384  {
385  // Get left name and check if dest is a directory, copy cannot copy to a
386  // dir.
387  $baseFile = basename($file);
388  if(is_dir($dest))
389  {
390  $to = $dest . "/$baseFile";
391  }
392  else
393  {
394  $to = $dest;
395  }
396  //echo "DB: copyfiles: file copied is:$file\n";
397  //echo "DB: copyfiles: to is:$to\n";
398  if(!copy($file, $to))
399  {
400  throw new Exception("Could not copy $file to $to");
401  }
402  //$lastcp = exec("cp -v $file $to", $cpout, $cprtn);
403  //echo "DB: copyfiles: cprtn is:$cprtn\n";
404  //echo "DB: copyfiles: lastcp is:$lastcp\n";
405  //echo "DB: copyfiles: out is:\n";print_r($cpout) . "\n";
406  }
407  }
408  else
409  {
410  $baseFile = basename($files);
411  if(is_dir($dest))
412  {
413  $to = $dest . "/$baseFile";
414  }
415  else
416  {
417  $to = $dest;
418  }
419  //echo "DB: copyfiles-single: file copied is:$files\n";
420  //echo "DB: copyfiles-single: to is:$to\n";
421  if(!copy($files,$to))
422  {
423  throw new Exception("Could not copy $file to $to");
424  }
425  }
426  return(TRUE);
427 } // copyFiles
428 
429 
440 function configDebian($osType, $osVersion)
441 {
442  if(empty($osType))
443  {
444  return(FALSE);
445  }
446  if(empty($osVersion))
447  {
448  return(FALSE);
449  }
450 
451  switch ($osVersion)
452  {
453  case '6.0':
454  case '7.0':
455  echo "debianConfig got os version 6.0!\n";
456  break;
457  case '10.04.3':
458  case '11.04':
459  case '11.10':
460  echo "debianConfig got os version $osVersion!\n";
461  break;
462  case '12.04.1':
463  case '12.04.2':
464  case '12.04.3':
465  case '12.04.4':
466  echo "debianConfig got os version $osVersion!\n";
467  echo "Old PHPunit installation with PEAR is deprecated, it is now done with composer.\n";
468  echo "To install composer type:\n";
469  echo "curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composer\n ";
470  break;
471  case '12.10':
472  echo "debianConfig got os version $osVersion!\n";
473  break;
474  default:
475  return(FALSE); // unsupported debian version
476  break;
477  }
478  return(TRUE);
479 } // configDebian
480 
490 function configYum($objRef)
491 {
492  if(!is_object($objRef))
493  {
494  return(FALSE);
495  }
496  if(empty($objRef->yum))
497  {
498  echo "FATAL, no yum install line to install\n";
499  return(FALSE);
500  }
501 
502  if ($objRef->osFlavor == 'RedHat')
503  {
504  $last = exec("yum -y install wget", $out, $rtn);
505  if($rtn != 0)
506  {
507  echo "FATAL! install EPEL repo fail\n";
508  echo "transcript is:\n";print_r($out) . "\n";
509  return(FALSE);
510  }
511  $last = exec("wget -e http_proxy=http://lart.usa.hp.com:3128 http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm", $out, $rtn);
512  if($rtn != 0)
513  {
514  echo "FATAL! install EPEL repo fail\n";
515  echo "transcript is:\n";print_r($out) . "\n";
516  return(FALSE);
517  }
518  $last = exec("rpm -ivh epel-release-6-8.noarch.rpm", $out, $rtn);
519  if($rtn != 0)
520  {
521  echo "FATAL! install EPEL repo fail\n";
522  echo "transcript is:\n";print_r($out) . "\n";
523  return(FALSE);
524  }
525  $last = exec("yum -y install php-phpunit-PHPUnit", $out, $rtn);
526  if($rtn != 0)
527  {
528  echo "FATAL! install PHPUnit fail\n";
529  echo "transcript is:\n";print_r($out) . "\n";
530  return(FALSE);
531  }
532  }
533  return(TRUE);
534 } // configYum
configYum($objRef)
config redhat based system to install fossology.
Definition: srcConfig.php:490
copyFiles($files, $dest)
copyFiles, copy one or more files to the destination, throws exception if file is not copied...
Definition: srcConfig.php:367
configFossology($objRef)
Config fossology.
Definition: srcConfig.php:282
stop($application)
stop the application Assumes application is restartable via /etc/init.d/<script>. The application pas...
configDebian($osType, $osVersion)
config a debian based system to install fossology.
Definition: srcConfig.php:440