예제 #1
0
 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;
     //SSL stuff
     if ($vHost->isSslEnabled) {
         $cert = new Scalr_Service_Ssl_Certificate();
         $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();
     $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;
 }
예제 #2
0
 public function xSaveAction()
 {
     $validator = new Validator();
     try {
         if (!$validator->IsDomain($this->getParam('domainName'))) {
             $err['domainName'] = _("Domain name is incorrect");
         }
         if (!$this->getParam('farmId')) {
             $err['farmId'] = _("Farm required");
         } else {
             $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
             $this->user->getPermissions()->validate($dbFarm);
         }
         if (!$this->getParam('farmRoleId')) {
             $err['farmRoleId'] = _("Role required");
         } else {
             $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
             if ($dbFarmRole->FarmID != $dbFarm->ID) {
                 $err['farmRoleId'] = _("Role not found");
             }
         }
         if (!$validator->IsEmail($this->getParam('serverAdmin'))) {
             $err['serverAdmin'] = _("Server admin's email is incorrect or empty ");
         }
         if (!$this->getParam('documentRoot')) {
             $err['documentRoot'] = _("Document root required");
         }
         if (!$this->getParam('logsDir')) {
             $err['logsDir'] = _("Logs directory required");
         }
         if ($this->db->GetOne("SELECT id FROM apache_vhosts WHERE env_id=? AND `name` = ? AND id != ?", array($this->getEnvironmentId(), $this->getParam('domainName'), $this->getParam('vhostId')))) {
             $err['domainName'] = "'{$this->getParam('domainName')}' virtualhost already exists";
         }
     } catch (Exception $e) {
         $err[] = $e->getMessage();
     }
     if (count($err) == 0) {
         $vHost = Scalr_Service_Apache_Vhost::init();
         if ($this->getParam('vhostId')) {
             $vHost->loadById($this->getParam('vhostId'));
             $this->user->getPermissions()->validate($vHost);
         } else {
             $vHost->envId = $this->getEnvironmentId();
             $vHost->clientId = $this->user->getAccountId();
         }
         $vHost->domainName = $this->getParam('domainName');
         $vHost->isSslEnabled = $this->getParam('isSslEnabled') == 'on' ? true : false;
         $vHost->farmId = $this->getParam('farmId');
         $vHost->farmRoleId = $this->getParam('farmRoleId');
         $vHost->httpdConf = $this->getParam("nonSslTemplate");
         $vHost->templateOptions = serialize(array("document_root" => trim($this->getParam('documentRoot')), "logs_dir" => trim($this->getParam('logsDir')), "server_admin" => trim($this->getParam('serverAdmin')), "server_alias" => trim($this->getParam('serverAlias'))));
         //SSL stuff
         if ($vHost->isSslEnabled) {
             if ($_FILES['certificate']['tmp_name']) {
                 $vHost->sslCert = file_get_contents($_FILES['certificate']['tmp_name']);
             }
             if ($_FILES['privateKey']['tmp_name']) {
                 $vHost->sslKey = file_get_contents($_FILES['privateKey']['tmp_name']);
             }
             if ($_FILES['certificateChain']['tmp_name']) {
                 $vHost->caCert = file_get_contents($_FILES['certificateChain']['tmp_name']);
             }
             $vHost->httpdConfSsl = $this->getParam("sslTemplate");
         } else {
             $vHost->sslCert = "";
             $vHost->sslKey = "";
             $vHost->caCert = "";
             $vHost->httpdConfSsl = "";
         }
         $vHost->save();
         $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->farmRoleId == $vHost->farmRoleId) {
                 $dBServer->SendMessage(new Scalr_Messaging_Msg_VhostReconfigure());
             }
         }
         $this->response->success(_('Virtualhost successfully saved'));
     } else {
         $this->response->failure();
         $this->response->data(array('errors' => $err));
     }
 }
예제 #3
0
파일: Vhosts.php 프로젝트: scalr/scalr
 public function xSaveAction()
 {
     $this->request->restrictAccess(Acl::RESOURCE_SERVICES_APACHE, Acl::PERM_SERVICES_APACHE_MANAGE);
     $validator = new Scalr_Validator();
     try {
         if ($validator->validateDomain($this->getParam('domainName')) !== true) {
             $err['domainName'] = _("Domain name is incorrect");
         }
         if (!$this->getParam('farmId')) {
             $err['farmId'] = _("Farm required");
         } else {
             $dbFarm = DBFarm::LoadByID($this->getParam('farmId'));
             $this->user->getPermissions()->validate($dbFarm);
         }
         if (!$this->getParam('farmRoleId')) {
             $err['farmRoleId'] = _("Role required");
         } else {
             $dbFarmRole = DBFarmRole::LoadByID($this->getParam('farmRoleId'));
             if ($dbFarmRole->FarmID != $dbFarm->ID) {
                 $err['farmRoleId'] = _("Role not found");
             }
         }
         if ($validator->validateEmail($this->getParam('serverAdmin'), null, true) !== true) {
             $err['serverAdmin'] = _("Server admin's email is incorrect or empty ");
         }
         if (!$this->getParam('documentRoot')) {
             $err['documentRoot'] = _("Document root required");
         }
         if (!$this->getParam('logsDir')) {
             $err['logsDir'] = _("Logs directory required");
         }
         if ($this->db->GetOne("SELECT id FROM apache_vhosts WHERE env_id=? AND `name` = ? AND id != ? AND farm_id = ? AND farm_roleid = ? LIMIT 1", array($this->getEnvironmentId(), $this->getParam('domainName'), $this->getParam('vhostId'), $this->getParam('farmId'), $this->getParam('farmRoleId')))) {
             $err['domainName'] = "'{$this->getParam('domainName')}' virtualhost already exists";
         }
         if (preg_match('/[^\\x01-\\x7f]/', $this->getParam('nonSslTemplate'))) {
             $err['nonSslTemplate'] = _("Template contains non-ASCII symbols or null");
         }
         if ($this->getParam('isSslEnabled') === 'on') {
             if (preg_match('/[^\\x01-\\x7f]/', $this->getParam('sslTemplate'))) {
                 $err['sslTemplate'] = _("Template contains non-ASCII symbols or null");
             }
         }
     } catch (Exception $e) {
         $err[] = $e->getMessage();
     }
     if (count($err) == 0) {
         $vHost = Scalr_Service_Apache_Vhost::init();
         if ($this->getParam('vhostId')) {
             $vHost->loadById($this->getParam('vhostId'));
             $this->user->getPermissions()->validate($vHost);
         } else {
             $vHost->envId = $this->getEnvironmentId();
             $vHost->clientId = $this->user->getAccountId();
         }
         $vHost->domainName = $this->getParam('domainName');
         $isSslEnabled = $this->getParam('isSslEnabled') == 'on' ? true : false;
         if ($vHost->farmRoleId && $vHost->farmRoleId != $this->getParam('farmRoleId')) {
             $oldFarmRoleId = $vHost->farmRoleId;
         }
         $vHost->farmId = $this->getParam('farmId');
         $vHost->farmRoleId = $this->getParam('farmRoleId');
         $vHost->isSslEnabled = $isSslEnabled ? 1 : 0;
         $vHost->httpdConf = $this->getParam("nonSslTemplate", true);
         $vHost->templateOptions = serialize(array("document_root" => trim($this->getParam('documentRoot')), "logs_dir" => trim($this->getParam('logsDir')), "server_admin" => trim($this->getParam('serverAdmin')), "server_alias" => trim($this->getParam('serverAlias'))));
         //SSL stuff
         if ($isSslEnabled) {
             $cert = Entity\SslCertificate::findPk($this->getParam('sslCertId'));
             $this->user->getPermissions()->validate($cert);
             $vHost->sslCertId = $cert->id;
             $vHost->httpdConfSsl = $this->getParam("sslTemplate", true);
         } else {
             $vHost->sslCertId = 0;
             $vHost->httpdConfSsl = "";
         }
         $vHost->save();
         $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->farmRoleId == $vHost->farmRoleId) {
                 $dBServer->SendMessage(new Scalr_Messaging_Msg_VhostReconfigure());
             }
         }
         if ($oldFarmRoleId) {
             $oldFarmRole = DBFarmRole::LoadByID($oldFarmRoleId);
             $servers = $oldFarmRole->GetServersByFilter(array('status' => array(SERVER_STATUS::INIT, SERVER_STATUS::RUNNING)));
             foreach ($servers as $dBServer) {
                 $dBServer->SendMessage(new Scalr_Messaging_Msg_VhostReconfigure());
             }
         }
         $this->response->success(_('Virtualhost successfully saved'));
     } else {
         $this->response->failure();
         $this->response->data(array('errors' => $err));
     }
 }