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 */
19/**
20 * \file pkgConfig.php
21 * \brief prepare a system for install testing.
22 *
23 *  pkgConfig prepares a system for the installation of fossology packages and
24 *  installs the fossology packages.
25 *
26 *  @param string $fossVersion the version of fossology to install?
27 *  @todo what should the api for this really be?
28 *
29 * @version "$Id $"
30 * Created on Jul 19, 2011 by Mark Donohoe
31 * Updated on Nov 15, 2012 by Vincent Ma
32 */
33
34require_once '../lib/TestRun.php';
35
36global $Debian;
37global $RedHat;
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 */
58
59// Check for Super User
60$euid = posix_getuid();
61if($euid != 0) {
62  print "Error, this script must be run as root\n";
63  exit(1);
64}
65
66// determine os flavor
67$distros = array();
68$f = exec('cat /etc/issue', $dist, $dRtn);
69$distros = explode(' ', $dist[0]);
70
71//echo "DB: distros[0] is:{$distros[0]}\n";
72
73// configure for fossology and install fossology packages.
74// Note that after this point, you could just stop if one could rely on the install
75// process to give a valid exit code... Another script will run the agent unit
76// and functional tests.
77
78// create this class which can be used by any release/os
79$testUtils = new TestRun();
80// distro can be Debian, Red, Fedora, Ubuntu
81switch ($distros[0]) {
82  case 'Debian':
83    $debian = TRUE;  // is this needed?
84    $debianVersion = $distros[2];
85    echo "debian version is:$debianVersion\n";
86    try
87    {
88      $Debian = new ConfigSys($distros[0], $debianVersion);
89    }
90    catch (Exception $e)
91    {
92      echo "FATAL! could not process ini file for Debian $debianVersion system\n";
93      exit(1);
94    }
95
96    if(insertDeb($Debian) === FALSE)
97    {
98      echo "FATAL! cannot insert deb line into /etc/apt/sources.list\n";
99      exit(1);
100    }
101    echo "*** Installing fossology ***\n";
102    if(!installFossology($Debian))
103    {
104      echo "FATAL! Could not install fossology on {$distros[0]} version $debianVersion\n";
105      exit(1);
106    }
107    echo "*** stopping scheduler ***\n";
108    // Stop scheduler so system files can be configured.
109    //$testUtils->stopScheduler();
110    /*
111    echo "*** Tuning kernel ***\n";
112    tuneKernel();
113
114    echo "*** Setting up config files ***\n";
115    if(configDebian($distros[0], $debianVersion) === FALSE)
116    {
117      echo "FATAL! could not configure postgres or php config files\n";
118      exit(1);
119    }
120    */
121    /*
122     echo "*** Checking apache config ***\n";
123     if(!configApache2($distros[0]))
124     {
125     echo "Fatal, could not configure apache2 to use fossology\n";
126     }
127
128    if(!restart('apache2'))
129    {
130      echo "Erorr! Could not restart apache2, please restart by hand\n";
131      exit(1);
132    }
133    */
134    /*
135    echo "*** Starting FOSSology ***\n";
136    if(!start('fossology'))
137    {
138      echo "Erorr! Could not start FOSSology, please restart by hand\n";
139      exit(1);
140    }
141    */
142    break;
143  case 'Red':
144    $redHat = 'RedHat';
145    $rhVersion = $distros[6];
146    //echo "rh version is:$rhVersion\n";
147    try
148    {
149      $RedHat = new ConfigSys($redHat, $rhVersion);
150    }
151    catch (Exception $e)
152    {
153      echo "FATAL! could not process ini file for RedHat $rhVersion system\n";
154      echo $e;
155      exit(1);
156    }
157    if(!configYum($RedHat))
158    {
159      echo "FATAL! could not install fossology.conf yum configuration file\n";
160      exit(1);
161    }
162    /*
163    echo "*** Tuning kernel ***\n";
164    tuneKernel();
165    */
166    echo "*** Installing fossology ***\n";
167    if(!installFossology($RedHat))
168    {
169      echo "FATAL! Could not install fossology on $redHat version $rhVersion\n";
170      exit(1);
171    }
172    echo "*** stopping scheduler ***\n";
173    // Stop scheduler so system files can be configured.
174    //$testUtils->stopScheduler();
175    /*
176    echo "*** Setting up config files ***\n";
177    if(!configRhel($redHat, $rhVersion))
178    {
179      echo "FATAL! could not install php and postgress configuration files\n";
180      exit(1);
181    }
182    */
183    /*
184     echo "*** Checking apache config ***\n";
185     if(!configApache2($distros[0]))
186     {
187     echo "Fatal, could not configure apache2 to use fossology\n";
188     }
189     */
190    /*
191    if(!restart('httpd'))
192    {
193      echo "Erorr! Could not restart httpd, please restart by hand\n";
194      exit(1);
195    }
196    */
197    if(!stop('iptables'))
198    {
199      echo "Erorr! Could not stop Firewall, please stop by hand\n";
200      exit(1);
201    }
202
203    break;
204  case 'CentOS':
205    $redHat = 'RedHat';
206    $rhVersion = $distros[2];
207    echo "rh version is:$rhVersion\n";
208    try
209    {
210      $RedHat = new ConfigSys($redHat, $rhVersion);
211    }
212    catch (Exception $e)
213    {
214      echo "FATAL! could not process ini file for RedHat $rhVersion system\n";
215      echo $e;
216      exit(1);
217    }
218    if(!configYum($RedHat))
219    {
220      echo "FATAL! could not install fossology.conf yum configuration file\n";
221      exit(1);
222    }
223    /*
224    echo "*** Tuning kernel ***\n";
225    tuneKernel();
226    */
227    echo "*** Installing fossology ***\n";
228    if(!installFossology($RedHat))
229    {
230      echo "FATAL! Could not install fossology on $redHat version $rhVersion\n";
231      exit(1);
232    }
233    echo "*** stopping scheduler ***\n";
234    // Stop scheduler so system files can be configured.
235    //$testUtils->stopScheduler();
236    /*
237    echo "*** Setting up config files ***\n";
238    if(!configRhel($redHat, $rhVersion))
239    {
240      echo "FATAL! could not install php and postgress configuration files\n";
241      exit(1);
242    }
243    */
244    /*
245     echo "*** Checking apache config ***\n";
246     if(!configApache2($distros[0]))
247     {
248     echo "Fatal, could not configure apache2 to use fossology\n";
249     }
250     */
251    /*
252    if(!stop('httpd'))
253    {
254      echo "Erorr! Could not restart httpd, please restart by hand\n";
255      exit(1);
256    }
257    if(!start('httpd'))
258    {
259      echo "Erorr! Could not restart httpd, please restart by hand\n";
260      exit(1);
261    }
262    */
263    if(!stop('iptables'))
264    {
265      echo "Erorr! Could not stop Firewall, please stop by hand\n";
266      exit(1);
267    }
268    /*
269    echo "*** Starting fossology ***\n";
270    if(!start('fossology'))
271    {
272      echo "Erorr! Could not start fossology, please restart by hand\n";
273      exit(1);
274    }
275    */
276    break;
277  case 'Fedora':
278    $fedora = 'Fedora';
279    $fedVersion = $distros[2];
280    try
281    {
282      $Fedora = new ConfigSys($fedora, $fedVersion);
283    }
284    catch (Exception $e)
285    {
286      echo "FATAL! could not process ini file for Fedora $fedVersion system\n";
287      echo $e;
288      exit(1);
289    }
290    if(!configYum($Fedora))
291    {
292      echo "FATAL! could not install fossology.repo yum configuration file\n";
293      exit(1);
294      break;
295    }
296    echo "*** Installing fossology ***\n";
297    if(!installFossology($Fedora))
298    {
299      echo "FATAL! Could not install fossology on $fedora version $fedVersion\n";
300      exit(1);
301    }
302    echo "*** stopping scheduler ***\n";
303    // Stop scheduler so system files can be configured.
304    //$testUtils->stopScheduler();
305    /*
306    echo "*** Tuning kernel ***\n";
307    tuneKernel();
308
309    echo "*** Setting up config files ***\n";
310    if(!configRhel($fedora, $fedVersion))
311    {
312      echo "FATAL! could not install php and postgress configuration files\n";
313      exit(1);
314    }
315    */
316    /*
317     echo "*** Checking apache config ***\n";
318     if(!configApache2($distros[0]))
319     {
320     echo "Fatal, could not configure apache2 to use fossology\n";
321     }
322     */
323    /*
324    $last = exec("systemctl restart httpd.service", $out, $rtn);
325    if($rtn != 0)
326    {
327      echo "Erorr! Could not restart httpd, please restart by hand\n";
328      exit(1);
329    }
330    */
331    $last = exec("systemctl stop iptables.service", $out, $rtn);
332    if($rtn != 0)
333    {
334      echo "Erorr! Could not stop Firewall, please stop by hand\n";
335      exit(1);
336    }
337    /*
338    echo "*** Starting fossology ***\n";
339    if(!start('fossology'))
340    {
341      echo "Erorr! Could not start fossology, please restart by hand\n";
342      exit(1);
343    }
344    */
345    break;
346  case 'Ubuntu':
347    $distro = 'Ubuntu';
348    $ubunVersion = $distros[1];
349    echo "Ubuntu version is:$ubunVersion\n";
350    try
351    {
352      $Ubuntu = new ConfigSys($distros[0], $ubunVersion);
353    }
354    catch (Exception $e)
355    {
356      echo "FATAL! could not process ini file for Ubuntu $ubunVersion system\n";
357      echo $e . "\n";
358      exit(1);
359    }
360    if(insertDeb($Ubuntu) === FALSE)
361    {
362      echo "FATAL! cannot insert deb line into /etc/apt/sources.list\n";
363      exit(1);
364    }
365    /*
366    echo "*** Tuning kernel ***\n";
367    tuneKernel();
368    */
369    echo "*** Installing fossology ***\n";
370    if(!installFossology($Ubuntu))
371    {
372      echo "FATAL! Could not install fossology on {$distros[0]} version $ubunVersion\n";
373      exit(1);
374    }
375    echo "*** stopping scheduler ***\n";
376    // Stop scheduler so system files can be configured.
377    //$testUtils->stopScheduler();
378
379    echo "*** Setting up config files ***\n";
380    if(configDebian($distros[0], $ubunVersion) === FALSE)
381    {
382      echo "FATAL! could not configure postgres or php config files\n";
383      exit(1);
384    }
385    /*
386     echo "*** Checking apache config ***\n";
387     if(!configApache2($distros[0]))
388     {
389     echo "Fatal, could not configure apache2 to use fossology\n";
390     }
391
392    if(!restart('apache2'))
393    {
394      echo "Erorr! Could not restart apache2, please restart by hand\n";
395      exit(1);
396    }
397    */
398    /*
399    echo "*** Starting FOSSology ***\n";
400    if(!start('fossology'))
401    {
402      echo "Erorr! Could not start FOSSology, please restart by hand\n";
403      exit(1);
404    }
405    */
406    break;
407  default:
408    echo "Fatal! unrecognized distribution! {$distros[0]}\n" ;
409    exit(1);
410    break;
411}
412class ConfigSys {
413
414  public $osFlavor;
415  public $osVersion = 0;
416  private $fossVersion;
417  private $osCodeName;
418  public $deb;
419  public $comment = '';
420  public $yum;
421
422  function __construct($osFlavor, $osVersion)
423  {
424    if(empty($osFlavor))
425    {
426      throw new Exception("No Os Flavor supplied\n");
427    }
428    if(empty($osVersion))
429    {
430      throw new Exception("No Os Version Supplied\n");
431    }
432
433    $dataFile = '../dataFiles/pkginstall/' . strtolower($osFlavor) . '.ini';
434    $releases = parse_ini_file($dataFile, 1);
435    //echo "DB: the parsed ini file is:\n";
436    //print_r($releases) . "\n";
437    foreach($releases as $release => $values)
438    {
439      if($values['osversion'] == $osVersion)
440      {
441        // found the correct os, gather attributes
442        $this->osFlavor = $values['osflavor'];
443        $this->osVersion =  $values['osversion'];
444        $this->fossVersion =  $values['fossversion'];
445        $this->osCodeName =  $values['codename'];
446        // code below is needed to avoid php notice
447        switch (strtolower($this->osFlavor)) {
448          case 'ubuntu':
449          case 'debian':
450            $this->deb =  $values['deb'];
451            break;
452          case 'fedora':
453          case 'redhat':
454            $this->yum = $values['yum'];
455            break;
456          default:
457            ;
458            break;
459        }
460        $this->comment = $values['comment'];
461      }
462    }
463    if($this->osVersion == 0)
464    {
465      throw new Exception("FATAL! no matching os flavor or version found\n");
466    }
467    return;
468  } // __construct
469
470  /**
471   * prints all the classes attributes (properties)
472   *
473   * @return void
474   */
475  public function printAttr()
476  {
477
478    echo "Attributes of ConfigSys:\n";
479    echo "\tosFlavor:$this->osFlavor\n";
480    echo "\tosVersion:$this->osVersion\n";
481    echo "\tfossVersion:$this->fossVersion\n";
482    echo "\tosCodeName:$this->osCodeName\n";
483    echo "\tdeb:$this->deb\n";
484    echo "\tcomment:$this->comment\n";
485    echo "\tyum:$this->yum\n";
486
487    return;
488  } //printAttr
489} // ConfigSys
490
491/**
492 * \brief insert the fossology debian line in /etc/apt/sources.list
493 *
494 * @param object $objRef the object with the deb attribute
495 *
496 * @return boolean
497 */
498function insertDeb($objRef)
499{
500
501  if(!is_object($objRef))
502  {
503    return(FALSE);
504  }
505  // open file for append
506  $APT = fopen('/etc/apt/sources.list', 'a+');
507  if(!is_resource($APT))
508  {
509    echo "FATAL! could not open /etc/apt/sources.list for modification\n";
510    return(FALSE);
511  }
512  $written = fwrite($APT, "\n");
513  fflush($APT);
514
515  if(empty($objRef->comment))
516  {
517    $comment = '# Automatically inserted by pkgConfig.php';
518  }
519
520  $com = fwrite($APT, $objRef->comment . "\n");
521  if(!$written = fwrite($APT, $objRef->deb))
522  {
523    echo "FATAL! could not write deb line to /etc/apt/sources.list\n";
524    return(FALSE);
525  }
526  fclose($APT);
527  return(TRUE);
528}  // insertDeb
529
530/**
531 * \brief Install fossology using either apt or yum
532 *
533 * installFossology assumes that the correct configuration for yum and the
534 * correct fossology version has been configured into the system.
535 *
536 * @param object $objRef an object reference (should be to ConfigSys)
537 *
538 * @return boolean
539 */
540function installFossology($objRef)
541{
542  if(!is_object($objRef))
543  {
544    return(FALSE);
545  }
546  $aptUpdate = 'apt-get update 2>&1';
547  $aptInstall = 'apt-get -y --force-yes install fossology > fossinstall.log 2>&1';
548  $yumClean = 'yum clean all';
549  $yumUpdate = 'yum -y update 2>&1';
550  $yumInstall = 'yum -y install fossology > fossinstall.log 2>&1';
551  $debLog = NULL;
552  $installLog = NULL;
553
554  //echo "DB: IFOSS: osFlavor is:$objRef->osFlavor\n";
555  switch ($objRef->osFlavor) {
556    case 'Ubuntu':
557    case 'Debian':
558      $last = exec($aptUpdate, $out, $rtn);
559      //echo "last is:$last\nresults of update are:\n";print_r($out) . "\n";
560      $last = exec($aptInstall, $iOut, $iRtn);
561      if($iRtn != 0)
562      {
563        echo "Failed to install fossology! aptInstall is:$aptInstall, iRtn is:$iRtn, last is:$last \nTranscript is:\n";
564        echo implode("\n",$iOut) . "\n";
565        return(FALSE);
566      }
567      // check for php or other errors that don't make apt return 1
568      echo "DB: in ubun/deb case, before installLog implode\n";
569      $debLog = implode("\n",$iOut);
570      if(!ckInstallLog($debLog))
571      {
572        echo "One or more of the phrases:\nPHP Stack trace:\nFATAL\n".
573          "Could not connect to FOSSology database:\n" .
574          "Unable to connect to PostgreSQL server:\n" .
575          "Was found in the install output. This install is suspect and is considered FAILED.\n";
576        return(FALSE);
577      }
578      // if any of the above are non zero, return false
579      break;
580    case 'Fedora':
581    case 'RedHat':
582      echo "** Running yum clean **\n";
583      $last = exec($yumClean, $out, $rtn);
584      if($rtn != 0)
585      {
586        echo "Failed to clean all cache data!\nTranscript is:\n";
587        echo implode("\n",$out) . "\n";
588        return(FALSE);
589      }
590      echo "** Running yum update **\n";
591      $last = exec($yumUpdate, $out, $rtn);
592      if($rtn != 0)
593      {
594        echo "Failed to update yum repositories with fossology!\nTranscript is:\n";
595        echo implode("\n",$out) . "\n";
596        return(FALSE);
597      }
598      echo "** Running yum install fossology **\n";
599      $last = exec($yumInstall, $yumOut, $yumRtn);
600      //echo "install of fossology finished, yumRtn is:$yumRtn\nlast is:$last\n";
601      //$clast = system('cat fossinstall.log');
602      if($yumRtn != 0)
603      {
604        echo "Failed to install fossology! yumInstall is:$yumInstall, yumRtn is:$yumRtn, last is:$last \nTranscript is:\n";
605        echo implode("\n",$yumOut) . "\n";
606        return(FALSE);
607      }
608      if(!($installLog = file_get_contents('fossinstall.log')))
609      {
610        echo "FATAL! could not read 'fossinstall.log\n";
611        return(FALSE);
612      }
613      if(!ckInstallLog($installLog))
614      {
615        echo "One or more of the phrases:\nPHP Stack trace:\nFATAL\n".
616          "Could not connect to FOSSology database:\n" .
617          "Unable to connect to PostgreSQL server:\n" .
618          "Was found in the install output. This install is suspect and is considered failed.\n";
619        return(FALSE);
620      }
621      break;
622
623    default:
624      echo "FATAL! Unrecongnized OS/Release, not one of Ubuntu, Debian, RedHat" .
625      " or Fedora\n";
626      return(FALSE);
627      break;
628  }
629  return(TRUE);
630}
631
632/**
633 * \brief Check the fossology install output for errors in the install.
634 *
635 * These errors do not cause apt to think that the install failed, so the output
636 * should be checked for typical failures during an install of packages.  See
637 * the code for the specific checks.
638 *
639 * @param string $log the output from a fossology install with packages
640 */
641function ckInstallLog($log) {
642  if(empty($log))
643  {
644    return(FALSE);
645  }
646  // check for php or other errors that don't make apt return 1
647  $traces = $fates = $connects = $postgresFail = 0;
648  $stack = '/PHP Stack trace:/';
649  $fatal = '/FATAL/';
650  $noConnect = '/Could not connect to FOSSology database/';
651  $noPG = '/Unable to connect to PostgreSQL server:/';
652
653  $traces = preg_match_all($stack, $log, $stackMatches);
654  $fates = preg_match_all($fatal, $log, $fatalMatches);
655  $connects =  preg_match_all($noConnect, $log, $noconMatches);
656  $postgresFail =  preg_match_all($noPG, $log, $noPGMatches);
657  echo "Number of PHP stack traces found:$traces\n";
658  echo "Number of FATAL's found:$fates\n";
659  echo "Number of 'cannot connect' found:$connects\n";
660  echo "Number of 'cannot connect to postgres server' found:$postgresFail\n";
661  //print "DB: install log is:\n$log\n";
662  if($traces ||
663  $fates ||
664  $connects ||
665  $postgresFail)
666  {
667    return(FALSE);
668  }
669  return(TRUE);
670}
671/**
672 * \brief copyFiles, copy one or more files to the destination,
673 * throws exception if file is not copied.
674 *
675 * The method can be used to rename a single file, but not a directory.  It
676 * cannot rename multiple files.
677 *
678 * @param mixed $file the file to copy (string), use an array for multiple files.
679 * @param string $dest the destination path (must exist, must be writable).
680 *
681 * @retrun boolean
682 *
683 */
684function copyFiles($files, $dest)
685{
686  if(empty($files))
687  {
688    throw new Exception('No file to copy', 0);
689  }
690  if(empty($dest))
691  {
692    throw new Exception('No destination for copy', 0);
693  }
694  //echo "DB: copyFiles: we are at:" . getcwd() . "\n";
695  $login = posix_getlogin();
696  //echo "DB: copyFiles: running as:$login\n";
697  //echo "DB: copyFiles: uid is:" . posix_getuid() . "\n";
698  if(is_array($files))
699  {
700    foreach($files as $file)
701    {
702      // Get left name and check if dest is a directory, copy cannot copy to a
703      // dir.
704      $baseFile = basename($file);
705      if(is_dir($dest))
706      {
707        $to = $dest . "/$baseFile";
708      }
709      else
710      {
711        $to = $dest;
712      }
713      //echo "DB: copyfiles: file copied is:$file\n";
714      //echo "DB: copyfiles: to is:$to\n";
715      if(!copy($file, $to))
716      {
717        throw new Exception("Could not copy $file to $to");
718      }
719      //$lastcp = exec("cp -v $file $to", $cpout, $cprtn);
720      //echo "DB: copyfiles: cprtn is:$cprtn\n";
721      //echo "DB: copyfiles: lastcp is:$lastcp\n";
722      //echo "DB: copyfiles: out is:\n";print_r($cpout) . "\n";
723    }
724  }
725  else
726  {
727    $baseFile = basename($files);
728    if(is_dir($dest))
729    {
730      $to = $dest . "/$baseFile";
731    }
732    else
733    {
734      $to = $dest;
735    }
736    //echo "DB: copyfiles-single: file copied is:$files\n";
737    //echo "DB: copyfiles-single: to is:$to\n";
738    if(!copy($files,$to))
739    {
740      throw new Exception("Could not copy $file to $to");
741    }
742  }
743  return(TRUE);
744} // copyFiles
745
746
747/**
748 * \brief find the version of postgres and return major release and sub release.
749 * For example, if postgres is at 8.4.8, this function will return 8.4.
750 *
751 * @return boolean
752 */
753function findVerPsql()
754{
755  $version = NULL;
756
757  $last = exec('psql --version', $out, $rtn);
758  if($rtn != 0)
759  {
760    return(FALSE);
761  }
762  else
763  {
764    // isolate the version number and return it
765    list( , ,$ver) = explode(' ', $out[0]);
766    $version = substr($ver, 0, 3);
767  }
768  return($version);
769}
770
771/**
772 * \brief tune the kernel for this boot and successive boots
773 *
774 * returns void
775 */
776function tuneKernel()
777{
778  // check to see if we have already done this... so the sysctl.conf file doesn't
779  // end up with dup entries.
780  $grepCmd = 'grep shmmax=512000000 /etc/sysctl.conf /dev/null 2>&1';
781  $last = exec($grepCmd, $out, $rtn);
782  if($rtn == 0)   // kernel already configured
783  {
784    echo "NOTE: kernel already configured.\n";
785    return;
786  }
787  $cmd1 = "echo 512000000 > /proc/sys/kernel/shmmax";
788  $cmd2 = "echo 'kernel.shmmax=512000000' >> /etc/sysctl.conf";
789  // Tune the kernel
790  $last1 = exec($cmd1, $cmd1Out, $rtn1);
791  if ($rtn1 != 0)
792  {
793    echo "Fatal! Could not set kernel shmmax in /proc/sys/kernel/shmmax\n";
794  }
795  $last2 = exec($cmd2, $cmd2Out, $rtn2);
796  // make it permanent
797  if ($rtn2 != 0)
798  {
799    echo "Fatal! Could not turn kernel.shmmax in /etc/sysctl.conf\n";
800  }
801  return;
802} // tuneKernel
803
804/**
805 * \brief check to see if fossology is configured into apache.  If not copy the
806 * config file and configure it.  Restart apache if configured.
807 *
808 * @param string $osType type of the os, e.g. Debian, Ubuntu, Red, Fedora
809 *
810 * @return boolean
811 */
812
813function configApache2($osType)
814{
815  if(empty($osType))
816  {
817    return(FALSE);
818  }
819  switch ($osType) {
820    case 'Ubuntu':
821    case 'Debian':
822      if(is_link('/etc/apache2/conf.d/fossology'))
823      {
824        break;
825      }
826      else
827      {
828        // copy config file, create sym link
829        if(!copy('../dataFiles/pkginstall/fo-apache.conf', '/etc/fossology/fo-apache.conf'))
830        {
831          echo "FATAL!, Cannot configure fossology into apache2\n";
832          return(FALSE);
833        }
834        if(!symlink('/etc/fossology/fo-apache.conf','/etc/apache2/conf.d/fossology'))
835        {
836          echo "FATAL! Could not create symlink in /etc/apache2/conf.d/ for fossology\n";
837          return(FALSE);
838        }
839        // restart apache so changes take effect
840        if(!restart('apache2'))
841        {
842          echo "Erorr! Could not restart apache2, please restart by hand\n";
843          return(FALSE);
844        }
845      }
846      break;
847    case 'Red':
848      // copy config file, no symlink needed for redhat
849      if(!copy('../dataFiles/pkginstall/fo-apache.conf', '/etc/httpd/conf.d/fossology.conf'))
850      {
851        echo "FATAL!, Cannot configure fossology into apache2\n";
852        return(FALSE);
853      }
854      // restart apapche so changes take effect
855      if(!restart('httpd'))
856      {
857        echo "Erorr! Could not restart httpd, please restart by hand\n";
858        return(FALSE);
859      }
860      break;
861    default:
862      ;
863      break;
864  }
865  return(TRUE);
866} // configApache2
867
868/**
869 * \brief config a debian based system to install fossology.
870 *
871 * copy postgres, php config files so that fossology can run.
872 *
873 * @param string $osType either Debian or Ubuntu
874 * @param string $osVersion the particular version to install
875 *
876 * @return boolean
877 */
878function configDebian($osType, $osVersion)
879{
880  if(empty($osType))
881  {
882    return(FALSE);
883  }
884  if(empty($osVersion))
885  {
886    return(FALSE);
887  }
888
889  // based on type read the appropriate ini file.
890
891  //echo "DB:configD: osType is:$osType\n";
892  //echo "DB:configD: osversion is:$osVersion\n";
893
894  // can't check in pg_hba.conf as it shows HP's firewall settings, get it
895  // internally
896
897  // No need to update pg_hba.conf file
898  //getPGhba('../dataFiles/pkginstall/debian/6/pg_hba.conf');
899
900  //$debPath = '../dataFiles/pkginstall/debian/6/';
901
902  //$psqlFiles = array(
903  //$debPath . 'pg_hba.conf',
904  //$debPath . 'postgresql.conf');
905
906  switch ($osVersion)
907  {
908    case '6.0':
909      echo "debianConfig got os version 6.0!\n";
910      // copy config files
911      /*
912      * Change the structure of data files:
913      * e.g. debian/5/pg_hba..., etc, all files that go with this version
914      *      debian/6/pg_hba....
915      *      and use a symlink for the 'codename' squeeze -> debian/6/
916      */
917      /*
918      try
919      {
920        copyFiles($psqlFiles, "/etc/postgresql/8.4/main");
921      }
922      catch (Exception $e)
923      {
924        echo "Failure: Could not copy postgres 8.4 config file\n";
925      }
926      try
927      {
928        copyFiles($debPath . 'cli-php.ini', '/etc/php5/cli/php.ini');
929      } catch (Exception $e)
930      {
931        echo "Failure: Could not copy php.ini to /etc/php5/cli/php.ini\n";
932        return(FALSE);
933      }
934      try
935      {
936        copyFiles($debPath . 'apache2-php.ini', '/etc/php5/apache2/php.ini');
937      } catch (Exception $e)
938      {
939        echo "Failure: Could not copy php.ini to /etc/php5/apache2/php.ini\n";
940        return(FALSE);
941      }
942      */
943      break;
944    case '10.04.3':
945    case '11.04':
946    case '11.10':
947      echo "debianConfig got os version $osVersion!\n";
948      /*
949      try
950      {
951        copyFiles($psqlFiles, "/etc/postgresql/8.4/main");
952      }
953      catch (Exception $e)
954      {
955        echo "Failure: Could not copy postgres 8.4 config file\n";
956      }
957      try
958      {
959        copyFiles($debPath . 'cli-php.ini', '/etc/php5/cli/php.ini');
960      } catch (Exception $e)
961      {
962        echo "Failure: Could not copy php.ini to /etc/php5/cli/php.ini\n";
963        return(FALSE);
964      }
965      try
966      {
967        copyFiles($debPath . 'apache2-php.ini', '/etc/php5/apache2/php.ini');
968      } catch (Exception $e)
969      {
970        echo "Failure: Could not copy php.ini to /etc/php5/apache2/php.ini\n";
971        return(FALSE);
972      }
973       */
974      break;
975    case '12.04.1':
976    case '12.04.2':
977    case '12.04.3':
978    case '12.04.4':
979      echo "debianConfig got os version $osVersion!\n";
980      echo "Old PHPunit installation with PEAR is deprecated, it is now done with composer.\n";
981      echo "To install composer type:\n";
982      echo "curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composer\n ";
983      break;
984    case '12.10':
985      echo "debianConfig got os version $osVersion!\n";
986      //postgresql-9.1 can't use 8.4 conf file
987      /*
988      try
989      {
990        copyFiles($psqlFiles, "/etc/postgresql/9.1/main");
991      }
992      catch (Exception $e)
993      {
994        echo "Failure: Could not copy postgres 9.1 config file\n";
995      }
996      */
997      /*
998      try
999      {
1000        copyFiles($debPath . 'cli-php.ini', '/etc/php5/cli/php.ini');
1001      } catch (Exception $e)
1002      {
1003        echo "Failure: Could not copy php.ini to /etc/php5/cli/php.ini\n";
1004        return(FALSE);
1005      }
1006      try
1007      {
1008        copyFiles($debPath . 'apache2-php.ini', '/etc/php5/apache2/php.ini');
1009      } catch (Exception $e)
1010      {
1011        echo "Failure: Could not copy php.ini to /etc/php5/apache2/php.ini\n";
1012        return(FALSE);
1013      }
1014      */
1015      break;
1016    default:
1017      return(FALSE);     // unsupported debian version
1018      break;
1019  }
1020  /*
1021  // restart apache and postgres so changes take effect
1022  if(!restart('apache2'))
1023  {
1024    echo "Erorr! Could not restart apache2, please restart by hand\n";
1025    return(FALSE);
1026  }
1027  // Get the postrgres version so the correct file is used.
1028  $pName = 'postgresql';
1029  if($osVersion == '10.04.3')
1030  {
1031    $ver = findVerPsql();
1032    //echo "DB: returned version is:$ver\n";
1033    $pName = 'postgresql-' . $ver;
1034  }
1035  echo "DB pName is:$pName\n";
1036  if(!restart($pName))
1037  {
1038    echo "Erorr! Could not restart $pName, please restart by hand\n";
1039    return(FALSE);
1040  }
1041  */
1042  return(TRUE);
1043}  // configDebian
1044
1045/**
1046 * \brief copy configuration files and restart apache and postgres
1047 *
1048 * @param string $osType
1049 * @param string $osVersion
1050 * @return boolean
1051 */
1052function configRhel($osType, $osVersion)
1053{
1054  if(empty($osType))
1055  {
1056    return(FALSE);
1057  }
1058  if(empty($osVersion))
1059  {
1060    return(FALSE);
1061  }
1062
1063  $rpmPath = '../dataFiles/pkginstall/redhat/6.x';
1064  // getPGhba($rpmPath . '/pg_hba.conf');
1065  $psqlFiles = array(
1066  //$rpmPath . '/pg_hba.conf',
1067  $rpmPath . '/postgresql.conf');
1068  // fossology tweaks the postgres files so the packages work....  don't copy
1069  /*
1070  try
1071  {
1072  copyFiles($psqlFiles, "/var/lib/pgsql/data/");
1073  }
1074  catch (Exception $e)
1075  {
1076  echo "Failure: Could not copy postgres 8.4 config files\n";
1077  }
1078  */
1079  try
1080  {
1081    copyFiles($rpmPath . '/php.ini', '/etc/php.ini');
1082  } catch (Exception $e)
1083  {
1084    echo "Failure: Could not copy php.ini to /etc/php.ini\n";
1085    return(FALSE);
1086  }
1087  // restart postgres, postgresql conf didn't change do not restart
1088  /*
1089  $pName = 'postgresql';
1090  if(!restart($pName))
1091  {
1092    echo "Erorr! Could not restart $pName, please restart by hand\n";
1093    return(FALSE);
1094  }
1095  */
1096  return(TRUE);
1097} // configRhel
1098
1099/**
1100 * \brief config yum on a redhat based system to install fossology.
1101 *
1102 * Copies the Yum configuration file for fossology to
1103 *
1104 * @param object $objRef, a reference to the ConfigSys object
1105 *
1106 * @return boolean
1107 */
1108function configYum($objRef)
1109{
1110  if(!is_object($objRef))
1111  {
1112    return(FALSE);
1113  }
1114  if(empty($objRef->yum))
1115  {
1116    echo "FATAL, no yum install line to install\n";
1117    return(FALSE);
1118  }
1119
1120  $RedFedRepo = 'redfed-fossology.repo';   // name of generic repo file.
1121  // replace the baseurl line with the current one.
1122  $n = "../dataFiles/pkginstall/" . $RedFedRepo;
1123  $fcont = file_get_contents($n);
1124  //if(!$fcont);
1125  //{
1126  //echo "FATAL! could not read repo file $n\n";
1127  //exit(1);
1128  //}
1129  //echo "DB: contents is:\n$fcont\n";
1130  $newRepo = preg_replace("/baseurl=(.*)?/", 'baseurl=' . $objRef->yum, $fcont,-1, $cnt);
1131  // write the file, fix below to copy the correct thing...
1132  if(!($written = file_put_contents("../dataFiles/pkginstall/" . $RedFedRepo, $newRepo)))
1133  {
1134    echo "FATAL! could not write repo file $RedFedRepo\n";
1135    exit(1);
1136  }
1137  // coe plays with yum stuff, check if yum.repos.d exists and if not create it.
1138  if(is_dir('/etc/yum.repos.d'))
1139  {
1140    copyFiles("../dataFiles/pkginstall/" . $RedFedRepo, '/etc/yum.repos.d/fossology.repo');
1141  }
1142  else
1143  {
1144    // create the dir and then copy
1145    if(!mkdir('/etc/yum.repos.d'))
1146    {
1147      echo "FATAL! could not create yum.repos.d\n";
1148      return(FALSE);
1149    }
1150    copyFiles("../dataFiles/pkginstall/" . $RedFedRepo, '/etc/yum.repos.d/fossology.repo');
1151  }
1152  //print_r($objRef);
1153  if ($objRef->osFlavor == 'RedHat')
1154  {
1155     $last = exec("yum -y install wget", $out, $rtn);
1156     if($rtn != 0)
1157     {
1158       echo "FATAL! install EPEL repo fail\n";
1159       echo "transcript is:\n";print_r($out) . "\n";
1160       return(FALSE);
1161     }
1162     /* comment out the epel repo installation
1163     $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);
1164     if($rtn != 0)
1165     {
1166       echo "FATAL! install EPEL repo fail\n";
1167       echo "transcript is:\n";print_r($out) . "\n";
1168       return(FALSE);
1169     }
1170     $last = exec("rpm -ivh epel-release-6-8.noarch.rpm", $out, $rtn);
1171     if($rtn != 0)
1172     {
1173       echo "FATAL! install EPEL repo fail\n";
1174       echo "transcript is:\n";print_r($out) . "\n";
1175       return(FALSE);
1176     }*/
1177     $last = exec("yum -y install php-phpunit-PHPUnit", $out, $rtn);
1178     if($rtn != 0)
1179     {
1180       echo "FATAL! install PHPUnit fail\n";
1181       echo "transcript is:\n";print_r($out) . "\n";
1182       return(FALSE);
1183     }
1184  }
1185  return(TRUE);
1186}  // configYum
1187
1188/**
1189 * \brief restart the application passed in, so any config changes will take
1190 * affect.  Assumes application is restartable via /etc/init.d/<script>.
1191 * The application passed in should match the script name in /etc/init.d
1192 *
1193 * @param string $application the application to restart. The application passed
1194 *  in should match the script name in /etc/init.d
1195 *
1196 *  @return boolen
1197 */
1198function restart($application)
1199{
1200  if(empty($application))
1201  {
1202    return(FALSE);
1203  }
1204
1205  $last = exec("/etc/init.d/$application restart 2>&1", $out, $rtn);
1206  if($rtn != 0)
1207  {
1208    echo "FATAL! could not restart $application\n";
1209    echo "transcript is:\n";print_r($out) . "\n";
1210    return(FALSE);
1211  }
1212  return(TRUE);
1213} // restart
1214
1215/**
1216 * \brief start the application
1217 * Assumes application is restartable via /etc/init.d/<script>.
1218 * The application passed in should match the script name in /etc/init.d
1219 *
1220 * @param string $application the application to start. The application passed
1221 *  in should match the script name in /etc/init.d
1222 *
1223 *  @return boolen
1224 */
1225function start($application)
1226{
1227  if(empty($application))
1228  {
1229    return(FALSE);
1230  }
1231
1232  echo "Starting $application ...\n";
1233  $last = exec("/etc/init.d/$application stop 2>&1", $out, $rtn);
1234  if($rtn != 0)
1235  {
1236    echo "FATAL! could not start $application\n";
1237    echo "transcript is:\n";print_r($out) . "\n";
1238    return(FALSE);
1239  }
1240  $last = exec("/etc/init.d/$application start 2>&1", $out, $rtn);
1241  if($rtn != 0)
1242  {
1243    echo "FATAL! could not start $application\n";
1244    echo "transcript is:\n";print_r($out) . "\n";
1245    return(FALSE);
1246  }
1247  return(TRUE);
1248} // start
1249/**
1250 * \brief stop the application
1251 * Assumes application is restartable via /etc/init.d/<script>.
1252 * The application passed in should match the script name in /etc/init.d
1253 *
1254 * @param string $application the application to stop. The application passed
1255 *  in should match the script name in /etc/init.d
1256 *
1257 *  @return boolen
1258 */
1259function stop($application)
1260{
1261  if(empty($application))
1262  {
1263    return(FALSE);
1264  }
1265
1266  $last = exec("/etc/init.d/$application stop 2>&1", $out, $rtn);
1267  if($rtn != 0)
1268  {
1269    echo "FATAL! could not stop $application\n";
1270    echo "transcript is:\n";print_r($out) . "\n";
1271    return(FALSE);
1272  }
1273  return(TRUE);
1274} // stop
1275
1276/**
1277 * \brief wget the pg_hba.conf file and place it in $destPath
1278 *
1279 * @param string $destPath the full path to the destination
1280 * @return boolean, TRUE on success, FALSE otherwise.
1281 *
1282 */
1283function getPGhba($destPath)
1284{
1285  if(empty($destPath))
1286  {
1287    return(FALSE);
1288  }
1289  $wcmd = "wget -q -O $destPath " .
1290    "http://fonightly.usa.hp.com/testfiles/pg_hba.conf ";
1291
1292  $last = exec($wcmd, $wOut, $wRtn);
1293  if($wRtn != 0)
1294  {
1295    echo "Error, could not download pg_hba.conf file, pleases configure by hand\n";
1296    echo "wget output is:\n" . implode("\n",$wOut) . "\n";
1297    return(FALSE);
1298  }
1299  return(TRUE);
1300}
1301?>
1302