Scalr\Upgrade\Updates\Update20150123100257::run1 PHP Method

run1() protected method

protected run1 ( $stage )
    protected function run1($stage)
    {
        $analytics = \Scalr::getContainer()->analytics;
        if (!\Scalr::isHostedScalr()) {
            $this->console->warning("Terminating as this upgrade script is only for Hosted Scalr installation.");
            return;
        }
        $this->console->out("Creates default Cost Center for an each Account");
        $rs = $this->db->Execute("SELECT id FROM `clients`");
        while ($rec = $rs->FetchRow()) {
            try {
                $account = Scalr_Account::init()->loadById($rec['id']);
            } catch (Exception $e) {
                continue;
            }
            $this->console->out("Processing %s (%d) account...", $account->name, $account->id);
            //Whether the Account already has account level Cost Center assigned to it
            $ccs = $account->getCostCenters()->filterByAccountId($account->id);
            if (count($ccs) > 0) {
                //We assume that the account has already been initialized
                continue;
            }
            try {
                //Gets account owner user to be CC Lead
                $owner = $account->getOwner();
            } catch (Exception $e) {
                continue;
            }
            //Creates default Cost Center and Project
            $cc = $analytics->usage->createHostedScalrAccountCostCenter($account, $owner);
            //Associates default CC with the account
            $accountCc = new AccountCostCenterEntity($account->id, $cc->ccId);
            $accountCc->save();
            //Gets project entity
            /* @var $project ProjectEntity */
            $project = $cc->getProjects()[0];
            foreach ($this->db->GetAll("SELECT id FROM client_environments WHERE client_id = ?", [$account->id]) as $row) {
                try {
                    $environment = Scalr_Environment::init()->loadById($row['id']);
                } catch (Exception $e) {
                    continue;
                }
                $this->console->out("- Environment: %s (%d) CC: %s", $environment->name, $environment->id, $cc->ccId);
                //Creates association
                $environment->setPlatformConfig([Scalr_Environment::SETTING_CC_ID => $cc->ccId]);
                foreach ($this->db->GetAll("SELECT id FROM farms WHERE env_id = ?", [$environment->id]) as $r) {
                    try {
                        $farm = DBFarm::LoadByID($r['id']);
                    } catch (Exception $e) {
                        continue;
                    }
                    $this->console->out("- - Farm: %s (%d) Project: %s", $farm->Name, $farm->ID, $project->projectId);
                    //Associates farm with default Project
                    $farm->SetSetting(Entity\FarmSetting::PROJECT_ID, $project->projectId);
                    unset($farm);
                }
                $this->console->out("- Updating server properties for environment %s (%d)", $environment->name, $environment->id);
                $this->db->Execute("\n                    INSERT `server_properties` (`server_id`, `name`, `value`)\n                    SELECT s.`server_id`, ?, ? FROM `servers` s WHERE s.env_id = ?\n                    ON DUPLICATE KEY UPDATE `value` = ?\n                ", [SERVER_PROPERTIES::FARM_PROJECT_ID, $project->projectId, $environment->id, $project->projectId]);
                $this->db->Execute("\n                    INSERT `server_properties` (`server_id`, `name`, `value`)\n                    SELECT s.`server_id`, ?, ? FROM `servers` s WHERE s.env_id = ?\n                    ON DUPLICATE KEY UPDATE `value` = ?\n                ", [SERVER_PROPERTIES::ENV_CC_ID, $cc->ccId, $environment->id, $cc->ccId]);
                unset($environment);
            }
            unset($ccs);
            unset($owner);
            unset($account);
        }
    }