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