Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param(
- [Parameter(Mandatory = $true)]
- [ValidateScript ( {$_.State -eq 'opened'})]
- [System.Management.Automation.Runspaces.PSSession]$Session,
- [Parameter(Mandatory = $false)]
- [string]$graphLocation = "D:\images"
- )
- if ($cimSession -eq $null) {
- $cimSession = New-CimSession -ComputerName $session.ComputerName -Credential $session.Runspace.ConnectionInfo.Credential -errorAction stop
- }
- $nodename = $cimSession.ComputerName
- [DSCLocalConfigurationManager()]
- configuration LCMConfig
- {
- # Parameter help description
- param(
- [Parameter(Mandatory = $false)]
- [ValidateNotNullOrEmpty()]
- [string]$nodename = $script:nodename
- )
- Node $nodename
- {
- Settings {
- RebootNodeIfNeeded = $true
- ConfigurationMode = 'ApplyAndMonitor'
- ActionAfterReboot = 'ContinueConfiguration'
- }
- }
- }
- Configuration SwarmConfiguration
- {
- param (
- [Parameter(Mandatory = $false)]
- [ValidateNotNullOrEmpty()]
- [string]$nodename = $script:nodename
- )
- Import-DscResource -ModuleName 'PSDesiredStateConfiguration', 'xPSDesiredStateConfiguration'
- Import-DSCResource -name "PackageManagement"
- Import-DSCResource -Module "cChoco"
- node $nodename {
- PackageManagement DockerExecutable {
- Ensure = 'present'
- Name = "Docker"
- ProviderName = "DockerMsftProvider"
- DependsOn = "[WindowsFeature]ContainerInstall"
- }
- WindowsFeature ContainerInstall {
- Ensure = "Present"
- Name = "Containers"
- }
- File DockerImagesFolder {
- DestinationPath = $graphLocation
- Type = 'Directory'
- }
- Service DockerServiceConfig {
- Name = "docker"
- State = "Running"
- DependsOn = @("[PackageManagement]DockerExecutable")
- }
- cChocoInstaller installChoco {
- InstallDir = "c:\choco"
- }
- cChocoPackageInstallerSet installSomeStuff {
- Ensure = 'Present'
- Name = @(
- "classic-shell"
- "7zip"
- "visualstudiocode"
- )
- DependsOn = "[cChocoInstaller]installChoco"
- }
- Script DockerConfigFile {
- TestScript = {
- $configFile = Join-path -Path "$($env:programdata)" -ChildPath "docker\config\daemon.json"
- If (Test-path $configFile) {
- $dockerConfig = Get-Content $configFile | convertFrom-json
- if ($dockerConfig.graph -eq $using:graphLocation) { return $true; } else {return $false}
- }
- else {
- return $false
- }
- }
- GetScript = {
- $configFile = Join-path -Path "$($env:programdata)" -ChildPath "docker\config\daemon.json"
- return {@{configValue = (Get-Content $configFile | convertFrom-json)}
- }
- }
- SetScript = {
- $dockerConfigFilePath = Join-path -Path "$($env:programdata)" -ChildPath "docker\config\daemon.json"
- If (Test-path $dockerConfigFilePath) {
- $dockerConfig = Get-Content $dockerConfigFilePath | convertfrom-json
- If ( (Get-Member -InputObject $dockerconfig -Name graph) -is [object] ) {
- $dockerConfig.graph = $using:graphLocation
- }
- else {
- Write-Verbose "ELSE $dockerConfig" | convertto-JSON
- $dockerConfig | Add-Member -MemberType NoteProperty -Name graph -Value $using:graphLocation
- }
- Write-Verbose $dockerConfig | convertto-JSON
- $dockerConfig | convertto-Json | out-file $dockerConfigFilePath -Encoding ASCII
- }
- else {
- @{graph = "$($using:graphlocation)"} | convertto-json | out-file $dockerConfigFilePath -Encoding ASCII
- }
- Restart-service docker
- }
- }
- }
- }
- Configuration InstallBasicModules
- {
- param (
- [Parameter(Mandatory = $false)]
- [ValidateNotNullOrEmpty()]
- [string]$nodename = $script:nodename
- )
- Import-DscResource -ModuleName 'PSDesiredStateConfiguration'
- Import-dscresource -Name "PackageManagement"
- #Import-DscResource -Module 'packagemanagement' -moduleversion '1.1.3.0'
- node $nodename {
- PackageManagement xPSDesiredStateConfiguration {
- Ensure = 'present'
- Name = "xPSDesiredStateConfiguration"
- Source = "PSGallery"
- }
- PackageManagement cChoco {
- Ensure = 'present'
- Name = "cChoco"
- Source = "PSGallery"
- }
- PackageManagement DockerModule {
- Ensure = 'present'
- name = 'DockerMsftProvider'
- Source = "PSGallery"
- }
- }
- }
- $scriptblock = {
- if ((Get-PackageProvider -listavailable -name nuget -erroraction SilentlyContinue).Count -eq 0) {Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force}
- #if ((Get-PackageProvider -listavailable -name Chocolatey -erroraction SilentlyContinue).Count -eq 0) {Register-PackageSource -Name chocolatey -ProviderName Chocolatey -Location http://chocolatey.org/api/v2/ -force }
- if ((Get-Module -listavailable -name PowerShellGet -erroraction SilentlyContinue).Count -eq 1) {Install-Module -Name PowerShellGet -Force -Verbose -Scope AllUsers -Repository PSGallery}
- }
- Invoke-Command -session $session -scriptblock $scriptblock -errorAction stop
- InstallBasicModules -OutputPath .\InstallBasicModules
- LCMConfig -OutputPath .\LCMConfig
- SwarmConfiguration -Outputpath .\SwarmConfiguration
- Set-DscLocalConfigurationManager -Verbose -Path .\LCMConfig -CimSession $cimSession
- Start-DscConfiguration -Wait -Verbose -Path .\InstallBasicModules -Force -CimSession $cimSession
- Start-DscConfiguration -Wait -Verbose -Path .\SwarmConfiguration -Force -CimSession $cimSession -ErrorAction ignore
- remove-item -Path @(".\InstallBasicModules", ".\LCMConfig", ".\SwarmConfiguration") -Force -Recurse
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement