ScalrAPI_2_1_0::ApacheVhostCreate PHP Method

ApacheVhostCreate() public method

public ApacheVhostCreate ( $DomainName, $FarmID, $FarmRoleID, $DocumentRootDir, $EnableSSL, $SSLPrivateKey = null, $SSLCertificate = null )
    public function ApacheVhostCreate($DomainName, $FarmID, $FarmRoleID, $DocumentRootDir, $EnableSSL, $SSLPrivateKey = null, $SSLCertificate = null)
    {
        $this->restrictAccess(Acl::RESOURCE_SERVICES_APACHE);
        $validator = new Scalr_Validator();
        if ($validator->validateDomain($DomainName) !== true) {
            $err[] = _("Domain name is incorrect");
        }
        $DBFarm = DBFarm::LoadByID($FarmID);
        if ($DBFarm->EnvID != $this->Environment->id) {
            throw new Exception(sprintf("Farm #%s not found", $FarmID));
        }
        $this->user->getPermissions()->validate($DBFarm);
        $DBFarmRole = DBFarmRole::LoadByID($FarmRoleID);
        if ($DBFarm->ID != $DBFarmRole->FarmID) {
            throw new Exception(sprintf("FarmRole #%s not found on Farm #%s", $FarmRoleID, $FarmID));
        }
        if (!$DocumentRootDir) {
            throw new Exception(_("DocumentRootDir required"));
        }
        $options = serialize(array("document_root" => trim($DocumentRootDir), "logs_dir" => "/var/log", "server_admin" => $this->user->getEmail()));
        $httpConfigTemplateSSL = @file_get_contents(dirname(__FILE__) . "/../../templates/services/apache/ssl.vhost.tpl");
        $httpConfigTemplate = @file_get_contents(dirname(__FILE__) . "/../../templates/services/apache/nonssl.vhost.tpl");
        $vHost = Scalr_Service_Apache_Vhost::init();
        $vHost->envId = (int) $this->Environment->id;
        $vHost->clientId = $this->user->getAccountId();
        $vHost->domainName = $DomainName;
        $vHost->isSslEnabled = $EnableSSL ? true : false;
        $vHost->farmId = $FarmID;
        $vHost->farmRoleId = $FarmRoleID;
        $vHost->httpdConf = $httpConfigTemplate;
        $vHost->templateOptions = $options;
        $this->DB->BeginTrans();
        try {
            //SSL stuff
            if ($vHost->isSslEnabled) {
                $cert = new Entity\SslCertificate();
                $cert->envId = $DBFarm->EnvID;
                $cert->name = $DomainName;
                $cert->privateKey = base64_decode($SSLPrivateKey);
                $cert->certificate = base64_decode($SSLCertificate);
                $cert->save();
                $vHost->sslCertId = $cert->id;
                $vHost->httpdConfSsl = $httpConfigTemplateSSL;
            } else {
                $vHost->sslCertId = 0;
            }
            $vHost->save();
            $this->DB->CommitTrans();
        } catch (\Exception $e) {
            $this->DB->RollbackTrans();
            throw new Exception('Error saving VHost. ' . $e->getMessage(), $e->getCode(), $e);
        }
        $servers = $DBFarm->GetServersByFilter(array('status' => array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING)));
        foreach ($servers as $DBServer) {
            if ($DBServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::NGINX) || $DBServer->GetFarmRoleObject()->GetRoleObject()->hasBehavior(ROLE_BEHAVIORS::APACHE)) {
                $DBServer->SendMessage(new Scalr_Messaging_Msg_VhostReconfigure());
            }
        }
        $response = $this->CreateInitialResponse();
        $response->Result = 1;
        return $response;
    }