"Fossies" - the Fresh Open Source Software Archive

Member "PowerShell-7.2.6/demos/Apache/Apache/Apache.psm1" (11 Aug 2022, 8294 Bytes) of package /linux/misc/PowerShell-7.2.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) Microsoft Corporation.
    2 # Licensed under the MIT License.
    3 
    4 #Region utility functions
    5 
    6 $global:sudocmd = "sudo"
    7 
    8 Function GetApacheCmd{
    9     if (Test-Path "/usr/sbin/apache2ctl"){
   10         $cmd = "/usr/sbin/apache2ctl"
   11     }elseif(Test-Path "/usr/sbin/httpd"){
   12         $cmd = "/usr/sbin/httpd"
   13     }else{
   14         Write-Error "Unable to find httpd or apache2ctl program. Unable to continue"
   15         exit -1
   16     }
   17     $cmd
   18 }
   19 
   20 Function GetApacheVHostDir{
   21     if (Test-Path "/etc/httpd/conf.d"){
   22         Return "/etc/httpd/conf.d/"
   23     }
   24     if (Test-Path "/etc/apache2/sites-enabled"){
   25         Return "/etc/apache2/sites-enabled"
   26     }
   27 }
   28 
   29 Function CleanInputString([string]$inputStr){
   30     $outputStr = $inputStr.Trim().Replace('`n','').Replace('\n','')
   31     $outputStr
   32 }
   33 
   34 #EndRegion utility functions
   35 
   36 #Region Class specifications
   37 
   38 Class ApacheModule{
   39         [string]$ModuleName
   40 
   41         ApacheModule([string]$aModule){
   42             $this.ModuleName = $aModule
   43         }
   44 }
   45 
   46 Class ApacheVirtualHost{
   47         [string]$ServerName
   48         [string]$DocumentRoot
   49         [string]$VirtualHostIPAddress = "*"
   50         [string[]]$ServerAliases
   51         [int]$VirtualHostPort = "80"
   52         [string]$ServerAdmin
   53         [string]$CustomLogPath
   54         [string]$ErrorLogPath
   55         [string]$ConfigurationFile
   56 
   57         #region class constructors
   58         ApacheVirtualHost([string]$ServerName, [string]$ConfFile, [string]$VirtualHostIPAddress,[int]$VirtualHostPort){
   59             $this.ServerName = $ServerName
   60             $this.ConfigurationFile = $ConfFile
   61             $this.VirtualHostIPAddress = $VirtualHostIPAddress
   62             $this.VirtualHostPort = $VirtualHostPort
   63         }
   64 
   65         #Full specification
   66         ApacheVirtualHost([string]$ServerName, [string]$DocumentRoot, [string[]]$ServerAliases, [string]$ServerAdmin, [string]$CustomLogPath, [string]$ErrorLogPath, [string]$VirtualHostIPAddress, [int]$VirtualHostPort, [string]$ConfigurationFile){
   67             $this.ServerName = $ServerName
   68             $this.DocumentRoot = $DocumentRoot
   69             $this.ServerAliases = $ServerAliases
   70             $this.ServerAdmin = $ServerAdmin
   71             $this.CustomLogPath = $CustomLogPath
   72             $this.ErrorLogPath = $ErrorLogPath
   73             $this.VirtualHostIPAddress = $VirtualHostIPAddress
   74             $this.VirtualHostPort = $VirtualHostPort
   75             $this.ConfigurationFile = $ConfigurationFile
   76         }
   77 
   78         #Default Port and IP
   79         #endregion
   80 
   81         #region class methods
   82         Save($ConfigurationFile){
   83             if (!(Test-Path $this.DocumentRoot)){ New-Item -Type Directory $this.DocumentRoot }
   84 
   85             $VHostsDirectory = GetApacheVHostDir
   86             if (!(Test-Path $VHostsDirectory)){
   87                 Write-Error "Specified virtual hosts directory does not exist: $VHostsDirectory"
   88                 exit 1
   89             }
   90             $VHostIPAddress = $this.VirtualHostIPAddress
   91             [string]$VhostPort = $this.VirtualHostPort
   92             $VHostDef = "<VirtualHost " + "$VHostIPAddress" + ":" + $VHostPort + " >`n"
   93             $vHostDef += "DocumentRoot " + $this.DocumentRoot + "`n"
   94             ForEach ($Alias in $this.ServerAliases){
   95                 if ($Alias.trim() -ne ""){
   96                     $vHostDef += "ServerAlias " + $Alias + "`n"
   97                 }
   98             }
   99             $vHostDef += "ServerName " + $this.ServerName +"`n"
  100             if ($this.ServerAdmin.Length -gt 1){$vHostDef += "ServerAdmin " + $this.ServerAdmin +"`n"}
  101             if ($this.CustomLogPath -like "*/*"){$vHostDef += "CustomLog " + $this.CustomLogPath +"`n"}
  102             if ($this.ErrorLogPath -like "*/*"){$vHostDef += "ErrorLog " + $this.ErrorLogpath +"`n"}
  103             $vHostDef += "</VirtualHost>"
  104             $filName = $ConfigurationFile
  105             $VhostDef | Out-File "/tmp/${filName}" -Force -Encoding:ascii
  106             & $global:sudocmd "mv" "/tmp/${filName}" "${VhostsDirectory}/${filName}"
  107             Write-Information "Restarting Apache HTTP Server"
  108             Restart-ApacheHTTPServer
  109         }
  110 
  111         #endregion
  112 }
  113 
  114 #EndRegion Class Specifications
  115 
  116 Function New-ApacheVHost {
  117     [CmdletBinding()]
  118     param(
  119         [parameter (Mandatory = $true)][string]$ServerName,
  120         [parameter (Mandatory = $true)][string]$DocumentRoot,
  121         [string]$VirtualHostIPAddress,
  122         [string[]]$ServerAliases,
  123         [int]$VirtualHostPort,
  124         [string]$ServerAdmin,
  125         [string]$CustomLogPath,
  126         [string]$ErrorLogPath
  127         )
  128 
  129         $NewConfFile = $VHostsDirectory + "/" + $ServerName + ".conf"
  130         if(!($VirtualHostIPAddress)){$VirtualHostIPAddress = "*"}
  131         if(!($VirtualHostPort)){$VirtualHostPort = "80"}
  132         $newVHost = [ApacheVirtualHost]::new("$ServerName","$DocumentRoot","$ServerAliases","$ServerAdmin","$CustomLogPath","$ErrorLogPath","$VirtualHostIPAddress",$VirtualHostPort,"$NewConfFile")
  133         $newVHost.Save("$ServerName.conf")
  134 }
  135 
  136 Function GetVHostProps([string]$ConfFile,[string]$ServerName,[string]$Listener){
  137     $confContents = Get-Content $ConfFile
  138     [boolean]$Match = $false
  139     $DocumentRoot = ""
  140     $CustomLogPath = ""
  141     $ErrorLogPath = ""
  142     $ServerAdmin = ""
  143     ForEach ($confline in $confContents){
  144         if ($confLine -like "<VirtualHost*${Listener}*"){
  145             $Match = $true
  146         }
  147         if($Match){
  148             Switch -wildcard  ($confline) {
  149                 "*DocumentRoot*"{$DocumentRoot = $confline.split()[1].trim()}
  150                 "*CustomLog*"{$CustomLogPath = $confline.split()[1].trim()}
  151                 "*ErrorLog*"{$ErrorLogPath = $confline.split()[1].trim()}
  152                 "*ServerAdmin*"{$ServerAdmin = $confline.split()[1].trim()}
  153                #Todo: Server aliases
  154             }
  155             if($confline -like "*</VirtualHost>*"){
  156                 $Match = $false
  157             }
  158         }
  159     }
  160     @{"DocumentRoot" = "$DocumentRoot"; "CustomLogPath" = "$CustomLogPath"; "ErrorLogPath" = "$ErrorLogPath"; "ServerAdmin" = $ServerAdmin}
  161 
  162 }
  163 
  164 Function Get-ApacheVHost{
  165     $cmd = GetApacheCmd
  166 
  167     $Vhosts = @()
  168     $res = & $global:sudocmd $cmd -t -D DUMP_VHOSTS
  169 
  170     ForEach ($line in $res){
  171         $ServerName = $null
  172         if ($line -like "*:*.conf*"){
  173             $RMatch = $line -match "(?<Listen>.*:[0-9]*)(?<ServerName>.*)\((?<ConfFile>.*)\)"
  174             $ListenAddress = $Matches.Listen.trim()
  175             $ServerName = $Matches.ServerName.trim()
  176             $ConfFile = $Matches.ConfFile.trim().split(":")[0].Replace('(','')
  177         }else{
  178             if ($line.trim().split()[0] -like "*:*"){
  179                 $ListenAddress = $line.trim().split()[0]
  180             }elseif($line -like "*.conf*"){
  181                 if ($line -like "*default*"){
  182                     $ServerName = "_Default"
  183                     $ConfFile = $line.trim().split()[3].split(":")[0].Replace('(','')
  184                 }elseif($line -like "*namevhost*"){
  185                     $ServerName = $line.trim().split()[3]
  186                     $ConfFile = $line.trim().split()[4].split(":")[0].Replace('(','')
  187                 }
  188             }
  189         }
  190 
  191         if ($null -ne $ServerName){
  192             $vHost = [ApacheVirtualHost]::New($ServerName, $ConfFile, $ListenAddress.Split(":")[0],$ListenAddress.Split(":")[1])
  193             $ExtProps = GetVHostProps $ConfFile $ServerName $ListenAddress
  194             $vHost.DocumentRoot = $ExtProps.DocumentRoot
  195             #Custom log requires additional handling. NYI
  196             #$vHost.CustomLogPath = $ExtProps.CustomLogPath
  197             $vHost.ErrorLogPath = $ExtProps.ErrorLogPath
  198             $vHost.ServerAdmin = $ExtProps.ServerAdmin
  199             $Vhosts += $vHost
  200             }
  201         }
  202 
  203     Return $Vhosts
  204     }
  205 
  206 Function Restart-ApacheHTTPServer{
  207   [CmdletBinding()]
  208   Param(
  209    [switch]$Graceful
  210    )
  211 
  212     if ($null -eq $Graceful){$Graceful = $false}
  213     $cmd = GetApacheCmd
  214         if ($Graceful){
  215                 & $global:sudocmd $cmd  -k graceful
  216         }else{
  217                 & $global:sudocmd $cmd  -k restart
  218         }
  219 
  220 }
  221 
  222 Function Get-ApacheModule{
  223     $cmd = GetApacheCmd
  224 
  225         $ApacheModules = @()
  226 
  227         $Results = & $global:sudocmd $cmd -M |grep -v Loaded
  228 
  229         Foreach ($mod in $Results){
  230         $modInst = [ApacheModule]::new($mod.trim())
  231         $ApacheModules += ($modInst)
  232         }
  233 
  234     $ApacheModules
  235 
  236 }