From ac9c5ed35204a23531e99b522a39f12a211de6df Mon Sep 17 00:00:00 2001 From: lksz Date: Wed, 4 Nov 2020 10:58:04 -0500 Subject: [PATCH] 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) --- Aliases/dps.ps1 | 1 + Edit-DockerCompose.ps1 | 6 +++--- Edit-MyConfig.ps1 | 5 +++-- Get-DockerProcess.ps1 | 24 ++++++++++++++++++++++++ dcc.ps1 | 4 +++- dps.ps1 | 12 ------------ profile.d/FromPowerShellCookbook.ps1 | 21 +++++++++++++++++++++ profile.d/MyConfig.class.ps1 | 1 + sys.Linux/Get-PlexInfo.ps1 | 23 +++++++++++++++++++++-- sys.Linux/Invoke-ViaAnsible.ps1 | 3 +++ 10 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 Aliases/dps.ps1 create mode 100644 Get-DockerProcess.ps1 delete mode 100644 dps.ps1 create mode 100644 profile.d/FromPowerShellCookbook.ps1 diff --git a/Aliases/dps.ps1 b/Aliases/dps.ps1 new file mode 100644 index 0000000..640b880 --- /dev/null +++ b/Aliases/dps.ps1 @@ -0,0 +1 @@ +Get-DockerProcess diff --git a/Edit-DockerCompose.ps1 b/Edit-DockerCompose.ps1 index 9557e54..9d491f4 100644 --- a/Edit-DockerCompose.ps1 +++ b/Edit-DockerCompose.ps1 @@ -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:editFiles = @() foreach( $local:fileName in $EditCandidates ) { foreach( $local:path in $ProjectPath ) { $local:testPath = Join-Path $path $fileName - if( Test-Path $testPath ) { $editFiles += $testPath } + if( $Force -or $(Test-Path $testPath) ) { $editFiles += $testPath } } } Edit-TextFile $editFiles -docker-compose config -q +$editFiles | Select-Object -First 1 | ForEach-Object { docker-compose --file $(Resolve-Path $_) config -q } diff --git a/Edit-MyConfig.ps1 b/Edit-MyConfig.ps1 index 882f5cf..92a098c 100644 --- a/Edit-MyConfig.ps1 +++ b/Edit-MyConfig.ps1 @@ -10,7 +10,8 @@ [MyConfig]::_GetValidValues($wordToComplete,$true) | Sort-Object })] [string[]]$ConfigName, - [switch]$Force + [switch]$Force, + [switch]$sudo ) if( -not $ConfigName ) { ([MyConfig]::_GetValidValues('',$true)) | Sort-Object @@ -18,4 +19,4 @@ if( -not $ConfigName ) { } $local:ScriptPaths = [MyConfig]::GetConfigPaths($ConfigName,$Force) -Edit-TextFile $ScriptPaths +Edit-TextFile -sudo:$sudo $ScriptPaths diff --git a/Get-DockerProcess.ps1 b/Get-DockerProcess.ps1 new file mode 100644 index 0000000..b6f1aa3 --- /dev/null +++ b/Get-DockerProcess.ps1 @@ -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 + diff --git a/dcc.ps1 b/dcc.ps1 index f8a1e1c..6154b3a 100644 --- a/dcc.ps1 +++ b/dcc.ps1 @@ -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 } diff --git a/dps.ps1 b/dps.ps1 deleted file mode 100644 index 85aa8c6..0000000 --- a/dps.ps1 +++ /dev/null @@ -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 diff --git a/profile.d/FromPowerShellCookbook.ps1 b/profile.d/FromPowerShellCookbook.ps1 new file mode 100644 index 0000000..a9e7f42 --- /dev/null +++ b/profile.d/FromPowerShellCookbook.ps1 @@ -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 +} diff --git a/profile.d/MyConfig.class.ps1 b/profile.d/MyConfig.class.ps1 index eb4a92f..fed67a7 100644 --- a/profile.d/MyConfig.class.ps1 +++ b/profile.d/MyConfig.class.ps1 @@ -12,6 +12,7 @@ class MyConfig { #: System.Management.Automation.IValidateSetValuesGenerator { 'sz-shrc' = '~/.sz.shrc.sh' 'sz-rc-all' = '#sz-aliases-sh', '#sz-local-rc', '#sz-shrc', '#sz-zshrc', '#zshrc' 'tmux' = '~/.tmux.conf', '~/.byobu/.tmux.conf' + 'ansible' = '/etc/ansible/ansible.cfg', '/opt/ansible/ansible.cfg' } } static [string[]] GetConfigPaths([string[]]$ConfigNames,[switch]$Force) { diff --git a/sys.Linux/Get-PlexInfo.ps1 b/sys.Linux/Get-PlexInfo.ps1 index 1a3b7a1..f679500 100644 --- a/sys.Linux/Get-PlexInfo.ps1 +++ b/sys.Linux/Get-PlexInfo.ps1 @@ -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 = $_ Type = $_.type Library = $_.librarySectionTitle @@ -8,5 +23,9 @@ $PlexInfo.ChildNodes | ForEach-Object { [PSCustomObject]@{ User = $_.User.title Offset = [TimeSpan]::FromMilliseconds($_.viewOffset).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() } } + +if( $PassThruOnly ) { return $result } +$result | Format-Table diff --git a/sys.Linux/Invoke-ViaAnsible.ps1 b/sys.Linux/Invoke-ViaAnsible.ps1 index 3e5c8ce..242ecdc 100644 --- a/sys.Linux/Invoke-ViaAnsible.ps1 +++ b/sys.Linux/Invoke-ViaAnsible.ps1 @@ -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' } if( $Command ) { + $ansible_cli += '-a' + $ansible_cli += 'ANSIBLE_PYTHON_INTERPRETER=auto_silent ' $ansible_cli += '-a' if( $NotPowerShell ) { $ansible_cli += $(($Command -join '; ').Replace("'","\`$(printf '\x27')").Replace('"',"\`$(printf '\x22')")) @@ -27,6 +29,7 @@ $ansible_cli += $MoreArgs $local:expr = $('& "'+$($ansible_cli -join '" "')+'"') Write-Verbose $expr $local:SaveConsoleColor = [Console]::ForegroundColor +$env:ANSIBLE_DEPRECATION_WARNINGS='false' Invoke-Expression $expr | ForEach-Object { if( -not $OneLine ) { return $_ }; $local:res = $_.Split('|');