DBServer::GetScriptingVars PHP Method

GetScriptingVars() public method

public GetScriptingVars ( ) : array
return array
    public function GetScriptingVars()
    {
        $dbFarmRole = $this->GetFarmRoleObject();
        $roleId = $dbFarmRole->RoleID;
        $dbRole = DBRole::loadById($roleId);
        $isDbMsr = $dbRole->getDbMsrBehavior();
        if ($isDbMsr) {
            $isMaster = $this->GetProperty(Scalr_Db_Msr::REPLICATION_MASTER);
        } else {
            $isMaster = $this->GetProperty(\SERVER_PROPERTIES::DB_MYSQL_MASTER);
        }
        $at = new Entity\Account\Team();
        $ft = new Entity\FarmTeam();
        $teams = join(",", Entity\Account\Team::find([\Scalr\Model\AbstractEntity::STMT_FROM => "{$at->table()} JOIN {$ft->table()} ON {$ft->columnTeamId} = {$at->columnId}", \Scalr\Model\AbstractEntity::STMT_WHERE => "{$ft->columnFarmId} = '{$this->farmId}'"])->map(function ($t) {
            return $t->name;
        }));
        $retval = array('image_id' => $dbRole->__getNewRoleObject()->getImage($this->platform, $dbFarmRole->CloudLocation)->imageId, 'external_ip' => $this->remoteIp, 'internal_ip' => $this->localIp, 'role_name' => $dbRole->name, 'isdbmaster' => $isMaster, 'instance_index' => $this->index, 'instance_farm_index' => $this->farmIndex, 'server_type' => $this->type, 'server_hostname' => $this->GetProperty(Scalr_Role_Behavior::SERVER_BASE_HOSTNAME), 'server_id' => $this->serverId, 'farm_id' => $this->farmId, 'farm_role_id' => $this->farmRoleId, 'farm_role_alias' => $this->GetFarmRoleObject()->Alias, 'farm_name' => $this->GetFarmObject()->Name, 'farm_hash' => $this->GetFarmObject()->Hash, 'farm_owner_email' => $this->GetFarmObject()->createdByUserEmail, 'farm_team' => $teams, 'behaviors' => implode(",", $dbRole->getBehaviors()), 'env_id' => $this->GetEnvironmentObject()->id, 'env_name' => $this->GetEnvironmentObject()->name, 'cloud_location' => $this->GetCloudLocation(), 'cloud_server_id' => $this->GetCloudServerID());
        if ($this->cloudLocationZone) {
            $retval['cloud_location_zone'] = $this->cloudLocationZone;
        }
        if ($this->platform == SERVER_PLATFORMS::EC2) {
            $retval['instance_id'] = $this->GetProperty(EC2_SERVER_PROPERTIES::INSTANCE_ID);
            $retval['ami_id'] = $this->GetProperty(EC2_SERVER_PROPERTIES::AMIID);
            $retval['region'] = $this->GetProperty(EC2_SERVER_PROPERTIES::REGION);
            $retval['avail_zone'] = $this->GetProperty(EC2_SERVER_PROPERTIES::AVAIL_ZONE);
            if ($dbFarmRole->GetSetting(Entity\FarmRoleSetting::AWS_ELB_ENABLED)) {
                $elbId = $dbFarmRole->GetSetting(Entity\FarmRoleSetting::AWS_ELB_ID);
                $retval['aws_elb_name'] = $elbId;
            }
        }
        if (\Scalr::getContainer()->analytics->enabled) {
            $ccId = $this->GetProperty(\SERVER_PROPERTIES::ENV_CC_ID);
            if ($ccId) {
                $cc = CostCentreEntity::findPk($ccId);
                if ($cc) {
                    /* @var $cc CostCentreEntity */
                    $retval['cost_center_id'] = $ccId;
                    $retval['cost_center_bc'] = $cc->getProperty(CostCentrePropertyEntity::NAME_BILLING_CODE);
                    $retval['cost_center_name'] = $cc->name;
                } else {
                    throw new Exception("Cost center {$ccId} not found");
                }
            }
            $projectId = $this->GetProperty(\SERVER_PROPERTIES::FARM_PROJECT_ID);
            if ($projectId) {
                $project = ProjectEntity::findPk($projectId);
                /* @var $project ProjectEntity */
                $retval['project_id'] = $projectId;
                $retval['project_bc'] = $project->getProperty(ProjectPropertyEntity::NAME_BILLING_CODE);
                $retval['project_name'] = $project->name;
            }
        }
        return $retval;
    }

Usage Example

Example #1
0
 private static function prepareScript($scriptSettings, DBServer $targetServer, Event $event = null)
 {
     $db = Core::GetDBInstance();
     //$scriptSettings['version'] = (int)$scriptSettings['version'];
     if ($scriptSettings['version'] == 'latest' || (int) $scriptSettings['version'] == -1) {
         $version = (int) $db->GetOne("SELECT MAX(revision) FROM script_revisions WHERE scriptid=?", array($scriptSettings['scriptid']));
     } else {
         $version = (int) $scriptSettings['version'];
     }
     $template = $db->GetRow("SELECT name,id FROM scripts WHERE id=?", array($scriptSettings['scriptid']));
     $template['timeout'] = $scriptSettings['timeout'];
     $template['issync'] = $scriptSettings['issync'];
     $template['body'] = $db->GetOne("SELECT script FROM script_revisions WHERE scriptid=? AND revision=?", array($template['id'], $version));
     if (!$template['body']) {
         return false;
     }
     $params = array_merge($targetServer->GetScriptingVars(), (array) unserialize($scriptSettings['params']));
     if ($event) {
         $eventServer = $event->DBServer;
         foreach ($eventServer->GetScriptingVars() as $k => $v) {
             $params["event_{$k}"] = $v;
         }
         foreach ($event->GetScriptingVars() as $k => $v) {
             $params[$k] = $event->{$v};
         }
     }
     // Prepare keys array and array with values for replacement in script
     $keys = array_keys($params);
     $f = create_function('$item', 'return "%".$item."%";');
     $keys = array_map($f, $keys);
     $values = array_values($params);
     // Generate script contents
     $script_contents = str_replace($keys, $values, $template['body']);
     $template['body'] = str_replace('\\%', "%", $script_contents);
     $template['name'] = preg_replace("/[^A-Za-z0-9]+/", "_", $template['name']);
     return $template;
 }
All Usage Examples Of DBServer::GetScriptingVars