Advertisement
artisticcheese

Untitled

Jun 27th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [Parameter(Mandatory = $true)]
  3.     [ValidateScript  ( {$_.State -eq 'opened'})]
  4.     [System.Management.Automation.Runspaces.PSSession]$Session,
  5.     [Parameter(Mandatory = $false)]
  6.     [string]$graphLocation = "D:\images"
  7. )
  8.  
  9. if ($cimSession -eq $null) {
  10.     $cimSession = New-CimSession -ComputerName $session.ComputerName -Credential $session.Runspace.ConnectionInfo.Credential -errorAction stop
  11. }
  12. $nodename = $cimSession.ComputerName
  13.  
  14. [DSCLocalConfigurationManager()]
  15. configuration LCMConfig
  16. {
  17.     # Parameter help description
  18.     param(
  19.         [Parameter(Mandatory = $false)]
  20.         [ValidateNotNullOrEmpty()]
  21.         [string]$nodename = $script:nodename
  22.     )
  23.     Node $nodename
  24.     {  
  25.         Settings {
  26.             RebootNodeIfNeeded = $true
  27.             ConfigurationMode = 'ApplyAndMonitor'
  28.             ActionAfterReboot = 'ContinueConfiguration'
  29.         }
  30.     }
  31. }
  32. Configuration SwarmConfiguration
  33. {
  34.     param (
  35.         [Parameter(Mandatory = $false)]
  36.         [ValidateNotNullOrEmpty()]
  37.         [string]$nodename = $script:nodename
  38.     )
  39.     Import-DscResource -ModuleName 'PSDesiredStateConfiguration', 'xPSDesiredStateConfiguration'
  40.     Import-DSCResource -name  "PackageManagement"
  41.     Import-DSCResource -Module "cChoco"
  42.     node $nodename {
  43.         PackageManagement DockerExecutable {
  44.             Ensure = 'present'
  45.             Name = "Docker"
  46.             ProviderName = "DockerMsftProvider"
  47.             DependsOn = "[WindowsFeature]ContainerInstall"
  48.         }
  49.         WindowsFeature ContainerInstall {
  50.             Ensure = "Present"
  51.             Name = "Containers"
  52.         }
  53.         File DockerImagesFolder {
  54.             DestinationPath = $graphLocation
  55.             Type = 'Directory'
  56.         }
  57.         Service DockerServiceConfig {
  58.             Name = "docker"
  59.             State = "Running"
  60.             DependsOn = @("[PackageManagement]DockerExecutable")
  61.         }
  62.         cChocoInstaller installChoco {
  63.             InstallDir = "c:\choco"
  64.         }
  65.         cChocoPackageInstallerSet installSomeStuff {
  66.             Ensure = 'Present'
  67.             Name = @(
  68.                 "classic-shell"
  69.                 "7zip"
  70.                 "visualstudiocode"
  71.             )
  72.             DependsOn = "[cChocoInstaller]installChoco"
  73.         }
  74.         Script DockerConfigFile {
  75.             TestScript = {
  76.                 $configFile = Join-path -Path  "$($env:programdata)" -ChildPath "docker\config\daemon.json"
  77.                 If (Test-path $configFile) {
  78.                     $dockerConfig = Get-Content $configFile | convertFrom-json
  79.                     if ($dockerConfig.graph -eq $using:graphLocation) { return $true; } else {return $false}
  80.                 }
  81.                 else {
  82.                     return $false
  83.                 }
  84.             }
  85.             GetScript = {
  86.                 $configFile = Join-path -Path  "$($env:programdata)" -ChildPath "docker\config\daemon.json"
  87.                 return {@{configValue = (Get-Content $configFile | convertFrom-json)}
  88.                 }
  89.             }
  90.             SetScript = {
  91.                 $dockerConfigFilePath = Join-path -Path  "$($env:programdata)" -ChildPath "docker\config\daemon.json"
  92.                 If (Test-path $dockerConfigFilePath) {
  93.                     $dockerConfig = Get-Content $dockerConfigFilePath | convertfrom-json
  94.                     If ( (Get-Member -InputObject $dockerconfig -Name graph) -is [object] ) {
  95.                        
  96.                         $dockerConfig.graph = $using:graphLocation
  97.                     }
  98.                     else {
  99.                         Write-Verbose "ELSE $dockerConfig" | convertto-JSON
  100.                         $dockerConfig | Add-Member -MemberType NoteProperty -Name graph -Value $using:graphLocation
  101.                     }
  102.                     Write-Verbose $dockerConfig | convertto-JSON
  103.                     $dockerConfig | convertto-Json | out-file $dockerConfigFilePath -Encoding ASCII
  104.                 }
  105.                 else {
  106.                     @{graph = "$($using:graphlocation)"} | convertto-json | out-file $dockerConfigFilePath -Encoding ASCII
  107.                 }
  108.                 Restart-service docker
  109.             }
  110.         }
  111.     }    
  112. }
  113. Configuration InstallBasicModules
  114. {
  115.     param (
  116.         [Parameter(Mandatory = $false)]
  117.         [ValidateNotNullOrEmpty()]
  118.         [string]$nodename = $script:nodename
  119.     )
  120.     Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
  121.     Import-dscresource -Name "PackageManagement"
  122.  
  123.     #Import-DscResource -Module 'packagemanagement' -moduleversion '1.1.3.0'
  124.     node $nodename {  
  125.         PackageManagement xPSDesiredStateConfiguration {
  126.             Ensure = 'present'
  127.             Name = "xPSDesiredStateConfiguration"
  128.             Source = "PSGallery"
  129.        
  130.         }
  131.         PackageManagement cChoco {
  132.             Ensure = 'present'
  133.             Name = "cChoco"
  134.             Source = "PSGallery"
  135.         }
  136.  
  137.         PackageManagement DockerModule {
  138.             Ensure = 'present'
  139.             name = 'DockerMsftProvider'
  140.             Source = "PSGallery"
  141.         }
  142.     }
  143. }
  144.  
  145. $scriptblock = {
  146.     if ((Get-PackageProvider -listavailable -name nuget -erroraction SilentlyContinue).Count -eq 0) {Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force}
  147.     #if ((Get-PackageProvider -listavailable -name Chocolatey -erroraction SilentlyContinue).Count -eq 0) {Register-PackageSource -Name chocolatey -ProviderName Chocolatey -Location http://chocolatey.org/api/v2/ -force }
  148.     if ((Get-Module -listavailable -name PowerShellGet -erroraction SilentlyContinue).Count -eq 1) {Install-Module -Name PowerShellGet -Force -Verbose -Scope AllUsers -Repository PSGallery}
  149. }
  150. Invoke-Command -session $session -scriptblock $scriptblock -errorAction stop
  151.  
  152. InstallBasicModules -OutputPath .\InstallBasicModules
  153. LCMConfig -OutputPath .\LCMConfig
  154. SwarmConfiguration -Outputpath .\SwarmConfiguration
  155. Set-DscLocalConfigurationManager -Verbose -Path .\LCMConfig -CimSession $cimSession
  156. Start-DscConfiguration -Wait -Verbose -Path .\InstallBasicModules -Force -CimSession $cimSession
  157. Start-DscConfiguration -Wait -Verbose -Path .\SwarmConfiguration -Force -CimSession $cimSession -ErrorAction ignore
  158. remove-item -Path @(".\InstallBasicModules", ".\LCMConfig", ".\SwarmConfiguration") -Force -Recurse
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement