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




                                                      

                            

                                    


                              
                                                              
                                                                      

                                                                        
                                                                                        


                                                                                                                                                                                                                     





                                                                     
         

                                     
         
 
         
 
                                        
         
                                 





























                                     



                                                  










                                                                                                   
                                                                                                                   


                                                                                           
                         
                 


                                                
                                      
                                        
                                    
                                
                                
                                 
                                                  
                                              

                                                    
                                           
                                                                                            
                  


                                                  
                                                                                           





                                                             
         
 
                                          
                                            
                                
                                


                                                    
                                                
                                           
                                                                                            











                                                                                                   
 









                                                 
                                                                                                 

















                                                             
                                                                                                 






                                                             
 
<?php

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

	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']);
			}
			$this->gearmanClient = new GearmanClient();
			$this->gearmanClient->addServer('127.0.0.1');
		} else {
			$this->_helper->redirector('login', 'auth');
			return;
		}
	}

	public function indexAction()
	{

	}

	public function somepingAction()
	{
		$ipArray = 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',
		);

		$ipString = json_encode($ipArray);

		$result = $this->gearmanClient->do("somePing", $ipString, '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';
		$updateRate = 1;
		$waitTime = 120;
		$clients = array(
			'eventName' => $eventName,
			'eventOS' => $eventOS,
			'updateRate' => $updateRate,
			'waitTime' => $waitTime,
			'clients' => array(
		0 => array('id' => '1', 'ip' =>'132.230.4.50', 'mac' =>'00:1b:78:3c:27:8a'),
		),
		);
			
		$ipString = json_encode($clients);
		$result = $this->gearmanClient->do("boot", $ipString, 'boot' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}

	public function shutdownAction() {
		$eventName = 'shutdownTest';
		$updateRate = 1;
		$waitTime = 120;
		$clients = array(
			'eventName' => $eventName,
			'updateRate' => $updateRate,
			'waitTime' => $waitTime,
			'clients' => array(
		0 => array('id' => '1', 'ip' =>'132.230.4.50', 'mac' =>'00:1b:78:3c:27:8a'),
		),
		);
			
		$ipString = json_encode($clients);
		$result = $this->gearmanClient->do("shutdown", $ipString, 'shutdown' . $eventName);
		if (! $this->gearmanClient->runTasks())
		{
			echo "ERROR " . $gmc->error() . "\n";
			exit;
		}
		$this->view->result = $result;
	}

	public function checkbootstatusAction() {
		$eventName = 'bootTest';
		$data = array(
			'type' => 'boot',
			'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' => 'shutdown',
			'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;
	}
}