Skip to content

Commit

Permalink
Adding LibrarySetToInclude/Exclude to project space
Browse files Browse the repository at this point in the history
  • Loading branch information
BobJWalker committed Nov 18, 2021
1 parent b366c29 commit 2f3e85a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 17 deletions.
9 changes: 8 additions & 1 deletion CloneSpace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ param (

$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
Expand Down Expand Up @@ -255,4 +257,9 @@ $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."
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."
10 changes: 10 additions & 0 deletions CloneSpaceProject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ param (
$TenantsToInclude,
$ChannelsToInclude,
$ChannelsToExclude,
$LibraryVariableSetsToInclude,
$LibraryVariableSetsToExclude,
$OverwriteExistingVariables,
$OverwriteExistingCustomStepTemplates,
$OverwriteExistingLifecyclesPhases,
Expand Down Expand Up @@ -101,6 +103,7 @@ $WorkersToInclude = Test-OctopusIncludeExcludeFilterParameter -includeFilters $W
$TargetsToInclude = Test-OctopusIncludeExcludeFilterParameter -includeFilters $TargetsToInclude -excludeFilters $TargetsToExclude -parameterName "Target" -DefaultIncludeFilter "$null"
$TenantsToInclude = Test-OctopusIncludeExcludeFilterParameter -includeFilters $TenantsToInclude -excludeFilters $TenantsToExclude -parameterName "Tenants" -DefaultIncludeFilter "all"
$ChannelsToInclude = Test-OctopusIncludeExcludeFilterParameter -includeFilters $ChannelsToInclude -excludeFilters $ChannelsToExclude -parameterName "Channels" -DefaultIncludeFilter "all"
$LibraryVariableSetsToInclude = Test-OctopusIncludeExcludeFilterParameter -includeFilters $LibraryVariableSetsToInclude -excludeFilters $LibraryVariableSetsToExclude -parameterName "Library Variable Sets" -DefaultIncludeFilter "all"

$cloneSpaceCommandLineOptions = @{
EnvironmentsToClone = $null;
Expand Down Expand Up @@ -579,6 +582,7 @@ $tenantListToExclude = Get-OctopusExclusionList -itemList $sourceData.TenantList
$envrionmentListToExclude = Get-OctopusExclusionList -itemList $sourceData.EnvironmentList -itemType "Environments" -filters $environmentsToExclude -includeFilters $EnvironmentsToInclude
$workerListToExclude = Get-OctopusExclusionList -itemList $sourceData.WorkerList -itemType "Workers" -filters $WorkersToExclude -includeFilters $WorkersToInclude
$targetListToExclude = Get-OctopusExclusionList -itemList $sourceData.TargetList -itemType "Targets" -filters $TargetsToExclude -includeFilters $TargetsToInclude
$variableSetListToExclude = Get-OctopusExclusionList -itemList $sourceData.VariableSetList -itemType "Library Variable Sets" -filters $LibraryVariableSetsToExclude -includeFilters $LibraryVariableSetsToInclude

Write-OctopusSuccess "Building all project list to clone"
$projectListToClone = Get-OctopusFilteredList -itemList $sourceData.ProjectList -itemType "Projects" -filters $ProjectsToClone
Expand All @@ -602,6 +606,12 @@ foreach ($project in $projectListToClone)
}
elseif ($null -ne $variableSet)
{
if (Get-OctopusIsInExclusionList -exclusionList $variableSetListToExclude -itemName $($variableSet.Name))
{
Write-OctopusVerbose " The variable set $($variableSet.Name) was in the exclusion list, skipping."
continue
}

Write-OctopusSuccess " Adding the variable set $($variableSet.Name) to the items variable sets to clone"
$cloneSpaceCommandLineOptions.LibraryVariableSetsToClone = Add-OctopusIdToCloneList -itemId $variableSetId -itemType "Library Variable Set" -destinationList $cloneSpaceCommandLineOptions.LibraryVariableSetsToClone -sourceList $sourceData.VariableSetList -exclusionList @()
$sourceVariableSetVariables = Get-OctopusVariableSetVariables -variableSet $variableSet -OctopusData $sourceData
Expand Down
4 changes: 4 additions & 0 deletions docs/CloneSpaceProjectParameterReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Channels
- `ChannelsToInclude`: The list of channels to include from this clone. The default is `all`.
- `ChannelsToExclude`: The list of channels to exclude from this clone. The default is `$null`.

Library Variable Sets
- `LibraryVariableSetsToInclude`: The list of channels to include from this clone. The default is `all`.
- `LibraryVariableSetsToExclude`: The list of channels to exclude from this clone. The default is `$null`.

## Scoping Match Options

Imagine if your source instance had the environments `Development` and `Test` while the destination only had `Production`. You have a step scoped to only run on `Development`. When that step is cloned over what should it do?
Expand Down
8 changes: 7 additions & 1 deletion src/Cloners/ProjectCloner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,13 @@ function Get-OctopusProjectReferencedVariableSets

if ($null -ne $sourceVariableSet)
{
$matchingVariableSetId = Convert-SourceIdToDestinationId -SourceList $sourceData.VariableSetList -DestinationList $destinationData.VariableSetList -IdValue $SourceVariableSetId -ItemName "$($project.Name) Variable Sets" -MatchingOption "ErrorUnlessExactMatch"
$matchingVariableSetId = Convert-SourceIdToDestinationId -SourceList $sourceData.VariableSetList -DestinationList $destinationData.VariableSetList -IdValue $SourceVariableSetId -ItemName "$($project.Name) Variable Sets" -MatchingOption "IgnoreMismatch"

if ($null -eq $matchingVariableSetId)
{
Write-OctopusCritical "The variable set $($sourceVariableSet.Name) could not be found on the destination instance. Your deployments might fail because variables are missing. Continuing as you might have intentionally skipped this variable set."
continue
}

$returnList += $matchingVariableSetId
}
Expand Down
12 changes: 6 additions & 6 deletions src/Cloners/WorkerPoolCloner.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ function Copy-OctopusWorkerPools
continue
}

if ($DestinationData.OctopusUrl -like "*.octopus.app" -and $SourceData.OctopusUrl -notlike "*.octopus.app" -and $workerPool.IsDefault -eq $true)
{
Write-OctopusWarning "The source is self-hosted, the destination is Octopus Cloud, and the worker pool is the default. Skipping."
continue
}

Write-OctopusVerbose "Starting Clone of Worker Pool $($workerPool.Name)"

$matchingItem = Get-OctopusItemByName -ItemName $workerPool.Name -ItemList $destinationData.WorkerPoolList
Expand All @@ -46,6 +40,12 @@ function Copy-OctopusWorkerPools

$copyOfItemToClone = Copy-OctopusObject -ItemToCopy $workerpool -SpaceId $destinationData.SpaceId -ClearIdValue $true

if ($DestinationData.OctopusUrl.ToLower().Trim() -like "*.octopus.app" -and $SourceData.OctopusUrl.ToLower().Trim() -notlike "*.octopus.app" -and $copyOfItemToClone.IsDefault -eq $true)
{
$copyOfItemToClone.IsDefault = $false
Write-OctopusVerbose "Cloning from self-hosted to the cloud and this worker pool is marked as the default. Updating to be not the default."
}

$added = Add-PropertyIfMissing -objectToTest $copyOfItemToClone -propertyName "WorkerPoolType" -propertyValue "StaticWorkerPool"

$newOctopusWorker = Save-OctopusWorkerPool -workerPool $copyOfItemToClone -destinationData $destinationData
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Logging.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$currentDate = Get-Date
$currentDateFormatted = $currentDate.ToString("yyyy_MM_dd_HH_mm_ss")
$clonerVersion = "3.1.0"
$clonerVersion = "3.2.0"

$logFolder = "$PSScriptRoot\..\..\"
$logArchiveFolder = "$PSScriptRoot\..\..\logs\archive_$currentDateFormatted"
Expand Down
27 changes: 19 additions & 8 deletions src/Core/Util.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,27 @@ function Get-OctopusItemById
$ItemId
)

Write-OctopusVerbose "Attempting to find $ItemId in the item list of $($ItemList.Length) item(s)"
Write-OctopusVerbose "Attempting to find $ItemId in the item list of $($ItemList.Length) item(s)"

foreach($item in $ItemList)
$itemListConverted = @($ItemList)

if ($null -eq $itemListConverted)
{
Write-OctopusVerbose "Checking to see if $($item.Id) matches with $ItemId"
if ($item.Id -eq $ItemId)
{
Write-OctopusVerbose "The Ids match, return the item $($item.Name)"
return $item
}
Write-OctopusVerbose "Unable to find $itemId in the list because the list is null"
return $null
}

if ($itemListConverted.Count -eq 0)
{
Write-OctopusVerbose "Unable to find $ItemId in the list because the list is empty"
return $null
}

$matchingItem = $itemListConverted | Where-Object {$_.Id -eq $itemId}
if ($null -ne $matchingItem)
{
Write-OctopusVerbose "Found matching id, returning the item $($matchingItem[0].Name)"
return $matchingItem[0]
}

Write-OctopusVerbose "No match found returning null"
Expand Down

0 comments on commit 2f3e85a

Please sign in to comment.