From dcc9113cdaf30be59d885e56440fa90e3134a265 Mon Sep 17 00:00:00 2001 From: lksz Date: Fri, 15 Jan 2021 01:15:54 -0500 Subject: [PATCH] DockerCompose commands refactoring All dc... commands are now based on Invoke-DockerCompose. dco is aliased to Invoke-DockerCompose. dco_ is aliased to the docker-compose binary. '&' and '!' shorthand introduced to dco to execut in bg as job and wait for all bg jobs to complete respectively. --- docker/Aliases/dco.ps1 | 2 +- docker/Aliases/dco_.ps1 | 1 + docker/Invoke-DockerCompose.ps1 | 12 ++++---- docker/dcc.ps1 | 2 +- docker/dcdown.ps1 | 10 ++++--- docker/dcl.ps1 | 20 ++++++++++++- docker/dcll.ps1 | 20 ++++++++++++- docker/dcr.ps1 | 20 ++++++++++++- docker/dcre.ps1 | 20 ++++++++++++- docker/dcrefresh.ps1 | 29 ------------------ docker/dcreup.ps1 | 33 ++++++++++++++------ docker/dcup.ps1 | 53 +++++++++++++++++++++++---------- docker/dcx.ps1 | 20 ++++++++++++- 13 files changed, 172 insertions(+), 70 deletions(-) create mode 100644 docker/Aliases/dco_.ps1 delete mode 100644 docker/dcrefresh.ps1 diff --git a/docker/Aliases/dco.ps1 b/docker/Aliases/dco.ps1 index e41f756..2168c3e 100644 --- a/docker/Aliases/dco.ps1 +++ b/docker/Aliases/dco.ps1 @@ -1 +1 @@ -docker-compose +Invoke-DockerCompose diff --git a/docker/Aliases/dco_.ps1 b/docker/Aliases/dco_.ps1 new file mode 100644 index 0000000..e41f756 --- /dev/null +++ b/docker/Aliases/dco_.ps1 @@ -0,0 +1 @@ +docker-compose diff --git a/docker/Invoke-DockerCompose.ps1 b/docker/Invoke-DockerCompose.ps1 index 1a8cc54..8224a4f 100644 --- a/docker/Invoke-DockerCompose.ps1 +++ b/docker/Invoke-DockerCompose.ps1 @@ -51,9 +51,6 @@ $local:dcPathList = $ProjectPath | ForEach-Object { Get-ChildItem -Path $_ -Include 'docker-compose.yml' @gciParams | Select-Object -ExpandProperty Directory | Where-Object { $Force -or -not (Test-Path $(Join-Path $_ .noauto.all)) } - - # _DIRS_=$( find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/docker-compose.yml \; -exec test -f {}/.ask.all \; -print | sort ) - # _DIRS_="$_DIRS_ $( find . -mindepth 1 -maxdepth 1 -type d -exec test -f {}/docker-compose.yml \; -not -exec test -f {}/.ask.all \; -not -exec test -f {}/.noauto.all \; -print | sort )" } } if( -not $dcPathList ) { return } @@ -75,7 +72,7 @@ foreach( $local:p in $CliParams ) { if( $skipFirst ) { $p = $p | Select-Object -Skip 1 } } - $dcParams = $p | ForEach-Object { + $dcParams = $p | Where-Object { $_ } | ForEach-Object { if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ } else { "'$($_.Replace( "'", "''" ))'" } } @@ -85,13 +82,15 @@ foreach( $local:p in $CliParams ) { if( -not (Test-Path $dcPath) ) { if( $Recurse ) { return } else { throw "ERROR: dcPath ($dcPath) does not exists" } } Write-Verbose "& docker-compose --file $dcPath $dcParams" - if( $Wait ) { + if( $Wait -and -not $WhatIfPreference ) { CompleteJobs } if( -not $dcParams ) { continue } - if( $AsJob ) { + if( $WhatIfPreference ) { + Write-Host "In $dcPath Run $(if($AsJob){"(bg) "})with: $dcParams" + } elseif( $AsJob ) { $JobQueue += Start-Job -ArgumentList @($dcPath,$dcParams) -ScriptBlock { param($dcPath,$dcParams) [PSCustomObject] $([ordered]@{ @@ -107,6 +106,7 @@ foreach( $local:p in $CliParams ) { } } +# if( $WhatIfPreference ) { return } CompleteJobs if( $CleanupJobQueue ) { diff --git a/docker/dcc.ps1 b/docker/dcc.ps1 index 9b65de9..2d2f3cb 100644 --- a/docker/dcc.ps1 +++ b/docker/dcc.ps1 @@ -11,4 +11,4 @@ [string[]]$ProjectPath=@($PWD) ) -$ProjectPath | ForEach-Object { docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) config | less } +Invoke-DockerCompose -ProjectPath $ProjectPath 'config' | Out-String -Stream | less diff --git a/docker/dcdown.ps1 b/docker/dcdown.ps1 index 8c83258..2efbc40 100644 --- a/docker/dcdown.ps1 +++ b/docker/dcdown.ps1 @@ -10,9 +10,11 @@ })] [string[]]$ProjectPath=@($PWD), [Parameter(Position = 0, ValueFromRemainingArguments = $true)] - [string[]]$CliParams + [array]$CliParams=@('--timeout=3','--volumes','--remove-orphans') ) -$ProjectPath | ForEach-Object { - docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) down --timeout=3 --volumes --remove-orphans "$CliParams" -} +if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) } +$CliParams[0] = @('down') + $CliParams[0] + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams + diff --git a/docker/dcl.ps1 b/docker/dcl.ps1 index d41ca60..8d72c39 100644 --- a/docker/dcl.ps1 +++ b/docker/dcl.ps1 @@ -1 +1,19 @@ -docker-compose logs --tail=40 --follow "$args" +[CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] + [string[]]$ProjectPath=@($PWD), + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [array]$CliParams +) + +if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) } +$CliParams[0] = @('logs','--tail=40','follow') + $CliParams[0] + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams diff --git a/docker/dcll.ps1 b/docker/dcll.ps1 index a5ffc0d..dd2b3cc 100644 --- a/docker/dcll.ps1 +++ b/docker/dcll.ps1 @@ -1 +1,19 @@ -docker-compose logs "$args" +[CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] + [string[]]$ProjectPath=@($PWD), + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [array]$CliParams +) + +if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) } +$CliParams[0] = @('logs') + $CliParams[0] + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams diff --git a/docker/dcr.ps1 b/docker/dcr.ps1 index 6406f9e..561f697 100644 --- a/docker/dcr.ps1 +++ b/docker/dcr.ps1 @@ -1 +1,19 @@ -docker-compose run --rm "$args" +[CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] + [string[]]$ProjectPath=@($PWD), + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [array]$CliParams=@('--rm') +) + +if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) } +$CliParams[0] = @('run') + $CliParams[0] + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams diff --git a/docker/dcre.ps1 b/docker/dcre.ps1 index f0fc796..f62ccce 100644 --- a/docker/dcre.ps1 +++ b/docker/dcre.ps1 @@ -1 +1,19 @@ -docker-compose restart "$args" +[CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] + [string[]]$ProjectPath=@($PWD), + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [array]$CliParams +) + +if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) } +$CliParams[0] = @('restart') + $CliParams[0] + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams diff --git a/docker/dcrefresh.ps1 b/docker/dcrefresh.ps1 deleted file mode 100644 index f8f38e2..0000000 --- a/docker/dcrefresh.ps1 +++ /dev/null @@ -1,29 +0,0 @@ -[CmdletBinding(SupportsShouldProcess)]param( - [ArgumentCompleter({ param ( - $commandName, - $parameterName, - $wordToComplete, - $commandAst, - $fakeBoundParameters - ) - [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) - })] - [string[]]$ProjectPath, - [switch]$Recurse, - [int]$Depth=1, - [switch]$Force, - [switch]$OneLine, - [Parameter(Position = 0, ValueFromRemainingArguments = $true)] - [array]$CliParams=@('&','up','-d') -) - -$local:passParams = [ordered]@{ - Depth = $Depth - Force = $Force - OneLine = $OneLine - CliParams = $CliParams -} -if( -not $ProjectPath -and -not $Recurse ) { $Recurse = $true } -if( $ProjectPath ) { $passParams.ProjectPath = $ProjectPath } -if( $Recurse ) { $passParams.Recurse = $Recurse } -Invoke-DockerCompose @passParams diff --git a/docker/dcreup.ps1 b/docker/dcreup.ps1 index 5019d06..4bfce4f 100644 --- a/docker/dcreup.ps1 +++ b/docker/dcreup.ps1 @@ -8,19 +8,34 @@ ) [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) })] - [string[]]$ProjectPath=@($PWD), - [string[]]$UpCliParams, + [string[]]$ProjectPath, + [switch]$Recurse, + [int]$Depth=1, + [switch]$NoLogs, + [switch]$NoPull, + [array]$PullParams, + [array]$LogsParams, + [array]$UpParams, [Parameter(Position = 0, ValueFromRemainingArguments = $true)] - [string[]]$CliParams + [array]$Containers ) +if( -not $Containers -or ($Containers[0] -is [string]) ) { $Containers = @($Containers,@($null)) } +if( -not $UpParams -or ($UpParams[0] -is [string]) ) { $UpParams = @($UpParams,@($null)) } +if( -not $PullParams -or ($PullParams[0] -is [string]) ) { $PullParams = @($PullParams,@($null)) } +if( -not $LogsParams -or ($LogsParams[0] -is [string]) ) { $LogsParams = @($LogsParams,@($null)) } + +$UpParams[0] = @('--force-recreate') + $UpParams[0] + $local:passParams = [ordered]@{ ProjectPath=$ProjectPath - UpCliParams=@('--force-recreate') + Recurse=$Recurse + Depth=$Depth + NoLogs=$NoLogs + NoPull=$NoPull + PullParams=$PullParams + LogsParams=$LogsParams + UpParams=$UpParams + Containers=$Containers } -if( $UpCliParams ) { $passParams.UpCliParams += $UpCliParams } -if( $CliParams ) { $passParams.CliParams = $CliParams } dcup @passParams - -# docker-compose --file $dcPath up -d --force-recreate --remove-orphans $dcParams && docker-compose logs --follow --tail 10 $dcParams - diff --git a/docker/dcup.ps1 b/docker/dcup.ps1 index 6ec1160..ff75733 100644 --- a/docker/dcup.ps1 +++ b/docker/dcup.ps1 @@ -8,24 +8,47 @@ ) [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) })] - [string[]]$ProjectPath=@($PWD), - [string[]]$UpCliParams, + [string[]]$ProjectPath, + [switch]$Recurse, + [int]$Depth=1, + [switch]$NoLogs, + [switch]$NoPull, + [array]$PullParams, + [array]$LogsParams, + [array]$UpParams, [Parameter(Position = 0, ValueFromRemainingArguments = $true)] - [string[]]$CliParams + [array]$Containers ) -$local:dcParams = [string]::Empty -if( $CliParams ) { - $dcParams = $CliParams | ForEach-Object { - if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ } - else { "'$($_.Replace( "'", "''" ))'" } +$local:ForceNoFollow = $false +if( -not $ProjectPath ) { + $ProjectPath=@($PWD) + $ForceNoFollow = $Recurse + +} elseif( -not $NoLogs ) { + $ForceNoFollow=$($ProjectPath.Length -gt 1) +} + +if( -not $Containers -or ($Containers[0] -is [string]) ) { $Containers = @($Containers,@($null)) } +if( -not $UpParams -or ($UpParams[0] -is [string]) ) { $UpParams = @($UpParams,@($null)) } +if( -not $PullParams -or ($PullParams[0] -is [string]) ) { $PullParams = @($PullParams,@($null)) } +if( -not $LogsParams -or ($LogsParams[0] -is [string]) ) { $LogsParams = @($LogsParams,@($null)) } + +if( -not $LogsParams[0] ) { + $local:tailLen = 10 + $logsParams[0] = @() + if( -not $ForceNoFollow ) { + $tailLen = 40 + $logsParams[0] += @('--follow') } -} -$local:dcUpParams = $UpCliParams + $dcParams - -$ProjectPath | ForEach-Object { - $local:dcPath = $(Resolve-Path $(Join-Path $_ docker-compose.yml)) - Write-Verbose "docker-compose --file $dcPath up -d --remove-orphans $dcUpParams && docker-compose --file $dcPath logs --follow --tail 10 $dcParams" - docker-compose --file $dcPath up -d --remove-orphans $dcUpParams && docker-compose --file $dcPath logs --follow --tail 10 $dcParams + $logsParams[0] += @("--tail=$tailLen") } +$PullParams[0] = @('&','pull' ) + $PullParams[0] + $Containers +$UpParams[0] = @('&', 'up' ) + $UpParams[0] + $Containers +$LogsParams[0] = @('logs' ) + $LogsParams[0] + $Containers + +$local:allParams = $PullParams + @(@('!')) + $UpParams +if( -not $NoLogs ) { $allParams += @(@('!')) + $LogsParams } + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $allParams -Recurse:$Recurse -Depth $Depth diff --git a/docker/dcx.ps1 b/docker/dcx.ps1 index 7e25a31..9551030 100644 --- a/docker/dcx.ps1 +++ b/docker/dcx.ps1 @@ -1 +1,19 @@ -docker-compose exec $args +[CmdletBinding(SupportsShouldProcess)]param( + [ArgumentCompleter({ param ( + $commandName, + $parameterName, + $wordToComplete, + $commandAst, + $fakeBoundParameters + ) + [DockerComposeDirs]::_GetValidValues($wordToComplete,$true) + })] + [string[]]$ProjectPath=@($PWD), + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [array]$CliParams +) + +if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) } +$CliParams[0] = @('exec') + $CliParams[0] + +Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams