39 Star 222 Fork 51

chuanjiao10 / kasini3000

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
k-commit.ps1 7.71 KB
一键复制 编辑 原始数据 按行查看 历史
chuanjiao10 提交于 2023-05-15 17:33 . 更新:优化持续输出功能
#建议保存编码为:bom头 + utf8
param
(
[Alias('file','脚本文件名')][String]$ps1脚本文件名,
[parameter(Position = 0)][Alias("scriptblock")][scriptblock]$powershell代码块 = { },
[Alias("port")][uint16]$端口,
[Alias("allparameter")][string]$全部传入参数, #'"ab c",(get-date)' or '$a' ---keep quotation marks
[Alias("timeout")][timespan]$任务超时时间 = [timespan]'00:30:00',
[Alias("remark")][String]$备注 = (Get-Date -Format 'yyyyMMdd HH:mm:ss'),
$复制主控机node_script目录到被控机 = $false,
[ValidateNotNullOrEmpty()]
[Alias("ipaddress")][String]$被控机ip地址 = ${global:当前被控机_ip}.ip,
[Alias("wait_output")][bool]$持续输出结果 = $false
)
function yg严格测试布尔型变量($被测试的布尔形变量)
{
if (($被测试的布尔形变量 -eq 1) -or ($被测试的布尔形变量 -eq $true) -or ($被测试的布尔形变量 -eq 'true'))
{
return $true
}
elseif (($被测试的布尔形变量 -eq 0) -or ($被测试的布尔形变量 -eq $false) -or ($被测试的布尔形变量 -eq 'false'))
{
return $false
}
else
{
Write-Error '错误:不合法的布尔型值,错误码999'
exit 999
}
}
[bool]$复制主控机node_script目录到被控机 = yg严格测试布尔型变量 $复制主控机node_script目录到被控机
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
& 'c:\ProgramData\kasini3000\0k_source.ps1'
}
if ($IsLinux -eq $True)
{
& '/etc/kasini3000/0k_source.ps1'
}
if ( ($ps1脚本文件名 -eq $null) -or ($ps1脚本文件名 -eq '') )
{
#这段代码在run_win2linux_key_pwd.ps1,run_win2win5985_pwd.ps1,run_linux2linux_key_pwd.ps1,k-commit.ps1,k_commit_ip.ps1,k_commit_uuid.ps1,k库_计划表_添加动作231.ps1
$有脚本文件 = $false
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
Write-Error "错误:没有输入脚本文件名,同时也没有输入代码块。退出码15"
exit 15
}
else
{
if (& 'kcd_被控机运行的代码块_含有kcd.ps1' -输入脚本代码块 $powershell代码块)
{
Write-Error '错误:脚本内含有kcd等关键字。退出码19'
exit 19
}
}
}
else
{
$有脚本文件 = $true
if ( ($powershell代码块.Ast.Extent.Text -eq '{ }') -or ($powershell代码块.Ast.Extent.Text -eq '{}') )
{
}
else
{
Write-Error "错误:有输入脚本文件名,同时有输入代码块"
exit 16
}
if (Test-Path -LiteralPath $ps1脚本文件名)
{
if (& 'kcd_被控机运行的脚本_含有kcd.ps1' -输入脚本文件名 $ps1脚本文件名)
{
Write-Error '错误!脚本内含有kcd等关键字'
exit 18
}
}
else
{
Write-Error "找不到脚本文件"
exit 17
}
}
#main
$msg =
@'
欢迎来到【卡死你3000 commit】,这是卡死你3000的默认命令运行器。别名=【鹉媚娘提交】
它采用生产者/消费者模式。生产者:延时提交任务到数据库。消费者:从数据库读取脚本,异步执行。
它只能提交脚本。无屏幕输出。有日志,运行状态,结果,保存在数据库。
它的使用场合是:采用【后台多进程】技术,运行【命令块】或【脚本】,用于调试。
'@
Write-Host $msg -ForegroundColor Green
if ($global:u库_进程级别任务_的ps版本)
{
$private:kaiiit的磁盘pid = & 'kaiiit_读pid.ps1'
if ( ($private:kaiiit的磁盘pid -eq $null) -or ($private:kaiiit的磁盘pid -eq '') )
{
$private:应启动pwsh进程 = $true
}
else
{
$private:kaiiit进程 = Get-Process -Id $private:kaiiit的磁盘pid -ErrorAction SilentlyContinue
if ($private:kaiiit进程 -eq $null)
{
$private:应启动pwsh进程 = $true
}
else
{
if ( ($private:kaiiit进程.ProcessName -eq 'pwsh') -or ($private:kaiiit进程.ProcessName -eq 'powershell') )
{
Write-Verbose "警告:已经有 k-commit.ps1 后台任务进程正在运行中!"
}
else
{
$private:应启动pwsh进程 = $true
}
}
}
if ($private:应启动pwsh进程 -eq $true)
{
Write-Verbose "未发现 k-commit.ps1 后台任务进程。正在fork新进程。"
if ( ($IsWindows -eq $True) -or ($PSVersionTable.psversion.major -lt 6) ) #win
{
Start-Process -WindowStyle "hidden" -FilePath "$global:u库_进程级别任务_的ps版本" -ArgumentList " -NonInteractive -NoProfile -file ${global:kasini3000目录}/kaiiit_后台任务.ps1"
}
if ($IsLinux -eq $True)
{
$private:linux_process_args = " $global:u库_进程级别任务_的ps版本 -NonInteractive -NoProfile -file ${global:kasini3000目录}/kaiiit_后台任务.ps1 "
Write-Verbose $private:linux_process_args
Start-Process -FilePath '/usr/bin/nohup' -ArgumentList $private:linux_process_args
}
}
}
else
{
Write-Error "错误:k-commit.ps1中,找不到变量【$global:u库_进程级别任务_的ps版本】,错误码1"
exit 1
}
$global:__持续输出结果_全局 = $true
if ($被控机ip地址 -ne '')
{
& 'k_commit_ip.ps1' -被控机ip地址 $被控机ip地址 -powershell代码块 $powershell代码块 -ps1脚本文件名 $ps1脚本文件名 -全部传入参数 $全部传入参数 -备注 $备注 -端口 $端口 -任务超时时间 $任务超时时间 -复制主控机node_script目录到被控机 $复制主控机node_script目录到被控机
}
else
{
if ($global:当前被控机_组 -ne $null)
{
& 'k_commit_g.ps1' -powershell代码块 $powershell代码块 -ps1脚本文件名 $ps1脚本文件名 -全部传入参数 $全部传入参数 -备注 $备注 -端口 $端口 -任务超时时间 $任务超时时间 -复制主控机node_script目录到被控机 $复制主控机node_script目录到被控机
}
else
{
if ($global:当前被控机_os -ne $null)
{
& 'k_commit_os.ps1' -powershell代码块 $powershell代码块 -ps1脚本文件名 $ps1脚本文件名 -全部传入参数 $全部传入参数 -备注 $备注 -端口 $端口 -任务超时时间 $任务超时时间 -复制主控机node_script目录到被控机 $复制主控机node_script目录到被控机
}
else
{
if ($global:所有被控机 -ne $null)
{
& 'k_commit_all.ps1' -powershell代码块 $powershell代码块 -ps1脚本文件名 $ps1脚本文件名 -全部传入参数 $全部传入参数 -备注 $备注 -端口 $端口 -任务超时时间 $任务超时时间 -复制主控机node_script目录到被控机 $复制主控机node_script目录到被控机
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
Write-Error "错误:未知被控机。需要先运行【cdip.ps1 被控机ip地址】。错误码2"
exit 2
}
else
{
Write-Error "error:node ip unkown. run 【cdip.ps1 node ip 】first. exit code2"
exit 2
}
}
}
}
}
$global:__持续输出结果_全局 = $false
Write-Host '卡死你3000 commit 任务提交完毕' -ForegroundColor Green
Write-Warning './kl.ps1 从福报表,获取最后n个任务结果输出,以list列表形式显示。n默认为5'
Write-Warning './kt.ps1 从福报表,获取最后n个任务结果输出,以table表格形式显示。n默认为10'
Write-Warning './klg.ps1 用【图形版list形式】显示【福报表】结果。只能在win版powershell中使用。'
Write-Warning './ktg.ps1 用【图形版table形式】显示【福报表】结果。只能在win版powershell中使用。'
Write-Warning './kl2.ps1 用【list形式】持续输出【福报表】结果,这类似于ansible。'
if ($持续输出结果 -eq $true)
{
& "${PSScriptRoot}/kl2.ps1"
}
else
{
if ($env:LANG -eq 'zh_CN.UTF-8')
{
$msg = @'
-持续输出结果 $true
即可开启自动输出。
可以手动输入命令:
./kl2
'@
Write-Warning $msg
}
else
{
$msg = @'
wait_output $true
to enable ansible like outout.
Manually execute commands:
./kl2
'@
Write-Warning $msg
}
}
exit 0
PowerShell
1
https://gitee.com/chuanjiao10/kasini3000.git
git@gitee.com:chuanjiao10/kasini3000.git
chuanjiao10
kasini3000
kasini3000
master

搜索帮助