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:
Kristopher Bash 2016-08-17 12:07:12 -07:00 committed by Jason Shirk
parent 7fae8490cc
commit 60c30190c8
8 changed files with 201 additions and 23 deletions

View file

@ -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())

View file

@ -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"
}

View file

@ -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.

122
demos/DSC/dsc-demo.ps1 Normal file
View file

@ -0,0 +1,122 @@
#Get Distro type and set distro-specific variables
$OSname = Get-Content "/etc/os-release" |Select-String -Pattern "^Name="
$OSName = $OSName.tostring().split("=")[1].Replace('"','')
if ($OSName -like "Ubuntu*"){
$distro = "Ubuntu"
$ApachePackages = @("apache2","php5","libapache2-mod-php5")
$ServiceName = "apache2"
$VHostDir = "/etc/apache2/sites-enabled"
$PackageManager = "apt"
}elseif (($OSName -like "CentOS*") -or ($OSName -like "Red Hat*") -or ($OSname -like "Oracle*")){
$distro = "Fedora"
$ApachePackages = @("httpd","mod_ssl","php","php-mysql")
$ServiceName = "httpd"
$VHostDir = "/etc/httpd/conf.d"
$PackageManager = "yum"
}else{
Write-Error "Unknown Linux operating system. Cannot continue."
}
#Get Service Controller
if ((Test-Path "/bin/systemctl") -or (Test-Path "/usr/bin/systemctl")){
$ServiceCtl = "SystemD"
}else{
$ServiceCtl = "init"
}
#Get FQDN
$hostname = & hostname --fqdn
Write-Host -ForegroundColor Blue "Compile a DSC MOF for the Apache Server configuration"
Configuration ApacheServer{
Node localhost{
ForEach ($Package in $ApachePackages){
nxPackage $Package{
Ensure = "Present"
Name = $Package
PackageManager = $PackageManager
}
}
nxFile vHostDirectory{
DestinationPath = $VhostDir
Type = "Directory"
Ensure = "Present"
Owner = "root"
Mode = "744"
}
#Ensure default content does not exist
nxFile DefVHost{
DestinationPath = "${VhostDir}/000-default.conf"
Ensure = "Absent"
}
nxFile Welcome.conf{
DestinationPath = "${VhostDir}/welcome.conf"
Ensure = "Absent"
}
nxFile UserDir.conf{
DestinationPath = "${VhostDir}/userdir.conf"
Ensure = "Absent"
}
#Ensure website is defined
nxFile DefaultSiteDir{
DestinationPath = "/var/www/html/defaultsite"
Type = "Directory"
Owner = "root"
Mode = "744"
Ensure = "Present"
}
nxFile DefaultSite.conf{
Destinationpath = "${VhostDir}/defaultsite.conf"
Owner = "root"
Mode = "744"
Ensure = "Present"
Contents = @"
<VirtualHost *:80>
DocumentRoot /var/www/html/defaultsite
ServerName $hostname
</VirtualHost>
"@
DependsOn = "[nxFile]DefaultSiteDir"
}
nxFile TestPhp{
DestinationPath = "/var/www/html/defaultsite/test.php"
Ensure = "Present"
Owner = "root"
Mode = "744"
Contents = @'
<?php phpinfo(); ?>
'@
}
#Configure Apache Service
nxService ApacheService{
Name = "$ServiceName"
Enabled = $true
State = "running"
Controller = $ServiceCtl
DependsOn = "[nxFile]DefaultSite.conf"
}
}
}
ApacheServer -OutputPath "/tmp"
Pause
Write-Host -ForegroundColor Blue "Apply the configuration locally"
& sudo /opt/microsoft/dsc/Scripts/StartDscConfiguration.py -configurationmof /tmp/localhost.mof | Out-Host
Pause
Write-Host -ForegroundColor Blue "Get the current configuration"
& sudo /opt/microsoft/dsc/Scripts/GetDscConfiguration.py | Out-Host

12
demos/DSC/readme.md Normal file
View file

@ -0,0 +1,12 @@
## DSC MOF Compilation Demo
[PowerShell Desired State Configuration](https://msdn.microsoft.com/en-us/PowerShell/dsc/overview) is a declarative configuration platform for Windows and Linux. DSC configurations can be authored in PowerShell and compiled into the resultant MOF document.
This demo shows use of PowerShell to author a DSC configuration to set the configuration of an Apache web server. PowerShell scripting is used to assess distro and version-specific properties, such as the service controller and repo manager tools, for use in the configuration.
## Prerequisites ##
- PowerShell >= 6.0.0-alpha.8 [https://github.com/PowerShell/PowerShell/releases](https://github.com/PowerShell/PowerShell/releases)
- OMI: >= 1.1.0 [https://www.github.com/microsoft/omi/releases](https://www.github.com/microsoft/omi/releases)
- Desired State Configuration for Linux >= 1.1.1-278 [https://github.com/Microsoft/PowerShell-DSC-for-Linux/releases](https://github.com/Microsoft/PowerShell-DSC-for-Linux/releases)
Note: applying the DSC configuration requires privileges. The user must have sudo authorization capabilities. You will be prompted for a sudo password when running the demo.

View file

@ -3,8 +3,9 @@ Function Get-SystemDJournal {
param (
[Alias("args")][string]$journalctlParameters
)
$sudocmd = "sudo"
$cmd = "journalctl"
$Result = & $cmd $journalctlParameters -o json --no-pager
$Result = & $sudocmd $cmd $journalctlParameters -o json --no-pager
Try
{
$JSONResult = $Result|ConvertFrom-JSON

View file

@ -1,6 +1,9 @@
Import-Module $PSScriptRoot/SystemD/SystemD.psm1
#list recent journal events
Get-SystemDJournal -args "-xe"
Write-host -Foreground Blue "Get recent SystemD journal messages"
Get-SystemDJournal -args "-xe" |Out-Host
#Drill into SystemD unit messages
Get-SystemDJournal -args "-xe" |where {$_._SYSTEMD_UNIT -like "*.service"} |ft _SYSTEMD_UNIT, MESSAGE |select-object -first 10
Write-host -Foreground Blue "Get recent SystemD jounal messages for services and return Unit, Message"
Get-SystemDJournal -args "-xe" | Where {$_._SYSTEMD_UNIT -like "*.service"} | Format-Table _SYSTEMD_UNIT, MESSAGE | Select-Object -first 10 | Out-Host

View file

@ -1,4 +1,10 @@
#Journalctl (JSON parsing) demo
Requires:
-SystemD-based system (RHEL/CentOS 7, Ubuntu 16.04)
-Privileges (launch powershell with sudo)
## SystemD: journalctl demo
This demo shows use of a PowerShell script module to wrap a native tool (journalctl) so that the output is structured for filtering and presentation control. `journalctl` is expressed as a cmdlet: Get-SystemDJournal, and the JSON output of journalctl is converted to a PowerShell object.
## Prerequisites ##
- Requires a SystemD-based operating system (Red Hat or CentOS 7, Ubuntu 16.04)
- Install PowerShell
Note: Accessing the SystemD journal requires privileges. The user must have authorization to elevate with sudo. You will be prompted for a sudo password when running the demo.