mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
-refactor Apache demo completely
-bug fix in SystemD (json) demo -Move demos to self-contained folders
This commit is contained in:
1 parent
af90f38f64
commit
4f2f0bb02c
7 files changed
+204
-173
No files matched your search
@@ -0,0 +1,161 @@
|
|||||||
|
|
||||||
|
#Region utility functions
|
||||||
|
Function GetApacheCmd{
|
||||||
|
if (Test-Path "/usr/sbin/apache2ctl"){
|
||||||
|
$cmd = "/usr/sbin/apache2ctl"
|
||||||
|
}elseif(Test-Path "/usr/sbin/httpd"){
|
||||||
|
$cmd = "/usr/sbin/httpd"
|
||||||
|
}else{
|
||||||
|
Write-Error "Unable to find httpd or apache2ctl program. Unable to continue"
|
||||||
|
exit -1
|
||||||
|
}
|
||||||
|
$cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
Function GetApacheVHostDir{
|
||||||
|
if (Test-Path "/etc/httpd/conf.d"){
|
||||||
|
$VHostsDirectory = "/etc/httpd/conf.d/"
|
||||||
|
}
|
||||||
|
if (Test-Path "/etc/apache2/sites-enabled"){
|
||||||
|
$VHostsDirectory = "/etc/apache2/sites-enabled"
|
||||||
|
}else{
|
||||||
|
$VHostsDirectory = $null
|
||||||
|
}
|
||||||
|
$VHostsDirectory
|
||||||
|
}
|
||||||
|
|
||||||
|
Function CleanInputString([string]$inputStr){
|
||||||
|
$outputStr = $inputStr.Trim().Replace('`n','').Replace('\n','')
|
||||||
|
$outputStr
|
||||||
|
}
|
||||||
|
|
||||||
|
#EndRegion utilty functions
|
||||||
|
|
||||||
|
#Region Class specifications
|
||||||
|
|
||||||
|
|
||||||
|
Class ApacheVirtualHost{
|
||||||
|
[string]$ServerName
|
||||||
|
[string]$DocumentRoot
|
||||||
|
[string]$VirtualHostIPAddress = "*"
|
||||||
|
[string[]]$ServerAliases
|
||||||
|
[string]$VirtualHostPort = "80"
|
||||||
|
[string]$ServerAdmin
|
||||||
|
[string]$CustomLogPath
|
||||||
|
[string]$ErrorLogPath
|
||||||
|
[string]$ConfigurationFile
|
||||||
|
|
||||||
|
#region class constructors
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region class methods
|
||||||
|
Save($ConfigurationFile){
|
||||||
|
if (!(Test-Path $this.DocumentRoot)){ New-Item -type Directory $this.DocumentRoot }
|
||||||
|
|
||||||
|
$VHostsDirectory = GetApacheVHostDir
|
||||||
|
if (!(Test-Path $VHostsDirectory)){
|
||||||
|
Write-Error "Specified virtual hosts directory does not exist: $VHostsDirectory"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
$vHostDef = "<VirtualHost $this.VirtualHostIPAddress:$this.VirtualHostPort >`n"
|
||||||
|
$vHostDef += "DocumentRoot " + $this.DocumentRoot + "`n"
|
||||||
|
ForEach ($Alias in $this.ServerAliases){
|
||||||
|
$vHostDef += "ServerAlias " + $Alias + "`n"
|
||||||
|
}
|
||||||
|
if ($this.ServerAdmin.Length -gt 1){$vHostDef += "ServerAdmin " + $this.ServerAdmin +"`n"}
|
||||||
|
if ($this.CustomLogPath -like "*/*"){$vHostDef += "CustomLog " + $this.CustomLogPath +"`n"}
|
||||||
|
if ($this.ErrorLogPath -like "*/*"){$vHostDef += "ErrorLog " + $this.ErrorLogpath +"`n"}
|
||||||
|
$vHostDef += "</VirtualHost>"
|
||||||
|
$filName = $ConfigurationFile
|
||||||
|
$VhostDef |out-file "${VHostsDirectory}/${filName}" -Force -Encoding:ascii
|
||||||
|
Write-Information "Restarting Apache HTTP Server"
|
||||||
|
Restart-ApacheHTTPServer
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
#EndRegion Class Specifications
|
||||||
|
|
||||||
|
Function New-ApacheVHost {
|
||||||
|
[CmdletBinding()]
|
||||||
|
param (
|
||||||
|
[parameter (Mandatory = $true)][string]$ServerName,
|
||||||
|
[parameter (Mandatory = $true)][string]$DocumentRoot,
|
||||||
|
[string]$VirtualHostIPAddress,
|
||||||
|
[string[]]$ServerAliases,
|
||||||
|
[string]$VirtualHostPort,
|
||||||
|
[string]$ServerAdmin,
|
||||||
|
[string]$CustomLogPath,
|
||||||
|
[string]$ErrorLogPath
|
||||||
|
)
|
||||||
|
|
||||||
|
$newVHost = [ApacheVirtualHost]::new()
|
||||||
|
$newVHost.ServerName = $ServerName
|
||||||
|
$newVHost.DocumentRoot = $DocumentRoot
|
||||||
|
$newVHost.ServerAliases = $ServerAliases
|
||||||
|
if ($VirtualHostIPAddress){$newVHost.VirtualHostIPAddress = $VirtualHostIPAddress}
|
||||||
|
if ($VirtualHostPort){$newVHost.VirtualHostPort = $VirtualHostPort}
|
||||||
|
$newVHost.ServerAdmin = $ServerAdmin
|
||||||
|
$newVHost.CustomLogPath = $CustomLogPath
|
||||||
|
$newVHost.ErrorLogPath = $ErrorLogPath
|
||||||
|
$newVHost.Save("$ServerName.conf")
|
||||||
|
}
|
||||||
|
|
||||||
|
Function Get-ApacheVHost{
|
||||||
|
$cmd = GetApacheCmd
|
||||||
|
|
||||||
|
$Vhosts = @()
|
||||||
|
$res = & $cmd -t -D DUMP_VHOSTS
|
||||||
|
|
||||||
|
ForEach ($line in $res){
|
||||||
|
$ServerName = $null
|
||||||
|
if ($line -like "*:*.conf*"){
|
||||||
|
#$vhobject = New-VHostObj
|
||||||
|
$RMatch = $line -match "(?<Listen>.*:[0-9]*)(?<ServerName>.*)\((?<ConfFile>.*)\)"
|
||||||
|
$ListenAddress = $Matches.Listen.trim()
|
||||||
|
$ServerName = $Matches.ServerName.trim()
|
||||||
|
$ConfFile = $Matches.ConfFile.trim()
|
||||||
|
}else{
|
||||||
|
if ($line.trim().split()[0] -like "*:*"){
|
||||||
|
$ListenAddress = $line.trim().split()[0]
|
||||||
|
}elseif($line -like "*.conf*"){
|
||||||
|
if ($line -like "*default*"){
|
||||||
|
$ServerName = "_Default"
|
||||||
|
$ConfFile = $line.trim().split()[3].split(":")[0].Replace('(','')
|
||||||
|
}elseif($line -like "*namevhost*"){
|
||||||
|
$ServerName = $line.trim().split()[3]
|
||||||
|
$ConfFile = $line.trim().split()[4].split(":")[0].Replace('(','')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ServerName -ne $null){
|
||||||
|
$vHost = [ApacheVirtualHost]::New
|
||||||
|
$vHost.ServerName = $ServerName
|
||||||
|
$vHost.ConfFile = $ConfFile
|
||||||
|
$vHost.VirtualHostIPAddress = $ListenAddress.Split(":")[0]
|
||||||
|
$vHost.VirtualHostPort = $ListenAddress.Split(":")[1]
|
||||||
|
$Vhosts += $vHost
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Return $Vhosts
|
||||||
|
}
|
||||||
|
|
||||||
|
Function Restart-ApacheHTTPServer{
|
||||||
|
[CmdletBinding()]
|
||||||
|
Param(
|
||||||
|
[switch]$Graceful
|
||||||
|
)
|
||||||
|
|
||||||
|
if ($Graceful -eq $null){$Graceful = $fase}
|
||||||
|
$cmd = GetApacheCmd
|
||||||
|
if ($Graceful){
|
||||||
|
& $cmd -k graceful
|
||||||
|
}else{
|
||||||
|
& $cmd -k restart
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
ipmo Apache
|
||||||
|
|
||||||
|
#list Apache Modules
|
||||||
|
Get-ApacheModules |Where {$_.Module -like "*proxy*"}|Sort-Object Module
|
||||||
|
|
||||||
|
#Graceful restart of Apache
|
||||||
|
Restart-ApacheHTTPServer -graceful
|
||||||
|
|
||||||
|
#Enumerate current virtual hosts (web sites)
|
||||||
|
Get-ApacheVHost
|
||||||
|
|
||||||
|
#Add a new virtual host
|
||||||
|
New-ApacheVHost -ServerName "mytestserver" -DocumentRoot /var/www/html/mystestserver -VirtualHostIPAddress * -VirtualHostPort 8090
|
||||||
|
|
||||||
|
#Enumerate new set of virtual hosts
|
||||||
|
Get-ApacheVHost
|
||||||
Whitespace-only changes.
@@ -0,0 +1,17 @@
|
|||||||
|
Function Get-SystemDJournal {
|
||||||
|
[CmdletBinding()]
|
||||||
|
param (
|
||||||
|
[Alias("args")][string]$journalctlParameters
|
||||||
|
)
|
||||||
|
$cmd = "journalctl"
|
||||||
|
$Result = & $cmd $journalctlParameters -o json --no-pager
|
||||||
|
Try
|
||||||
|
{
|
||||||
|
$JSONResult = $Result|ConvertFrom-JSON
|
||||||
|
$JSONResult
|
||||||
|
}
|
||||||
|
Catch
|
||||||
|
{
|
||||||
|
$Result
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
#list recent journal events
|
||||||
|
Get-SystemDJournal -args "-xe"
|
||||||
|
|
||||||
|
#Drill into SystemD unit messages
|
||||||
|
Get-SystemDJournal -args "-xe" |where {$_._SYSTEMD_UNIT -like "*.service"} |ft _SYSTEMD_UNIT, MESSAGE |select-object -first 10
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
#Journalctl (JSON parsing) demo
|
||||||
|
Requires:
|
||||||
|
-SystemD-based system (RHEL/CentOS 7, Ubuntu 16.04)
|
||||||
|
-Privileges (launch powershell with sudo)
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
$moduleRoot = Split-Path -Path $MyInvocation.MyCommand.Path
|
|
||||||
|
|
||||||
|
|
||||||
Function New-ApacheVHost {
|
|
||||||
[CmdletBinding()]
|
|
||||||
param (
|
|
||||||
[parameter (Mandatory = $true)][string]$ServerName,
|
|
||||||
[string]$VHostsDirectory,
|
|
||||||
[parameter (Mandatory = $true)][string]$DocumentRoot,
|
|
||||||
[parameter (Mandatory = $true)][string]$VirtualHostIPAddress,
|
|
||||||
[string[]]$ServerAliases,
|
|
||||||
[parameter (Mandatory = $true)][string]$VirtualHostPort,
|
|
||||||
[string]$ServerAdmin,
|
|
||||||
[string]$CustomLogPath,
|
|
||||||
[string]$ErrorLogPath
|
|
||||||
)
|
|
||||||
|
|
||||||
if ($VHostsDirectory -notlike "*/*"){
|
|
||||||
#Try to find default
|
|
||||||
if (Test-Path "/etc/httpd/conf.d"){
|
|
||||||
$VHostsDirectory = "/etc/httpd/conf.d/"
|
|
||||||
}
|
|
||||||
if (Test-Path "/etc/apache2/sites-enabled"){
|
|
||||||
$VHostsDirectory = "/etc/apache2/sites-enabled"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(Test-Path $VHostsDirectory)){
|
|
||||||
Write-Error "Specified virtual hosts directory does not exist: $VHostsDirectory"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
$vHostDef = "<VirtualHost "
|
|
||||||
if ($VirtualHostIPAddress -ne $null){
|
|
||||||
$vHostDef += $VirtualHostIPAddress
|
|
||||||
}
|
|
||||||
if ($VirtualHostPort -ne $null){
|
|
||||||
$vHostDef += ":$VirtualHostPort"
|
|
||||||
}
|
|
||||||
$vHostDef += ">"
|
|
||||||
|
|
||||||
$vHostDef += "`n"
|
|
||||||
$vHostDef += "DocumentRoot " + $DocumentRoot + "`n"
|
|
||||||
$vHostDef += "ServerName " + $ServerName + "`n"
|
|
||||||
if ($ServerAliases -ne $null){
|
|
||||||
ForEach ($Alias in $ServerAliases){
|
|
||||||
$vHostDef += "ServerAlias " + $Alias + "`n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ($ServerAdmin.Length -gt 1){
|
|
||||||
$vHostDef += "ServerAdmin " + $ServerAdmin +"`n"
|
|
||||||
}
|
|
||||||
if ($CustomLogPath -like "*/*"){
|
|
||||||
$vHostDef += "CustomLog " + $CustomLogPath +"`n"
|
|
||||||
}
|
|
||||||
if ($ErrorLogPath -like "*/*"){
|
|
||||||
$vHostDef += "ErrorLog " + $ErrorLogpath +"`n"
|
|
||||||
}
|
|
||||||
$vHostDef += "</VirtualHost>"
|
|
||||||
$filName = $ServerName + ".conf"
|
|
||||||
Write-Output "Writing $filName to $VHostsDirectory"
|
|
||||||
$VhostDef |out-file "$VHostsDirectory/$filName" -Force -Encoding:ascii
|
|
||||||
|
|
||||||
if (Test-Path "/usr/sbin/apache2ctl"){
|
|
||||||
$cmd = "/usr/sbin/apache2ctl"
|
|
||||||
}elseif(Test-Path "/usr/sbin/httpd"){
|
|
||||||
$cmd = "/usr/sbin/httpd"
|
|
||||||
}
|
|
||||||
$cmd += " -k restart"
|
|
||||||
write-output "Restarting Apache HTTP daemon"
|
|
||||||
Invoke-expression $cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
Function Get-ApacheVHosts{
|
|
||||||
$cmd = $null
|
|
||||||
if (Test-Path "/usr/sbin/apache2ctl"){
|
|
||||||
$cmd = "/usr/sbin/apache2ctl"
|
|
||||||
}elseif(Test-Path "/usr/sbin/httpd"){
|
|
||||||
$cmd = "/usr/sbin/httpd"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function New-VHostObj {
|
|
||||||
New-Object PSObject -Property @{}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if ($cmd -eq $null){
|
|
||||||
Write-Error "Unable to find httpd/apache2ctl command"
|
|
||||||
exit -1
|
|
||||||
}
|
|
||||||
|
|
||||||
$cmd = $cmd += " -t -D DUMP_VHOSTS"
|
|
||||||
$Vhosts = @()
|
|
||||||
$res = Invoke-expression $cmd
|
|
||||||
ForEach ($line in $res){
|
|
||||||
if ($line -like "*.conf*"){
|
|
||||||
$vhobject = New-VHostObj
|
|
||||||
$RMatch = $line -match "(?<Listen>.*:[0-9]*)(?<ServerName>.*)\((?<ConfFile>.*)\)"
|
|
||||||
$VHostProps = $matches
|
|
||||||
$vhobject |Add-Member -type NoteProperty -name "ServerName" -value $matches.ServerName.Trim()
|
|
||||||
$vhobject |Add-Member -type NoteProperty -name "ListenAddress" -value $matches.Listen.Trim()
|
|
||||||
$vhobject |Add-Member -type NoteProperty -name "ConfFile" -value $matches.ConfFile.Trim()
|
|
||||||
$Vhosts += $vhobject
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Return $Vhosts
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Function Restart-ApacheHTTPServer{
|
|
||||||
[CmdletBinding()]
|
|
||||||
Param(
|
|
||||||
[boolean]$Graceful
|
|
||||||
)
|
|
||||||
|
|
||||||
if ($Graceful -eq $null){$Graceful = $fase}
|
|
||||||
|
|
||||||
$cmd = $null
|
|
||||||
if (Test-Path "/usr/sbin/apache2ctl"){
|
|
||||||
$cmd = "/usr/sbin/apache2ctl"
|
|
||||||
}elseif(Test-Path "/usr/sbin/httpd"){
|
|
||||||
$cmd = "/usr/sbin/httpd"
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($cmd -eq $null){
|
|
||||||
Write-Error "Unable to find httpd/apache2ctl command"
|
|
||||||
exit -1
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($Graceful){
|
|
||||||
$cmd = $cmd += " -k graceful"
|
|
||||||
}else{
|
|
||||||
$cmd = $cmd += " -k restart"
|
|
||||||
}
|
|
||||||
|
|
||||||
Invoke-Expression $Cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
Function Get-ApacheModules{
|
|
||||||
$cmd = $null
|
|
||||||
if (Test-Path "/usr/sbin/apache2ctl"){
|
|
||||||
$cmd = "/usr/sbin/apache2ctl"
|
|
||||||
}elseif(Test-Path "/usr/sbin/httpd"){
|
|
||||||
$cmd = "/usr/sbin/httpd"
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($cmd -eq $null){
|
|
||||||
Write-Error "Unable to find httpd/apache2ctl command"
|
|
||||||
exit -1
|
|
||||||
}
|
|
||||||
|
|
||||||
$cmd = $cmd += " -M |grep -v Loaded"
|
|
||||||
|
|
||||||
$ApacheModules = @()
|
|
||||||
function New-ModuleObj {
|
|
||||||
New-Object PSObject -Property @{}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$Results = Invoke-Expression $cmd
|
|
||||||
Foreach ($mod in $Results){
|
|
||||||
$modObj = New-ModuleObj
|
|
||||||
$modObj | Add-Member -type NoteProperty -name "Module" -value $mod.trim()
|
|
||||||
$ApacheModules += $modObj
|
|
||||||
}
|
|
||||||
|
|
||||||
Return $ApacheModules
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user