super-test 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGE.txt +9 -0
  3. data/README.txt +13 -0
  4. data/bin/idb +19 -0
  5. data/bin/ii +113 -0
  6. data/bin/istart_mock +36 -0
  7. data/bin/istop_mock +16 -0
  8. data/bin/kk +181 -0
  9. data/bin/rubyatp +42 -0
  10. data/bin/start_mock +36 -0
  11. data/bin/stop_mock +16 -0
  12. data/bin/xgo +45 -0
  13. data/conf/conf.rb +11 -0
  14. data/conf/db03.yml +31 -0
  15. data/conf/db13.yml +37 -0
  16. data/conf/db19.yml +33 -0
  17. data/conf/online.yml +32 -0
  18. data/conf/super_test.yml +40 -0
  19. data/conf/system.yml +26 -0
  20. data/conf/sz00.yml +31 -0
  21. data/conf/sz06.yml +34 -0
  22. data/conf/wise00.yml +11 -0
  23. data/data/case.house +0 -0
  24. data/data/char.house +0 -0
  25. data/data/images/Bluehills.jpg +0 -0
  26. data/data/images/Sunset.jpg +0 -0
  27. data/data/images/Waterlilies.jpg +0 -0
  28. data/data/images/Winter.jpg +0 -0
  29. data/data/query.house +18595 -0
  30. data/data/user.house +8 -0
  31. data/lib/action/module/check_action.rb +58 -0
  32. data/lib/action/module/cnnt_action.rb +161 -0
  33. data/lib/action/module/datacenter_action.rb +48 -0
  34. data/lib/action/module/http_pack_helper.rb +163 -0
  35. data/lib/action/module/httpserver_action.rb +297 -0
  36. data/lib/action/module/inc.rb +21 -0
  37. data/lib/action/module/mcpack_action.rb +57 -0
  38. data/lib/action/module/sql_action.rb +84 -0
  39. data/lib/action/module/ssh_action.rb +37 -0
  40. data/lib/action/system/user_action.rb +180 -0
  41. data/lib/common/appium_helper.rb +217 -0
  42. data/lib/common/atp_helper.rb +74 -0
  43. data/lib/common/atp_report.rb +344 -0
  44. data/lib/common/data_house.rb +405 -0
  45. data/lib/common/data_model.rb +87 -0
  46. data/lib/common/http/assert_helper.rb +343 -0
  47. data/lib/common/http/fix_hpricot.rb +118 -0
  48. data/lib/common/http/html_helper.rb +362 -0
  49. data/lib/common/http/http_helper.rb +469 -0
  50. data/lib/common/http/multipart.rb +81 -0
  51. data/lib/common/isandbox_helper.rb +195 -0
  52. data/lib/common/json_helper.rb +43 -0
  53. data/lib/common/mock_helper.rb +168 -0
  54. data/lib/common/mtop_helper.rb +141 -0
  55. data/lib/common/myconf_helper.rb +49 -0
  56. data/lib/common/mylog_helper.rb +77 -0
  57. data/lib/common/pairwise.rb +121 -0
  58. data/lib/common/query_house.rb +89 -0
  59. data/lib/common/report_helper.rb +97 -0
  60. data/lib/common/robot_helper.rb +85 -0
  61. data/lib/common/socket/check.rb +325 -0
  62. data/lib/common/socket/conf_modifier.rb +149 -0
  63. data/lib/common/socket/context.rb +55 -0
  64. data/lib/common/socket/data.rb +392 -0
  65. data/lib/common/socket/data_helper.rb +41 -0
  66. data/lib/common/socket/env.rb +317 -0
  67. data/lib/common/socket/inc.rb +24 -0
  68. data/lib/common/socket/log.rb +213 -0
  69. data/lib/common/socket/log4s.rb +58 -0
  70. data/lib/common/socket/log_helper.rb +332 -0
  71. data/lib/common/socket/my_sql.rb +77 -0
  72. data/lib/common/socket/net.rb +74 -0
  73. data/lib/common/socket/network.rb +115 -0
  74. data/lib/common/socket/rlib/conf_modifier.rb +130 -0
  75. data/lib/common/socket/rlib/data_helper.rb +33 -0
  76. data/lib/common/socket/rlib/log_helper.rb +303 -0
  77. data/lib/common/socket/rlib/process_helper.rb +91 -0
  78. data/lib/common/socket/stub.rb +276 -0
  79. data/lib/common/socket/util.rb +266 -0
  80. data/lib/patterns/inc.rb +10 -0
  81. data/lib/patterns/mc_base_pattern.rb +474 -0
  82. data/lib/patterns/some_pattern.rb +145 -0
  83. data/lib/super_test.rb +114 -0
  84. data/log/super_test.log +0 -0
  85. data/log/super_test.log.wf +0 -0
  86. data/test/cover_me.rb +6 -0
  87. data/tool/jenny +0 -0
  88. data/tool/mcsend/mcsend2 +0 -0
  89. data/tool/mcsend/mcsend2_shead +0 -0
  90. data/tool/mcserver/conf/mcserver.conf +77 -0
  91. data/tool/mcserver/conf/mcserverauthip +17 -0
  92. data/tool/mcserver/mcserver +0 -0
  93. data/tool/mcserver/mcserver_trans +0 -0
  94. data/tool/mysql +0 -0
  95. data/tool/net/mcsend/mcsend +0 -0
  96. data/tool/net/mcsend/mcsend_trans +0 -0
  97. data/tool/net/mcsend/nshead.data +7 -0
  98. data/tool/net/mcsend/res_nshead.data +7 -0
  99. data/tool/net/my_client/my_client +0 -0
  100. data/tool/net/tsclient/client.tcl +205 -0
  101. data/tool/others/kk +190 -0
  102. data/tool/others/rubyatp +42 -0
  103. data/tool/others/supertest_install.sh +34 -0
  104. data/tool/trip/idb +0 -0
  105. data/tool/ts-agent/nshead.bft +12 -0
  106. data/tool/ts-agent/server.conf +10 -0
  107. data/tool/ts-agent/server.tcl +376 -0
  108. metadata +275 -0
@@ -0,0 +1,91 @@
1
+ #$LOAD_PATH << File.join(File.dirname(__FILE__), '../')
2
+ #!/usr/bin/env ruby
3
+ # coding: utf-8
4
+
5
+ #require 'log4s'
6
+ #============================================================
7
+ # This class suply local process related mehtods
8
+ # Author:: chenjie
9
+ # Date:: 2009-4-30
10
+ #============================================================
11
+ class ProcessHelper
12
+
13
+ # 用supervise启动单个模块进程
14
+ # - module_name module name, e.g.: appui
15
+ # - super_path default "/home/space/space"
16
+ # - sample Env.start_process("/home/space/space", "loadspace.sh")
17
+ # return true/false
18
+ def ProcessHelper.start_process(module_name,super_path="/home/space/space/", child_path="./bin/")
19
+ cmd_str = "cd #{super_path}; nohup #{child_path}#{module_name} >/dev/null 2>&1 &"
20
+ # $log.debug "start process: "+cmd_str
21
+ system "#{cmd_str}"
22
+
23
+ sleep 2
24
+ if ProcessHelper.is_process_running?(module_name) then
25
+ # $log.debug "process #{module_name} is running"
26
+ return true
27
+ else
28
+ # $log.debug "process #{module_name} is not running"
29
+ return false
30
+ end
31
+ end
32
+
33
+ # killall方式杀死进程
34
+ # execute killall to kill a process, if the process is supervised, then it will restart
35
+ # return: void
36
+ def ProcessHelper.kill_process(module_name)
37
+ cmd_str = "killall -9 #{module_name}"
38
+ # $log.debug "kill process cmd = #{cmd_str}"
39
+ `#{cmd_str}`
40
+ sleep 1
41
+ end
42
+
43
+
44
+ # 判断进程是否正在运行
45
+ # return true/false
46
+ def ProcessHelper.is_process_running?(module_name)
47
+ res = `pstree | grep #{module_name} | wc -l`
48
+ if res.to_i > 0 then
49
+ return true
50
+ else
51
+ return false
52
+ end
53
+ end
54
+
55
+ # 获取模块进程的线程数
56
+ # - module_name
57
+ # return thead number or 0(process not running)
58
+ def ProcessHelper.get_thread_num(module_name)
59
+ str = `pstree | grep #{module_name}`
60
+ mdata = str.scan /.*---(\d+).*/
61
+ if mdata.size > 0 then
62
+ return mdata[0][0].to_i
63
+ else
64
+ return 0
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ if ARGV.length > 0 then
71
+ case ARGV[0]
72
+ when "start_process"
73
+ module_name = ARGV[1]
74
+ super_path = ARGV[2]
75
+ puts ProcessHelper.start_process(module_name, super_path)
76
+
77
+ when "kill_process"
78
+ module_name = ARGV[1]
79
+ puts ProcessHelper.kill_process(module_name)
80
+
81
+ when "is_process_running?"
82
+ module_name = ARGV[1]
83
+ puts ProcessHelper.is_process_running?(module_name)
84
+
85
+ when "get_thread_num"
86
+ module_name = ARGV[1]
87
+ puts ProcessHelper.get_thread_num(module_name)
88
+ else
89
+ puts "ProcessHelper: bad method name? #{ARGV[0]}"
90
+ end
91
+ end
@@ -0,0 +1,276 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '.')
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../conf/')
3
+ #!/usr/bin/env ruby
4
+ # coding: utf-8
5
+ # author: chenjie
6
+ # data: 2009-7-13
7
+
8
+ require 'conf'
9
+ require 'context'
10
+ require 'env'
11
+
12
+ #========================================================
13
+ # 下游桩相关的服务
14
+ #========================================================
15
+
16
+ module Stub
17
+ include Env
18
+
19
+ # === 功能
20
+ # 启动1到多个下游桩
21
+ # === 参数
22
+ # - phash: 参数hash
23
+ # - return:如果启动失败,抛出异常
24
+ #
25
+ # === Example
26
+ #
27
+ # start_stub 'stub1'=>9998, 'stub2'=>9999
28
+ def start_stub phash
29
+ clear_context
30
+
31
+ #load into mem
32
+ stubs = []
33
+ ports = []
34
+ actions = []
35
+ heads = []
36
+ files = []
37
+ phash.each_pair{|key, value|
38
+ stubs << key
39
+ ports << value
40
+
41
+ #by default
42
+ actions << 0
43
+ heads << ""
44
+ files << ""
45
+ }
46
+
47
+ Context.set :STUBS, stubs
48
+ Context.set :STUB_PORTS, ports
49
+ Context.set :STUB_ACTIONS, actions
50
+ Context.set :STUB_HEADS, heads
51
+ Context.set :STUB_FILES, files
52
+
53
+
54
+ #modify server.conf
55
+ save_to_conf
56
+
57
+ #start server
58
+ #cmd_str = "cd #{STUB_PATH}; nohup ts-agent -f server.tcl >/dev/null 2>&1 &"
59
+ cmd_str = "cd #{STUB_PATH}; nohup ts-agent -f server.tcl &"
60
+ system "#{cmd_str}"
61
+
62
+ sleep 1
63
+
64
+ #TODO get it's background pid?
65
+ end
66
+
67
+ # ===功能
68
+ # 停止前一次启动的下游桩,需要上下文支持
69
+ # ===example
70
+ # stop_stub
71
+ def stop_stub
72
+ `killall -9 ts-agent`
73
+ clear_context
74
+
75
+ sleep 1
76
+ #TODO kill just by pid
77
+ end
78
+
79
+ # ===功能
80
+ # 设置下游桩的工作模式
81
+ # ===参数
82
+ # - phash: 参数hash
83
+ #
84
+ # === Example
85
+ #
86
+ # set_stub_action 'stub1'=>1, 'stub2'=>2
87
+ # set_stub_action 'stub2'=>2
88
+ def set_stub_action phash
89
+ phash.each_pair{|key, value|
90
+ #get idx
91
+ idx = get_idx_by_name key, Context.get(:STUBS)
92
+
93
+ #modify context according to the idx
94
+ actions = Context.get :STUB_ACTIONS
95
+ actions[idx] = value
96
+ Context.set :STUB_ACTIONS, actions
97
+ }
98
+
99
+ save_actions
100
+ end
101
+
102
+ # ===功能
103
+ # 设置下游桩的响应头
104
+ # ===参数
105
+ # - phash: 参数hash
106
+ #
107
+ # === Example
108
+ #
109
+ # set_stub_file 'stub1'=>'/home/.../head1', 'stub2'=>'/home/.../head2'
110
+ # set_stub_file 'stub1'=>'/home/.../head1'
111
+ def set_stub_head phash
112
+ phash.each_pair{|key, value|
113
+ #get idx
114
+ idx = get_idx_by_name key, Context.get(:STUBS)
115
+
116
+ #modify context according to the idx
117
+ heads = Context.get :STUB_HEADS
118
+ heads[idx] = value
119
+ Context.set :STUB_HEADS, heads
120
+ }
121
+
122
+ save_heads
123
+ end
124
+
125
+ # ===功能
126
+ # 设置下游桩的响应文件
127
+ # ===参数
128
+ # - phash: 参数hash
129
+ #
130
+ # === Example
131
+ #
132
+ # set_stub_file 'stub1'=>'/home/.../data1', 'stub2'=>'/home/.../data2'
133
+ # set_stub_file 'stub1'=>'/home/.../data1'
134
+ def set_stub_file phash
135
+ phash.each_pair{|key, value|
136
+ #get idx
137
+ idx = get_idx_by_name key, Context.get(:STUBS)
138
+
139
+ #modify context according to the idx
140
+ files = Context.get :STUB_FILES
141
+ files[idx] = value
142
+ Context.set :STUB_FILES, files
143
+ }
144
+
145
+ save_files
146
+ end
147
+
148
+
149
+ private
150
+ # ===功能
151
+ # 清理上下文中保存的所有stub相关内容
152
+ # :STUBS, :STUB_PORTS, :STUB_ACTIONS, :STUB_FILES
153
+ # ===example
154
+ # clear_context
155
+ def clear_context
156
+ $log.info "clearing context for stubs..."
157
+ Context.delete :STUBS
158
+ Context.delete :STUB_PORTS
159
+ Context.delete :STUB_ACTIONSa
160
+ Context.delete :STUB_HEADS
161
+ Context.delete :STUB_FILES
162
+ end
163
+
164
+ # ===功能
165
+ # 将数组序列化成以','隔开的字符串
166
+ # ===example
167
+ # str = arr_to_s array
168
+ def arr_to_s arr
169
+ if arr == nil
170
+ return ''
171
+ end
172
+
173
+ str = ""
174
+ for i in 0..arr.size-1 do
175
+ if i == 0
176
+ str = arr[0]
177
+ else
178
+ str = "#{str}, #{arr[i]}"
179
+ end
180
+ end
181
+
182
+ str
183
+ end
184
+
185
+ # ===功能
186
+ # 获取指定stub在上下文数组中的索引号
187
+ # ===参数
188
+ # - stub_name : stub名称
189
+ # - stubs : the stubs array
190
+ # return: sucess -> idx in stubs array, fail -> -1
191
+ # ===example
192
+ # get_idx_by_name "name1", stubs
193
+ def get_idx_by_name stub_name, stubs
194
+ if ( (stubs == nil) || (stubs.size == 0) )
195
+ return -1
196
+ end
197
+
198
+ idx = 0
199
+ stubs.each{|stub|
200
+ if stub == stub_name
201
+ return idx
202
+ end
203
+
204
+ idx += 1
205
+ }
206
+
207
+ return -1
208
+ end
209
+
210
+ def save_stubs
211
+ stubs_str = arr_to_s(Context.get :STUBS)
212
+ $log.debug "saving stub conf: stubs=>#{stubs_str}"
213
+ modify_conf '127.0.0.1', USERNAME, PASSWORD, "#{STUB_PATH}server.conf", {'stubs'=>stubs_str}
214
+ end
215
+
216
+ def save_ports
217
+ ports_str = arr_to_s(Context.get :STUB_PORTS)
218
+ $log.debug "saving stub conf: ports=>#{ports_str}"
219
+ modify_conf '127.0.0.1', USERNAME, PASSWORD, "#{STUB_PATH}server.conf", {'ports'=>ports_str}
220
+ end
221
+
222
+ def save_actions
223
+ actions_str = arr_to_s(Context.get :STUB_ACTIONS)
224
+ $log.debug "saving stub conf: actions=>#{actions_str}"
225
+ modify_conf '127.0.0.1', USERNAME, PASSWORD, "#{STUB_PATH}server.conf", {'actions'=>actions_str}
226
+ end
227
+
228
+ def save_heads
229
+ heads_str = arr_to_s(Context.get :STUB_HEADS)
230
+ $log.debug "saving stub conf: heads=>#{heads_str}"
231
+ modify_conf '127.0.0.1', USERNAME, PASSWORD, "#{STUB_PATH}server.conf", {'heads'=>heads_str}
232
+ end
233
+
234
+ def save_files
235
+ files_str = arr_to_s(Context.get :STUB_FILES)
236
+ $log.debug "saving stub conf: files=>#{files_str}"
237
+ modify_conf '127.0.0.1', USERNAME, PASSWORD, "#{STUB_PATH}server.conf", {'files'=>files_str}
238
+ end
239
+
240
+ # ===功能
241
+ # 将上下文中的桩相关内容持久化到server.conf文件中
242
+ # ===example
243
+ # save_to_conf
244
+ def save_to_conf
245
+ save_stubs
246
+ save_ports
247
+ save_actions
248
+ save_heads
249
+ save_files
250
+ end
251
+ end
252
+
253
+
254
+ if __FILE__ == $0
255
+ require 'test/unit'
256
+ require "#{File.dirname(__FILE__)}/../conf/conf"
257
+
258
+ class StubTest < Test::Unit::TestCase
259
+ include Stub
260
+ def no_test_start_stub
261
+ start_stub 'stub1'=>11111, 'stub2'=>22222
262
+ end
263
+
264
+ def no_test_arr_to_s
265
+ puts arr_to_s ["stub1", "stub2"]
266
+ end
267
+
268
+ def no_test_get_idx_by_name
269
+ puts get_idx_by_name "stub2", ['stub1', 'stub2', 'stub3']
270
+ puts get_idx_by_name "stub4", ['stub1', 'stub2', 'stub3']
271
+ puts get_idx_by_name "stub2", []
272
+ puts get_idx_by_name "stub2", nil
273
+
274
+ end
275
+ end
276
+ end
@@ -0,0 +1,266 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '.')
4
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../conf/')
5
+ #require 'log4s'
6
+ require 'conf'
7
+
8
+
9
+ #============================================================
10
+ # 基础库辅助类
11
+ # Author:: chenjie
12
+ # Data:: 2009-4-30
13
+ #============================================================
14
+ class Util
15
+
16
+ # ===功能
17
+ # 判断传入ip是否是本机ip
18
+ # return: true/false
19
+ # ===example
20
+ # Util.is_localhost? "10.4.34.23"
21
+ def Util.is_localhost? ip
22
+ #get localhost ip
23
+ local_ip = `hostname -i`.chomp
24
+
25
+ if ( (ip == nil) || (ip == local_ip) || (ip == "127.0.0.1") || (ip == "localhost") ) then
26
+ return true
27
+ else
28
+ return false
29
+ end
30
+ end
31
+ =begin
32
+ # get the data value from the staf returned string like below:
33
+ # - staf_str
34
+ # {
35
+ # Return Code: 0
36
+ # Key : <None>
37
+ # Files : [
38
+ # {
39
+ # Return Code: 0
40
+ # Data : 10555005
41
+ # }
42
+ # ]
43
+ # }
44
+ # will return the data value 10555005,and return empty array if fail
45
+ #
46
+ # return the data value string array
47
+ def Util.get_staf_data(staf_str)
48
+ data = Array.new
49
+ flag = false
50
+
51
+
52
+ staf_str.split("\n").each{|line|
53
+ if flag == false then
54
+ mdata = line.strip.scan(/^Data.*:(.*)/)
55
+ if mdata.size > 0 then
56
+ flag = true
57
+ data<<mdata[0][0].to_s.strip.chomp
58
+ end
59
+ else
60
+ #get all the data below until "}"
61
+ s = line.strip.chomp
62
+ if s == "}" then
63
+ return data
64
+ else
65
+ if s.length > 0 then
66
+ data << s
67
+ end
68
+ end
69
+ end
70
+ }
71
+
72
+ return data
73
+ end
74
+
75
+ # 执行staf命令
76
+ # - host remote machine ip
77
+ # - cmdstr shell cmd string
78
+ # return data segment string
79
+ def Util.exe_staf_cmd(host, cmdstr)
80
+ staf_cmd = "staf #{host} process start shell command 'source ~/.bash_profile && #{cmdstr}' WAIT RETURNSTDOUT STDERRTOSTDOUT"
81
+ $log.debug "STAF_CMD=#{staf_cmd}"
82
+ res = `#{staf_cmd}`
83
+ $log.debug "STAF_RES=#{res}"
84
+ return Util.get_staf_data(res)
85
+ end
86
+ =end
87
+
88
+
89
+ # ===功能
90
+ # 返回指定机器的lib路径, 执行staf命令相关
91
+ # - ip 目标机器ip, string
92
+ # return string, 目标机器上部署的lib路径
93
+ # ===example
94
+ # Util.get_lib_path host
95
+ def Util.get_lib_path host, user=USERNAME, password=PASSWORD
96
+ if Util.is_localhost? host then
97
+ return LIB_ROOT
98
+ else
99
+ cmd_str = "echo $HOME"
100
+ home = Util.exe_ssh_cmd! host, cmd_str, user, password
101
+ #return "#{home}/tools/app-test/search/space/module-test/lib/"
102
+ return "#{home}/tools/rlib/"
103
+ end
104
+ end
105
+
106
+ # ===功能
107
+ # 在远程机器上执行命令
108
+ # 封装Net::SSH::Connection::Session.exe!
109
+ # Same as exec, except this will block until the command finishes
110
+ # ===example
111
+ # Util.exe_ssh_cmd! host, "killall -9 friend"
112
+ def Util.exe_ssh_cmd! host, cmd_str, user=USERNAME, password=PASSWORD
113
+ ssh_cmd = "source ~/.bash_profile && #{cmd_str}"
114
+ $log.debug "SSH CMD = #{ssh_cmd}"
115
+ res = ''
116
+ Net::SSH.start(host, user, :password=>password) do |session|
117
+ res = session.exec! ssh_cmd
118
+ end
119
+ res = res.to_s.strip.chomp
120
+ $log.debug "SSH CMD RES = #{res}"
121
+ return res
122
+ end
123
+
124
+ # ===功能
125
+ # 在远程机器上执行命令
126
+ # 封装Net::SSH::Connection::Session.exe
127
+ # "Note that this method returns immediately, and requires an event loop (see Session#loop) in order for the command to actually execute."
128
+ # ===example
129
+ # Util.exe_ssh_cmd host, "ls"
130
+ def Util.exe_ssh_cmd host, cmd_str, user=USERNAME, password=PASSWORD
131
+ ssh_cmd = "source ~/.bash_profile && #{cmd_str}"
132
+ $log.debug "SSH CMD = #{ssh_cmd}"
133
+ res = ''
134
+ Net::SSH.start(host, user, :password=>password) do |session|
135
+ res = session.exec ssh_cmd
136
+ end
137
+ res = res.to_s.strip.chomp
138
+ $log.debug "SSH CMD RES = #{res}"
139
+ return res
140
+ end
141
+
142
+ # ===功能
143
+ #返回本机IP(非127.0.0.1)
144
+ # ===example
145
+ # lip = Util.get_local_ip
146
+ def Util.get_local_ip
147
+ ip = `hostname -i`
148
+ return ip
149
+ end
150
+
151
+ # ===功能
152
+ # scp远端文件到本机
153
+ # ===example
154
+ # Util.download host, remote, local
155
+ def Util.download host, remote_path, local_path, user=USERNAME, password=PASSWORD
156
+ Net::SCP.start(host, user, :password=>password) do | scp |
157
+ scp.download! remote_path, local_path
158
+ end
159
+ end
160
+
161
+ # ===功能
162
+ # scp本机文件到远端
163
+ # ===example
164
+ # Util.upload host, local, remote
165
+ def Util.upload host, local_path, remote_path, user=USERNAME, password=PASSWORD
166
+ Net::SCP.start(host, user, :password=>password) do | scp |
167
+ scp.upload! local_path, remote_path
168
+ end
169
+ end
170
+
171
+ # ===功能
172
+ # scp文件从机器a到机器b
173
+ # 如果远程多层子目录不存在则自动的进行创建
174
+ # ===example
175
+ # Util.scpfromto hosta, remotea, hostb, remoteb, usera, passa, userb, passb
176
+ def Util.scpfromto host_a, remote_path_a, host_b, remote_path_b, user_a, password_a, user_b, password_b
177
+ sfix = Util.suffix
178
+ local_path="/tmp/localfile.#{sfix}"
179
+ Net::SCP.start(host_a, user_a, :password=>password_a) do | scp |
180
+ scp.download! remote_path_a, local_path do |ch, name, sent, total|
181
+ print "."
182
+ end
183
+ puts ""
184
+ end
185
+ if(remote_path_b=~/(\/.*)\/[^\/]*/)
186
+ puts "Make sure dir exist #{$1}"
187
+ `mkdir -p #{$1}`
188
+ end
189
+ Net::SCP.start(host_b, user_b, :password=>password_b) do | scp |
190
+ scp.upload! local_path, remote_path_b do |ch, name, sent, total|
191
+ print "."
192
+ end
193
+ puts ""
194
+ end
195
+ File.delete(local_path)
196
+ end
197
+
198
+ # ===功能
199
+ # scp文件夹从机器a到机器b
200
+ # 如果远程多层子目录不存在则自动的进行创建
201
+ # ===example
202
+ # Util.scpfromtod hosta, remotea, hostb, remoteb, usera, passa, userb, passb
203
+ def Util.scpfromtod host_a, remote_path_a, host_b, remote_path_b, user_a, password_a, user_b, password_b
204
+ sfix = Util.suffix
205
+ dirname = `basename #{remote_path_b}`.chop!
206
+ local_path="/tmp/localfile.#{sfix}"
207
+ Net::SCP.start(host_a, user_a, :password=>password_a) do | scp |
208
+ scp.download! remote_path_a, local_path, :recursive=>"yes" do |ch, name, sent, total|
209
+ print "."
210
+ end
211
+ puts ""
212
+ end
213
+ if(remote_path_b=~/(\/.*)\/[^\/]*/)
214
+ puts "Make sure dir exist #{$1}"
215
+ `mkdir -p #{$1}`
216
+ end
217
+ Net::SCP.start(host_b, user_b, :password=>password_b) do | scp |
218
+ scp.upload! local_path+"/#{dirname}", remote_path_b, :recursive=>"yes" do |ch, name, sent, total|
219
+ print "."
220
+ end
221
+ puts ""
222
+ end
223
+ #Dir.rmdir(local_path)
224
+ `rm -fr #{local_path}`
225
+ end
226
+
227
+ # ===功能
228
+ # 获取文件,操作文件,放回文件
229
+ # ===example
230
+ # Util.change_in_local host, remotepath
231
+ def Util.change_in_local host, remote_path, user=USERNAME, password=PASSWORD
232
+ sfix = Util.suffix
233
+ local_path="/tmp/localfile.#{sfix}"
234
+ Util.download host, remote_path, local_path, user, password
235
+ yield local_path
236
+ Util.upload host, local_path, remote_path, user, password
237
+ File.delete(local_path)
238
+ end
239
+
240
+ # ===功能
241
+ # 获取全局唯一的后缀
242
+ # ===example
243
+ # su = Util.suffix
244
+ def Util.suffix
245
+ return "#{Time.now.tv_sec}#{Time.now.tv_usec}"
246
+ end
247
+ end
248
+
249
+
250
+ if __FILE__ == $0 then
251
+ require 'test/unit'
252
+ class UtilTest < Test::Unit::TestCase
253
+ def no_test_ssh_cmd!
254
+ puts Util.exe_ssh_cmd! '10.241.14.56', 'ls'
255
+ puts Util.exe_ssh_cmd! '10.241.14.56', 'cd app-test/search/space/module-test/lib/data/ && ruby data_helper.rb file_exist? data_helper.rb'
256
+ end
257
+
258
+ def no_test_get_lib_path
259
+ puts Util.get_lib_path '10.241.14.56'
260
+ end
261
+
262
+ def test_get_local_ip
263
+ puts Util.get_local_ip
264
+ end
265
+ end
266
+ end