deploy-java-files.exp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #!/usr/bin/env expect
  2. ################################################################################
  3. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. ################################################################################
  17. proc usage {} {
  18. puts ""
  19. puts "Usage: [lindex [split $::argv0 '/'] end] --ip <value> --source <value>"
  20. puts " --sudo --target <value> --user <value> --userhome <value>"
  21. puts ""
  22. }
  23. proc validateParameters {} {
  24. global ip user target source user_home
  25. set message ""
  26. if { [string length $user] == 0 } {
  27. set message " ${message} user,"
  28. }
  29. if { [string length $ip] == 0 } {
  30. set message "${message} ip,"
  31. }
  32. if { [string length ${target}] == 0 } {
  33. set message "${message} target,"
  34. }
  35. if { [string length ${source}] == 0 } {
  36. set message "${message} source,"
  37. }
  38. if { [string length $user_home] == 0 } {
  39. set message "${message} userhome,"
  40. }
  41. if { [string length $message] > 0 } {
  42. puts ""
  43. puts "Missing[string trimright $message ,]"
  44. usage
  45. exit 1
  46. }
  47. }
  48. ##############################################################
  49. # #
  50. # Check ssh connection and target directory #
  51. # #
  52. ##############################################################
  53. proc checkConnectionAndTarget {} {
  54. global ip user target target_dir prefix prompt password user_home
  55. set timeout 30
  56. #exp_internal 1
  57. puts "Test connection and make sure target directory exists."
  58. if { [ string length $::env(password)] == 0 } {
  59. spawn ssh -i ${user_home}/.ssh/id_rsa -o BatchMode=yes \
  60. -o LogLevel=QUIET -o StrictHostKeyChecking=no ${user}@${ip}
  61. } else {
  62. spawn ssh -o LogLevel=QUIET ${user}@${ip}
  63. }
  64. expect {
  65. *?assword:* {
  66. send "${password}\r"
  67. exp_continue
  68. } "*?ermission denied*" {
  69. puts "The user or password is wrong"
  70. exit 1
  71. } yes/no)? {
  72. send "yes\r"
  73. exp_continue
  74. } timeout {
  75. exit 1;
  76. } eof {
  77. wait
  78. exit 1;
  79. } -re "${prompt}" { }
  80. }
  81. send "if \[ ! -d ${target} \]; then mkdir -p ${target}; fi\r"
  82. expect -re ".*"
  83. send "echo \$?\r"
  84. expect -re "(\r\n| )(\[0-9]*)\r\n" {
  85. if { [string compare $expect_out(2,string) "0" ] != 0 } {
  86. puts "Failed to check ${target}"
  87. close;wait
  88. exit 1
  89. }
  90. }
  91. if { [string compare $prefix "sudo"] == 0 } {
  92. send "mkdir ${target_dir}\r"
  93. expect -re ".*"
  94. send "echo \$?\r"
  95. expect -re "(\r\n| )(\[0-9]*)\r\n" {
  96. if { [string compare $expect_out(2,string) "0" ] != 0 } {
  97. puts "Failed to check ${target}"
  98. close;wait
  99. exit 1
  100. }
  101. }
  102. }
  103. send "exit\r "
  104. interact
  105. }
  106. ##############################################################
  107. # #
  108. # Transfer file to remote system #
  109. # #
  110. ##############################################################
  111. proc transferFiles {} {
  112. global ip user source target_dir prompt password user_home
  113. set timeout 60
  114. puts "${ip}: Copying ${source} to ${target_dir} on ${ip}"
  115. if { [ string length ${password}] == 0 } {
  116. spawn bash -c "scp -r -i ${user_home}/.ssh/id_rsa ${source} ${user}@${ip}:${target_dir}"
  117. } else {
  118. spawn bash -c "scp -r ${source} ${user}@$ip:${target_dir}"
  119. }
  120. expect {
  121. *?assword:* {
  122. send "${password}\r"
  123. exp_continue
  124. } "*?ermission denied*" {
  125. puts "The user or password is wrong"
  126. exit 1
  127. } yes/no)? {
  128. send "yes\r"
  129. exp_continue
  130. } timeout {
  131. exit 1;
  132. } eof {
  133. catch wait result
  134. if { [lindex $result 3] != 0 } {
  135. exit [lindex $result 3]
  136. }
  137. }
  138. }
  139. }
  140. ##############################################################
  141. # #
  142. # mv transfered files to target directory through sudo #
  143. # #
  144. ##############################################################
  145. proc moveFiles {} {
  146. global ip user prefix target target_dir prompt password user_home
  147. set timeout 30
  148. #exp_internal 1
  149. if { [string compare $prefix "sudo" ] != 0 } {
  150. return
  151. }
  152. puts "Move files from temporay directory to target through sudo"
  153. if { [string length ${password}] == 0 } {
  154. spawn ssh -i ${user_home}/.ssh/id_rsa -o BatchMode=yes \
  155. -o LogLevel=QUIET -o StrictHostKeyChecking=no ${user}@${ip}
  156. } else {
  157. spawn ssh -o LogLevel=QUIET ${user}@${ip}
  158. }
  159. expect {
  160. *?assword:* {
  161. send "${password}\r"
  162. exp_continue
  163. } "*?ermission denied*" {
  164. puts "The user or password is wrong"
  165. exit 1
  166. } yes/no)? {
  167. send "yes\r"
  168. exp_continue
  169. } timeout {
  170. exit 1;
  171. } eof {
  172. exit 1;
  173. } -re "${prompt}" { }
  174. }
  175. sleep 1
  176. expect -re .*
  177. send "sudo cp -r ${target_dir}/* ${target}\r"
  178. sleep 2
  179. expect {
  180. *?assword* {
  181. send "${password}\r"
  182. exp_continue
  183. } "orry, try again" {
  184. puts "The user or password is wrong"
  185. exit 1
  186. } timeout {
  187. exit 1;
  188. } eof {
  189. exit 1;
  190. } -re "${prompt}" { }
  191. }
  192. expect -re .*
  193. send "echo \$?\r"
  194. expect -re "(\r\n| )(\[0-9]*)\r\n" {
  195. if { [string compare $expect_out(2,string) "0" ] == 0 } {
  196. send "rm -rf ${target_dir}\r"
  197. expect {
  198. timeout {
  199. exit 1;
  200. } eof {
  201. exit 1;
  202. } -re "${prompt}" { }
  203. }
  204. send "exit\r"
  205. } else {
  206. puts "Failed to copy to $target through sudo"
  207. close;wait
  208. exit 1
  209. }
  210. }
  211. interact
  212. }
  213. ##############################################################
  214. # #
  215. # Main #
  216. # #
  217. ##############################################################
  218. set user ""
  219. set ip ""
  220. set prefix ""
  221. set source ""
  222. set target ""
  223. set user_home ""
  224. for { set i 0 } { $i < $argc } { incr i } {
  225. if { [string compare -nocase [lindex $argv $i] "--ip"] == 0 } {
  226. incr i
  227. set ip [lindex $argv $i]
  228. } elseif { [string compare -nocase [lindex $argv $i] "--user"] == 0 } {
  229. incr i
  230. set user [lindex $argv $i]
  231. } elseif { [string compare -nocase [lindex $argv $i] "--source"] == 0 } {
  232. incr i
  233. set source [lindex $argv $i]
  234. } elseif { [string compare -nocase [lindex $argv $i] "--target"] == 0 } {
  235. incr i
  236. set target [lindex $argv $i]
  237. } elseif { [string compare -nocase [lindex $argv $i] "--userhome"] == 0 } {
  238. incr i
  239. set user_home [lindex $argv $i]
  240. } elseif { [string compare -nocase [lindex $argv $i] "--sudo"] == 0 } {
  241. set prefix "sudo"
  242. }
  243. }
  244. set prompt "(${user}@|\\r\\n\\$ |\\r\\n# )"
  245. set password $::env(password)
  246. validateParameters
  247. if { [string compare $prefix "sudo"] == 0 } {
  248. set target_dir /tmp/target_dir_[pid]
  249. } else {
  250. set target_dir ${target}
  251. }
  252. checkConnectionAndTarget
  253. transferFiles
  254. moveFiles