loadByName() public static méthode

public static loadByName ( unknown_type $name ) : Scalr_Role_Behavior
$name unknown_type
Résultat Scalr_Role_Behavior
 public function ListFarmRoleParamsJson()
 {
     $farmRoleId = $this->GetArg("farm-role-id");
     if (!$farmRoleId) {
         throw new Exception("'farm-role-id' required");
     }
     $dbFarmRole = DBFarmRole::LoadByID($farmRoleId);
     if ($dbFarmRole->FarmID != $this->DBServer->farmId) {
         throw new Exception("You can request this information ONLY for roles within server farm");
     }
     $result = new stdClass();
     // Base configuration
     if ($this->DBServer->farmRoleId == $farmRoleId) {
         $data = Scalr_Role_Behavior::loadByName(ROLE_BEHAVIORS::BASE)->getBaseConfiguration($this->DBServer);
         foreach ((array) $data as $k => $v) {
             $result->{$k} = $v;
         }
     }
     $role = $dbFarmRole->GetRoleObject();
     $behaviors = $role->getBehaviors();
     foreach ($behaviors as $behavior) {
         $data = null;
         if ($behavior == ROLE_BEHAVIORS::MONGODB || $behavior == ROLE_BEHAVIORS::CHEF || $behavior == ROLE_BEHAVIORS::HAPROXY || $behavior == ROLE_BEHAVIORS::NGINX || $behavior == ROLE_BEHAVIORS::RABBITMQ || $behavior == ROLE_BEHAVIORS::APACHE || $behavior == ROLE_BEHAVIORS::VPC_ROUTER) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($data === null) {
             if ($behavior == ROLE_BEHAVIORS::MYSQL) {
                 $data = new stdClass();
                 $data->logFile = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_LOG_FILE);
                 $data->logPos = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_LOG_POS);
                 $data->rootPassword = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_ROOT_PASSWORD);
                 $data->replPassword = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_REPL_PASSWORD);
                 $data->statPassword = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_STAT_PASSWORD);
                 $data->replicationMaster = (int) $this->DBServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER);
             } else {
                 try {
                     $dbMsrInfo = Scalr_Db_Msr_Info::init($dbFarmRole, $this->DBServer, $behavior);
                     $data = $dbMsrInfo->getMessageProperties();
                 } catch (Exception $e) {
                 }
             }
         }
         if ($data) {
             $result->{$behavior} = $data;
         }
     }
     return $result;
 }
Exemple #2
0
 public function statusAction()
 {
     $this->request->defineParams(array('farmId' => array('type' => 'int'), 'farmRoleId' => array('type' => 'int'), 'type'));
     $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
     $this->user->getPermissions()->validate($dbFarm);
     if ($this->getParam('farmRoleId')) {
         $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
         if ($dbFarmRole->FarmID != $dbFarm->ID) {
             throw new Exception("Role not found");
         }
     } elseif ($this->getParam('type')) {
         foreach ($dbFarm->GetFarmRoles() as $sDbFarmRole) {
             if ($sDbFarmRole->GetRoleObject()->hasBehavior($this->getParam('type'))) {
                 $dbFarmRole = $sDbFarmRole;
                 break;
             }
         }
         if (!$dbFarmRole) {
             throw new Exception("Role not found");
         }
     } else {
         throw new Scalr_UI_Exception_NotFound();
     }
     $data = array('farmRoleId' => $dbFarmRole->ID, 'farmHash' => $dbFarm->Hash, 'pmaAccessConfigured' => false, 'staticDnsSupported' => \Scalr::config('scalr.dns.static.enabled'));
     $data['backupsNotSupported'] = in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::CLOUDSTACK, SERVER_PLATFORMS::IDCF));
     if ($dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_PMA_USER)) {
         $data['pmaAccessConfigured'] = true;
     } else {
         $errmsg = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_PMA_REQUEST_ERROR);
         if (!$errmsg) {
             $time = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_PMA_REQUEST_TIME);
             if ($time) {
                 if ($time + 3600 < time()) {
                     $data['pmaAccessError'] = _("Scalr didn't receive auth info from MySQL instance. Please check that MySQL running and Scalr has access to it.");
                 } else {
                     $data['pmaAccessSetupInProgress'] = true;
                 }
             }
         } else {
             $data['pmaAccessError'] = $errmsg;
         }
     }
     //TODO: Legacy code. Move to DB_MSR
     if ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL)) {
         $data['dbType'] = Scalr_Db_Msr::DB_TYPE_MYSQL;
         $data['dtLastBundle'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BUNDLE_TS) ? Scalr_Util_DateTime::convertTz((int) $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BUNDLE_TS), 'd M Y \\a\\t H:i:s') : 'Never';
         $data['dtLastBackup'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BCP_TS) ? Scalr_Util_DateTime::convertTz((int) $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LAST_BCP_TS), 'd M Y \\a\\t H:i:s') : 'Never';
         $data['additionalInfo']['MasterUsername'] = '******';
         $data['additionalInfo']['MasterPassword'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_ROOT_PASSWORD);
         $slaveNumber = 0;
         foreach ($dbFarmRole->GetServersByFilter() as $dbServer) {
             if ($dbServer->status != SERVER_STATUS::RUNNING) {
                 //TODO:
                 continue;
             }
             if ($dbServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER) == 1) {
                 $data['isBundleRunning'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_IS_BUNDLE_RUNNING);
                 $data['bundleServerId'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BUNDLE_SERVER_ID);
             }
             $data['isBackupRunning'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_IS_BCP_RUNNING);
             $data['backupServerId'] = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_BCP_SERVER_ID);
             try {
                 $isCloudstack = in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::CLOUDSTACK, SERVER_PLATFORMS::IDCF));
                 $isMaster = $dbServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER) == 1;
                 if (!$isCloudstack) {
                     $rStatus = $this->getMySqlReplicationStatus($isMaster ? 'MASTER' : 'SLAVE', $dbServer->remoteIp, 'scalr_stat', $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_STAT_PASSWORD));
                 }
                 if ($isMaster) {
                     $MasterPosition = $rStatus['Position'];
                     $master_ip = $dbServer->remoteIp;
                     $master_iid = $dbServer->serverId;
                 } else {
                     $num = ++$slaveNumber;
                     $SlavePosition = $rStatus['Exec_Master_Log_Pos'];
                 }
                 $d = array("serverId" => $dbServer->serverId, "localIp" => $dbServer->localIp, "remoteIp" => $dbServer->remoteIp, "replicationRole" => $isMaster ? 'Master' : "Slave #{$num}");
                 if (!$isCloudstack) {
                     $d['data'] = $rStatus;
                     $d['masterPosition'] = $MasterPosition;
                     $d['slavePosition'] = $SlavePosition;
                 }
                 $data["replicationStatus"][] = $d;
             } catch (Exception $e) {
                 $data["replicationStatus"][] = array("serverId" => $dbServer->serverId, "localIp" => $dbServer->localIp, "remoteIp" => $dbServer->remoteIp, "error" => $e->msg ? $e->msg : $e->getMessage(), "replicationRole" => $isMaster ? 'Master' : 'Slave');
             }
         }
     } else {
         $data['dbType'] = $dbFarmRole->GetRoleObject()->getDbMsrBehavior();
         if (!$data['dbType']) {
             $this->response->failure("Unknown db type");
         }
         $behavior = Scalr_Role_Behavior::loadByName($data['dbType']);
         $masterServer = $behavior->getMasterServer($dbFarmRole);
         // Get Stoarge usage
         $size = array('total' => -1, 'used' => -1, 'free' => -1);
         if ($masterServer) {
             try {
                 $port = $masterServer->GetProperty(SERVER_PROPERTIES::SZR_API_PORT);
                 if (!$port) {
                     $port = 8010;
                 }
                 if ($data['dbType'] == ROLE_BEHAVIORS::REDIS) {
                     $mpoint = '/mnt/redisstorage';
                 } elseif ($data['dbType'] == ROLE_BEHAVIORS::POSTGRESQL) {
                     $mpoint = '/mnt/pgstorage';
                 } else {
                     $mpoint = '/mnt/dbstorage';
                 }
                 $usage = (array) $masterServer->scalarizr->system->statvfs(array($mpoint));
                 $size = (array) $usage[$mpoint];
                 if ($size['total']) {
                     $size['used'] = $size['total'] - $size['free'];
                     // Convert KB to GB
                     foreach ($size as $k => $v) {
                         $size[$k] = round($v / 1024 / 1024, 2);
                     }
                 }
             } catch (Exception $e) {
                 $this->response->debugException($e);
             }
         }
         $data['storage'] = array('engine' => $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_STORAGE_ENGINE), 'id' => $dbFarmRole->GetSetting(Scalr_Db_Msr::VOLUME_ID) ? $dbFarmRole->GetSetting(Scalr_Db_Msr::VOLUME_ID) : '', 'fs' => $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_STORAGE_FSTYPE), 'size' => $size);
         switch ($data['storage']['engine']) {
             case MYSQL_STORAGE_ENGINE::EBS:
                 $data['storage']['engineName'] = 'Single EBS volume';
                 break;
             case MYSQL_STORAGE_ENGINE::RAID_EBS:
                 $data['storage']['engineName'] = sprintf('RAID %s on %s EBS volumes', $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_STORAGE_RAID_LEVEL), $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_STORAGE_RAID_DISKS_COUNT));
                 break;
             case MYSQL_STORAGE_ENGINE::LVM:
                 $data['storage']['engineName'] = 'LVM on ephemeral device(s)';
                 break;
             case MYSQL_STORAGE_ENGINE::EPH:
                 $data['storage']['engineName'] = 'Ephemeral device';
                 break;
             case MYSQL_STORAGE_ENGINE::CSVOL:
                 $data['storage']['engineName'] = 'Single Cloudstack volume';
                 break;
             default:
                 $data['storage']['engineName'] = $data['storage']['engine'];
                 break;
         }
         $data['additionalInfo']['MasterUsername'] = '******';
         if ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::POSTGRESQL)) {
             $data['additionalInfo']['MasterPassword'] = $dbFarmRole->GetSetting(Scalr_Db_Msr_Postgresql::ROOT_PASSWORD);
             $name = 'PostgreSQL';
         } elseif ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::REDIS)) {
             $ports = @json_decode($dbFarmRole->GetSetting(Scalr_Db_Msr_Redis::PORTS_ARRAY));
             $passwords = @json_decode($dbFarmRole->GetSetting(Scalr_Db_Msr_Redis::PASSWD_ARRAY));
             if (!$ports && !$passwords) {
                 $data['additionalInfo']['MasterPassword'] = $dbFarmRole->GetSetting(Scalr_Db_Msr_Redis::MASTER_PASSWORD);
             } else {
                 $data['additionalInfo']['MasterPassword'] = "";
                 foreach ($ports as $i => $port) {
                     $data['additionalInfo']['MasterPassword'] .= "Port {$port}: {$passwords[$i]}<br>";
                 }
             }
             $name = 'Redis';
         } elseif ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MYSQL2)) {
             $data['additionalInfo']['MasterPassword'] = $dbFarmRole->GetSetting(Scalr_Db_Msr_Mysql2::ROOT_PASSWORD);
             $name = 'MySQL';
         } elseif ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::PERCONA)) {
             $data['additionalInfo']['MasterPassword'] = $dbFarmRole->GetSetting(Scalr_Db_Msr_Percona::ROOT_PASSWORD);
             $name = 'Percona Server';
         } elseif ($dbFarmRole->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::MARIADB)) {
             $data['additionalInfo']['MasterPassword'] = $dbFarmRole->GetSetting(Scalr_Db_Msr_Mariadb::ROOT_PASSWORD);
             $name = 'MariaDB';
         }
         if (!$data['additionalInfo']['MasterPassword']) {
             $data['additionalInfo']['MasterPassword'] = "";
         }
         $data['name'] = $name;
         if ($dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_STORAGE_ENGINE) == 'lvm') {
             $data['noDataBundleForSlaves'] = $dbFarmRole->GetSetting(Scalr_Role_DbMsrBehavior::ROLE_NO_DATA_BUNDLE_FOR_SLAVES) ? true : false;
         }
         $data['dtLastBackup'] = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_LAST_TS) ? Scalr_Util_DateTime::convertTz((int) $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_LAST_TS), 'd M Y \\a\\t H:i:s') : 'Never';
         $data['dtLastBundle'] = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_LAST_TS) ? Scalr_Util_DateTime::convertTz((int) $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_LAST_TS), 'd M Y \\a\\t H:i:s') : 'Never';
         $slaveNumber = 0;
         foreach ($dbFarmRole->GetServersByFilter() as $dbServer) {
             if ($dbServer->status != SERVER_STATUS::RUNNING) {
                 //TODO:
                 continue;
             }
             if ($dbServer->GetProperty(Scalr_Db_Msr::REPLICATION_MASTER) == 1) {
                 $data['isBundleRunning'] = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_IS_RUNNING);
                 $data['bundleServerId'] = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BUNDLE_SERVER_ID);
             }
             $data['isBackupRunning'] = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_IS_RUNNING);
             $data['backupServerId'] = $dbFarmRole->GetSetting(Scalr_Db_Msr::DATA_BACKUP_SERVER_ID);
             try {
                 $isCloudstack = in_array($dbFarmRole->Platform, array(SERVER_PLATFORMS::CLOUDSTACK, SERVER_PLATFORMS::IDCF));
                 $isMaster = $dbServer->GetProperty(Scalr_Db_Msr::REPLICATION_MASTER) == 1;
                 if (!$isCloudstack && in_array($this->getParam('type'), array(ROLE_BEHAVIORS::MYSQL2, ROLE_BEHAVIORS::PERCONA, ROLE_BEHAVIORS::MARIADB))) {
                     $password = $dbFarmRole->GetSetting(Scalr_Db_Msr_Mysql2::STAT_PASSWORD);
                     $rStatus = $this->getMySqlReplicationStatus($isMaster ? 'MASTER' : 'SLAVE', $dbServer->remoteIp, 'scalr_stat', $password);
                 }
                 if ($isMaster) {
                     $MasterPosition = $rStatus['Position'];
                     $master_ip = $dbServer->remoteIp;
                     $master_iid = $dbServer->serverId;
                 } else {
                     $num = ++$slaveNumber;
                     $SlavePosition = $rStatus['Exec_Master_Log_Pos'];
                 }
                 $d = array("serverId" => $dbServer->serverId, "localIp" => $dbServer->localIp, "remoteIp" => $dbServer->remoteIp, "replicationRole" => $isMaster ? 'Master' : "Slave #{$num}");
                 if (!$isCloudstack) {
                     $d['data'] = $rStatus;
                     $d['masterPosition'] = $MasterPosition;
                     $d['slavePosition'] = $SlavePosition;
                 }
                 $data["replicationStatus"][] = $d;
             } catch (Exception $e) {
                 $data["replicationStatus"][] = array("serverId" => $dbServer->serverId, "localIp" => $dbServer->localIp, "remoteIp" => $dbServer->remoteIp, "error" => $e->msg ? $e->msg : $e->getMessage(), "replicationRole" => $isMaster ? 'Master' : 'Slave');
             }
         }
     }
     $this->response->page('ui/dbmsr/status.js', $data);
 }
Exemple #3
0
 public function xAddReplicaSetAction()
 {
     $dbFarmRole = $this->getFarmRole();
     if ($dbFarmRole->GetSetting(Scalr_Role_Behavior_MongoDB::ROLE_CLUSTER_STATUS) != Scalr_Role_Behavior_MongoDB::STATUS_ACTIVE) {
         throw new Exception("You cannot add replica set from non-active cluster");
     }
     $replicasCount = $dbFarmRole->GetSetting(Scalr_Role_Behavior_MongoDB::ROLE_REPLICAS_COUNT);
     $dbFarmRole->SetSetting(Scalr_Role_Behavior_MongoDB::ROLE_REPLICAS_COUNT, $replicasCount + 1);
     Scalr_Role_Behavior::loadByName(ROLE_BEHAVIORS::MONGODB)->log($dbFarmRole, sprintf("Requested new replica set. Adding #%s replica set to the cluster", $replicasCount + 1));
     $this->response->success('Replica successfully added. It may take a few minutes before it becomes available.');
 }
Exemple #4
0
 public function xCreateBackupAction()
 {
     $this->request->defineParams(array('farmId' => array('type' => 'int'), 'farmRoleId' => array('type' => 'int')));
     $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
     $this->user->getPermissions()->validate($dbFarm);
     $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
     if ($dbFarmRole->FarmID != $dbFarm->ID) {
         throw new Exception("Role not found");
     }
     $behavior = Scalr_Role_Behavior::loadByName($dbFarmRole->GetRoleObject()->getDbMsrBehavior());
     $behavior->createBackup($dbFarmRole);
     $this->response->success('Backup successfully initiated');
 }
Exemple #5
0
 private function performDbMsrAction($action, DBFarmRole $dbFarmRole, $tz)
 {
     $timeouted = false;
     if ($dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_ENABLED")) && $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_EVERY")) != 0) {
         if ($dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_IS_RUNNING")) == 1) {
             // Wait for timeout time * 2 (Example: NIVs problem with big mysql snapshots)
             // We must wait for running bundle process.
             $timeout = $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_EVERY")) * (3600 * 2);
             $lastTs = $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_RUNNING_TS"));
             if ($lastTs + $timeout < time()) {
                 $timeouted = true;
             }
             if ($timeouted) {
                 $dbFarmRole->SetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_IS_RUNNING"), 0, DBFarmRole::TYPE_LCL);
             }
         } else {
             /*
              * Check bundle window
              */
             $period = $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_EVERY"));
             $timeout = $period * 3600;
             $lastActionTime = $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_LAST_TS"));
             $performAction = false;
             if ($period % 24 == 0) {
                 if ($lastActionTime) {
                     $days = $period / 24;
                     $dateTime = new DateTime(null, new DateTimeZone($tz));
                     $currentDate = (int) $dateTime->format("Ymd");
                     $dateTime->setTimestamp(strtotime("+{$days} day", $lastActionTime));
                     $nextDate = (int) $dateTime->format("Ymd");
                     if ($nextDate > $currentDate) {
                         return;
                     }
                 }
                 $pbwFrom = (int) ($dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_TIMEFRAME_START_HH")) . $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_TIMEFRAME_START_MM")));
                 $pbwTo = (int) ($dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_TIMEFRAME_END_HH")) . $dbFarmRole->GetSetting(Scalr_Db_Msr::getConstant("DATA_{$action}_TIMEFRAME_END_MM")));
                 if ($pbwFrom && $pbwTo) {
                     $dateTime = new DateTime(null, new DateTimeZone($tz));
                     $currentTime = (int) $dateTime->format("Hi");
                     //$current_time = (int)date("Hi");
                     //BUG HERE: 22:00 - 6:00 - doesn't work
                     if ($pbwFrom <= $currentTime && $pbwTo >= $currentTime) {
                         $performAction = true;
                     }
                 } else {
                     $performAction = true;
                 }
             } else {
                 //Check timeout
                 if ($lastActionTime + $timeout < time()) {
                     $performAction = true;
                 }
             }
             if ($performAction) {
                 $behavior = Scalr_Role_Behavior::loadByName($dbFarmRole->GetRoleObject()->getDbMsrBehavior());
                 if ($action == 'BUNDLE') {
                     $behavior->createDataBundle($dbFarmRole, array('compressor' => $dbFarmRole->GetSetting(Scalr_Role_DbMsrBehavior::ROLE_DATA_BUNDLE_COMPRESSION), 'useSlave' => $dbFarmRole->GetSetting(Scalr_Role_DbMsrBehavior::ROLE_DATA_BUNDLE_USE_SLAVE)));
                 }
                 if ($action == 'BACKUP') {
                     $behavior->createBackup($dbFarmRole);
                 }
             }
         }
     }
 }
Exemple #6
0
 if ($dbInfoId) {
     $dbRole = DBRole::loadById($dbInfoId);
 } else {
     $dbRole = new DBRole(0);
     $dbRole->generation = 2;
     $dbRole->origin = ROLE_TYPE::SHARED;
     $dbRole->envId = 0;
     $dbRole->clientId = 0;
     $dbRole->catId = $info['catId'];
     $dbRole->name = $info['name'];
     $dbRole->os = $info['os'];
     $dbRole->osFamily = $info['osFamily'];
     $dbRole->osGeneration = $info['osGeneration'];
     $dbRole->osVersion = $info['osVersion'];
     foreach ($info['behaviors'] as $behavior) {
         foreach (Scalr_Role_Behavior::loadByName($behavior)->getSecurityRules() as $rr) {
             $rules[] = array('rule' => $rr);
         }
     }
 }
 $dbRole->setBehaviors(array_values($info['behaviors']));
 $dbRole->description = $info['description'];
 $dbRole = $dbRole->save();
 $db->Execute("DELETE FROM role_security_rules WHERE role_id = ?", array($dbRole->id));
 foreach ($rules as $rule) {
     $db->Execute("INSERT INTO role_security_rules SET `role_id`=?, `rule`=?", array($dbRole->id, $rule['rule']));
     if ($rule['comment']) {
         $db->Execute("REPLACE INTO `comments` SET `env_id` = ?, `comment` = ?, `sg_name` = ?, `rule` = ?", array(0, $rule['comment'], "role:{$dbRole->id}", $rule['rule']));
     }
 }
 foreach ($info['images'] as $image) {
 public function ListFarmRoleParams()
 {
     $farmRoleId = $this->GetArg("farm-role-id");
     if (!$farmRoleId) {
         throw new Exception("'farm-role-id' required");
     }
     $dbFarmRole = DBFarmRole::LoadByID($farmRoleId);
     if ($dbFarmRole->FarmID != $this->DBServer->farmId) {
         throw new Exception("You can request this information ONLY for roles within server farm");
     }
     $ResponseDOMDocument = $this->CreateResponse();
     // Add volumes information
     try {
         if ($this->DBServer->farmRoleId == $farmRoleId) {
             $storage = new FarmRoleStorage($dbFarmRole);
             $vols = $storage->getVolumesConfigs($this->DBServer->index);
             $volumes = array();
             foreach ($vols as $i => $volume) {
                 if ($volume->id) {
                     $volumes[] = $volume;
                 }
             }
             $bodyEl = $this->serialize($volumes, 'volumes', $ResponseDOMDocument);
             $ResponseDOMDocument->documentElement->appendChild($bodyEl);
         }
     } catch (Exception $e) {
         $this->Logger->fatal("ListFarmRoleParams::Storage: {$e->getMessage()}");
     }
     $role = $dbFarmRole->GetRoleObject();
     $behaviors = $role->getBehaviors();
     foreach ($behaviors as $behavior) {
         $data = null;
         if ($behavior == ROLE_BEHAVIORS::MONGODB) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($behavior == ROLE_BEHAVIORS::CHEF) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($behavior == ROLE_BEHAVIORS::HAPROXY) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($behavior == ROLE_BEHAVIORS::NGINX) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($behavior == ROLE_BEHAVIORS::RABBITMQ) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($behavior == ROLE_BEHAVIORS::APACHE) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($data === null) {
             if ($behavior == ROLE_BEHAVIORS::CF_CLOUD_CONTROLLER) {
                 $data = new stdClass();
                 $data->version = $dbFarmRole->GetSetting(Scalr_Role_Behavior_CfCloudController::ROLE_VERSION);
             } else {
                 if ($behavior == ROLE_BEHAVIORS::MYSQL) {
                     $data = new stdClass();
                     $data->logFile = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_LOG_FILE);
                     $data->logPos = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_LOG_POS);
                     $data->rootPassword = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_ROOT_PASSWORD);
                     $data->replPassword = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_REPL_PASSWORD);
                     $data->statPassword = $dbFarmRole->GetSetting(DBFarmRole::SETTING_MYSQL_STAT_PASSWORD);
                     $data->replicationMaster = (int) $this->DBServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER);
                     //TODO: Storage
                 } else {
                     try {
                         $dbMsrInfo = Scalr_Db_Msr_Info::init($dbFarmRole, $this->DBServer, $behavior);
                         $data = $dbMsrInfo->getMessageProperties();
                     } catch (Exception $e) {
                     }
                 }
             }
         }
         if ($data) {
             $bodyEl = $this->serialize($data, $behavior, $ResponseDOMDocument);
             $ResponseDOMDocument->documentElement->appendChild($bodyEl);
         }
     }
     return $ResponseDOMDocument;
 }
Exemple #8
0
 /**
  * Save role informatiom
  */
 public function xSaveRoleAction()
 {
     $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_MANAGE);
     $this->request->defineParams(array('roleId' => array('type' => 'int'), 'agent' => array('type' => 'int'), 'behaviors' => array('type' => 'array'), 'tags' => array('type' => 'array'), 'arch', 'description', 'name', 'os', 'parameters' => array('type' => 'json'), 'remove_images' => array('type' => 'json'), 'images' => array('type' => 'json'), 'properties' => array('type' => 'json'), 'scripts' => array('type' => 'json')));
     $id = $this->getParam('roleId');
     $parameters = $this->getParam('parameters');
     if ($id == 0) {
         if ($this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
             $origin = ROLE_TYPE::CUSTOM;
             $envId = $this->environment->id;
             $clientId = $this->user->getAccountId();
         } else {
             $origin = ROLE_TYPE::SHARED;
             $envId = 0;
             $clientId = 0;
         }
         $dbRole = new DBRole(0);
         $dbRole->generation = 2;
         $dbRole->origin = $origin;
         $dbRole->envId = $envId;
         $dbRole->clientId = $clientId;
         $dbRole->catId = $this->getParam('cat_id');
         $dbRole->name = $this->getParam('name');
         $dbRole->os = $this->getParam('os');
         $dbRole->osGeneration = $this->getParam('os_generation');
         $dbRole->osFamily = $this->getParam('os_family');
         $dbRole->osVersion = $this->getParam('os_version');
         $dbRole->addedByEmail = $this->user->getEmail();
         $dbRole->addedByUserId = $this->user->getId();
         $rules = array(array('rule' => 'icmp:-1:-1:0.0.0.0/0'), array('rule' => 'tcp:22:22:0.0.0.0/0'));
         foreach ($this->getParam('behaviors') as $behavior) {
             if ($behavior == ROLE_BEHAVIORS::NGINX || $behavior == ROLE_BEHAVIORS::APACHE) {
                 if (empty($parameters)) {
                     $param = new stdClass();
                     $param->name = 'Nginx HTTPS Vhost Template';
                     $param->required = '1';
                     $param->defval = @file_get_contents(dirname(__FILE__) . "/../../../../templates/services/nginx/ssl.vhost.tpl");
                     $param->type = 'textarea';
                     $parameters[] = $param;
                 }
             }
             if ($behavior == ROLE_BEHAVIORS::MYSQL) {
                 $rules[] = array('rule' => "tcp:3306:3306:0.0.0.0/0");
             }
             if ($behavior == ROLE_BEHAVIORS::CASSANDRA) {
                 $rules[] = array('rule' => "tcp:9160:9160:0.0.0.0/0");
             }
             if ($behavior == ROLE_BEHAVIORS::CF_DEA) {
                 $rules[] = array('rule' => "tcp:12345:12345:0.0.0.0/0");
             }
             if ($behavior == ROLE_BEHAVIORS::CF_ROUTER) {
                 $rules[] = array('rule' => "tcp:2222:2222:0.0.0.0/0");
             }
             foreach (Scalr_Role_Behavior::loadByName($behavior)->getSecurityRules() as $rr) {
                 $rules[] = array('rule' => $rr);
             }
         }
         $dbRole = $dbRole->save();
         $soft = explode("\n", trim($this->getParam('software')));
         $software = array();
         if (count($soft) > 0) {
             foreach ($soft as $softItem) {
                 $itm = explode("=", $softItem);
                 $software[trim($itm[0])] = trim($itm[1]);
             }
             $dbRole->setSoftware($software);
         }
         $dbRole->setBehaviors(array_values($this->getParam('behaviors')));
     } else {
         $dbRole = DBRole::loadById($id);
         if ($this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
             $this->user->getPermissions()->validate($dbRole);
         }
         if ($dbRole->origin == ROLE_TYPE::CUSTOM) {
             $variables = new Scalr_Scripting_GlobalVariables($this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_ROLE);
             $variables->setValues(json_decode($this->getParam('variables'), true), $dbRole->id);
         }
         $rules = json_decode($this->getParam('security_rules'), true);
         foreach ($rules as &$r) {
             $r['rule'] = "{$r['ipProtocol']}:{$r['fromPort']}:{$r['toPort']}:{$r['cidrIp']}";
         }
     }
     $dbRole->description = $this->getParam('description');
     $this->db->Execute("DELETE FROM role_security_rules WHERE role_id = ?", array($dbRole->id));
     foreach ($rules as $rule) {
         $this->db->Execute("INSERT INTO role_security_rules SET `role_id`=?, `rule`=?", array($dbRole->id, $rule['rule']));
         if ($rule['comment']) {
             //UNIQUE KEY `main` (`env_id`,`sg_name`,`rule`)
             $this->db->Execute("\n                    INSERT `comments`\n                    SET `env_id` = ?,\n                        `sg_name` = ?,\n                        `rule` = ?,\n                        `comment` = ?\n                    ON DUPLICATE KEY UPDATE\n                        `comment` = ?\n                    ", array($this->getEnvironment() ? $this->getEnvironmentId() : 0, "role:{$dbRole->id}", $rule['rule'], $rule['comment'], $rule['comment']));
         }
     }
     foreach ($this->getParam('remove_images') as $imageId) {
         $dbRole->removeImage($imageId);
     }
     foreach ($this->getParam('images') as $image) {
         $image = (array) $image;
         $dbRole->setImage($image['image_id'], $image['platform'], $image['location'], $image['szr_version'], $image['architecture']);
     }
     $props = $this->getParam('properties');
     if (!empty($props) && (is_array($props) || $props instanceof \Traversable)) {
         foreach ($props as $k => $v) {
             $dbRole->setProperty($k, $v);
         }
     }
     $dbRole->setParameters($parameters);
     $dbRole->setScripts($this->getParam('scripts'));
     if ($this->user->getType() == Scalr_Account_User::TYPE_SCALR_ADMIN) {
         $dbRole->setTags($this->getParam('tags'));
     }
     $dbRole->save();
     $this->response->success('Role saved');
 }
Exemple #9
0
 /**
  * Save role informatiom
  */
 public function xSaveRoleAction()
 {
     $this->request->defineParams(array('roleId' => array('type' => 'int'), 'agent' => array('type' => 'int'), 'behaviors' => array('type' => 'array'), 'tags' => array('type' => 'array'), 'arch', 'description', 'name', 'os', 'parameters' => array('type' => 'json'), 'remove_images' => array('type' => 'json'), 'images' => array('type' => 'json'), 'properties' => array('type' => 'json'), 'scripts' => array('type' => 'json'), 'szr_version' => array('type' => 'string')));
     $id = $this->getParam('roleId');
     $parameters = $this->getParam('parameters');
     if ($id == 0) {
         if ($this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
             throw new Scalr_Exception_InsufficientPermissions();
         }
         $dbRole = new DBRole(0);
         $dbRole->generation = $this->getParam('agent') == 'scalarizr' || $this->getParam('agent') == 2 ? 2 : 1;
         // ($post_agent != 'scalarizr') ? 1 : 2;
         $dbRole->architecture = $this->getParam('arch');
         $dbRole->origin = ROLE_TYPE::SHARED;
         $dbRole->envId = 0;
         $dbRole->clientId = 0;
         $dbRole->name = $this->getParam('name');
         $dbRole->os = $this->getParam('os');
         $dbRole->szrVersion = $this->getParam('szr_version');
         $rules = array('icmp:-1:-1:0.0.0.0/0', 'tcp:22:22:0.0.0.0/0');
         foreach ($this->getParam('behaviors') as $behavior) {
             if ($behavior == ROLE_BEHAVIORS::NGINX || $behavior == ROLE_BEHAVIORS::APACHE) {
                 if (empty($parameters)) {
                     $param = new stdClass();
                     $param->name = 'Nginx HTTPS Vhost Template';
                     $param->required = '1';
                     $param->defval = @file_get_contents(dirname(__FILE__) . "/../../../../templates/services/nginx/ssl.vhost.tpl");
                     $param->type = 'textarea';
                     $parameters[] = $param;
                 }
             }
             if ($behavior == ROLE_BEHAVIORS::MYSQL) {
                 $rules[] = "tcp:3306:3306:0.0.0.0/0";
             }
             if ($behavior == ROLE_BEHAVIORS::CASSANDRA) {
                 $rules[] = "tcp:9160:9160:0.0.0.0/0";
             }
             if ($behavior == ROLE_BEHAVIORS::CF_DEA) {
                 $rules[] = "tcp:12345:12345:0.0.0.0/0";
             }
             if ($behavior == ROLE_BEHAVIORS::CF_ROUTER) {
                 $rules[] = "tcp:2222:2222:0.0.0.0/0";
             }
             $rules = array_merge($rules, Scalr_Role_Behavior::loadByName($behavior)->getSecurityRules());
         }
         $dbRole = $dbRole->save();
         $soft = explode("\n", trim($this->getParam('software')));
         $software = array();
         if (count($soft) > 0) {
             foreach ($soft as $softItem) {
                 $itm = explode("=", $softItem);
                 $software[trim($itm[0])] = trim($itm[1]);
             }
             $dbRole->setSoftware($software);
         }
         $dbRole->setBehaviors(array_values($this->getParam('behaviors')));
     } else {
         $dbRole = DBRole::loadById($id);
         if ($this->user->getType() != Scalr_Account_User::TYPE_SCALR_ADMIN) {
             $this->user->getPermissions()->validate($dbRole);
         }
         $rules = json_decode($this->getParam('security_rules'), true);
         foreach ($rules as &$r) {
             $r['rule'] = "{$r['ipProtocol']}:{$r['fromPort']}:{$r['toPort']}:{$r['cidrIp']}";
         }
     }
     $dbRole->description = $this->getParam('description');
     $this->db->Execute("DELETE FROM role_security_rules WHERE role_id = ?", array($dbRole->id));
     foreach ($rules as $rule) {
         $this->db->Execute("INSERT INTO role_security_rules SET `role_id`=?, `rule`=?", array($dbRole->id, $rule['rule']));
         if ($rule['comment']) {
             $this->db->Execute("REPLACE INTO `comments` SET `env_id` = ?, `comment` = ?, `sg_name` = ?, `rule` = ?", array($this->getEnvironment() ? $this->getEnvironmentId() : 0, $rule['comment'], "role:{$dbRole->id}", $rule['rule']));
         }
     }
     foreach ($this->getParam('remove_images') as $imageId) {
         $dbRole->removeImage($imageId);
     }
     foreach ($this->getParam('images') as $image) {
         $image = (array) $image;
         $dbRole->setImage($image['image_id'], $image['platform'], $image['location'], $image['szr_version'], $image['distributor'], $image['codename'], $image['release'], $image['architecture']);
     }
     foreach ($this->getParam('properties') as $k => $v) {
         $dbRole->setProperty($k, $v);
     }
     $dbRole->setParameters($parameters);
     $dbRole->setScripts($this->getParam('scripts'));
     if ($this->user->getType() == Scalr_Account_User::TYPE_SCALR_ADMIN) {
         $dbRole->setTags($this->getParam('tags'));
     }
     $dbRole->save();
     $this->response->success('Role saved');
 }
 public function ListFarmRoleParams()
 {
     $farmRoleId = $this->GetArg("farm-role-id");
     if (!$farmRoleId) {
         throw new Exception("'farm-role-id' required");
     }
     $dbFarmRole = DBFarmRole::LoadByID($farmRoleId);
     if ($dbFarmRole->FarmID != $this->DBServer->farmId) {
         throw new Exception("You can request this information ONLY for roles within server farm");
     }
     $ResponseDOMDocument = $this->CreateResponse();
     // Base configuration
     if ($this->DBServer->farmRoleId == $farmRoleId) {
         $data = Scalr_Role_Behavior::loadByName(ROLE_BEHAVIORS::BASE)->getBaseConfiguration($this->DBServer);
         foreach ((array) $data as $k => $v) {
             $bodyEl = $this->serialize($v, $k, $ResponseDOMDocument);
             $ResponseDOMDocument->documentElement->appendChild($bodyEl);
         }
     }
     $role = $dbFarmRole->GetRoleObject();
     $behaviors = $role->getBehaviors();
     foreach ($behaviors as $behavior) {
         $data = null;
         if ($behavior == ROLE_BEHAVIORS::MONGODB || $behavior == ROLE_BEHAVIORS::CHEF || $behavior == ROLE_BEHAVIORS::HAPROXY || $behavior == ROLE_BEHAVIORS::NGINX || $behavior == ROLE_BEHAVIORS::RABBITMQ || $behavior == ROLE_BEHAVIORS::APACHE || $behavior == ROLE_BEHAVIORS::VPC_ROUTER) {
             $data = Scalr_Role_Behavior::loadByName($behavior)->getConfiguration($this->DBServer);
         }
         if ($data === null) {
             if ($behavior == ROLE_BEHAVIORS::CF_CLOUD_CONTROLLER) {
                 $data = new stdClass();
                 $data->version = $dbFarmRole->GetSetting(Scalr_Role_Behavior_CfCloudController::ROLE_VERSION);
             } else {
                 if ($behavior == ROLE_BEHAVIORS::MYSQL) {
                     $data = new stdClass();
                     $data->logFile = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LOG_FILE);
                     $data->logPos = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_LOG_POS);
                     $data->rootPassword = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_ROOT_PASSWORD);
                     $data->replPassword = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_REPL_PASSWORD);
                     $data->statPassword = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::MYSQL_STAT_PASSWORD);
                     $data->replicationMaster = (int) $this->DBServer->GetProperty(SERVER_PROPERTIES::DB_MYSQL_MASTER);
                     //TODO: Storage
                 } else {
                     try {
                         $dbMsrInfo = Scalr_Db_Msr_Info::init($dbFarmRole, $this->DBServer, $behavior);
                         $data = $dbMsrInfo->getMessageProperties();
                     } catch (Exception $e) {
                     }
                 }
             }
         }
         if ($data) {
             $bodyEl = $this->serialize($data, $behavior, $ResponseDOMDocument);
             $ResponseDOMDocument->documentElement->appendChild($bodyEl);
         }
     }
     return $ResponseDOMDocument;
 }