Skip to content

Commit 1670f82

Browse files
committed
Adding PWSH module to support local publishing
Defines two cmdlets comparable with those that come wth Classic Pull Server for publishing Modules and Configs.
1 parent b3d1f66 commit 1670f82

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
#!/usr/bin/env pwsh
2+
3+
function Publish-ToLocalTug {
4+
<#
5+
.SYNOPSIS
6+
This cmdlet is used to publish configurations (MOF files) and supporting modules to the
7+
appropriate file locations used by a local TugDSC Server instance for serving them up.
8+
9+
.PARAMETER Source
10+
One ore more paths to DSC configurations (MOF files) or to directories containing
11+
configurations to be published. If not specified, defaults to the current directory.
12+
13+
.PARAMETER ModuleNameList
14+
One or more module names that will be located in the current PowerShell module path,
15+
packaged up and published for retrieval from the TugDSC server.
16+
17+
.PARAMETER Force
18+
Unless this flag is specified, existing configurations and modules (of the same version)
19+
that are already published won't be overwritten.
20+
21+
.PARAMETER ConfigSettings
22+
By default, the current TugDSC configuration settings are resolved against a well-known
23+
path to a configuration settings file that defines the location of configurations and
24+
modules that TugDSC can serve up. You can use this parameter to override that path
25+
if your setup does not contain the TugDSC config settings file in its default location.
26+
#>
27+
[CmdletBinding()]
28+
param(
29+
[string[]]$Source=$PWD,
30+
[string[]]$ModuleNameList,
31+
[switch]$Force,
32+
[string]$ConfigSettings="$($env:ProgramData)\TugDSC\Server\appsettings.json"
33+
)
34+
35+
Write-Verbose "Loading TugDSC Server configuration settings from [$($ConfigSettings)]"
36+
if (-not (Test-Path -PathType Leaf $ConfigSettings)) {
37+
Write-Error "Missing TugDSC Server configuration settings file"
38+
return
39+
}
40+
41+
$configJson = ConvertFrom-Json $ConfigSettings -ErrorAction Stop
42+
43+
Write-Verbose "Read in configuration settings:"
44+
$checksum = $configJson.appSettings.checksum.default
45+
Write-Verbose " * defaults to checksum [$($checksum)]"
46+
$mofFilesPath = $configJson.appSettings.handler.params.ConfigurationPath
47+
Write-Verbose " * configuration MOF files stored at [$($mofFilesPath)]"
48+
$modulesPath = $configJson.appSettings.handler.params.ModulePath
49+
Write-Verbose "Configuration default to checksum [$($checksum)]"
50+
51+
$tempMofFilesZip = [System.IO.Path]::GetTempFileName()
52+
$zipSources = @()
53+
foreach ($s in $Source) {
54+
[string]$sourcePath = $s -replace "[/\\]+$","" ## Remove any trailing slashes
55+
if ($sourcePath.EndsWith(".mof", $true)) {
56+
$zipSources += $s
57+
}
58+
else {
59+
$zipSources += ($s + "/*.mof")
60+
}
61+
}
62+
Compress-Archive -DestinationPath $tempMofFilesZip -Path $zipSources
63+
}
64+
65+
66+
function Publish-ModuleToLocalTug {
67+
<#
68+
.SYNOPSIS
69+
Deploy DSC modules to TugDSC Server.
70+
71+
.DESCRIPTION
72+
Publish DSC module using Module Info object as an input.
73+
The cmdlet will figure out the location of the module repository using appsettings.json of the TugDSC Server.
74+
75+
.PARAMETER Name
76+
Name of module.
77+
78+
.PARAMETER ModuleBase
79+
This is the location of the base of the module.
80+
81+
.PARAMETER Version
82+
This is the version of the module.
83+
84+
.PARAMETER ConfigSettings
85+
Optionally, override the default location to read TugDSC Server configuration settings from.
86+
This is needed to resolve the directory to which the modules will be published. This is not
87+
needed if the output directory is explecitly specified.
88+
89+
.PARAMETER OutputFolderPath
90+
By default, this will be resolved by inspecting the TugDSC Server's configuration settings.
91+
92+
.PARAMETER Force
93+
Forces an overwrite of the published module files if they already exist in the target publish
94+
directory.
95+
96+
.EXAMPLE
97+
Get-Module <ModuleName> | Publish-ModuleToLocalTug
98+
#>
99+
[CmdletBinding()]
100+
[Alias("mod2tug")]
101+
[OutputType([void])]
102+
param(
103+
[Parameter(Mandatory=$true, Position=0,
104+
ValueFromPipelineByPropertyName=$true)]
105+
[string]$Name,
106+
107+
[Parameter(Mandatory=$true, Position=1,
108+
ValueFromPipelineByPropertyName=$true)]
109+
[string]$ModuleBase,
110+
111+
[Parameter(Mandatory = $true, Position=2,
112+
ValueFromPipelineByPropertyName = $true)]
113+
[string]$Version,
114+
115+
[string]$ConfigSettings = "$($env:ProgramData)\TugDSC\Server\appsettings.json",
116+
117+
[string]$OutputFolderPath = $null,
118+
119+
[switch]$Force
120+
)
121+
122+
begin {
123+
if (-not $OutputFolderPath) {
124+
Write-Verbose "Loading TugDSC Server configuration settings from [$($ConfigSettings)]"
125+
if (-not (Test-Path -PathType Leaf $ConfigSettings)) {
126+
Write-Error "Missing TugDSC Server configuration settings file (appsettings.json)"
127+
return
128+
}
129+
130+
$configJson = ConvertFrom-Json $ConfigSettings -ErrorAction Stop
131+
132+
Write-Verbose "Read in configuration settings:"
133+
$modulesPath = $configJson.appSettings.handler.params.ModulePath
134+
Write-Verbose " * modules stored at [$($modulesPath)]"
135+
$mofFilesPath = $configJson.appSettings.handler.params.ConfigurationPath
136+
Write-Verbose " * configuration MOF files stored at [$($mofFilesPath)]"
137+
138+
$OutputFolderPath = $modulesPath
139+
}
140+
}
141+
process {
142+
Write-Verbose "Name: $Name , ModuleBase : $ModuleBase ,Version: $Version"
143+
$targetPath = Join-Path $OutputFolderPath "$($Name)_$($Version).zip"
144+
145+
if (Test-Path $targetPath) {
146+
if (-not $Force) {
147+
Write-Error "Existing published module found. Specify -Force flag to overwrite."
148+
return
149+
}
150+
Compress-Archive -DestinationPath $targetPath -Path "$($ModuleBase)\*" -Update -Verbose
151+
}
152+
else {
153+
Compress-Archive -DestinationPath $targetPath -Path "$($ModuleBase)\*" -Verbose
154+
}
155+
}
156+
end {
157+
## NOTE, we don't have to compute a checksum because that's
158+
## handled by the BasicDscHandler that comes with TugDSC
159+
}
160+
}
161+
162+
function Publish-MOFToLocalTug {
163+
<#
164+
.SYNOPSIS
165+
Deploy DSC Configuration document to the TugDSC Server.
166+
167+
.DESCRIPTION
168+
Publish MOF file to the TugDSC Server. It takes File Info object as pipeline input.
169+
It also auto detects the location of the configuration repository using the appsettings.json of the TugDSC.
170+
171+
.PARAMETER FullName
172+
Absolute path to the DSC configuration (MOF) file.
173+
174+
.PARAMETER ConfigSettings
175+
Optionally, override the default location to read TugDSC Server configuration settings from.
176+
This is needed to resolve the directory to which the MOF files will be published. This is not
177+
needed if the output directory is explecitly specified.
178+
179+
.PARAMETER OutputFolderPath
180+
By default, this will be resolved by inspecting the TugDSC Server's configuration settings.
181+
182+
.PARAMETER Force
183+
Forces an overwrite of the published MOF files if they already exist in the target publish
184+
directory.
185+
186+
.EXAMPLE
187+
dir <path>\*.mof | Publish-MOFToLocalTug
188+
#>
189+
[CmdletBinding()]
190+
[Alias("mof2tug")]
191+
[OutputType([void])]
192+
param(
193+
# Mof file Name
194+
[Parameter(Mandatory = $true,Position=0,
195+
ValueFromPipelineByPropertyName = $true)]
196+
[string]$FullName,
197+
198+
[string]$ConfigSettings = "$($env:ProgramData)\TugDSC\Server\appsettings.json",
199+
200+
[string]$OutputFolderPath = $null,
201+
202+
[switch]$Force
203+
)
204+
205+
begin {
206+
if (-not $OutputFolderPath) {
207+
Write-Verbose "Loading TugDSC Server configuration settings from [$($ConfigSettings)]"
208+
if (-not (Test-Path -PathType Leaf $ConfigSettings)) {
209+
Write-Error "Missing TugDSC Server configuration settings file (appsettings.json)"
210+
return
211+
}
212+
213+
$configJson = ConvertFrom-Json $ConfigSettings -ErrorAction Stop
214+
215+
Write-Verbose "Read in configuration settings:"
216+
$modulesPath = $configJson.appSettings.handler.params.ModulePath
217+
Write-Verbose " * modules stored at [$($modulesPath)]"
218+
$mofFilesPath = $configJson.appSettings.handler.params.ConfigurationPath
219+
Write-Verbose " * configuration MOF files stored at [$($mofFilesPath)]"
220+
221+
$OutputFolderPath = $mofFilesPath
222+
}
223+
}
224+
process {
225+
$sourceInfo = [System.IO.FileInfo]::new($FullName)
226+
if ($sourceInfo.Extension -ine '.mof') {
227+
Write-Error "Invalid file $FullName. Only mof files can be copied to the pullserver configuration repository"
228+
return
229+
}
230+
231+
$targetPath = Join-Path $OutputFolderPath $sourceInfo.Name
232+
if ((Test-Path $targetPath) -and -not $Force) {
233+
Write-Error "Existing published configuration found. Specify -Force flag to overwrite."
234+
return
235+
}
236+
237+
if (-not (Test-Path $FullName)) {
238+
Write-Error "File not found at $FullName"
239+
}
240+
241+
Copy-Item $FullName $OutputFolderPath -Verbose -Force
242+
}
243+
end {
244+
## NOTE, we don't have to compute a checksum because that's
245+
## handled by the BasicDscHandler that comes with TugDSC
246+
}
247+
}

0 commit comments

Comments
 (0)