diff options
Diffstat (limited to 'application/modules/backend/controllers/SystemController.php')
-rw-r--r-- | application/modules/backend/controllers/SystemController.php | 273 |
1 files changed, 273 insertions, 0 deletions
diff --git a/application/modules/backend/controllers/SystemController.php b/application/modules/backend/controllers/SystemController.php new file mode 100644 index 0000000..a13ed2a --- /dev/null +++ b/application/modules/backend/controllers/SystemController.php @@ -0,0 +1,273 @@ +<?php + +class Backend_SystemController extends Zend_Controller_Action +{ + + private $_msystems; + private $_mkernel; + private $_logger; + + public function init() + { + $this->_helper->layout->disableLayout(); + $this->_msystems = new Model_Systems(); + $this->_mkernels = new Model_Kernels(); + $this->_msources = new Model_Sources(); + + + $this->_logger = new Zend_Log(); + $w = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/debug.log'); + + $this->_logger->addWriter($w); + } + + public function indexAction() + { + // action body + } + + public function updateinitramfsAction() + { + // we don't need any view for json output + $this->_helper->viewRenderer->setNoRender(); + + $l = $this->_logger; + $adapter = new Zend_File_Transfer_Adapter_Http(); + $kernel = $this->getRequest()->getParam('kernel'); + + $s = $this->_mkernels->select(); + $s->where('kernel=?', $data['kernel']); + $r = $this->_mkernels->fetchAll($s)->toArray(); + + $id = $r[0]['id']; + + #TODO: bild in more checks + $targetpath = APPLICATION_PATH . "/../data/kernels/$kernel/"; + + if (!is_dir($targetpath)) + mkdir($targetpath, 0777, true); + + $adapter->setDestination($targetpath); + + #todo add to db + + if (!$adapter->receive()) { + $messages = $adapter->getMessages(); + foreach ($messages as $m) { + $l->crit($m); + } + } else { + $db_data = array( + 'initramfs_md5' => $adapter->getHash('md5', 'initramfsFile') + ); + $w = $this->_mkernels->getAdapter()->quoteInto('id = ?', $id); + $this->_mkernels->update($db_data, $w); + $l->info(sprintf( + "updated initramfs with following md5s:\n\tinitramfs:\t%s", + $adapter->getHash('md5', 'initramfsFile') + )); + } + } + + public function updatekernelAction() + { + // we don't need any view for json output + $this->_helper->viewRenderer->setNoRender(); + + $l = $this->_logger; + $adapter = new Zend_File_Transfer_Adapter_Http(); + $kernel = $this->getRequest()->getParam('kernel'); + + $s = $this->_mkernels->select(); + $s->where('kernel=?', $data['kernel']); + $r = $this->_mkernels->fetchAll($s)->toArray(); + + $id = $r[0]['id']; + + #TODO: bild in more checks + $targetpath = APPLICATION_PATH . "/../data/kernels/$kernel/"; + + if (!is_dir($targetpath)) + mkdir($targetpath, 0777, true); + + $adapter->setDestination($targetpath); + + #todo add to db + + if (!$adapter->receive()) { + $messages = $adapter->getMessages(); + foreach ($messages as $m) { + $l->crit($m); + } + } else { + $db_data = array( + 'kernel_md5' => $adapter->getHash('md5', 'kernelFile') + ); + $w = $this->_mkernels->getAdapter()->quoteInto('id = ?', $id); + $this->_mkernels->update($db_data, $w); + $l->info(sprintf( + "updated kernel with following md5s:\n\tkernel:\t%s", + $adapter->getHash('md5', 'kernelFile') + )); + } + } + + public function addkernelAction() + { + // we don't need any view for json output + $this->_helper->viewRenderer->setNoRender(); + + $l = $this->_logger; + $adapter = new Zend_File_Transfer_Adapter_Http(); + $kernel = $this->getRequest()->getParam('kernel'); + + #TODO: bild in more checks + $targetpath = APPLICATION_PATH . "/../data/kernels/$kernel/"; + + if (!is_dir($targetpath)) + mkdir($targetpath, 0777, true); + + $adapter->setDestination($targetpath); + + #todo add to db + + if (!$adapter->receive()) { + $messages = $adapter->getMessages(); + foreach ($messages as $m) { + $l->crit($m); + } + } else { + $db_data = array( + 'kernel' => $kernel, + 'kernel_md5' => $adapter->getHash('md5', 'kernelFile'), + 'initramfs_md5' => $adapter->getHash('md5', 'initramfsFile') + ); + $this->_mkernels->insert($db_data); + $l->info(sprintf( + "added kernel/initramfs with following md5s:\n\tkernel:\t\t%s\n\tinitramfs:\t%s", + $adapter->getHash('md5', 'kernelFile'), + $adapter->getHash('md5', 'initramfsFile') + )); + } + } + + public function syncAction() + { + $l = $this->_logger; + + // we don't need any view for json output + $this->_helper->viewRenderer->setNoRender(); + + // get data from slxconfig-demuxer + $data = $this->getRequest()->getParam('data'); + $data = json_decode(stripslashes($data), true); + + if (empty($data)) return; + + $source = $this->getRequest()->getServer('REMOTE_ADDR'); + + // compare md5sums with old values from db + $s = $this->_mkernels->select(); + $s->where('kernel=?', $data['kernel']); + $r = $this->_mkernels->fetchAll($s)->toArray(); + + $response = array(); + if (empty($r)) { + $response['getKernel'] = "fresh"; + $response['getInitramfs'] = "fresh"; + $l->info("request new kernel/initramfs pair"); + } else { + $r = $r[0]; + if ($data['kernel_md5'] == $r['kernel_md5']) { + $response['getKernel'] = "no"; + $l->info("kernel is still up to date"); + } else { + $response['getKernel'] = "update"; + $l->info("request updated kernel"); + } + if ($data['initramfs_md5'] == $r['initramfs_md5']) { + $response['getInitramfs'] = "no"; + $l->info("initramfs is still up to date"); + } else { + $response['getInitramfs'] = "update"; + $l->info("request updated initramfs"); + } + } + + echo(json_encode($response)); + + // add systems to db.. + if (!empty($data['systems'])) { + $s = $this->_msystems->select(); + $s->where('source=?', $source); + $r = $this->_msystems->fetchAll($s)->toArray(); + $externalIDs = array(); + if (!empty($r)) { + foreach ($r as $r_) { + $externalIDs[] = $r_['external_id']; + } + } + + foreach ($data['systems'] as $d) { + if (in_array($d['external-id'],$externalIDs)) { + $db_data = array( + 'name' => $d["label"], + 'external_id' => $d["external-id"], + 'description' => $d["description"], + 'kernel' => $d['vendor-os']['name'] . "/" . basename($d["kernel-file"]), + 'initrd' => $d['vendor-os']['name'] . "/" .$d["initramfs-name"], + 'append' => $d["append"], + 'source' => $source + ); + $w[] = $this->_msystems->getAdapter() + ->quoteInto('external_id = ?', $d["external-id"]); + $w[] = $this->_msystems->getAdapter() + ->quoteInto('source = ?', $source); + + $this->_msystems->update($db_data, $w); + $l->info(sprintf("updated system: %s", $d["external-id"])); + } else { + $db_data = array( + 'name' => $d["label"], + 'external_id' => $d["external-id"], + 'description' => $d["description"], + 'kernel' => $d['vendor-os']['name'] . "/" . basename($d["kernel-file"]), + 'initrd' => $d['vendor-os']['name'] . "/" .$d["initramfs-name"], + 'append' => $d["append"], + 'source' => $source + ); + $this->_msystems->insert($db_data); + $l->info(sprintf("added system: %s", $d["external-id"])); + } + } + } + + // add / modify systems table + $s = $this->_msources->select(); + $s->where('ip=?', $source); + $r = $this->_msources->fetchAll($s)->toArray(); + + if (!empty($r)) { + $db_data = array( + 'version' => $data['slxinfo'], + 'lastdemux' => mktime() + ); + $w = $this->_msources->getAdapter()->quoteInto('ip = ?', $source); + $this->_msources->update($db_data, $w); + } else { + $db_data = array( + 'ip' => $source, + 'version' => $data['slxinfo'], + 'lastdemux' => mktime() + ); + $this->_msources->insert($db_data); + } + + + } + + +} + + + |