public function xSaveAction()
{
//TODO: Think about WebSockets
$this->request->defineParams(array('config' => array('type' => 'json')));
$farmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
$this->user->getPermissions()->validate($farmRole);
$behavior = $this->getParam('behavior');
$updateFarmRoleSettings = false;
if (!$farmRole->GetRoleObject()->hasBehavior($behavior)) {
throw new Exception("Behavior not assigned to this role");
}
$config = array();
foreach ($this->getParam('config') as $conf) {
if (!$config[$conf['configFile']]) {
$config[$conf['configFile']] = array();
}
if ($config[$conf['configFile']][$conf['key']] === null) {
$config[$conf['configFile']][$conf['key']] = $conf['value'];
} else {
throw new Exception("Variable {$conf['key']} from {$conf['configFile']} already defined. Please remove second definition");
}
}
// Update master
if ($this->getParam('masterServerId')) {
$dbServer = DBServer::LoadByID($this->getParam('masterServerId'));
$this->user->getPermissions()->validate($dbServer);
if ($dbServer->farmRoleId != $farmRole->ID) {
throw new Exception("Server not found");
}
if ($dbServer->status != SERVER_STATUS::RUNNING) {
throw new Exception("Master server is not running. Config cannot be applied.");
}
$this->setConfig($dbServer, $behavior, $config);
$servers = 0;
$savedServers = 1;
$updateFarmRoleSettings = true;
foreach ($farmRole->GetServersByFilter(array('status' => array(SERVER_STATUS::RUNNING, SERVER_STATUS::INIT))) as $server) {
$servers++;
try {
if ($server->serverId == $dbServer->serverId) {
continue;
}
$this->setConfig($server, $behavior, $config);
$savedServers++;
} catch (Exception $e) {
$warn[] = sprintf("Cannot update configuration on %s (%s): %s", $server->serverId, $server->remoteIp, $e->getMessage());
}
}
} else {
$updateFarmRoleSettings = true;
}
if ($updateFarmRoleSettings) {
$farmRole->SetServiceConfiguration($behavior, $config);
}
if (!$warn) {
$this->response->success(sprintf("Config successfully applied on %s of %s servers", $savedServers, $servers));
} else {
$this->response->warning(sprintf("Config was applied on %s of %s servers: %s", $savedServers, $servers, implode("\n", $warn)));
}
}