Various additions

dps is now an Alias to Get-DockerProcess
Get-DockerProcess has been cleaned up and search features added to it.

Added FromPowerShellCookbook, which loads a piece of code from the
module, but does not load the entire module.
MyConfig now includes ansible and tmux configs, also added -sudo switch
Get-PlexInfo added, with ability to read directly from Server
Invoke-ViaAnsible modified - cleaned up output
Added -Force to Edit-DockerCompose
dcc modified to read from raw log (which allows filtering by source, and
has timestamps)
This commit is contained in:
lksz 2020-11-04 10:58:04 -05:00
parent 59a2949268
commit ac9c5ed352
10 changed files with 80 additions and 20 deletions

1
Aliases/dps.ps1 Normal file
View File

@ -0,0 +1 @@
Get-DockerProcess

View File

@ -1,14 +1,14 @@
[CmdletBinding(SupportsShouldProcess)]param([string[]]$ProjectPath=@($PWD)) [CmdletBinding(SupportsShouldProcess)]param([string[]]$ProjectPath=@($PWD),[switch]$Force)
$local:editCandidates = @('docker-compose.yml','.base.docker-compose.yml') $local:editCandidates = @('docker-compose.yml','.base.docker-compose.yml')
$local:editFiles = @() $local:editFiles = @()
foreach( $local:fileName in $EditCandidates ) { foreach( $local:fileName in $EditCandidates ) {
foreach( $local:path in $ProjectPath ) { foreach( $local:path in $ProjectPath ) {
$local:testPath = Join-Path $path $fileName $local:testPath = Join-Path $path $fileName
if( Test-Path $testPath ) { $editFiles += $testPath } if( $Force -or $(Test-Path $testPath) ) { $editFiles += $testPath }
} }
} }
Edit-TextFile $editFiles Edit-TextFile $editFiles
docker-compose config -q $editFiles | Select-Object -First 1 | ForEach-Object { docker-compose --file $(Resolve-Path $_) config -q }

View File

@ -10,7 +10,8 @@
[MyConfig]::_GetValidValues($wordToComplete,$true) | Sort-Object [MyConfig]::_GetValidValues($wordToComplete,$true) | Sort-Object
})] })]
[string[]]$ConfigName, [string[]]$ConfigName,
[switch]$Force [switch]$Force,
[switch]$sudo
) )
if( -not $ConfigName ) { if( -not $ConfigName ) {
([MyConfig]::_GetValidValues('',$true)) | Sort-Object ([MyConfig]::_GetValidValues('',$true)) | Sort-Object
@ -18,4 +19,4 @@ if( -not $ConfigName ) {
} }
$local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force) $local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force)
Edit-TextFile $ScriptPaths Edit-TextFile -sudo:$sudo $ScriptPaths

24
Get-DockerProcess.ps1 Normal file
View File

@ -0,0 +1,24 @@
#[CmdletBinding(SupportsShouldProcess)]param(
[CmdletBinding()]param([string[]]$MatchName,[string[]]$OrderBy,[switch]$PassThru,[string[]]$MatchAny)
$local:result = $(docker ps --format='{{ .Image }}\t{{ .Names }}\t{{ .Status }}\t{{ .Ports }}' |
ForEach-Object {
$local:l = $_ -split '\t';
[PSCustomObject]([ordered]@{Image=$l[0];Name=$l[1];Status=$l[2];Ports=$l[3] -replace ', ',"`n"})
} ) | Sort-Object Name
if( $MatchName ) {
$result = $result | Where-Object Name -match $($MatchName -join '|')
}
if( $MatchAny ) {
$result = $result | Where-Object { $_ | Out-String | Where-Object { $_ -match $($MatchAny -join '|') } }
}
if( $OrderBy ) {
$result = $result | Sort-Object $OrderBy
}
if( $PassThru ) { return $result }
$result | Format-Table -Wrap

View File

@ -1 +1,3 @@
docker-compose config | less [CmdletBinding(SupportsShouldProcess)]param([string[]]$ProjectPath=@($PWD))
$ProjectPath | ForEach-Object { docker-compose --file $(Resolve-Path $(Join-Path $_ docker-compose.yml)) config | less }

12
dps.ps1
View File

@ -1,12 +0,0 @@
#[CmdletBinding(SupportsShouldProcess)]param(
[CmdletBinding()]param([string[]]$OrderBy,[switch]$PassThru)
$local:result = $(docker ps --format='{{ .Image }}\t{{ .Names }}\t{{ .Status }}\t{{ .Ports }}' | ForEach-Object { $local:l = $_ -split '\t'; [PSCustomObject](@{Image=$l[0];Names=$l[1];Status=$l[2];Ports=$l[3] -replace ', ',"
"}) } ) | Sort-Object Names
if( $OrderBy ) {
$result = $result | Sort-Object $OrderBy
}
if( $PassThru ) { return $result }
$result | Format-Table -Wrap -Property Image,Names,Status,Ports

View File

@ -0,0 +1,21 @@
# PowerShell Cookbook has a lot of code there, I'm only interested in
# the Add-ObjectCollector which adds an overloaded Out-Default version,
# which stores a history of output objects.
# One major problem with loading this from the module, is that when the
# module is removed the shell BREAKS.
#
# The code below, makes sure the module is removed, and then imports
# only the Add-ObjectCollector code from it.
# All I really want is the Out-Default function created by
# Add-ObjectCollector, and so, I'll also remove the function once done.
if( Get-Module PowerShellCookbook ) {
Remove-Module PowerShellCookbook
Import-Module PowerShellCookbook -Cmdlet Add-ObjectCollector
}
#
. Get-Command Add-ObjectCollector SilentlyContinue | ForEach-Object {
Add-ObjectCollector
Get-Item function:/Add-ObjectCollector | Remove-Item
}

View File

@ -12,6 +12,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator {
'sz-shrc' = '~/.sz.shrc.sh' 'sz-shrc' = '~/.sz.shrc.sh'
'sz-rc-all' = '#sz-aliases-sh', '#sz-local-rc', '#sz-shrc', '#sz-zshrc', '#zshrc' 'sz-rc-all' = '#sz-aliases-sh', '#sz-local-rc', '#sz-shrc', '#sz-zshrc', '#zshrc'
'tmux' = '~/.tmux.conf', '~/.byobu/.tmux.conf' 'tmux' = '~/.tmux.conf', '~/.byobu/.tmux.conf'
'ansible' = '/etc/ansible/ansible.cfg', '/opt/ansible/ansible.cfg'
} }
} }
static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) { static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) {

View File

@ -1,6 +1,21 @@
$local:PlexInfo = $(try{([xml](Get-Content /run/plex.stream.counter/sessions -Raw -ErrorAction Stop)).MediaContainer}catch{}) param([switch]$FetchFromServer,[string]$PlexHost='localhost',[switch]$PassThruOnly)
$PlexInfo.ChildNodes | ForEach-Object { [PSCustomObject]@{ $local:content = $(try{(Get-Content /run/plex.stream.counter/sessions -Raw -ErrorAction Stop)}catch{})
if( $FetchFromServer ) {
$local:creds = New-Object System.Management.Automation.PsCredential($env:PLEX_USERNAME,$(ConvertTo-SecureString -AsPlainText $env:PLEX_PASSWORD -Force))
$local:webResp = Invoke-WebRequest -Headers @{ "Content-Length" = "0"; "X-Plex-Client-Identifier" = "my-app" } -Credential $creds -Method Post -Uri "https://my.plexapp.com/users/sign_in.xml"
$local:plexHeaders = @{ "X-Plex-Token" = ([xml]$webResp.Content).user.authenticationToken }
$webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400"
$local:plexVersion = ([xml]$webResp.Content).MediaContainer.version
Write-Host -ForegroundColor Cyan "Plex Version: $plexVersion"
$webResp = Invoke-WebRequest -Headers $plexHeaders "http://${PlexHost}:32400/status/sessions"
$content = $webResp.Content
}
$local:PlexInfo = $(try{([xml]$content).MediaContainer}catch{})
$local:result = $PlexInfo.ChildNodes | ForEach-Object { [PSCustomObject]@{
Object = $_ Object = $_
Type = $_.type Type = $_.type
Library = $_.librarySectionTitle Library = $_.librarySectionTitle
@ -8,5 +23,9 @@ $PlexInfo.ChildNodes | ForEach-Object { [PSCustomObject]@{
User = $_.User.title User = $_.User.title
Offset = [TimeSpan]::FromMilliseconds($_.viewOffset).ToString() -split '\.' | select-object -first 1 Offset = [TimeSpan]::FromMilliseconds($_.viewOffset).ToString() -split '\.' | select-object -first 1
Duration = [TimeSpan]::FromMilliseconds($_.duration).ToString() -split '\.' | select-object -first 1 Duration = [TimeSpan]::FromMilliseconds($_.duration).ToString() -split '\.' | select-object -first 1
Player = "$($_.Player.Platform)|$($_.Player.State)"
# UpdateTime = [TimeSpan]::FromMilliseconds($_.).ToString() # UpdateTime = [TimeSpan]::FromMilliseconds($_.).ToString()
} } } }
if( $PassThruOnly ) { return $result }
$result | Format-Table

View File

@ -11,6 +11,8 @@ if( (Test-Path './ansible.cfg') -or (Test-Path '/opt/ansible/hosts') -and (Test-
$ansible_cli += '-i', '/opt/ansible/hosts', '--private-key=/opt/ansible/ssh_key_for_ansible' $ansible_cli += '-i', '/opt/ansible/hosts', '--private-key=/opt/ansible/ssh_key_for_ansible'
} }
if( $Command ) { if( $Command ) {
$ansible_cli += '-a'
$ansible_cli += 'ANSIBLE_PYTHON_INTERPRETER=auto_silent '
$ansible_cli += '-a' $ansible_cli += '-a'
if( $NotPowerShell ) { if( $NotPowerShell ) {
$ansible_cli += $(($Command -join '; ').Replace("'","\`$(printf '\x27')").Replace('"',"\`$(printf '\x22')")) $ansible_cli += $(($Command -join '; ').Replace("'","\`$(printf '\x27')").Replace('"',"\`$(printf '\x22')"))
@ -27,6 +29,7 @@ $ansible_cli += $MoreArgs
$local:expr = $('& "'+$($ansible_cli -join '" "')+'"') $local:expr = $('& "'+$($ansible_cli -join '" "')+'"')
Write-Verbose $expr Write-Verbose $expr
$local:SaveConsoleColor = [Console]::ForegroundColor $local:SaveConsoleColor = [Console]::ForegroundColor
$env:ANSIBLE_DEPRECATION_WARNINGS='false'
Invoke-Expression $expr | ForEach-Object { Invoke-Expression $expr | ForEach-Object {
if( -not $OneLine ) { return $_ }; if( -not $OneLine ) { return $_ };
$local:res = $_.Split('|'); $local:res = $_.Split('|');