summaryrefslogblamecommitdiffstats
path: root/application/controllers/GearmanController.php
blob: 9308a5ed4806d17ead8a9de79b8e0e9fdd80d85f (plain) (tree)
1
2
3
4
5
6
7
8
9
10

     





                                              

                                                      

                            

                                    
                                 
                                     


                              
                                                              
                                                                      

                                                                        
                                                                                        
                                                                      




                                                                                                                                                                                                                     
                         
                                                                                              
                                                                   
                                                                                  
                        

                                                                    
                 
         

                                     
         
 
         
 
                                        
         
                              





























                                     

                  
                                                 
 
                                                                                        









                                                                                                   
                                                                                                                   


                                                                                           
                         
                 


                                                
                                      
                                        
                                    
                              
                                                  
                                              
                                           
                                                                                            
                  

                        

                                                                                             





                                                             
         
 
                                          
                                            
                              
                                                  
                                           



                                                                                            

                                                                                                     






                                                             
 

                                                                              
                              
                                                  

                                                                                             
                                                                                             












                                                                                                           


                        

                                                                                                     






                                                             
 


                                                 
                                                 





                                                 
                                                                                                 










                                                             
                                                     





                                                 
                                                                                                 






                                                             
 
<?php

/*
 * Gearman - (Test)Controller
 * 
 * This controller was just for test purposes!
 */

class GearmanController extends Zend_Controller_Action
{
	protected $config;
	protected $pbs2host;
	protected $userIDsNamespace;
	protected $acl;
	protected $gearmanClient;
	protected $gearmanServerHost;

	public function init()
	{
		if (Zend_Auth::getInstance()->hasIdentity()) {
			$bootstrap = $this->getInvokeArg('bootstrap');
			$this->config = $bootstrap->getOptions();
			$this->pbs2host = $this->config['pbs2']['host'];
			$this->userIDsNamespace = Zend_Session::namespaceGet('userIDs');
			if(isset($this->userIDsNamespace['apikey'])) {
				$this->acl = new Poolctrl_Acl($this->pbs2host, $this->config['pbs2']['checkright'] . $this->userIDsNamespace['apikey'], 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
			}
			if(!$this->acl->checkRight('er')) {
				header('HTTP/1.0 403 No Right to run events');
				die();
			}
			$this->gearmanServerHost = $this->config['gearman']['server']['host'];
			$this->gearmanClient = new GearmanClient();
			$this->gearmanClient->addServer($this->gearmanServerHost);
		} else {
			$this->_helper->redirector('login', 'auth');
			return;
		}
	}

	public function indexAction()
	{

	}

	public function somepingAction()
	{
		$data = array(
		0 => '132.230.4.1',
		1 => '132.230.4.2',
		2 => '132.230.4.3',
		3 => '132.230.4.4',
		4 => '132.230.4.5',
		5 => '132.230.4.6',
		6 => '132.230.4.7',
		7 => '132.230.4.8',
		8 => '132.230.4.9',
		9 => '132.230.4.10',
		10 => '132.230.4.11',
		11 => '132.230.4.12',
		12 => '132.230.4.13',
		13 => '132.230.4.14',
		14 => '132.230.4.15',
		15 => '132.230.4.16',
		16 => '132.230.4.17',
		17 => '132.230.4.18',
		18 => '132.230.4.19',
		19 => '132.230.4.20',
		20 => '132.230.4.21',
		21 => '132.230.4.22',
		22 => '132.230.4.23',
		23 => '132.230.4.24',
		24 => '132.230.4.25',
		25 => '132.230.4.26',
		26 => '132.230.4.27',
		27 => '132.230.4.28',
		28 => '132.230.4.29',
		29 => '132.230.4.30',
		);

		$dataString = json_encode($data);

		$result = $this->gearmanClient->do("somePing", $dataString, 'somePing');
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$resultsExp = explode('; ', $result);
		if(is_array($resultsExp)) {
			foreach($resultsExp as $result) {
				$resultDecode = json_decode($result);
				$resultDecode->rawoutput = sprintf('%s', $resultDecode->rawoutput);
				$resultDecode->rawoutput = str_replace("\n", "<br />\t", $resultDecode->rawoutput);
				$resultDecode->alive = sprintf('%s', $resultDecode->alive);
				$resultDecode->rc = sprintf('%s', $resultDecode->rc);
				$results[] = $resultDecode;
			}
		}
		$this->view->results = $results;
	}

	public function bootAction() {
		$eventName = 'bootTest';
		$eventOS = 'testOS';
		$data = array(
			'eventName' => $eventName,
			'eventOS' => $eventOS,
			'clients' => array(
		0 => array('id' => '1', 'ip' =>'132.230.4.24', 'mac' =>'00:13:72:C7:FD:A1'),
		),
		);
			
		$dataString = json_encode($data);
		$result = $this->gearmanClient->do("boot", $dataString, 'boot' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}

	public function shutdownAction() {
		$eventName = 'shutdownTest';
		$data = array(
			'eventName' => $eventName,
			'clients' => array(
		0 => array('id' => '1', 'ip' =>'132.230.4.24', 'mac' =>'00:13:72:C7:FD:A1'),
		),
		);
			
		$dataString = json_encode($data);
		$result = $this->gearmanClient->do("shutdown", $dataString, 'shutdown' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}

	public function maintenanceAction() {
		$eventName = 'maintenance_pool-113'; //maintenance of pool-113
		$data = array(
			'eventName' => $eventName,
			'clients' => array(
		0 => array('id' => '1', 'ip' =>'132.230.4.121', 'mac' =>'00:1e:0b:a6:f8:19'),
		0 => array('id' => '2', 'ip' =>'132.230.4.122', 'mac' =>'00:1e:0b:a6:d5:de'),
		0 => array('id' => '3', 'ip' =>'132.230.4.123', 'mac' =>'00:1e:0b:a6:d5:8e'),
		0 => array('id' => '4', 'ip' =>'132.230.4.124', 'mac' =>'00:1e:0b:a6:f8:2c'),
		0 => array('id' => '5', 'ip' =>'132.230.4.125', 'mac' =>'00:1e:0b:a6:d5:e9'),
		0 => array('id' => '6', 'ip' =>'132.230.4.126', 'mac' =>'00:1e:0b:a6:d5:07'),
		0 => array('id' => '7', 'ip' =>'132.230.4.127', 'mac' =>'00:1e:0b:a6:'), // unvollständig
		0 => array('id' => '8', 'ip' =>'132.230.4.128', 'mac' =>'00:1e:0b:a6:'), // unvollständig
		0 => array('id' => '9', 'ip' =>'132.230.4.129', 'mac' =>'00:1e:0b:a6:'), // unvollständig
		0 => array('id' => '10', 'ip' =>'132.230.4.130', 'mac' =>'00:1e:0b:a6:f7:eb'),
		0 => array('id' => '11', 'ip' =>'132.230.4.131', 'mac' =>'00:1e:0b:a6:f8:a7'),
		0 => array('id' => '12', 'ip' =>'132.230.4.132', 'mac' =>'00:1e:0b:a6:d5:e4'),
		0 => array('id' => '13', 'ip' =>'132.230.4.133', 'mac' =>'00:1e:0b:a6:'), // unvollständig
		0 => array('id' => '14', 'ip' =>'132.230.4.134', 'mac' =>'00:1e:0b:a6:'), // unvollständig
		0 => array('id' => '15', 'ip' =>'132.230.4.135', 'mac' =>'00:1e:0b:a6:'), // unvollständig
		),
		);
			
		$dataString = json_encode($data);
		$result = $this->gearmanClient->do("shutdown", $dataString, 'shutdown' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}

	public function checkbootstatusAction() {
		$eventName = 'bootTest';
		$data = array(
			'type' => 'getBootState',
			'events' => array(
		0 => $eventName,
		),
		);

		$dataString = json_encode($data);
		$result = $this->gearmanClient->do("status", $dataString, 'status' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}

	public function checkshutdownstatusAction() {
		$eventName = 'shutdownTest';
		$data = array(
			'type' => 'getShutdownState',
			'events' => array(
		0 => $eventName,
		),
		);

		$dataString = json_encode($data);
		$result = $this->gearmanClient->do("status", $dataString, 'status' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}
}