Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5120129
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: LINUX

2013-06-22 12:28:34

In a previous post I wrote a small expect script to update a remote web server’s deployed code on a new commit to a Subversion repository using Expect and Subversion’s post-commit hooks. That first script was extraordinarily basic, so I’ve been wanting to add some sanity and error checking to it for a while. I finally got around to it today.

This improved version of the post-commit hook does the same thing as the last one (that is, it logs into your web server over SSH with the given user and password, and yes, I’m aware of the scariness of embedding a password in such a way, so you should really set up SSH to use public keys for authentication for this), except now it also produces useful output.

Here’s the same script as before, but improved:


  1. #!/usr/bin/expect -f

  2. #
  3. # AUTHOR: Meitar Moscovitz
  4. # DATE : Thu Jun 21 16:32:42 EDT 2007
  5. #

  6. set HOST my.web.server
  7. set USER someuser
  8. set PASS xxx

  9. # the working copy we're going to update
  10. set WC /path/to/working/copy

  11. # the path to the svn executable on the remote web server
  12. set SVNBIN /usr/local/bin/svn

  13. # our network is slow, set a long timeout
  14. set timeout 30

  15. ##### DO NOT EDIT PAST THIS LINE! #####

  16. # POST-COMMIT HOOK
  17. #
  18. # The post-commit hook is invoked after a commit. Subversion runs
  19. # this hook by invoking a program (script, executable, binary, etc.)
  20. # named 'post-commit' (this file) with the
  21. # following ordered arguments:
  22. #
  23. # [1] REPOS-PATH (the path to this repository)
  24. # [2] REV (the number of the revision just committed)
  25. #
  26. # Note that Subversion does not provide this program with an environment
  27. # of any kind. That means this program lacks a current working directory,
  28. # a home directory, a $PATH, and so on.

  29. set REPOS [lindex $argv 0]
  30. set REV [lindex $argv 1]

  31. # Define error codes
  32. set E_NO_SSH 1 ;# can't find a usable SSH on our system
  33. set E_NO_CONNECT 2 ;# failure to connect to remote server (timed out)
  34. set E_WRONG_PASS 3 ;# password provided does not work
  35. set E_UNKNOWN 25 ;# unexpected failure

  36. # find the SSH binary on our system
  37. if {[file executable /usr/bin/ssh]} {
  38.     set SSHBIN /usr/bin/ssh
  39. } elseif {[file executable /usr/local/bin/ssh]} {
  40.     set SSHBIN /usr/local/bin/ssh
  41. } else {
  42.     send_error "Can't find a usable SSH on this system.\n"
  43.     exit $E_NO_SSH
  44. }

  45. spawn $SSHBIN $USER@$HOST $SVNBIN update $WC

  46. expect {
  47.     "continue connecting (yes/no)? " { send "yes\r"; exp_continue; }
  48.     -nocase "password:" { send "$PASS\r"; }
  49.     timeout {
  50.         send_error "\nWe have timed out after $timeout seconds while trying to connect to $HOST!\n";
  51.         exit $E_NO_CONNECT;
  52.     }
  53. }

  54. expect {
  55.     -nocase "password:" { ;# if we are asked for the password again, then we have provided the wrong password
  56.         send_error "\nCan not log in to $HOST because the password provided for user $USER has been rejected.\n";
  57.         exit $E_WRONG_PASS;
  58.     }
  59.     -re "revision (\[0-9]+)." {
  60.         if {$REV == $expect_out(1,string)} {
  61.             send_user "\nSuccessfully updated $WC on $HOST to revision $REV.\n"
  62.         } else {
  63.             send_user "\nUpdated repository to revision $expect_out(1,string), but svn reports that we are at revision number $REV.\n"
  64.             send_error "CAUTION: Repository updated to revision $expect_out(1,string), but committed revision $REV.\n"
  65.         }
  66.     }
  67.     default {
  68.         send_error "An unexpected error has occured. The process at spawn ID $spawn_id has produced the following output:\n"
  69.         send_error $expect_out(buffer)
  70.         exit $E_UNKNOWN
  71.     }
  72. }


from: http://maymay.net/blog/2007/06/21/a-better-expect-subversion-post-commit-hook/

Found a way to get around the svn password prob.

spawn $SSHBIN $USER@$HOST -t “$SVNBIN update $WC -password $PASS2″

阅读(962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~