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