############### MAIN ############### # main function # $Main = { [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $destHost, [Parameter(Mandatory=$false)] [string[]] $apps ) $applist=[System.Collections.Generic.List[object]]::new() If($PSBoundParameters.ContainsKey("apps")) { foreach ($appname in $apps) { if ($appname.Contains('*')) { $wclist=Get-ChildItem $appname -Directory | ForEach-Object { $_.Name } # if wildcard is specified, we only add folders with a 'stacks' subfolder foreach ($wcname in $wclist) { if ((Test-Path -Path "$wcname\stacks")) { $applist.Add($wcname) } } } else { # here we don't have to check for a 'stacks' subfolder as the user specified appname explicitly $applist.Add($appname) } } # remove duplicates $uniqlist=$applist | Sort-Object -Unique foreach ($appname in $uniqlist) { copyfolder $destHost $appname "$appname\" } } else { # we're in the app subfolder, so only one stacks folder to copy $appname="$pwd".Split("\\")[-1] copyfolder $destHost $appname ".\" } } ####################################################################### # copies specified app's stacks and traefik-rules subfolders to truenas # Function copyfolder { [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $destHost, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $appname, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $srcdir ) $srcStacksDir=$srcdir+"stacks" $srcTraefikRulesDir=$srcdir+"traefik-rules" if (-Not(Test-Path -Path $srcStacksDir)) { Write-Host "Source path doesn't exist: ""$PSScriptRoot\$srcStacksDir\*.*"". No files copied!" -ForegroundColor Red Write-Host "Usage: in app folder : ./cp2nas DEST|IP" Write-Host " in app parent folder: ./cp2nas DEST|IP app1, app2, app3, ..." Write-Host " in app parent folder: ./cp2nas DEST|IP *" Write-Host " in app parent folder: ./cp2nas DEST|IP wcstring1, wcstring2, ..." return } $destStacks="/mnt/SSD1/docker/stacks/" $destDir=$destStacks+$appname+"/" $destTraefikRulesDir=$destStacks+"traefik/rules/" $date=Get-Date -format "yyyy-MM-ddTHH:mm:ss" Write-Host "$date# pscp -P 22 -r ""$srcStacksDir\*.*"" root@${destHost}:""${destDir}""" -ForegroundColor Green Write-Host "Copying to ${destHost}:""${destStacks}" -NoNewline Write-Host "${appname}/" -ForegroundColor DarkYellow -NoNewline Write-Host """:" pscp -P 22 -r "$srcStacksDir\*.*" root@${destHost}:""${destDir}"" if ((Test-Path -Path "$srcTraefikRulesDir")) { Write-Host "$date# pscp -P 22 -r ""$srcTraefikRulesDir\*.*"" root@${destHost}:""${destTraefikRulesDir}""" -ForegroundColor Green Write-Host "Copying to ${destHost}:""${destStacks}" -NoNewline Write-Host "traefik/rules/" -ForegroundColor DarkYellow -NoNewline Write-Host """:" pscp -P 22 -r "$srcTraefikRulesDir\*.*" root@${destHost}:""${destTraefikRulesDir}"" } } ################################### & $Main @Args