mirror of https://github.com/MISP/MISP
Merge pull request #7725 from JakubOnderka/server-shell
chg: [internal] Simplify ServerShell codepull/7726/merge
commit
9afce6f2cf
|
@ -26,8 +26,8 @@ App::uses('AppModel', 'Model');
|
|||
*
|
||||
* @package app.Console.Command
|
||||
*/
|
||||
class AppShell extends Shell {
|
||||
|
||||
class AppShell extends Shell
|
||||
{
|
||||
public $tasks = array('ConfigLoad');
|
||||
|
||||
public function perform()
|
||||
|
|
|
@ -12,11 +12,9 @@ require_once 'AppShell.php';
|
|||
class ServerShell extends AppShell
|
||||
{
|
||||
public $uses = array('Server', 'Task', 'Job', 'User', 'Feed');
|
||||
public $tasks = array('ConfigLoad');
|
||||
|
||||
public function list()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$servers = $this->Server->find('all', [
|
||||
'fields' => ['Server.id', 'Server.name', 'Server.url'],
|
||||
'recursive' => 0
|
||||
|
@ -35,47 +33,31 @@ class ServerShell extends AppShell
|
|||
|
||||
public function listServers()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$res = ['servers'=>[]];
|
||||
$servers = $this->Server->find('all', [
|
||||
'fields' => ['Server.id', 'Server.name', 'Server.url'],
|
||||
'recursive' => 0
|
||||
]);
|
||||
foreach ($servers as $server)
|
||||
$res['servers'][] = $server['Server'];
|
||||
|
||||
echo json_encode($res) . PHP_EOL;
|
||||
$res = ['servers' => array_column($servers, 'Server')];
|
||||
echo $this->json($res) . PHP_EOL;
|
||||
}
|
||||
|
||||
public function test()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
if (empty($this->args[0])) {
|
||||
die('Usage: ' . $this->Server->command_line_functions['console_automation_tasks']['data']['Test'] . PHP_EOL);
|
||||
}
|
||||
|
||||
$serverId = intval($this->args[0]);
|
||||
$server = $this->Server->find('first', [
|
||||
'conditions' => ['Server.id' => $serverId],
|
||||
'recursive' => -1,
|
||||
]);
|
||||
if (!$server) {
|
||||
die("Server with ID $serverId doesn't exists.");
|
||||
}
|
||||
$server = $this->getServer($serverId);
|
||||
|
||||
$res = $this->Server->runConnectionTest($server, false);
|
||||
echo json_encode($res) . PHP_EOL;
|
||||
echo $this->json($res) . PHP_EOL;
|
||||
}
|
||||
|
||||
public function pullAll()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) {
|
||||
die('User ID do not match an existing user.' . PHP_EOL);
|
||||
}
|
||||
$user = $this->getUser($userId);
|
||||
|
||||
if (!empty($this->args[1])) {
|
||||
$technique = $this->args[1];
|
||||
|
@ -83,35 +65,32 @@ class ServerShell extends AppShell
|
|||
$technique = 'full';
|
||||
}
|
||||
|
||||
$servers = $this->Server->find('all', array(
|
||||
$servers = $this->Server->find('list', array(
|
||||
'conditions' => array('Server.pull' => 1),
|
||||
'recursive' => -1,
|
||||
'order' => 'Server.priority',
|
||||
'fields' => array('Server.name', 'Server.id'),
|
||||
'fields' => array('Server.id', 'Server.name'),
|
||||
));
|
||||
|
||||
foreach ($servers as $server) {
|
||||
foreach ($servers as $serverId => $serverName) {
|
||||
$jobId = CakeResque::enqueue(
|
||||
'default',
|
||||
'ServerShell',
|
||||
array('pull', $userId, $server['Server']['id'], $technique)
|
||||
array('pull', $user['id'], $serverId, $technique)
|
||||
);
|
||||
$this->out("Enqueued pulling from {$server['Server']['name']} server as job $jobId");
|
||||
$this->out("Enqueued pulling from $serverName server as job $jobId");
|
||||
}
|
||||
}
|
||||
|
||||
public function pull()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
if (empty($this->args[0]) || empty($this->args[1])) {
|
||||
die('Usage: ' . $this->Server->command_line_functions['console_automation_tasks']['data']['pull'] . PHP_EOL);
|
||||
}
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) {
|
||||
die('User ID do not match an existing user.' . PHP_EOL);
|
||||
}
|
||||
$user = $this->getUser($userId);
|
||||
$serverId = $this->args[1];
|
||||
$server = $this->getServer($serverId);
|
||||
if (!empty($this->args[2])) {
|
||||
$technique = $this->args[2];
|
||||
} else {
|
||||
|
@ -137,11 +116,6 @@ class ServerShell extends AppShell
|
|||
if (!empty($this->args[4]) && $this->args[4] === 'force') {
|
||||
$force = true;
|
||||
}
|
||||
$this->Server->id = $serverId;
|
||||
$server = $this->Server->read(null, $serverId);
|
||||
if (!$server) {
|
||||
die("Remote server with ID $serverId not found");
|
||||
}
|
||||
try {
|
||||
$result = $this->Server->pull($user, $serverId, $technique, $server, $jobId, $force);
|
||||
if (is_array($result)) {
|
||||
|
@ -160,14 +134,13 @@ class ServerShell extends AppShell
|
|||
|
||||
public function push()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
if (empty($this->args[0]) || empty($this->args[1])) {
|
||||
die('Usage: ' . $this->Server->command_line_functions['console_automation_tasks']['data']['push'] . PHP_EOL);
|
||||
}
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) die('Invalid user.' . PHP_EOL);
|
||||
$user = $this->getUser($userId);
|
||||
$serverId = $this->args[1];
|
||||
$server = $this->getServer($serverId);
|
||||
if (!empty($this->args[2])) {
|
||||
$jobId = $this->args[2];
|
||||
} else {
|
||||
|
@ -186,10 +159,7 @@ class ServerShell extends AppShell
|
|||
}
|
||||
$technique = empty($this->args[3]) ? 'full' : $this->args[3];
|
||||
$this->Job->read(null, $jobId);
|
||||
$server = $this->Server->read(null, $serverId);
|
||||
if (!$server) {
|
||||
die("Remote server with ID $serverId not found");
|
||||
}
|
||||
|
||||
App::uses('SyncTool', 'Tools');
|
||||
$syncTool = new SyncTool();
|
||||
$HttpSocket = $syncTool->setupHttpSocket($server);
|
||||
|
@ -213,43 +183,35 @@ class ServerShell extends AppShell
|
|||
|
||||
public function pushAll()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) {
|
||||
die('User ID do not match an existing user.' . PHP_EOL);
|
||||
}
|
||||
$user = $this->getUser($userId);
|
||||
|
||||
$servers = $this->Server->find('all', array(
|
||||
$technique = isset($this->args[1]) ? $this->args[1] : 'full';
|
||||
|
||||
$servers = $this->Server->find('list', array(
|
||||
'conditions' => array('Server.push' => 1),
|
||||
'recursive' => -1,
|
||||
'order' => 'Server.priority',
|
||||
'fields' => array('Server.name', 'Server.id'),
|
||||
'fields' => array('Server.id', 'Server.name'),
|
||||
));
|
||||
|
||||
foreach ($servers as $server) {
|
||||
foreach ($servers as $serverId => $serverName) {
|
||||
$jobId = CakeResque::enqueue(
|
||||
'default',
|
||||
'ServerShell',
|
||||
array('push', $userId, $server['Server']['id'], $technique)
|
||||
array('push', $user['id'], $serverId, $technique)
|
||||
);
|
||||
$this->out("Enqueued pushing from {$server['Server']['name']} server as job $jobId");
|
||||
$this->out("Enqueued pushing from $serverName server as job $jobId");
|
||||
}
|
||||
}
|
||||
|
||||
public function fetchFeed()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
if (empty($this->args[0]) || empty($this->args[1])) {
|
||||
die('Usage: ' . $this->Server->command_line_functions['console_automation_tasks']['data']['Fetch feeds as local data'] . PHP_EOL);
|
||||
}
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) {
|
||||
echo 'Invalid user.';
|
||||
die();
|
||||
}
|
||||
$user = $this->getUser($userId);
|
||||
$feedId = $this->args[1];
|
||||
if (!empty($this->args[2])) {
|
||||
$jobId = $this->args[2];
|
||||
|
@ -311,13 +273,11 @@ class ServerShell extends AppShell
|
|||
|
||||
public function cacheServer()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
if (empty($this->args[0]) || empty($this->args[1])) {
|
||||
die('Usage: ' . $this->Server->command_line_functions['console_automation_tasks']['data']['cacheServer'] . PHP_EOL);
|
||||
}
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) die('Invalid user.' . PHP_EOL);
|
||||
$user = $this->getUser($userId);
|
||||
$scope = $this->args[1];
|
||||
if (!empty($this->args[2])) {
|
||||
$jobId = $this->args[2];
|
||||
|
@ -348,41 +308,33 @@ class ServerShell extends AppShell
|
|||
|
||||
public function cacheServerAll()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) {
|
||||
die('User ID do not match an existing user.' . PHP_EOL);
|
||||
}
|
||||
$user = $this->getUser($userId);
|
||||
|
||||
$servers = $this->Server->find('all', array(
|
||||
$servers = $this->Server->find('list', array(
|
||||
'conditions' => array('Server.pull' => 1),
|
||||
'recursive' => -1,
|
||||
'order' => 'Server.priority',
|
||||
'fields' => array('Server.name', 'Server.id'),
|
||||
'fields' => array('Server.id', 'Server.name'),
|
||||
));
|
||||
|
||||
foreach ($servers as $server) {
|
||||
foreach ($servers as $serverId => $serverName) {
|
||||
$jobId = CakeResque::enqueue(
|
||||
'default',
|
||||
'ServerShell',
|
||||
array('cacheServer', $userId, $server['Server']['id'])
|
||||
array('cacheServer', $user['id'], $serverId)
|
||||
);
|
||||
$this->out("Enqueued cacheServer from {$server['Server']['name']} server as job $jobId");
|
||||
$this->out("Enqueued cacheServer from {$serverName} server as job $jobId");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function cacheFeed()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
if (empty($this->args[0]) || empty($this->args[1])) {
|
||||
die('Usage: ' . $this->Server->command_line_functions['console_automation_tasks']['data']['Cache feeds for quick lookups'] . PHP_EOL);
|
||||
}
|
||||
$userId = $this->args[0];
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) die('Invalid user.' . PHP_EOL);
|
||||
$user = $this->getUser($userId);
|
||||
$scope = $this->args[1];
|
||||
if (!empty($this->args[2])) {
|
||||
$jobId = $this->args[2];
|
||||
|
@ -429,7 +381,6 @@ class ServerShell extends AppShell
|
|||
|
||||
public function enqueuePull()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$timestamp = $this->args[0];
|
||||
$userId = $this->args[1];
|
||||
$taskId = $this->args[2];
|
||||
|
@ -488,7 +439,6 @@ class ServerShell extends AppShell
|
|||
|
||||
public function enqueueFeedFetch()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$timestamp = $this->args[0];
|
||||
$userId = $this->args[1];
|
||||
$taskId = $this->args[2];
|
||||
|
@ -534,7 +484,6 @@ class ServerShell extends AppShell
|
|||
|
||||
public function enqueueFeedCache()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$timestamp = $this->args[0];
|
||||
$userId = $this->args[1];
|
||||
$taskId = $this->args[2];
|
||||
|
@ -587,7 +536,6 @@ class ServerShell extends AppShell
|
|||
|
||||
public function enqueuePush()
|
||||
{
|
||||
$this->ConfigLoad->execute();
|
||||
$timestamp = $this->args[0];
|
||||
$taskId = $this->args[1];
|
||||
$userId = $this->args[2];
|
||||
|
@ -623,4 +571,33 @@ class ServerShell extends AppShell
|
|||
$this->Task->id = $task['Task']['id'];
|
||||
$this->Task->saveField('message', count($servers) . ' job(s) completed at ' . date('d/m/Y - H:i:s') . '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $userId
|
||||
* @return array
|
||||
*/
|
||||
private function getUser($userId)
|
||||
{
|
||||
$user = $this->User->getAuthUser($userId);
|
||||
if (empty($user)) {
|
||||
$this->error('User ID do not match an existing user.');
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $serverId
|
||||
* @return array
|
||||
*/
|
||||
private function getServer($serverId)
|
||||
{
|
||||
$server = $this->Server->find('first', [
|
||||
'conditions' => ['Server.id' => $serverId],
|
||||
'recursive' => -1,
|
||||
]);
|
||||
if (!$server) {
|
||||
$this->error("Server with ID $serverId doesn't exists.");
|
||||
}
|
||||
return $server;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue