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

脚本中涉及到下面工具:

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

2、powershell的ssh-session模块。通过载入该模块连接到Linuxserver,运行相关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

    }

}

#解密函数。主要用于解密server登录password

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 备份信息已拼接完毕。是否登录到server进行备份(Y/N)

            switch($question){

                Y{

                    #连接到server运行备份命令

                    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

#>

Powershell 的自己主动部署的更多相关文章

  1. 菜鸟教程工具(三)——Maven自己主动部署Tomcat

    书连接至背面,在博客上,他介绍了如何使用Maven该项目包,这篇文章说,关于如何使用Maven会踢war部署包Tomcat.而不是手动copy过去. 眼下比較流行的方式有两种:一种是利用Tomcat官 ...

  2. 【大话QT之十三】系统软件自己主动部署实现方案

    本篇文章是对[大话QT之十二]基于CTK Plugin Framework的插件版本号动态升级文章的补充,在上篇文章中我们阐述的重点是新版本号的插件已经下载到plugins文件夹后应该怎样更新本地正在 ...

  3. Eclipse中的Web项目自己主动部署到Tomcat

    一.原因. 1.写java程序有一段时间了,但非常久没用eclipse了.所以使用eclipse编写的web项目部署到tomcat 的方式也不是非常清楚,以下记录一下将Eclipse 上的web项目自 ...

  4. SVN配置以及自己主动部署到apache虚拟文件夹

    SVN配置以及自己主动部署到apache虚拟文件夹 一.VisualSVN server 服务端和TortoiseSVNclient下载 VisualSVN下载:http://subversion.a ...

  5. MAVEN自己主动部署到tomcat

    前面几篇文章maven生成的war包都是手动部署到tomcat,显然这样是非常麻烦的.那么这一篇文章就来介绍一个怎样使用maven高速自己主动的部署项目到tomcat容器中. 1.首先我们须要配置to ...

  6. 【转】Powershell与jenkins集成部署的运用(powershell运用)

    powershell简介: 远程管理采用的一种新的通信协议,Web Services for Management,简称WS-MAN它通过http或者https进行工作,WS-WAN的实现主要基于一个 ...

  7. Eclipse中的Web项目自己主动部署到Tomcat以及怎样在Eclipse中使用My Eclipseproject

    我是一个新手学习Java,servlet和Jsp. 痛苦的是我时候一个.net程序猿,习惯了微软的VS IDE一切都是封装好的.傻瓜式的使用, 不须要关心内部实现. 悲催的是我看到资料都是My Ecl ...

  8. Service Fabric 用 Powershell 部署应用到本地

    前置说明 安装 Service Fabric SDK,会在本机 C:\Program Files\Microsoft SDKs\Service Fabric\Tools\PSModule\Servic ...

  9. Maven实现Web应用集成測试自己主动化 -- 部署自己主动化(WebTest Maven Plugin)

    上篇:Maven实现Web应用集成測试自己主动化 -- 測试自己主动化(WebTest Maven Plugin) 之前介绍了怎样在maven中使用webtest插件实现web的集成測试,这里有个遗留 ...

随机推荐

  1. modelsim 保存波形文件

    1. do文件记录了仿真的过程和加载的各种库. do文件的保存过程: file——>save format——>D:/modeltech_6.5b/examples/run_wave.do ...

  2. [na]wac无线控制器集中转发部署的几种情况

    1,背景: sta属于vlan20.ap属于vlan20.本地转发. 现象: Ap获取到的地址是vlan20的地址池的某地址 用户无法获取地址. 注意:本地转发,ap获取地址dhcp包不走隧道.用户获 ...

  3. 收集Cocos2d提供的字体!共57种

    很多游戏中,便于开发就会直接采用Cocos2d提供的字体库来使用,但是因为提供的种类很多,那么Himi利用一些时间整理了57种字体样式,这些我想足够你用了: 不多说下面一起来看看吧:(每张图对应11种 ...

  4. 简单好用的hash表-----uthash

    在软件开发中,不可不免的会使用到hash表,hash表的优点这里就不说了,以下介绍一个hash表的C实现, uthash是用宏实现的,使用的时候非常方便,只用包含uthash.h即可. Uthash的 ...

  5. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  6. 解决linux分区提示doesn't contain a valid partition table

    目前 partition table 大概有叁种: 最传统的 mbr.大容量的 gpt.小设备的 无; 遇上最后那种就会出现 "doesn't contain a valid partiti ...

  7. IOS之TableViewCell重用机制避免重复显示问题

    常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复. 1 // 这样配置的话超过页面显示的内容会重复出现 2 - (UITableViewCell *)tableVi ...

  8. iOS-打包成ipa的4种方法

    打包ipa的前提 1.证书的申请和设置和上面文章的一样 从第一步到第四步都是一样的http://www.cnblogs.com/sunfuyou/p/5900592.html2.还有第六步的 1-3都 ...

  9. Thrift RPC框架介绍

    u 简介 Thrift是一种开源的跨语言的RPC服务框架.Thrift最初由facebook公司开发的,在2007年facebook将其提交apache基金会开源了.对于当时的facebook来说创造 ...

  10. 纯css实现进度条效果

    去年7月份做一个公司商城的微信页面(微信用的chrome内核)需要写一个提示返现进度的进度条效果. 一个完整的进度条效果其实可以拆分一下: 一段背景: 一小段的静态的斜纹进度条: 斜纹进度条用线性渐变 ...