Skip to content

Instantly share code, notes, and snippets.

@taylorbyte
Last active May 10, 2017 15:15
Show Gist options
  • Save taylorbyte/26cb2cebc647114eb327fbd4e9507881 to your computer and use it in GitHub Desktop.
Save taylorbyte/26cb2cebc647114eb327fbd4e9507881 to your computer and use it in GitHub Desktop.
Generate Froxlor service or daemon config files (from distribution xml) for future stackscript to deploy Froxlor quicker.
<?php
class ConfigParser {
/**
* Holding the available services in the XML
* @var array
*/
private $services = array();
/**
* Store the parsed SimpleXMLElement for usage
* @var SimpleXMLElement
*/
private $xml;
/**
* Memorize if we already parsed the XML
* @var bool
*/
private $isparsed = false;
/**
* Name of the distribution this configuration is for
* @var string
*/
public $distributionName = '';
/**
* Codename of the distribution this configuration is for
* @var string
*/
public $distributionCodename = '';
/**
* Version of the distribution this configuration is for
* @var string
*/
public $distributionVersion = '';
/**
* Recommended editor
* @var string
*/
public $distributionEditor = '/bin/nano';
/**
* Show if this configuration is deprecated
* @var bool
*/
public $deprecated = false;
/**
* Constructor
*
* Initialize the XML - ConfigParser
* @param string $filename filename of the froxlor - configurationfile
* @return void
*/
public function __construct($filename) {
if (!is_readable($filename)) {
throw new Exception('File not readable');
}
$this->xml = simplexml_load_file($filename);
if ($this->xml === false) {
$error = '';
foreach(libxml_get_errors() as $error) {
$error .= "\t" . $error->message;
}
throw new \Exception($error);
}
}
/**
* Parse the XML and populate $this->services
* @return bool
*/
private function _parse() {
// We only want to parse the stuff one time
if ($this->isparsed == true) {
return true;
}
if (!is_array($this->xml->xpath('//distribution'))) {
throw new \Exception('Invalid XML, or not a distribution xml config file');
}
$confCount = 0;
$services = $this->xml->xpath('//service');
foreach ($services as $service) {
if ($service->getName() == 'comment') {
continue;
}
foreach ($service->daemon as $daemon) {
$daemonName = (string)$daemon['name'];
$fileCount = 0;
foreach ($daemon->file as $file) {
foreach($file->attributes() as $key => $value) {
switch ((string)$key) {
case "name":
$this->configs[$daemonName][$fileCount]['name'] = (string)$value;
break;
case "chmod":
$this->configs[$daemonName][$fileCount]['chmod'] = (string)$value;
break;
case "chown":
$this->configs[$daemonName][$fileCount]['chmod'] = (string)$value;
break;
}
$this->configs[$daemonName][$fileCount]['content'] = (string)$file->content[0];
}
$fileCount++;
}
}
foreach ($service->general as $general) {
foreach ($general->files as $files) {
$fileCount = 0;
foreach ($files->file as $file) {
foreach($file->attributes() as $key => $value) {
switch ((string)$key) {
case "name":
$this->configs['general'][$fileCount]['name'] = (string)$value;
break;
case "chmod":
$this->configs['general'][$fileCount]['chmod'] = (string)$value;
break;
case "chown":
$this->configs['general'][$fileCount]['chmod'] = (string)$value;
break;
}
$this->configs['general'][$fileCount]['content'] = (string)$file[$confCount]->content[0];
}
$fileCount++;
}
}
}
}
// Switch flag to indicate we parsed our data
$this->isparsed = true;
return true;
}
public function getConfigs() {
$this->_parse();
return $this->configs;
}
}
$ConfigParser = new ConfigParser('jessie.xml');
//print_r($ConfigParser->getFiles());
//print_r($ConfigParser->getDaemons());
//print_r($ConfigParser->getConfigs());
/*
* Input daemon names
* Extract config assets
* Write deployment bash script
*/
$daemons = array(
'nginx',
'bind',
'postfix_dovecot',
'dovecot_postfix',
'proftpd',
'cron',
'awstats',
'libnss',
'libnssextrausers',
'logrotate',
'fcgid',
'php-fpm',
'general'
);
echo "\nSELECTED CONFIGURATIONS:\n";
foreach ($ConfigParser->getConfigs() as $daemon => $config) {
if (in_array($daemon, $daemons)) {
echo "Daemon: ".$daemon."\n";
foreach ($config as $file) {
echo " File: ".basename($file['name'])."\n";
if (!is_dir(dirname('assets'.$file['name']))) {
mkdir(dirname('assets'.$file['name']),0750,true);
}
if (isset($file['content'])) {
file_put_contents('assets'.$file['name'], (string)$file['content']);
} else {
file_put_contents('assets'.$file['name'], "");
}
}
}
}
echo "\nSKIPPED CONFIGURATIONS:\n";
foreach ($ConfigParser->getConfigs() as $daemon => $config) {
if (!in_array($daemon, $daemons)) {
echo "Daemon: ".$daemon."\n";
foreach ($config as $file) {
echo " File: ".basename($file['name'])."\n";
}
}
}
/**********
**********/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment