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.
This commit is contained in:
lksz 2021-01-15 01:15:54 -05:00
parent c453e5bdea
commit dcc9113cda
13 changed files with 172 additions and 70 deletions

View File

@ -1 +1 @@
docker-compose Invoke-DockerCompose

1
docker/Aliases/dco_.ps1 Normal file
View File

@ -0,0 +1 @@
docker-compose

View File

@ -51,9 +51,6 @@ $local:dcPathList = $ProjectPath | ForEach-Object {
Get-ChildItem -Path $_ -Include 'docker-compose.yml' @gciParams | Get-ChildItem -Path $_ -Include 'docker-compose.yml' @gciParams |
Select-Object -ExpandProperty Directory | Select-Object -ExpandProperty Directory |
Where-Object { $Force -or -not (Test-Path $(Join-Path $_ .noauto.all)) } 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 } if( -not $dcPathList ) { return }
@ -75,7 +72,7 @@ foreach( $local:p in $CliParams ) {
if( $skipFirst ) { $p = $p | Select-Object -Skip 1 } if( $skipFirst ) { $p = $p | Select-Object -Skip 1 }
} }
$dcParams = $p | ForEach-Object { $dcParams = $p | Where-Object { $_ } | ForEach-Object {
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ } if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ }
else { "'$($_.Replace( "'", "''" ))'" } 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" } } if( -not (Test-Path $dcPath) ) { if( $Recurse ) { return } else { throw "ERROR: dcPath ($dcPath) does not exists" } }
Write-Verbose "& docker-compose --file $dcPath $dcParams" Write-Verbose "& docker-compose --file $dcPath $dcParams"
if( $Wait ) { if( $Wait -and -not $WhatIfPreference ) {
CompleteJobs CompleteJobs
} }
if( -not $dcParams ) { continue } 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 { $JobQueue += Start-Job -ArgumentList @($dcPath,$dcParams) -ScriptBlock {
param($dcPath,$dcParams) param($dcPath,$dcParams)
[PSCustomObject] $([ordered]@{ [PSCustomObject] $([ordered]@{
@ -107,6 +106,7 @@ foreach( $local:p in $CliParams ) {
} }
} }
# if( $WhatIfPreference ) { return }
CompleteJobs CompleteJobs
if( $CleanupJobQueue ) { if( $CleanupJobQueue ) {

View File

@ -11,4 +11,4 @@
[string[]]$ProjectPath=@($PWD) [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

View File

@ -10,9 +10,11 @@
})] })]
[string[]]$ProjectPath=@($PWD), [string[]]$ProjectPath=@($PWD),
[Parameter(Position = 0, ValueFromRemainingArguments = $true)] [Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$CliParams [array]$CliParams=@('--timeout=3','--volumes','--remove-orphans')
) )
$ProjectPath | ForEach-Object { if( $CliParams[0] -is [string] ) { $CliParams = @($CliParams,@($null)) }
docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) down --timeout=3 --volumes --remove-orphans "$CliParams" $CliParams[0] = @('down') + $CliParams[0]
}
Invoke-DockerCompose -ProjectPath $ProjectPath -CliParams $CliParams

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -8,19 +8,34 @@
) )
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true) [DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
})] })]
[string[]]$ProjectPath=@($PWD), [string[]]$ProjectPath,
[string[]]$UpCliParams, [switch]$Recurse,
[int]$Depth=1,
[switch]$NoLogs,
[switch]$NoPull,
[array]$PullParams,
[array]$LogsParams,
[array]$UpParams,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)] [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]@{ $local:passParams = [ordered]@{
ProjectPath=$ProjectPath 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 dcup @passParams
# docker-compose --file $dcPath up -d --force-recreate --remove-orphans $dcParams && docker-compose logs --follow --tail 10 $dcParams

View File

@ -8,24 +8,47 @@
) )
[DockerComposeDirs]::_GetValidValues($wordToComplete,$true) [DockerComposeDirs]::_GetValidValues($wordToComplete,$true)
})] })]
[string[]]$ProjectPath=@($PWD), [string[]]$ProjectPath,
[string[]]$UpCliParams, [switch]$Recurse,
[int]$Depth=1,
[switch]$NoLogs,
[switch]$NoPull,
[array]$PullParams,
[array]$LogsParams,
[array]$UpParams,
[Parameter(Position = 0, ValueFromRemainingArguments = $true)] [Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string[]]$CliParams [array]$Containers
) )
$local:dcParams = [string]::Empty $local:ForceNoFollow = $false
if( $CliParams ) { if( -not $ProjectPath ) {
$dcParams = $CliParams | ForEach-Object { $ProjectPath=@($PWD)
if( $_ -match "(?:^'[^']+'$)|(?:^`"[^`"]+`"$)|(?:^[^\s]+$)" ) { $_ } $ForceNoFollow = $Recurse
else { "'$($_.Replace( "'", "''" ))'" }
} 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')
} }
} $logsParams[0] += @("--tail=$tailLen")
$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
} }
$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

View File

@ -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