Skip to content

Commit

Permalink
implemented invokescript method to cWindowsContainer class. Added Not…
Browse files Browse the repository at this point in the history
…Configurable DSC Properties IPAddress and ContainerId and implemented logic to populate when Get-DscConfiguration is called. Added verbose messages. Added Try Catch for Set method
  • Loading branch information
bgelens committed Sep 29, 2015
1 parent a89bc97 commit f003f2f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 30 deletions.
Binary file added GetDSCConfigIPandID.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Ben Gelens

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ Start-Process nginx
ContainerNginX -StartupScript $script
Start-DscConfiguration .\ContainerNginX -Wait -Verbose -Force
```
```
**Update**
Get-DscConfiguration now shows ContainerId and currently assigned IP Address
![Config](https://github.com/bgelens/cWindowsContainer/blob/master/GetDSCConfigIPandID.jpg)
Binary file modified cWindowsContainer.psd1
Binary file not shown.
83 changes: 54 additions & 29 deletions cWindowsContainer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,46 @@ class cWindowsContainer {
[DscProperty()]
[String] $StartUpScript

[DscProperty(NotConfigurable)]
[String] $IPAddress

[DscProperty(NotConfigurable)]
[String] $ContainerId

[void] Set () {
if ($this.Ensure -eq [Ensure]::Present) {
if ($null -eq ($Image = Get-ContainerImage -Name $this.ContainerImage)) {
$Image = Get-ContainerImage | Select-Object -First 1
}
$ErrorActionPreference = 'Stop'
try {
if ($this.Ensure -eq [Ensure]::Present) {
Write-Verbose -Message 'Creating Container';
if ($null -eq ($Image = Get-ContainerImage -Name $this.ContainerImage)) {
Write-Error -Message "ContainerImage with name $($this.ContainerImage) was not found";
}

$Params = @{
Name = $this.Name;
ContainerImage = $Image;
}
if ($this.SwitchName -and ($null -ne (Get-VMSwitch -Name $this.SwitchName))) {
$Params += @{
SwitchName = $this.SwitchName;
$Params = @{
Name = $this.Name;
ContainerImage = $Image;
}
}
$Container = New-Container @Params;
$Container | Start-Container | Out-Null;
if ($this.SwitchName -and ($null -ne (Get-VMSwitch -Name $this.SwitchName))) {
Write-Verbose -Message 'VMSwitch was found and will be bound';
$Params += @{
SwitchName = $this.SwitchName;
}
} elseif ($this.SwitchName -and ($null -eq (Get-VMSwitch -Name $this.SwitchName))) {
Write-Error -Message "VMSwitch with name $($this.SwitchName) was not found";
}
$Container = New-Container @Params;
$Container | Start-Container | Out-Null;

if ($null -ne $this.StartUpScript) {
Invoke-Command -ContainerId $Container.ContainerId -RunAsAdministrator -ScriptBlock ([scriptblock]::Create($this.StartUpScript)) | Out-Null
}
} else {
Get-Container -Name $this.Name | Stop-Container -Passthru | Remove-Container -Force | Out-Null;
if ($null -ne $this.StartUpScript) {
Write-Verbose -Message 'Startup Script specified, passing script to InvokeScript method'
[void] $this.InvokeScript(([scriptblock]::Create($this.StartUpScript)),$Container.ContainerId);
}
} else {
Write-Verbose -Message 'Removing Container';
Get-Container -Name $this.Name | Stop-Container -Passthru | Remove-Container -Force | Out-Null;
}
} catch {
Write-Error -ErrorRecord $_ -ErrorAction Stop;
}
}

Expand All @@ -54,18 +71,26 @@ class cWindowsContainer {
}
}

[String] InvokeScript ([String] $Script, [String] $ContainerId) {
$Output = Invoke-Command -ContainerId $ContainerId -RunAsAdministrator -ScriptBlock ([scriptblock]::Create($Script)) -ErrorAction SilentlyContinue;
return $Output;
}

[cWindowsContainer] Get () {
$Configuration = [hashtable]::new()
$Configuration.Add('Name',$this.Name)
$Configuration.Add('ContainerImage',$this.ContainerImage)
$Configuration.Add('SwitchName',$this.SwitchName)
$Configuration.Add('StartUpScript',$this.StartUpScript)
$Configuration = [hashtable]::new();
$Configuration.Add('Name',$this.Name);
$Configuration.Add('ContainerImage',$this.ContainerImage);
$Configuration.Add('SwitchName',$this.SwitchName);
$Configuration.Add('StartUpScript',$this.StartUpScript);
if (($this.Ensure -eq [Ensure]::Present) -and ($this.Test())) {
$Configuration.Add('Ensure','Present')
}
else {
$Configuration.Add('Ensure','Absent')
Write-Verbose -Message 'Acquiring ContainerId';
$Configuration.Add('ContainerId',(Get-Container -Name $this.Name).ContainerId);
$Configuration.Add('Ensure','Present');
Write-Verbose -Message 'Acquiring IPAddress';
$Configuration.Add('IPAddress',$this.InvokeScript('(Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Manual).IPAddress',$Configuration.ContainerId));
} else {
$Configuration.Add('Ensure','Absent');
}
return $Configuration
return $Configuration;
}
}

0 comments on commit f003f2f

Please sign in to comment.