FOSSology  4.4.0
Open Source License Compliance by Open Source Software
migrateConfig.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2011-2014 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
22 require_once '../lib/TestRun.php';
23 
24 global $Debian;
25 global $RedHat;
26 global $Migrate;
27 
28 $debian = NULL;
29 $redHat = NULL;
30 $fedora = NULL;
31 $ubuntu = NULL;
32 
33 /*
34  * determine what os and version:
35  * configure yum or apt for fossology
36  * install fossology
37  * stop scheduler (if install is good).
38  * do the steps below.
39  * 1. tune kernel
40  * 2. postgres files
41  * 3. php ini files
42  * 4. fossology.org apache file (No needec)
43  * 5. checkout fossology
44  * 6. run fo-installdeps
45  * 7. for RHEL what else?
46  * reconfigure yum or apt for fossology new version
47  * upgrade fossology
48  */
49 
50 // Check for Super User
51 $euid = posix_getuid();
52 if($euid != 0) {
53  print "Error, this script must be run as root\n";
54  exit(1);
55 }
56 
57 //determine if install of upgrade
58 if($argc > 1){
59  $Migrate = $argv[1];
60  echo "fossVersion: $Migrate\n";
61 }
62 // determine os flavor
63 $distros = array();
64 $f = exec('cat /etc/issue', $dist, $dRtn);
65 $distros = explode(' ', $dist[0]);
66 
67 //echo "DB: distros[0] is:{$distros[0]}\n";
68 
69 // create this class which can be used by any release/os
70 $testUtils = new TestRun();
71 // distro can be Debian, Red, Fedora, Ubuntu
72 switch ($distros[0]) {
73  case 'Debian':
74  $debian = TRUE; // is this needed?
75  $debianVersion = $distros[2];
76  echo "debian version is:$debianVersion\n";
77  try
78  {
79  $Debian = new ConfigSys($distros[0], $debianVersion,$Migrate);
80  }
81  catch (Exception $e)
82  {
83  echo "FATAL! could not process ini file for Debian $debianVersion system\n";
84  exit(1);
85  }
86 
87  if(insertDeb($Debian) === FALSE)
88  {
89  echo "FATAL! cannot insert deb line into /etc/apt/sources.list\n";
90  exit(1);
91  }
92  echo "*** Installing fossology ***\n";
93  if(!installFossology($Debian,$Migrate))
94  {
95  echo "FATAL! Could not install fossology on {$distros[0]} version $debianVersion\n";
96  exit(1);
97  }
98  break;
99  case 'Red':
100  $redHat = 'RedHat';
101  $rhVersion = $distros[6];
102  //echo "rh version is:$rhVersion\n";
103  try
104  {
105  $RedHat = new ConfigSys($redHat, $rhVersion,$Migrate);
106  }
107  catch (Exception $e)
108  {
109  echo "FATAL! could not process ini file for RedHat $rhVersion system\n";
110  echo $e;
111  exit(1);
112  }
113  if(!configYum($RedHat,$Migrate))
114  {
115  echo "FATAL! could not install fossology.conf yum configuration file\n";
116  exit(1);
117  }
118  echo "*** Installing fossology ***\n";
119  if(!installFossology($RedHat,$Migrate))
120  {
121  echo "FATAL! Could not install fossology on $redHat version $rhVersion\n";
122  exit(1);
123  }
124  echo "*** stopping scheduler ***\n";
125 
126  if(!stop('iptables'))
127  {
128  echo "Erorr! Could not stop Firewall, please stop by hand\n";
129  exit(1);
130  }
131 
132  break;
133  case 'CentOS':
134  $redHat = 'RedHat';
135  //$rhVersion = $distros[2];
136  $rhVersion = '6.3';
137  echo "rh version is:$rhVersion\n";
138  try
139  {
140  $RedHat = new ConfigSys($redHat, $rhVersion,$Migrate);
141  }
142  catch (Exception $e)
143  {
144  echo "FATAL! could not process ini file for RedHat $rhVersion system\n";
145  echo $e;
146  exit(1);
147  }
148  if(!configYum($RedHat,$Migrate))
149  {
150  echo "FATAL! could not install fossology.conf yum configuration file\n";
151  exit(1);
152  }
153  echo "*** Installing fossology ***\n";
154  if(!installFossology($RedHat,$Migrate))
155  {
156  echo "FATAL! Could not install fossology on $redHat version $rhVersion\n";
157  exit(1);
158  }
159  echo "*** stopping scheduler ***\n";
160 
161  if(!stop('iptables'))
162  {
163  echo "Erorr! Could not stop Firewall, please stop by hand\n";
164  exit(1);
165  }
166  break;
167  case 'Fedora':
168  $fedora = 'Fedora';
169  $fedVersion = $distros[2];
170  try
171  {
172  $Fedora = new ConfigSys($fedora, $fedVersion, $Migrate);
173  }
174  catch (Exception $e)
175  {
176  echo "FATAL! could not process ini file for Fedora $fedVersion system\n";
177  echo $e;
178  exit(1);
179  }
180  if(!configYum($Fedora,$Migrate))
181  {
182  echo "FATAL! could not install fossology.repo yum configuration file\n";
183  exit(1);
184  break;
185  }
186  echo "*** Installing fossology ***\n";
187  if(!installFossology($Fedora,$Migrate))
188  {
189  echo "FATAL! Could not install fossology on $fedora version $fedVersion\n";
190  exit(1);
191  }
192  echo "*** stopping scheduler ***\n";
193 
194  $last = exec("systemctl stop iptables.service", $out, $rtn);
195  if($rtn != 0)
196  {
197  echo "Erorr! Could not stop Firewall, please stop by hand\n";
198  exit(1);
199  }
200  break;
201  case 'Ubuntu':
202  $distro = 'Ubuntu';
203  $ubunVersion = $distros[1];
204  echo "Ubuntu version is:$ubunVersion\n";
205  try
206  {
207  $Ubuntu = new ConfigSys($distros[0], $ubunVersion, $Migrate);
208  }
209  catch (Exception $e)
210  {
211  echo "FATAL! could not process ini file for Ubuntu $ubunVersion system\n";
212  echo $e . "\n";
213  exit(1);
214  }
215  if(insertDeb($Ubuntu) === FALSE)
216  {
217  echo "FATAL! cannot insert deb line into /etc/apt/sources.list\n";
218  exit(1);
219  }
220  echo "*** Installing fossology ***\n";
221  if(!installFossology($Ubuntu, $Migrate))
222  {
223  echo "FATAL! Could not install fossology on {$distros[0]} version $ubunVersion\n";
224  exit(1);
225  }
226  echo "*** stopping scheduler ***\n";
227  // Stop scheduler so system files can be configured.
228  //$testUtils->stopScheduler();
229 
230  echo "*** Setting up config files ***\n";
231  if(configDebian($distros[0], $ubunVersion) === FALSE)
232  {
233  echo "FATAL! could not configure postgres or php config files\n";
234  exit(1);
235  }
236  break;
237  default:
238  echo "Fatal! unrecognized distribution! {$distros[0]}\n" ;
239  exit(1);
240  break;
241 }
242 class ConfigSys {
243 
244  public $osFlavor;
245  public $osVersion = 0;
246  private $fossVersion;
247  private $osCodeName;
248  public $deb;
249  public $comment = '';
250  public $yum;
251 
252  function __construct($osFlavor, $osVersion, $migrate)
253  {
254  if(empty($osFlavor))
255  {
256  throw new Exception("No Os Flavor supplied\n");
257  }
258  if(empty($osVersion))
259  {
260  throw new Exception("No Os Version Supplied\n");
261  }
262 
263  if(empty($migrate))
264  {
265  $dataFile = '../dataFiles/miginstall/' . strtolower($osFlavor) . '.ini';
266  }else
267  {
268  $dataFile = '../dataFiles/pkginstall/' . strtolower($osFlavor) . '.ini';
269  }
270  //$dataFile = '../dataFiles/pkginstall/' . strtolower($osFlavor) . '.ini';
271  $releases = parse_ini_file($dataFile, 1);
272  //echo "DB: the parsed ini file is:\n";
273  //print_r($releases) . "\n";
274  foreach($releases as $release => $values)
275  {
276  if($values['osversion'] == $osVersion)
277  {
278  // found the correct os, gather attributes
279  $this->osFlavor = $values['osflavor'];
280  $this->osVersion = $values['osversion'];
281  $this->fossVersion = $values['fossversion'];
282  $this->osCodeName = $values['codename'];
283  // code below is needed to avoid php notice
284  switch (strtolower($this->osFlavor)) {
285  case 'ubuntu':
286  case 'debian':
287  $this->deb = $values['deb'];
288  break;
289  case 'fedora':
290  case 'redhat':
291  $this->yum = $values['yum'];
292  break;
293  default:
294  ;
295  break;
296  }
297  $this->comment = $values['comment'];
298  }
299  }
300  if($this->osVersion == 0)
301  {
302  throw new Exception("FATAL! no matching os flavor or version found\n");
303  }
304  return;
305  } // __construct
306 
312  public function printAttr()
313  {
314 
315  echo "Attributes of ConfigSys:\n";
316  echo "\tosFlavor:$this->osFlavor\n";
317  echo "\tosVersion:$this->osVersion\n";
318  echo "\tfossVersion:$this->fossVersion\n";
319  echo "\tosCodeName:$this->osCodeName\n";
320  echo "\tdeb:$this->deb\n";
321  echo "\tcomment:$this->comment\n";
322  echo "\tyum:$this->yum\n";
323 
324  return;
325  } //printAttr
326 } // ConfigSys
327 
335 function insertDeb($objRef)
336 {
337 
338  if(!is_object($objRef))
339  {
340  return(FALSE);
341  }
342  // open file for append
343  $APT = fopen('/etc/apt/sources.list', 'a+');
344  if(!is_resource($APT))
345  {
346  echo "FATAL! could not open /etc/apt/sources.list for modification\n";
347  return(FALSE);
348  }
349  $written = fwrite($APT, "\n");
350  fflush($APT);
351 
352  if(empty($objRef->comment))
353  {
354  $comment = '# Automatically inserted by pkgConfig.php';
355  }
356 
357  $com = fwrite($APT, $objRef->comment . "\n");
358  if(!$written = fwrite($APT, $objRef->deb))
359  {
360  echo "FATAL! could not write deb line to /etc/apt/sources.list\n";
361  return(FALSE);
362  }
363  fclose($APT);
364  return(TRUE);
365 } // insertDeb
366 
377 function installFossology($objRef, $migrate)
378 {
379  if(!is_object($objRef))
380  {
381  return(FALSE);
382  }
383  $aptUpdate = 'apt-get update 2>&1';
384  $aptInstall = 'apt-get -y --force-yes install fossology 2>&1';
385  $yumClean = 'yum clean all';
386  $yumUpdate = 'yum -y update 2>&1';
387  $yumInstall = 'yum -y install fossology > fossinstall.log 2>&1';
388 
389  $aptUpgrade = 'apt-get -y --force-yes dist-upgrade 2>&1';
390  $yumUpgrade = 'yum -y upgrade fossology* > fossinstall.log 2>&1';
391 
392  $debLog = NULL;
393  $installLog = NULL;
394 
395  //echo "DB: IFOSS: osFlavor is:$objRef->osFlavor\n";
396  switch ($objRef->osFlavor) {
397  case 'Ubuntu':
398  case 'Debian':
399  $last = exec($aptUpdate, $out, $rtn);
400  //echo "last is:$last\nresults of update are:\n";print_r($out) . "\n";
401  if (empty($migrate))
402  $last = exec($aptInstall, $iOut, $iRtn);
403  else
404  $last = exec($aptUpgrade, $iOut, $iRtn);
405  if($iRtn != 0)
406  {
407  echo "Failed to install fossology!\nTranscript is:\n";
408  echo implode("\n",$iOut) . "\n";
409  return(FALSE);
410  }
411  // check for php or other errors that don't make apt return 1
412  echo "DB: in ubun/deb case, before installLog implode\n";
413  $debLog = implode("\n",$iOut);
414  if(!ckInstallLog($debLog))
415  {
416  echo "One or more of the phrases:\nPHP Stack trace:\nFATAL\n".
417  "Could not connect to FOSSology database:\n" .
418  "Unable to connect to PostgreSQL server:\n" .
419  "Was found in the install output. This install is suspect and is considered FAILED.\n";
420  return(FALSE);
421  }
422  // if any of the above are non zero, return false
423  break;
424  case 'Fedora':
425  case 'RedHat':
426  echo "** Running yum clean **\n";
427  $last = exec($yumClean, $out, $rtn);
428  if($rtn != 0)
429  {
430  echo "Failed to clean all cache data!\nTranscript is:\n";
431  echo implode("\n",$out) . "\n";
432  return(FALSE);
433  }
434  if(empty($migrate))
435  {
436  echo "** Running yum update **\n";
437  $last = exec($yumUpdate, $out, $rtn);
438  if($rtn != 0)
439  {
440  echo "Failed to update yum repositories with fossology!\nTranscript is:\n";
441  echo implode("\n",$out) . "\n";
442  return(FALSE);
443  }
444  }
445  //echo "** Running yum install fossology **\n";
446  if(empty($migrate))
447  {
448  echo "** Running yum install fossology **\n";
449  $last = exec($yumInstall, $yumOut, $yumRtn);
450  }
451  else
452  {
453  echo "** Running yum upgrade fossology **\n";
454  $last = exec($yumUpgrade, $yumOut, $yumRtn);
455  }
456  //echo "install of fossology finished, yumRtn is:$yumRtn\nlast is:$last\n";
457  //$clast = system('cat fossinstall.log');
458  if($yumRtn != 0)
459  {
460  echo "Failed to install fossology!\nTranscript is:\n";
461  system('cat fossinstall.log');
462  return(FALSE);
463  }
464  if(!($installLog = file_get_contents('fossinstall.log')))
465  {
466  echo "FATAL! could not read 'fossinstall.log\n";
467  return(FALSE);
468  }
469  if(!ckInstallLog($installLog))
470  {
471  echo "One or more of the phrases:\nPHP Stack trace:\nFATAL\n".
472  "Could not connect to FOSSology database:\n" .
473  "Unable to connect to PostgreSQL server:\n" .
474  "Was found in the install output. This install is suspect and is considered failed.\n";
475  return(FALSE);
476  }
477  break;
478 
479  default:
480  echo "FATAL! Unrecongnized OS/Release, not one of Ubuntu, Debian, RedHat" .
481  " or Fedora\n";
482  return(FALSE);
483  break;
484  }
485  return(TRUE);
486 }
487 
497 function ckInstallLog($log) {
498  if(empty($log))
499  {
500  return(FALSE);
501  }
502  // check for php or other errors that don't make apt return 1
503  $traces = $fates = $connects = $postgresFail = 0;
504  $stack = '/PHP Stack trace:/';
505  $fatal = '/FATAL/';
506  $noConnect = '/Could not connect to FOSSology database/';
507  $noPG = '/Unable to connect to PostgreSQL server:/';
508 
509  $traces = preg_match_all($stack, $log, $stackMatches);
510  $fates = preg_match_all($fatal, $log, $fatalMatches);
511  $connects = preg_match_all($noConnect, $log, $noconMatches);
512  $postgresFail = preg_match_all($noPG, $log, $noPGMatches);
513  echo "Number of PHP stack traces found:$traces\n";
514  echo "Number of FATAL's found:$fates\n";
515  echo "Number of 'cannot connect' found:$connects\n";
516  echo "Number of 'cannot connect to postgres server' found:$postgresFail\n";
517  print "DB: install log is:\n$log\n";
518  if($traces ||
519  $fates ||
520  $connects ||
521  $postgresFail)
522  {
523  return(FALSE);
524  }
525  return(TRUE);
526 }
540 function copyFiles($files, $dest)
541 {
542  if(empty($files))
543  {
544  throw new Exception('No file to copy', 0);
545  }
546  if(empty($dest))
547  {
548  throw new Exception('No destination for copy', 0);
549  }
550  //echo "DB: copyFiles: we are at:" . getcwd() . "\n";
551  $login = posix_getlogin();
552  //echo "DB: copyFiles: running as:$login\n";
553  //echo "DB: copyFiles: uid is:" . posix_getuid() . "\n";
554  if(is_array($files))
555  {
556  foreach($files as $file)
557  {
558  // Get left name and check if dest is a directory, copy cannot copy to a
559  // dir.
560  $baseFile = basename($file);
561  if(is_dir($dest))
562  {
563  $to = $dest . "/$baseFile";
564  }
565  else
566  {
567  $to = $dest;
568  }
569  //echo "DB: copyfiles: file copied is:$file\n";
570  //echo "DB: copyfiles: to is:$to\n";
571  if(!copy($file, $to))
572  {
573  throw new Exception("Could not copy $file to $to");
574  }
575  //$lastcp = exec("cp -v $file $to", $cpout, $cprtn);
576  //echo "DB: copyfiles: cprtn is:$cprtn\n";
577  //echo "DB: copyfiles: lastcp is:$lastcp\n";
578  //echo "DB: copyfiles: out is:\n";print_r($cpout) . "\n";
579  }
580  }
581  else
582  {
583  $baseFile = basename($files);
584  if(is_dir($dest))
585  {
586  $to = $dest . "/$baseFile";
587  }
588  else
589  {
590  $to = $dest;
591  }
592  //echo "DB: copyfiles-single: file copied is:$files\n";
593  //echo "DB: copyfiles-single: to is:$to\n";
594  if(!copy($files,$to))
595  {
596  throw new Exception("Could not copy $file to $to");
597  }
598  }
599  return(TRUE);
600 } // copyFiles
601 
612 function configDebian($osType, $osVersion)
613 {
614  if(empty($osType))
615  {
616  return(FALSE);
617  }
618  if(empty($osVersion))
619  {
620  return(FALSE);
621  }
622 
623  // based on type read the appropriate ini file.
624 
625  //echo "DB:configD: osType is:$osType\n";
626  //echo "DB:configD: osversion is:$osVersion\n";
627 
628  switch ($osVersion)
629  {
630  case '6.0':
631  echo "debianConfig got os version 6.0!\n";
632  break;
633  case '10.04.3':
634  case '11.04':
635  case '11.10':
636  echo "debianConfig got os version $osVersion!\n";
637  break;
638  case '12.04.1':
639  case '12.04.2':
640  case '12.04.3':
641  case '12.04.4':
642  echo "debianConfig got os version $osVersion!\n";
643 
644  echo "Old PHPunit installation with PEAR is deprecated, it is now done with composer.\n";
645  echo "To install composer type:\n";
646  echo "curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composer\n ";
647 
648 
649  break;
650  case '12.10':
651  echo "debianConfig got os version $osVersion!\n";
652  //postgresql-9.1 can't use 8.4 conf file
653  break;
654  default:
655  return(FALSE); // unsupported debian version
656  break;
657  }
658  return(TRUE);
659 } // configDebian
660 
670 function configYum($objRef,$migrate)
671 {
672  if(!is_object($objRef))
673  {
674  return(FALSE);
675  }
676  if(empty($objRef->yum))
677  {
678  echo "FATAL, no yum install line to install\n";
679  return(FALSE);
680  }
681 
682  $RedFedRepo = 'redfed-fossology.repo'; // name of generic repo file.
683  // replace the baseurl line with the current one.
684  $n = "../dataFiles/pkginstall/" . $RedFedRepo;
685  $fcont = file_get_contents($n);
686  //echo "DB: contents is:\n$fcont\n";
687  $newRepo = preg_replace("/baseurl=(.*)?/", 'baseurl=' . $objRef->yum, $fcont,-1, $cnt);
688  // write the file, fix below to copy the correct thing...
689  if(!($written = file_put_contents("../dataFiles/pkginstall/" . $RedFedRepo, $newRepo)))
690  {
691  echo "FATAL! could not write repo file $RedFedRepo\n";
692  exit(1);
693  }
694  // coe plays with yum stuff, check if yum.repos.d exists and if not create it.
695  if(is_dir('/etc/yum.repos.d'))
696  {
697  copyFiles("../dataFiles/pkginstall/" . $RedFedRepo, '/etc/yum.repos.d/fossology.repo');
698  }
699  else
700  {
701  // create the dir and then copy
702  if(!mkdir('/etc/yum.repos.d'))
703  {
704  echo "FATAL! could not create yum.repos.d\n";
705  return(FALSE);
706  }
707  copyFiles("../dataFiles/pkginstall/" . $RedFedRepo, '/etc/yum.repos.d/fossology.repo');
708  }
709  //print_r($objRef);
710  if (($objRef->osFlavor == 'RedHat') && (empty($migrate)))
711  {
712  $last = exec("yum -y install wget", $out, $rtn);
713  if($rtn != 0)
714  {
715  echo "FATAL! install EPEL repo fail\n";
716  echo "transcript is:\n";print_r($out) . "\n";
717  return(FALSE);
718  }
719  $last = exec("wget -e http_proxy=http://web-proxy.cce.hp.com:8088 http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm", $out, $rtn);
720  if($rtn != 0)
721  {
722  echo "FATAL! install EPEL repo fail\n";
723  echo "transcript is:\n";print_r($out) . "\n";
724  return(FALSE);
725  }
726  $last = exec("rpm -ivh epel-release-6-8.noarch.rpm", $out, $rtn);
727  if($rtn != 0)
728  {
729  echo "FATAL! install EPEL repo fail\n";
730  echo "transcript is:\n";print_r($out) . "\n";
731  return(FALSE);
732  }
733  $last = exec("yum -y install php-phpunit-PHPUnit", $out, $rtn);
734  if($rtn != 0)
735  {
736  echo "FATAL! install PHPUnit fail\n";
737  echo "transcript is:\n";print_r($out) . "\n";
738  return(FALSE);
739  }
740  }
741  return(TRUE);
742 } // configYum
743 
754 function stop($application)
755 {
756  if(empty($application))
757  {
758  return(FALSE);
759  }
760 
761  $last = exec("/etc/init.d/$application stop 2>&1", $out, $rtn);
762  if($rtn != 0)
763  {
764  echo "FATAL! could not stop $application\n";
765  echo "transcript is:\n";print_r($out) . "\n";
766  return(FALSE);
767  }
768  return(TRUE);
769 } // stop
insertDeb($objRef)
insert the fossology debian line in /etc/apt/sources.list
configYum($objRef, $migrate)
config yum on a redhat based system to install fossology.
copyFiles($files, $dest)
copyFiles, copy one or more files to the destination, throws exception if file is not copied.
ckInstallLog($log)
Check the fossology install output for errors in the install.
installFossology($objRef, $migrate)
Install fossology using either apt or yum.
configDebian($osType, $osVersion)
config a debian based system to install fossology.
stop($application)
stop the application Assumes application is restartable via /etc/init.d/<script>. The application pas...