DBFarmRole::SetSetting PHP Method

SetSetting() public method

Set farm role setting
public SetSetting ( string $name, string | null $value, string $type = null ) : void
$name string The name of the setting
$value string | null The value of the setting
$type string optional
return void
    public function SetSetting($name, $value, $type = null)
    {
        if ($value === "" || $value === null) {
            $this->DB->Execute("DELETE FROM farm_role_settings WHERE name=? AND farm_roleid=?", array($name, $this->ID));
        } else {
            $this->DB->Execute("\n                INSERT INTO farm_role_settings\n                SET `name`=?,\n                    `value`=?,\n                    `farm_roleid`=?,\n                    `type`=?\n                ON DUPLICATE KEY UPDATE\n                    `value`=?,\n                    `type`=?\n            ", array($name, $value, $this->ID, $type, $value, $type));
        }
        $this->SettingsCache[$name] = $value;
        return true;
    }

Usage Example

Beispiel #1
0
 public function onFarmSave(DBFarm $dbFarm, DBFarmRole $dbFarmRole)
 {
     try {
         $account = Scalr_Account::init()->loadById($dbFarm->ClientID);
         if (!$account->isFeatureEnabled(Scalr_Limits::FEATURE_CHEF)) {
             $dbFarmRole->ClearSettings("chef.");
             return false;
         }
         $db = Core::GetDBInstance();
         $runListId = $dbFarmRole->GetSetting(self::ROLE_CHEF_RUNLIST_ID);
         $attributes = $dbFarmRole->GetSetting(self::ROLE_CHEF_ATTRIBUTES);
         $checksum = $dbFarmRole->GetSetting(self::ROLE_CHEF_CHECKSUM);
         $chefRoleName = $dbFarmRole->GetSetting(self::ROLE_CHEF_ROLE_NAME);
         $chefServerId = $dbFarmRole->GetSetting(self::ROLE_CHEF_SERVER_ID);
         // Need to remove chef role if chef was disabled for current farmrole
         if (!$runListId && $chefRoleName) {
             $this->removeChefRole($chefServerId, $chefRoleName);
             $dbFarmRole->ClearSettings("chef.");
             return true;
         }
         if ($runListId) {
             $runListInfo = $this->db->GetRow("SELECT chef_server_id, runlist FROM services_chef_runlists WHERE id=?", array($runListId));
             $newChefServerId = $runListInfo['chef_server_id'];
             if ($newChefServerId != $chefServerId && $chefServerId) {
                 // Remove role from old server
                 $this->removeChefRole($chefServerId, $chefRoleName);
                 $createNew = true;
             }
             if (!$chefServerId) {
                 $createNew = true;
             }
             $chefServerInfo = $this->db->GetRow("SELECT * FROM services_chef_servers WHERE id=?", array($runListInfo['chef_server_id']));
             $chefServerInfo['auth_key'] = $this->getCrypto()->decrypt($chefServerInfo['auth_key'], $this->cryptoKey);
             $chefClient = Scalr_Service_Chef_Client::getChef($chefServerInfo['url'], $chefServerInfo['username'], trim($chefServerInfo['auth_key']));
             $roleName = "scalr-{$dbFarmRole->ID}";
             $setSettings = false;
             if ($createNew) {
                 $chefClient->createRole($roleName, $roleName, json_decode($runListInfo['runlist']), json_decode($attributes), $runListInfo['chef_environment']);
                 $setSettings = true;
             } else {
                 if ($dbFarmRole->GetSetting(self::ROLE_CHEF_CHECKSUM) != md5("{$runListInfo['runlist']}.{$attributes}")) {
                     $chefClient->updateRole($roleName, $roleName, json_decode($runListInfo['runlist']), json_decode($attributes), $runListInfo['chef_environment']);
                     $setSettings = true;
                 }
             }
             if ($setSettings) {
                 $dbFarmRole->SetSetting(self::ROLE_CHEF_ROLE_NAME, $roleName);
                 $dbFarmRole->SetSetting(self::ROLE_CHEF_SERVER_ID, $runListInfo['chef_server_id']);
                 $dbFarmRole->SetSetting(self::ROLE_CHEF_CHECKSUM, md5("{$runListInfo['runlist']}.{$attributes}"));
             }
         }
     } catch (Exception $e) {
         throw new Exception("Chef settings error: {$e->getMessage()} ({$e->getTraceAsString()})");
     }
 }
All Usage Examples Of DBFarmRole::SetSetting