mirror of
https://github.com/aphrody-labs/PowerShell.git
synced 2026-10-17 04:36:39 +00:00
Add DSC demo. Bugfix Apache and SystemD demo. (#1814)
* Add DSC demo. Bugfix Apache and SystemD demo. * DSC, Apache, SystemD demos, address comments * Fix capitalization in apache-demo.ps1
This commit is contained in:
1 parent
7fae8490cc
commit
60c30190c8
8 files changed
+201
-23
No files matched your search
@@ -1,4 +1,7 @@
|
||||
#Region utility functions
|
||||
|
||||
$global:sudocmd = "sudo"
|
||||
|
||||
Function GetApacheCmd{
|
||||
if (Test-Path "/usr/sbin/apache2ctl"){
|
||||
$cmd = "/usr/sbin/apache2ctl"
|
||||
@@ -57,7 +60,7 @@ Class ApacheVirtualHost{
|
||||
}
|
||||
|
||||
#Full specification
|
||||
ApacheVirtualHost([string]$ServerName, [string]$DocumentRoot, [string[]]$ServerAliases, [string]$ServerAdmin,[string]$CustomLogPath,[string]$ErrorLogPath,[string]$VirtualHostIPAddress,[int]$VirtualHostPort,[string]$ConfigurationFile){
|
||||
ApacheVirtualHost([string]$ServerName, [string]$DocumentRoot, [string[]]$ServerAliases, [string]$ServerAdmin, [string]$CustomLogPath, [string]$ErrorLogPath, [string]$VirtualHostIPAddress, [int]$VirtualHostPort, [string]$ConfigurationFile){
|
||||
$this.ServerName = $ServerName
|
||||
$this.DocumentRoot = $DocumentRoot
|
||||
$this.ServerAliases = $ServerAliases
|
||||
@@ -69,14 +72,12 @@ Class ApacheVirtualHost{
|
||||
$this.ConfigurationFile = $ConfigurationFile
|
||||
}
|
||||
|
||||
|
||||
|
||||
#Default Port and IP
|
||||
#endregion
|
||||
|
||||
#region class methods
|
||||
Save($ConfigurationFile){
|
||||
if (!(Test-Path $this.DocumentRoot)){ New-Item -type Directory $this.DocumentRoot }
|
||||
if (!(Test-Path $this.DocumentRoot)){ New-Item -Type Directory $this.DocumentRoot }
|
||||
|
||||
$VHostsDirectory = GetApacheVHostDir
|
||||
if (!(Test-Path $VHostsDirectory)){
|
||||
@@ -98,7 +99,8 @@ Class ApacheVirtualHost{
|
||||
if ($this.ErrorLogPath -like "*/*"){$vHostDef += "ErrorLog " + $this.ErrorLogpath +"`n"}
|
||||
$vHostDef += "</VirtualHost>"
|
||||
$filName = $ConfigurationFile
|
||||
$VhostDef |out-file "${VHostsDirectory}/${filName}" -Force -Encoding:ascii
|
||||
$VhostDef | Out-File "/tmp/${filName}" -Force -Encoding:ascii
|
||||
& $global:sudocmd "mv" "/tmp/${filName}" "${VhostsDirectory}/${filName}"
|
||||
Write-Information "Restarting Apache HTTP Server"
|
||||
Restart-ApacheHTTPServer
|
||||
}
|
||||
@@ -110,7 +112,7 @@ Class ApacheVirtualHost{
|
||||
|
||||
Function New-ApacheVHost {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
param(
|
||||
[parameter (Mandatory = $true)][string]$ServerName,
|
||||
[parameter (Mandatory = $true)][string]$DocumentRoot,
|
||||
[string]$VirtualHostIPAddress,
|
||||
@@ -160,7 +162,7 @@ Function Get-ApacheVHost{
|
||||
$cmd = GetApacheCmd
|
||||
|
||||
$Vhosts = @()
|
||||
$res = & $cmd -t -D DUMP_VHOSTS
|
||||
$res = & $global:sudocmd $cmd -t -D DUMP_VHOSTS
|
||||
|
||||
ForEach ($line in $res){
|
||||
$ServerName = $null
|
||||
@@ -207,9 +209,9 @@ Function Restart-ApacheHTTPServer{
|
||||
if ($Graceful -eq $null){$Graceful = $fase}
|
||||
$cmd = GetApacheCmd
|
||||
if ($Graceful){
|
||||
& $cmd -k graceful
|
||||
& $global:sudocmd $cmd -k graceful
|
||||
}else{
|
||||
& $cmd -k restart
|
||||
& $global:sudocmd $cmd -k restart
|
||||
}
|
||||
|
||||
}
|
||||
@@ -220,7 +222,7 @@ Function Get-ApacheModule{
|
||||
|
||||
$ApacheModules = @()
|
||||
|
||||
$Results = & $cmd -M |grep -v Loaded
|
||||
$Results = & $global:sudocmd $cmd -M |grep -v Loaded
|
||||
|
||||
Foreach ($mod in $Results){
|
||||
$modInst = [ApacheModule]::new($mod.trim())
|
||||
|
||||
@@ -1,16 +1,30 @@
|
||||
ipmo Apache
|
||||
Import-Module $PSScriptRoot/Apache/Apache.psm1
|
||||
|
||||
#list Apache Modules
|
||||
Get-ApacheModules |Where {$_.Module -like "*proxy*"}|Sort-Object Module
|
||||
Write-Host -Foreground Blue "Get installed Apache Modules like *proxy* and Sort by name"
|
||||
Get-ApacheModule |Where {$_.ModuleName -like "*proxy*"}|Sort-Object ModuleName | Out-Host
|
||||
|
||||
#Graceful restart of Apache
|
||||
Restart-ApacheHTTPServer -graceful
|
||||
Write-host -Foreground Blue "Restart Apache Server gracefully"
|
||||
Restart-ApacheHTTPServer -Graceful | Out-Host
|
||||
|
||||
#Enumerate current virtual hosts (web sites)
|
||||
Get-ApacheVHost
|
||||
Write-Host -Foreground Blue "Enumerate configured Apache Virtual Hosts"
|
||||
Get-ApacheVHost |out-host
|
||||
|
||||
#Add a new virtual host
|
||||
New-ApacheVHost -ServerName "mytestserver" -DocumentRoot /var/www/html/mystestserver -VirtualHostIPAddress * -VirtualHostPort 8090
|
||||
Write-Host -Foreground Yellow "Create a new Apache Virtual Host"
|
||||
New-ApacheVHost -ServerName "mytestserver" -DocumentRoot /var/www/html/mystestserver -VirtualHostIPAddress * -VirtualHostPort 8090 | Out-Host
|
||||
|
||||
#Enumerate new set of virtual hosts
|
||||
Get-ApacheVHost
|
||||
Write-Host -Foreground Blue "Enumerate Apache Virtual Hosts Again"
|
||||
Get-ApacheVHost |out-host
|
||||
|
||||
#Cleanup
|
||||
Write-Host -Foreground Blue "Remove demo virtual host"
|
||||
if (Test-Path "/etc/httpd/conf.d"){
|
||||
& sudo rm "/etc/httpd/conf.d/mytestserver.conf"
|
||||
}
|
||||
if (Test-Path "/etc/apache2/sites-enabled"){
|
||||
& sudo rm "/etc/apache2/sites-enabled/mytestserver.conf"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
## Apache Management Demo
|
||||
|
||||
This demo shows management of Apache HTTP Server with PowerShell cmdlets implemented in a script module.
|
||||
|
||||
- **Get-ApacheVHost**: Enumerate configured Apache Virtual Host (website) instances as objects.
|
||||
- **Get-ApacheModule**: Enumerate loaded Apache modules
|
||||
- **Restart-ApacheHTTPserver**: Restart the Apache web server
|
||||
- **New-ApacheVHost**: Create a new Apache Virtual Host (website) based on supplied parameters
|
||||
|
||||
|
||||
## Prerequisites ##
|
||||
- Install PowerShell
|
||||
- Install Apache packages
|
||||
- `sudo apt-get install apache2`
|
||||
- `sudo yum install httpd`
|
||||
|
||||
|
||||
Note: Management of Apache requires privileges. The user must have authorization to elevate with sudo. You will be prompted for a sudo password when running the demo.
|
||||
Reference in new issue
Block a user