关闭

Powershell 的自动部署

标签: linuxpowershell服务器脚本
784人阅读 评论(0) 收藏 举报
分类:


工作中重复性的版本移植,一天上线10几次,让我痛不欲生,频繁的操作也可能出现疲劳性失误,导致严重的生产故障。于是乎,闲暇时间,我开始研究使用powershell自动部署程序到Linux服务器。

脚本中涉及到以下工具:

1、Wincp:借助其自身的命令行模式完成程序部署

2、powershell的ssh-session模块,通过加载该模块连接到Linux服务器,执行相关shell命令

3、.net


开始上代码


#Public environment configure
$script:linuxPath="D:\test\newpath.txt"
$script:parentPath="D:\test\"
$script:parentPath2=$parentPath -replace "\\","/"
$script:projectConfigureFile="D:\test\projectConfigureFile.csv"


#执行shell函数
function Exec-Bash($computers,$user,$pwd,$linux_command){
    #Check SshSessions Module
    $modules=Get-Command -Module ssh-sessions
    if($modules -eq $null){
        Import-Module ssh-sessions
    }
    if($computers.GetType().IsArray){
        foreach($computer in $computers){
            $sshsession=Get-SshSession|?{$_.computername -eq $computer}
            if($sshsession.Connected -ne "true"){
                New-SshSession -ComputerName $computer -Username $user -Password $pwd -ErrorAction Stop
            }
            Invoke-SshCommand -ComputerName $computer -Quiet -Command $Linux_command
        }
    }else{
        $sshsession=Get-SshSession|?{$_.computername -eq $computers}
        if($sshsession -eq $null){
            New-SshSession -ComputerName $computers -Username $user -Password $pwd -ErrorAction Stop
        }
        Invoke-SshCommand -ComputerName $computers -Quiet -Command $Linux_command
    }
    Remove-Item $linuxPath
}

#回收session函数

function Recycle-Session{
    param($computers)
    if($computers.GetType().IsArray){
        foreach($c in $computers){
            Remove-SshSession -ComputerName $c | out-null
        }
    }else{
        Remove-SshSession -ComputerName $computers | out-null
    }
}

#解密函数,主要用于解密服务器登录密码

function Decrypt-String($Encrypted, $Passphrase, $salt="SaltCrypto", $init="IV_Password"){
    if($Encrypted -is [string]){
        $Encrypted = try{[Convert]::FromBase64String($Encrypted)}catch{return $false}
    }
    $r = new-Object System.Security.Cryptography.RijndaelManaged
    $pass = [Text.Encoding]::UTF8.GetBytes($Passphrase)
    $salt = [Text.Encoding]::UTF8.GetBytes($salt)
    $r.Key = (new-Object Security.Cryptography.PasswordDeriveBytes $pass, $salt, "SHA1", 5).GetBytes(32) #256/8
    $r.IV = (new-Object Security.Cryptography.SHA1Managed).ComputeHash( [Text.Encoding]::UTF8.GetBytes($init) )[0..15]
    $d = $r.CreateDecryptor()
    $ms = new-Object IO.MemoryStream @(,$Encrypted)
    $cs = new-Object Security.Cryptography.CryptoStream $ms,$d,"Read"
    $sr = new-Object IO.StreamReader $cs
    Write-Output $sr.ReadToEnd()
    $sr.Close()
    $cs.Close()
    $ms.Close()
    $r.Clear()
}

#在Windows环境下创建备份信息

function Creat-Path{
    Clear-Content $linuxPath -ErrorAction SilentlyContinue
    $toLinuxPath=dir -r $parentPath |%{$_.versioninfo}|select filename
    $n=""
    foreach($p in $toLinuxPath){
        $newPath=$p.filename -replace "\\","/"|%{$_ -replace $parentPath2,""}
        foreach($newP in $newPath){
            $n=$n +  ' ' + $newp
        }
    }
    $n=$n -replace 'newpath.txt',''
    $n > $linuxPath
    $n=""
}

#上传操作

function Upload{
    $projectNames=dir -Directory $parentPath|%{$_.Name}
    if($projectNames -eq $null){
        Write-Warning "$parentPath 中没有需要操作的项目信息,请确认后再执行"
        pause
        break
    }
    if($projectNames.GetType().IsArray){
        Write-Warning 为避免误操作,请逐个项目备份移植,谢谢!
        pause
        break
    }else{
        $projectConfigureInfo=Import-Csv $projectConfigureFile|?{$_.project -eq $projectNames}
        if($projectConfigureInfo -ne $null){
            $project=$projectConfigureInfo.project
            $computers=$projectConfigureInfo.iplist
            $port=$projectConfigureInfo.port
            $user=$projectConfigureInfo.account
            $passwd=Decrypt-String $projectConfigureInfo.passwd "MyStrongPassword"
            $webpath=$projectConfigureInfo.webpath
            #创建备份信息
            Creat-Path
            $question=Read-Host 备份信息已拼接完成,是否登录到服务器进行备份(Y/N)
            switch($question){
                Y{
                    #连接到服务器执行备份命令
                    if($computers.IndexOf(',') -gt 0){
                        $computers=$computers -split ","
                    }
                    $sourcePath=gc $linuxPath
                    $linux_command="cd $webpath;/sbin/backup $sourcePath"

                    #Exec-Bash $computers[0] $user $passwd $linux_command
                    Recycle-Session $computers
                    if($?){
                        Write-Host 文件备份已完成 -ForegroundColor Green
                        Remove-Item $linuxPath -Force -ErrorAction Stop
                    }
                    break
                }
                N{return $false}
                default{return $false}
            }
            #准备移植
            Write-Host "$projectNames 项目配置信息如下:" -ForegroundColor Yellow
            $projectConfigureInfo|select project,iplist,port,account,webpath|ft
            $rh=Read-Host 请确认是否开始上传文件(Y/N)
            switch($rh){
                Y{
                    if($computers.gettype().isarray){
                        foreach($computer in $computers){
                            $openstr="open"+" sftp://"+$user+":"+$passwd+"@"+$computer+":"+$port
                            D:\Tools\winscp520\WinSCP.exe /console /command "option batch continue" "option confirm off" $openstr "option transfer binary" "put $parentPath*" #"exit"
                        }
                    }else{
                        $openstr="open"+" sftp://"+$user+":"+$passwd+"@"+$computers+":"+$port
                        D:\Tools\winscp520\WinSCP.exe /console /command "option batch continue" "option confirm off" $openstr "option transfer binary" "put $parentPath*" #"exit"
                    }
                    break
                }
                N{return $false}
                default{return $false}
            }

        }else{
            Write-Warning "没有匹配到项目 $projectNames 的配置信息,请检查配置文件 $script:projectConfigureFile"
            pause
            break
        }
    }
}


Upload

#>

2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15487次
    • 积分:334
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论