Introducing Packages

Major Overhaul with Breaking Changes
split into packaged, default behavior moved into 'base' package
each package has a json package description file with criteria for
loading

Modified Setup-Profile to refer to 'base' package path for auto loading

moved Linux aliases and command to 'base.linux' package
created 'docker' package to address docker supported systems

modified Get-MyScripts, Edit-MyScripts and Reload-MyScripts accordingly.

Dropped -System and sys.*, package json conditions will take care of it.

Supplied command to create/edit package json files:
  - New-MyPackage
  - Add-PackageCondition
  - Set-MyPackage
This commit is contained in:
lksz 2020-11-25 00:46:33 -05:00
parent 80a488484b
commit 42b39f4e25
83 changed files with 505 additions and 130 deletions

View file

@ -0,0 +1,31 @@
param([switch]$FetchFromServer,[string]$PlexHost='localhost',[switch]$PassThruOnly)
$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
Title = $(@($_.grandparentTitle,$_.parentIndex,$_.parentTitle,$_.index,$_.title) | Where-Object {$_}) -join ' | '
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

View file

@ -0,0 +1,49 @@
[CmdletBinding()]param(
[string[]]$Command,
[string[]]$Remotes = 'all',
[string[]]$MoreArgs,
[switch]$NotPowerShell,
[switch]$OneLine
)
$script:ansible_cli=@('ansible')
if( (Test-Path './ansible.cfg') -or (Test-Path '/opt/ansible/hosts') -and (Test-Path '/opt/ansible/ssh_key_for_ansible') ) {
$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')"))
} else {
$ansible_cli += "pwsh -encodedcommand $(ConvertTo-Base64 $($Command -join '; '))"
}
}
if( $OneLine ) {
$ansible_cli += '--one-line'
}
$ansible_cli += $Remotes
$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('|');
$res = [PSCustomObject]([ordered]@{
Machine = $res[0].Trim();
Status = $res[1].Trim();
RC = "$($res[2])".Trim();
Output = "$($res[3])".Trim();
})
if( $res.Status -match '^\w+!:' ) {
[Console]::ForegroundColor = [ConsoleColor]::Red
} else {
[Console]::ForegroundColor = [ConsoleColor]::Yellow
}
$res
}
[Console]::ForegroundColor = $SaveConsoleColor

View file

@ -0,0 +1,3 @@
[CmdletBinding()]param([string[]]$Remotes = 'All')
Invoke-ViaAnsible -Command "git pull" -Remotes:$($Remotes.ToLower())

View file

@ -0,0 +1,6 @@
[CmdletBinding()]param(
[string]$Name,
[object]$Value
)
Set-Item -Path (Join-Path "env:" $Name) -Value $Value

View file

@ -0,0 +1,13 @@
param(
[ValidateSet('List','Update','Auto','All')]
[string]$Mode = 'List'
)
$script:yayCli = "cat /run/check.yay.updates/list"
switch( $Mode ){
'Update' { $yayCli = "yay -Syu --needed --ignore docker,linux,linux-api-headers,linux-firmware,linux-headers,zfs-linux" }
'Auto' { $yayCli = "yay -Syu --needed --noconfirm --ignore docker || yay -Syu --needed --ignore docker,linux,linux-api-headers,linux-firmware,linux-headers,zfs-linux --noconfirm" }
'All' { $yayCli = "yay -Syu --needed" }
}
& sh "-c" "$yayCli" -replace '#args',"$args"

10
base.linux/Update-OSz.ps1 Normal file
View file

@ -0,0 +1,10 @@
param(
[ValidateSet('List','Update','Auto','All')]
[string]$Mode = 'List'
)
$local:detectedDisistro = cat /etc/os-release | Select-String -Pattern "^ID=" | ForEach-Object { $_ -split '=' | Select-Object -Skip 1 }
switch ($detectedDisistro) {
'arch' { Update-ArchOSz -Mode:$Mode $args }
'ubuntu' { Update-UbuntuOSz -Mode:$Mode $args }
}

View file

@ -0,0 +1,14 @@
param(
[ValidateSet('List','Update','Auto','All')]
[string]$Mode = 'List'
)
$script:aptCli = "echo 'not defined'"
switch( $Mode ){
'List' { }
'Auto' { $aptCli = "sudo apt-get update && sudo apt-get upgrade --auto-remove --assume-yes" }
default { $aptCli = "sudo apt-get update && sudo apt-get upgrade --auto-remove " }
}
& sh "-c" "$aptCli" -replace '#args',"$args"

View file

@ -0,0 +1,3 @@
[CmdletBinding()]param([string[]]$Remotes = 'All')
Invoke-ViaAnsible -Command "Update-OSz -Mode Auto" -Remotes:$($Remotes.ToLower())

16
base.linux/_.package.json Normal file
View file

@ -0,0 +1,16 @@
{
"package": {
"Condition": [
{
"custom": null,
"System": [
"Linux"
],
"Hostname": null,
"Username": null,
"Logic": 0
}
],
"Name": "base.linux"
}
}

5
base.linux/ls.ps1 Normal file
View file

@ -0,0 +1,5 @@
$local:params = $args -join ' '
if( -not $params ) { $params = '-lah' }
/usr/bin/env ls --color=auto $params

1
base.linux/sranger.ps1 Normal file
View file

@ -0,0 +1 @@
Invoke-ExpressionEx -sudo "`$env:TERM='tmux-256color'; `$env:EDITOR='$env:EDITOR'; ranger $args"

35
base.linux/sz-df.ps1 Normal file
View file

@ -0,0 +1,35 @@
param([ValidateSet('KB','GB','TB')]$SizeUnitName = 'GB')
$local:zpool_cmd = $null
$local:excludeType = ''
$script:SizeUnit = Invoke-Expression "1$SizeUnitName"
$script:SizeRound = 2
$SizeUnit /= 1KB
function NewDataRow{param($Size,$Used,$Available,$Percent,$Source,$FSType,$Target)
[PSCustomObject]@{
"Size$SizeUnitName" = [Math]::Round($Size /$SizeUnit, $SizeRound)
"Used$SizeUnitName" = [Math]::Round($Used /$SizeUnit, $SizeRound)
"Available$SizeUnitName" = [Math]::Round($Available/$SizeUnit, $SizeRound)
'Used%' = [int]($Percent -replace '%$','')
Source = $Source
FSType = $FSType
Target = $Target
}
}
$local:df = @()
& df '--output=size,used,avail,pcent,source,fstype,target' |
Select-Object -Skip 1 | ForEach-Object {
$local:df = $_.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)
NewDataRow $df[0] $df[1] $df[2] $df[3] $df[4] $df[5] $df[6]
} | Where-Object {
-not ($_.FSType -eq 'zfs' -and $_.Source -match '/')
} | Sort-Object -Property Target
#$zpool_cmd = get-command zpool | Where-Object CommandType -eq 'Application' | Select-Object -ExpandProperty Source
#if( $zpool_cmd ) {
# $excludeType = '--exclude-type=zfs'
#}

1
base.linux/sz-du.ps1 Normal file
View file

@ -0,0 +1 @@
sudo /bin/du --human-readable --max-depth=1 --one-file-system $args | sort --human-numeric-sort --reverse

6
base.linux/sz-rsync.ps1 Normal file
View file

@ -0,0 +1,6 @@
#setopt dotglob
$szSrc="'$($args -join "','")'"
$szSrcCount=$(Invoke-ExpressionEx -sudo "& find $szSrc -type f | wc -l )
Invoke-ExpressionEx -sudo "& rsync --archive --acls --xattrs --human-readable -v $szSrc /szmedia/media/ | pv -lepts $szSrcCount"
#unsetopt dotglob

1
base.linux/txm.ps1 Normal file
View file

@ -0,0 +1 @@
tmux set-option mouse $args