-
Notifications
You must be signed in to change notification settings - Fork 18
/
CloneSpace.ps1
268 lines (241 loc) · 19.2 KB
/
CloneSpace.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
param (
$SourceOctopusUrl,
$SourceOctopusApiKey,
$SourceSpaceName,
$DestinationOctopusUrl,
$DestinationOctopusApiKey,
$DestinationSpaceName,
$EnvironmentsToClone,
$WorkerPoolsToClone,
$ProjectGroupsToClone,
$TenantTagsToClone,
$ExternalFeedsToClone,
$StepTemplatesToClone,
$InfrastructureAccountsToClone,
$LibraryVariableSetsToClone,
$LifeCyclesToClone,
$ScriptModulesToClone,
$MachinePoliciesToClone,
$WorkersToClone,
$TargetsToClone,
$ProjectsToClone,
$ParentProjectName,
$ChildProjectsToSync,
$TenantsToClone,
$SpaceTeamsToClone,
$PackagesToClone,
$RunbooksToClone,
$CertificatesToClone,
$ChannelsToClone,
$OverwriteExistingVariables,
$OverwriteExistingCustomStepTemplates,
$OverwriteExistingLifecyclesPhases,
$CloneProjectRunbooks,
$CloneTeamUserRoleScoping,
$CloneProjectChannelRules,
$CloneProjectVersioningReleaseCreationSettings,
$CloneProjectDeploymentProcess,
$CloneProjectLogos,
$CloneTenantLogos,
$CloneStepTemplateLogos,
$CloneTenantVariables,
$ProcessEnvironmentScopingMatch,
$ProcessChannelScopingMatch,
$ProcessTenantTagScopingMatch,
$VariableChannelScopingMatch,
$VariableEnvironmentScopingMatch,
$VariableProcessOwnerScopingMatch,
$VariableActionScopingMatch,
$VariableMachineScopingMatch,
$VariableAccountScopingMatch,
$VariableCertificateScopingMatch,
$VariableTenantTagScopingMatch,
$VariableWorkerPoolScopingMatch,
$InfrastructureEnvironmentScopingMatch,
$InfrastructureTenantScopingMatch,
$InfrastructureTenantTagScopingMatch,
$ProcessCloningOption,
$IgnoreVersionCheckResult,
$SkipPausingWhenIgnoringVersionCheckResult,
$WhatIf
)
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Core", "Logging.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Core", "Util.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Core", "FilteredLists.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Core", "ParameterVerification.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "DataAccess", "OctopusDataAdapter.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "DataAccess", "OctopusDataFactory.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "DataAccess", "OctopusRepository.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "DataAccess", "OctopusFakeFactory.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "AccountCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ActionCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "CertificateCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "EnvironmentCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ExternalFeedCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "LibraryVariableSetCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "LifecycleCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "LogoCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "MachinePolicyCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "PackageCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ParentProjectTemplateSyncer.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProcessCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectChannelCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectChannelRuleCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectDeploymentProcessCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectGroupCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectRunbookCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ProjectVariableCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "ScriptModuleCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "StepTemplateCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "TargetCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "TeamCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "TeamUserRoleCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "TenantCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "TenantVariableCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "TenantTagSetCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "VariableSetValuesCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "WorkerCloner.ps1"))
. ([System.IO.Path]::Combine($PSScriptRoot, "src", "Cloners", "WorkerPoolCloner.ps1"))
$ErrorActionPreference = "Stop"
$cloneStartDateTime = Get-Date
$OverwriteExistingVariables = Test-OctopusOverwriteExistingVariablesParameter -parameterValue $OverwriteExistingVariables -parameterName "OverwriteExistingVariables" -defaultValue $false
$OverwriteExistingCustomStepTemplates = Test-OctopusTrueFalseParameter -parameterValue $OverwriteExistingCustomStepTemplates -parameterName "OverwriteExistingCustomStepTemplates" -defaultValue $false
$OverwriteExistingLifecyclesPhases = Test-OctopusOverwriteExistingLifecyclesPhasesParameter -parameterValue $OverwriteExistingLifecyclesPhases
$CloneProjectRunbooks = Test-OctopusTrueFalseParameter -parameterValue $CloneProjectRunbooks -parameterName "CloneProjectRunbooks" -defaultValue $true
$CloneTeamUserRoleScoping = Test-OctopusTrueFalseParameter -parameterValue $CloneTeamUserRoleScoping -parameterName "CloneTeamUserRoleScoping" -defaultValue $false
$CloneProjectChannelRules = Test-OctopusTrueFalseParameter -parameterValue $CloneProjectChannelRules -parameterName "CloneProjectChannelRules" -defaultValue $false
$CloneProjectVersioningReleaseCreationSettings = Test-OctopusTrueFalseParameter -parameterValue $CloneProjectVersioningReleaseCreationSettings -parameterName "CloneProjectVersioningReleaseCreationSettings" -defaultValue $false
$CloneProjectDeploymentProcess = Test-OctopusTrueFalseParameter -parameterValue $CloneProjectDeploymentProcess -parameterName "CloneProjectDeploymentProcess" -defaultValue $true
$CloneProjectLogos = Test-OctopusTrueFalseParameter -parameterValue $CloneProjectLogos -parameterName "CloneProjectLogos" -defaultValue $true
$CloneTenantLogos = Test-OctopusTrueFalseParameter -parameterValue $CloneTenantLogos -parameterName "CloneTenantLogos" -defaultValue $true
$CloneStepTemplateLogos = Test-OctopusTrueFalseParameter -parameterValue $CloneStepTemplateLogos -parameterName "CloneStepTemplateLogos" -defaultValue $true
$CloneTenantVariables = Test-OctopusTrueFalseParameter -parameterValue $CloneTenantVariables -parameterName "CloneTenantVariables" -defaultValue $false
if ($null -ne $CertificatesToClone -and $CertificatesToClone.ToLower().Trim() -eq "all")
{
Write-OctopusCritical "The parameter CertificatesToClone is set to 'all'. That is the one parameter that cannot be set to all. You must specify specific certificates to clone with their password."
Exit 1
}
$ProcessCloningOption = Test-OctopusProcessCloningParameter -ParameterValue $ProcessCloningOption
$ProcessEnvironmentScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "ProcessEnvironmentScopingMatch" -ParameterValue $ProcessEnvironmentScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$ProcessChannelScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "ProcessChannelScopingMatch" -ParameterValue $ProcessChannelScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$ProcessTenantTagScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "ProcessTenantTagScopingMatch" -ParameterValue $ProcessTenantTagScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableChannelScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableChannelScopingMatch" -ParameterValue $VariableChannelScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableEnvironmentScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableEnvironmentScopingMatch" -ParameterValue $VariableEnvironmentScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableProcessOwnerScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableProcessOwnerScopingMatch" -ParameterValue $VariableProcessOwnerScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableActionScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableActionScopingMatch" -ParameterValue $VariableActionScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableMachineScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableMachineScopingMatch" -ParameterValue $VariableMachineScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableAccountScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableAccountScopingMatch" -ParameterValue $VariableAccountScopingMatch -DefaultValue "SkipUnlessExactMatch" -SingleValueItem $true
$VariableCertificateScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableCertificateScopingMatch" -ParameterValue $VariableCertificateScopingMatch -DefaultValue "SkipUnlessExactMatch" -SingleValueItem $true
$VariableTenantTagScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableTenantTagScopingMatch" -ParameterValue $VariableTenantTagScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$VariableWorkerPoolScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "VariableWorkerPoolScopingMatch" -ParameterValue $VariableWorkerPoolScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $true
$InfrastructureEnvironmentScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "InfrastructureEnvironmentScopingMatch" -ParameterValue $InfrastructureEnvironmentScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$InfrastructureTenantScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "InfrastructureTenantScopingMatch" -ParameterValue $InfrastructureTenantScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$InfrastructureTenantTagScopingMatch = Test-OctopusScopeMatchParameter -ParameterName "InfrastructureTenantTagScopingMatch" -ParameterValue $InfrastructureTenantTagScopingMatch -DefaultValue "SkipUnlessPartialMatch" -SingleValueItem $false
$IgnoreVersionCheckResult = Test-OctopusTrueFalseParameter -parameterValue $IgnoreVersionCheckResult -parameterName "IgnoreVersionCheckResult" -defaultValue $false
$SkipPausingWhenIgnoringVersionCheckResult = Test-OctopusTrueFalseParameter -parameterValue $SkipPausingWhenIgnoringVersionCheckResult -parameterName "SkipPausingWhenIgnoringVersionCheckResult" -defaultValue $false
$WhatIf = Test-OctopusTrueFalseParameter -parameterValue $WhatIf -parameterName "WhatIf" -defaultValue $false
$CloneScriptOptions = @{
EnvironmentsToClone = $EnvironmentsToClone;
WorkerPoolsToClone = $WorkerPoolsToClone;
ProjectGroupsToClone = $ProjectGroupsToClone;
TenantTagsToClone = $TenantTagsToClone;
ExternalFeedsToClone = $ExternalFeedsToClone;
StepTemplatesToClone = $StepTemplatesToClone;
InfrastructureAccountsToClone = $InfrastructureAccountsToClone;
LibraryVariableSetsToClone = $LibraryVariableSetsToClone;
LifeCyclesToClone = $LifeCyclesToClone;
ProjectsToClone = $ProjectsToClone;
OverwriteExistingVariables = $OverwriteExistingVariables;
OverwriteExistingCustomStepTemplates = $OverwriteExistingCustomStepTemplates;
OverwriteExistingLifecyclesPhases = $OverwriteExistingLifecyclesPhases;
TenantsToClone = $TenantsToClone;
ScriptModulesToClone = $ScriptModulesToClone;
TargetsToClone = $TargetsToClone;
MachinePoliciesToClone = $MachinePoliciesToClone;
WorkersToClone = $WorkersToClone;
CertificatesToClone = $CertificatesToClone;
CloneProjectRunbooks = $CloneProjectRunbooks;
ChildProjectsToSync = $ChildProjectsToSync;
ParentProjectName = $ParentProjectName;
SpaceTeamsToClone = $SpaceTeamsToClone;
PackagesToClone = $PackagesToClone;
RunbooksToClone = $RunbooksToClone;
ChannelsToClone = $ChannelsToClone;
CloneTeamUserRoleScoping = $CloneTeamUserRoleScoping;
CloneProjectChannelRules = $CloneProjectChannelRules;
CloneProjectVersioningReleaseCreationSettings = $CloneProjectVersioningReleaseCreationSettings;
CloneProjectDeploymentProcess = $CloneProjectDeploymentProcess;
CloneTenantVariables = $CloneTenantVariables;
CloneTenantLogos = $CloneTenantLogos;
CloneStepTemplateLogos = $CloneStepTemplateLogos;
CloneProjectLogos = $CloneProjectLogos;
ProcessEnvironmentScopingMatch = $ProcessEnvironmentScopingMatch;
ProcessChannelScopingMatch = $ProcessChannelScopingMatch;
ProcessTenantTagScopingMatch = $ProcessTenantTagScopingMatch;
VariableChannelScopingMatch = $VariableChannelScopingMatch;
VariableEnvironmentScopingMatch = $VariableEnvironmentScopingMatch;
VariableProcessOwnerScopingMatch = $VariableProcessOwnerScopingMatch;
VariableActionScopingMatch = $VariableActionScopingMatch;
VariableMachineScopingMatch = $VariableMachineScopingMatch;
VariableAccountScopingMatch = $VariableAccountScopingMatch;
VariableCertificateScopingMatch = $VariableCertificateScopingMatch;
VariableWorkerPoolScopingMatch = $VariableWorkerPoolScopingMatch;
VariableTenantTagScopingMatch = $VariableTenantTagScopingMatch;
InfrastructureEnvironmentScopingMatch = $InfrastructureEnvironmentScopingMatch;
InfrastructureTenantScopingMatch = $InfrastructureTenantScopingMatch;
InfrastructureTenantTagScopingMatch = $InfrastructureTenantTagScopingMatch;
ProcessCloningOption = $ProcessCloningOption;
}
Write-OctopusVerbose "The clone parameters sent in are:"
Write-OctopusVerbose $($CloneScriptOptions | ConvertTo-Json -Depth 10)
$sourceData = Get-OctopusData -octopusUrl $SourceOctopusUrl -octopusApiKey $SourceOctopusApiKey -spaceName $SourceSpaceName -whatIf $whatIf
$destinationData = Get-OctopusData -octopusUrl $DestinationOctopusUrl -octopusApiKey $DestinationOctopusApiKey -spaceName $DestinationSpaceName -whatIf $whatIf
Compare-OctopusVersions -SourceData $sourceData -DestinationData $destinationData -IgnoreVersionCheckResult $IgnoreVersionCheckResult -SkipPausingWhenIgnoringVersionCheckResult $SkipPausingWhenIgnoringVersionCheckResult
if ($sourceData.OctopusUrl -eq $destinationData.OctopusUrl -and $SourceSpaceName -eq $DestinationSpaceName)
{
$canProceed = $true
$CloneScriptOptions.PSObject.Properties | ForEach-Object {
$optionName = $_.Name
if ($optionName -like "*ToClone" -and [string]::IsNullOrWhiteSpace($_.Value) -eq $false)
{
Write-OctopusCritical "You are cloning to the same space on the same instance, but have a value for $optionName. This is not allowed. Please remove that parameter."
$canProceed = $false
}
}
if ($canProceed -eq $false)
{
Write-OctopusCritical "Invalid parameters detected. Please check log and correct them."
Exit 1
}
}
Copy-OctopusEnvironments -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusProjectGroups -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusTenantTags -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusBuiltInPackages -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Copy-OctopusExternalFeeds -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusSpaceTeams -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusStepTemplates -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusInfrastructureAccounts -SourceData $sourceData -DestinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Copy-OctopusScriptModules -SourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusMachinePolicies -SourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusLifecycles -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusWorkerPools -sourceData $sourceData -destinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusTenants -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions -firstRun $true
Copy-OctopusWorkers -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Copy-OctopusTargets -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Copy-OctopusCertificates -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Copy-OctopusLibraryVariableSets -SourceData $sourceData -DestinationData $destinationData -cloneScriptOptions $CloneScriptOptions
Copy-OctopusProjects -SourceData $sourceData -DestinationData $destinationData -CloneScriptOptions $CloneScriptOptions
# Repeating tenant clone to get all the project assignments
Copy-OctopusTenants -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions -firstRun $false
Copy-OctopusTenantVariables -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Copy-OctopusSpaceTeamUserRoles -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
Sync-OctopusMasterOctopusProjectWithChildProjects -sourceData $sourceData -destinationData $destinationData -CloneScriptOptions $CloneScriptOptions
$logPath = Get-OctopusLogPath
$cleanupLogPath = Get-OctopusCleanUpLogPath
Write-OctopusSuccess "The script to clone $SourceSpaceName from $SourceOctopusUrl to $DestinationSpaceName in $DestinationOctopusUrl has completed. Please see $logPath for more details."
Write-OctopusWarning "You have post clean-up tasks to finish. Any sensitive variables or encrypted values were created with dummy values which you must replace. Please see $cleanUpLogPath for a list of items to fix."
$cloneEndDateTime = Get-Date
$cloneElapsedTime = New-TimeSpan $cloneStartDateTime $cloneEndDateTime
Write-OctopusSuccess "It took $($cloneElapsedTime.Hours):$($cloneElapsedTime.Minutes):$($cloneElapsedTime.Seconds).$($cloneElapsedTime.Milliseconds) for the clone to finish."