Created
August 26, 2019 21:47
-
-
Save peplau/1f699b638c0b20e5ef9a2d22982c800d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Deployer-Prepare { | |
Param($source,$target,$anti) | |
# Pre-validations | |
$sourceExists = Test-Path $source | |
if ($sourceExists -eq $false){ | |
Write-Output "Source directory is wrong - $source" | |
return $false | |
} | |
$targetExists = Test-Path $target | |
if ($targetExists -eq $false){ | |
Write-Output "Target directory is wrong - $target" | |
return $false | |
} | |
$sourceFolder = Get-Item $source | |
$targetFolder = Get-Item $target | |
# Build Anti | |
# Create or get antiFolder | |
$antiExists = Test-Path $anti | |
if ($antiExists -eq $false){ | |
$antiFolder = New-Item $anti -ItemType Directory | |
} | |
else { | |
$antiFolder = Get-Item $anti | |
} | |
# Loop thru Source to create anti | |
Write-Output "Create Anti-package on $anti" | |
$sourceFolderChild = Get-ChildItem $sourceFolder -Recurse | |
Foreach($sourceEntry in $sourceFolderChild) { | |
$relativePath = $($sourceEntry.FullName).Replace($source,"") | |
$antiPath = "$anti$relativePath" | |
$antiEntryExists = Test-Path $antiPath | |
$targetPath = "$target$relativePath" | |
$targetEntryExists = Test-Path $targetPath | |
#$antiEntry = Get-Item $antiPath | |
if ($targetEntryExists -eq $false) { | |
# If file is not on target - its a new file - should be *.delete on anti | |
Copy-Item $sourceEntry.FullName -Destination $antiPath | |
$antiEntry = Get-Item $antiPath | |
$antiEntryName = $antiEntry.Name; | |
$antiEntry = Rename-Item $antiEntry -NewName "$($antiEntryName).delete" | |
} | |
else { | |
# Copy from target to Anti | |
$targetEntry = Get-Item $targetPath | |
$antiEntry = Copy-Item $targetEntry -Destination $antiPath | |
} | |
} | |
Write-Output "Anti-package created on $anti" | |
Write-Output "To deploy -> Deployer-Run 'c:\sourcepath' -target 'c:\targetpath' " | |
Write-Output "To rollback -> Deployer-Rollback 'c:\targetpath' -anti 'c:\antipath' " | |
} | |
Function Deployer-Run { | |
Param($source,$target) | |
# Pre-validations | |
$sourceExists = Test-Path $source | |
if ($sourceExists -eq $false){ | |
Write-Output "Source directory is wrong - $source" | |
return $false | |
} | |
$targetExists = Test-Path $target | |
if ($targetExists -eq $false){ | |
Write-Output "Target directory is wrong - $target" | |
return $false | |
} | |
$sourceFolder = Get-Item $source | |
$targetFolder = Get-Item $target | |
Write-Output "Deploy start - from: '$source' to '$target'" | |
$sourceFolderChild = Get-ChildItem $sourceFolder -Recurse | |
Foreach($sourceEntry in $sourceFolderChild) { | |
$relativePath = $($sourceEntry.FullName).Replace($source,"") | |
$targetPath = "$target$relativePath" | |
Copy-Item $sourceEntry.FullName -Destination $targetPath -Force | |
Write-Output "Copied: $targetPath" | |
} | |
Write-Output "Deploy finished" | |
Write-Output "To rollback -> Deployer-Rollback 'c:\targetpath' -anti 'c:\antipath' " | |
} | |
Function Deployer-Rollback { | |
Param($target,$anti) | |
# Pre-validations | |
$antiExists = Test-Path $anti | |
if ($antiExists -eq $false){ | |
Write-Output "Anti-package directory is wrong - $anti" | |
return $false | |
} | |
$targetExists = Test-Path $target | |
if ($targetExists -eq $false){ | |
Write-Output "Target directory is wrong - $target" | |
return $false | |
} | |
$antiFolder = Get-Item $anti | |
$targetFolder = Get-Item $target | |
# Undo | |
$antiFolderChild = Get-ChildItem $antiFolder -Recurse | |
Foreach($antiEntry in $antiFolderChild) { | |
$relativePath = $($antiEntry.FullName).Replace($anti,"") | |
$targetPath = "$target$relativePath" | |
$isDelete = $($relativePath).EndsWith(".delete") | |
if ($isDelete){ | |
# Delete from target | |
$targetPath = $targetPath.Replace(".delete","") | |
Remove-Item $targetPath -Force | |
Write-Output "Deleted: $targetPath" | |
} | |
else { | |
# Copy from anti to target | |
Copy-Item $antiEntry.FullName -Destination $targetPath -Force | |
Write-Output "Rolled back: $targetPath" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment