1. 程式人生 > >使用 expect 重啟失敗的 git pull/push 操作

使用 expect 重啟失敗的 git pull/push 操作

問題的提出

最近使用 github 上傳、下載專案程式碼時,經常會卡很久,有時候在命令列打了 git push 然後就去上廁所了,結果等我回來的時候,發現 push 早已經失敗了,還得重新提交一下。如果有一個工具,可以不停的重啟失敗的 git push 直到它成功才退出,那就好了。

什麼是 expect

在介紹使用 expect 重啟 git 操作之前,先簡單說明一下這個命令。其實它並不是一個新潮的東西,在很早以前就存在了,以至於現在一些系統預設都不帶這個命令了,需要自己手工安裝下:

$ sudo yum install expect
$ expect -v
expect version 5.44.1.15

 

簡單的說,expect 就是完成一些需要與使用者互動的任務,例如 telnet、ftp、ssh 遠端登入機器的時候,這些命令會要求使用者輸入使用者名稱、密碼等相關資訊,而這些,是無法通過 shell 指令碼來完成的。這是因為這些命令是從控制終端而不是標準輸入上讀取的,所以無法事先將資訊重定向到標準輸入從而實現自動化執行。而 expect 就是用來解決這類問題的,下面是一個使用 expect 進行 ssh 登入的例子:

 1 #!/usr/bin/expect -f
 2 set ipaddr "localhost"
 3 set passwd "iforgot"
 4 
 5 spawn ssh root@$ipaddr
 6 expect {
 7 "yes/no" { send "yes\r"; exp_continue}
 8 "password:" { send "$passwd\r" }
 9 }
10 
11 expect "]# "
12 send "touch a.txt\r" 
13 send "exit\r"
14 expect eof
15 exit

 

expect 腳本里有這麼幾個關鍵動作:

  • spawn :啟動需要執行的命令;
  • expect :解析命令輸出,並根據下面的匹配語句進入子控制塊;
  • send :向命令傳送資訊,這些資訊相當於是命令從控制終端讀取的;
  • interact :繼續命令與控制終端的互動,此時使用者可以正常向命令輸入資訊(本例未展示)。
  • ……

好了,熟悉了 expect 的用法後,有人可能有疑問了,這個 git pull/push 操作也不涉及密碼,用它做什麼呢?這就是因人而異了,有些人是因為密碼的關係用它,而我只看中了它的 expect 動作。

失敗日誌與正常日誌

以 git pull 為例,失敗時,它的輸出如下:

$ git pull
ssh: connect to host github.com port 22: Connection refused
fatal: The remote end hung up unexpectedly

 

成功時,它的輸出是這樣的:

$ git pull 
remote: Enumerating objects: 38, done.
remote: Counting objects: 100% (38/38), done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 36 (delta 24), reused 24 (delta 12), pack-reused 0
Unpacking objects: 100% (36/36), done.
From github.com:goodpaperman/apue
   86b80d3..e0cc835  master     -> origin/master
Updating 386fd43..e0cc835
Fast-forward
 apue.c |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

 

如果已經沒有更新的內容可以拉取,它的輸出是這樣的:

$ git pull
Already up-to-date.

 

對於 git push 而言也是大同小異,失敗時:

$ git push
Connection reset by 13.229.188.59 port 22
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

 

成功時:

$ git push
Counting objects: 16, done.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.05 KiB, done.
Total 10 (delta 7), reused 0 (delta 0)
remote: Resolving deltas: 100% (7/7), completed with 6 local objects.
To [email protected]:goodpaperman/apue.git
   87748c7..08e3a1e  master -> master

 

已經是最新時:

$ git push
Everything up-to-date

 

於是很自然的想到了一個解決方案:一直 spawn git pull / push 直到 expect 到我們想要的輸出 "xxx up-to-date."

重啟失敗的操作

利用上面的思路,寫出了下面的 expect 指令碼

pull.exp

1 #! /usr/bin/expect -f
2 set timeout 30
3 for {set i 0} {$i<=10} {incr i} {
4     puts "start pulling git $i"
5     spawn git pull 
6     expect "Already up-to-date." { puts "pulling ok"; exit }
7 }

 

這段指令碼使用了 expect 迴圈,最多嘗試 10 次,如果仍然拉取不成功,則可能是其它原因導致的,此時退出迴圈。

push.exp

1 #! /usr/bin/expect -f
2 set timeout 30
3 for {set i 0} {$i<=10} {incr i} {
4     puts "start pushing git $i"
5     spawn git push 
6     expect "Everything up-to-date" { puts "pushing ok"; exit }
7 }

 

與 pull 類似,只是 expect 的特徵串不同,這裡使用 “Everything up-to-date” 代替 “Already up-to-date.”

但是這樣寫有個缺點,就是如果這個做成指令碼放在某個目錄下的話,我需要明確指定對應的路徑才可以呼叫它。有沒有什麼辦法可以像命令一樣隨時隨地的呼叫這個指令碼呢?

使用 alias

在你的系統上輸入 alias 可以檢視當前開啟的命令別名。

$ alias
alias cd='cdls'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

 

可以看到我的機器上 cd 命令被重定義為 cdls,這又是個什麼神奇的東東呢,開啟 ~/.bashrc,可以看到它的定義:

 1 $ cat ~/.bashrc
 2 # .bashrc
 3 
 4 # Source global definitions
 5 if [ -f /etc/bashrc ]; then
 6     . /etc/bashrc
 7 fi
 8 
 9 # User specific aliases and functions
10 cdls() {  
11     cd "${1}";  
12     ls;  
13 }  
14 
15 alias cd='cdls' 

 

其實就是一個 shell function,裡面組合呼叫了 cd 與 ls 命令,達到切換到新目錄同時列出目錄內容的功能。看到這裡,類比著去實現一個 gpull / gpush 應該不難了吧:

 1 git_pull() { 
 2     expect -c 'set timeout 30; for {set i 0} {$i<=10} {incr i} { puts "start pulling git $i"; spawn git pull; expect "Already up-to-date." { puts "pulling ok"; exit } }'
 3 }
 4 
 5 git_push() {
 6     expect -c 'set timeout 30; for {set i 0} {$i<=10} {incr i} { puts "start pushing git $i"; spawn git push; expect "Everything up-to-date" { puts "pushing ok"; exit } }'
 7 }
 8 
 9 alias gpull='git_pull'
10 alias gpush='git_push'

 

這裡使用 expect 的 -c 選項來在一行內輸入所有指令碼語句,各個語句之間使用分號分隔。在 ~/.bashrc 中加入上面的內容,然後執行以下命令過載 bashrc 檔案

$ . ~/.bashrc

 

就可以使剛加入的 gpull 與 gpush 別名生效啦!當然,這樣做了以後,只對當前使用者生效,其它使用者登入後是無法使用的。可以將這個別名定義在 /etc/bashrc 中,這樣所有使用者就都可以使用啦~ 下面是執行的效果:

$gpull
start pulling git 0
spawn git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:goodpaperman/apue
   65d83a6..8560ad0  master     -> origin/master
Updating 65d83a6..8560ad0
Fast-forward
 apue.c |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
start pulling git 1
spawn git pull
Already up-to-date.
pulling ok
$gpush
start pushing git 0
spawn git push
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 316 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To [email protected]:goodpaperman/apue.git
   8560ad0..0d3c3c7  master -> master
start pushing git 1
spawn git push
Everything up-to-date
pushing ok

 

從上面的輸出可以看到一個問題,就是第一次實際上已經 pull / push 成功了,但是由於沒有得到我們想要的輸出,操作又被重啟了一次,直到它輸出 xxxx up-to-date 為止。可見我們的 expect 也不是非常智慧啊,有關於這個的改進就留給各位看官了……

結語

其實我們這裡只是用了 expect 的指令碼語法,並沒有用到它更高深的部分:終端控制,其實與 expect 類似的還有 script 和 tee 等命令,它們都是在內部開一個偽終端對,來實現對終端輸入/輸出的重定向能力的。與終端相關的內容,可以參考我之前寫的這篇文章:[apue] 書中關於偽終端的一個紕漏

參考

[1]. Linux-expect命令詳解

[2]. expect用法

[3]. expect語法基礎: while、for 迴圈、if 語句的用法示例

[4]. expect(spawn) 自動化git提交和scp拷貝---centos(li