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