New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DSC demo. Bugfix Apache and SystemD demo. #1814
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"ToString" and "Split"