Class: Util

Inherits:
Object
  • Object
show all
Defined in:
lib/common/socket/util.rb

Overview

============================================================ 基础库辅助类

Author

chenjie

Data

2009-4-30

============================================================

Class Method Summary collapse

Class Method Details

.change_in_local(host, remote_path, user = USERNAME, password = PASSWORD) {|local_path| ... } ⇒ Object

===功能 获取文件,操作文件,放回文件 ===example Util.change_in_local host, remotepath

Yields:

  • (local_path)


231
232
233
234
235
236
237
238
# File 'lib/common/socket/util.rb', line 231

def Util.change_in_local host, remote_path, user=USERNAME, password=PASSWORD
	sfix = Util.suffix
	local_path="/tmp/localfile.#{sfix}"
	Util.download host, remote_path, local_path, user, password
	yield local_path
	Util.upload host, local_path, remote_path, user, password
	File.delete(local_path)
end

.download(host, remote_path, local_path, user = USERNAME, password = PASSWORD) ⇒ Object

===功能 scp远端文件到本机 ===example Util.download host, remote, local



155
156
157
158
159
# File 'lib/common/socket/util.rb', line 155

def Util.download host, remote_path, local_path, user=USERNAME, password=PASSWORD
	Net::SCP.start(host, user, :password=>password) do | scp |
		scp.download! remote_path, local_path
	end
end

.exe_ssh_cmd(host, cmd_str, user = USERNAME, password = PASSWORD) ⇒ Object

===功能 在远程机器上执行命令 封装Net::SSH::Connection::Session.exe "Note that this method returns immediately, and requires an event loop (see Session#loop) in order for the command to actually execute." ===example Util.exe_ssh_cmd host, "ls"



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/common/socket/util.rb', line 130

def Util.exe_ssh_cmd host, cmd_str, user=USERNAME, password=PASSWORD
 		ssh_cmd = "source ~/.bash_profile && #{cmd_str}"
	$log.debug "SSH CMD = #{ssh_cmd}"
		res = ''
		Net::SSH.start(host, user, :password=>password) do |session|
res = session.exec ssh_cmd
		end
		res = res.to_s.strip.chomp
		$log.debug "SSH CMD RES = #{res}"
		return res
end

.exe_ssh_cmd!(host, cmd_str, user = USERNAME, password = PASSWORD) ⇒ Object

===功能 在远程机器上执行命令 封装Net::SSH::Connection::Session.exe! Same as exec, except this will block until the command finishes ===example Util.exe_ssh_cmd! host, "killall -9 friend"



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/common/socket/util.rb', line 112

def Util.exe_ssh_cmd! host, cmd_str, user=USERNAME, password=PASSWORD
 		ssh_cmd = "source ~/.bash_profile && #{cmd_str}"
	$log.debug "SSH CMD = #{ssh_cmd}"
		res = ''
		Net::SSH.start(host, user, :password=>password) do |session|
res = session.exec! ssh_cmd
		end
		res = res.to_s.strip.chomp
		$log.debug "SSH CMD RES = #{res}"
		return res
end

.get_lib_path(host, user = USERNAME, password = PASSWORD) ⇒ Object

===功能 返回指定机器的lib路径, 执行staf命令相关

  • ip 目标机器ip, string return string, 目标机器上部署的lib路径 ===example Util.get_lib_path host


95
96
97
98
99
100
101
102
103
104
# File 'lib/common/socket/util.rb', line 95

def Util.get_lib_path host, user=USERNAME, password=PASSWORD
      if Util.is_localhost? host then 
    		return LIB_ROOT
  	else
    		cmd_str = "echo $HOME"
        	home = Util.exe_ssh_cmd! host, cmd_str, user, password
        	#return "#{home}/tools/app-test/search/space/module-test/lib/"
        	return "#{home}/tools/rlib/"
  	end
end

.get_local_ip ⇒ Object

===功能 返回本机IP(非127.0.0.1) ===example lip = Util.get_local_ip



146
147
148
149
# File 'lib/common/socket/util.rb', line 146

def Util.get_local_ip
	ip = `hostname -i`
	return ip
end

.is_localhost?(ip) ⇒ Boolean

===功能 判断传入ip是否是本机ip return: true/false ===example Util.is_localhost? "10.4.34.23"

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
# File 'lib/common/socket/util.rb', line 21

def Util.is_localhost? ip
	#get localhost ip
	local_ip = `hostname -i`.chomp

  	if ( (ip == nil) || (ip == local_ip) || (ip == "127.0.0.1") || (ip == "localhost") ) then
      		return true
	else
		return false
  	end
end

.scpfromto(host_a, remote_path_a, host_b, remote_path_b, user_a, password_a, user_b, password_b) ⇒ Object

===功能 scp文件从机器a到机器b 如果远程多层子目录不存在则自动的进行创建 ===example Util.scpfromto hosta, remotea, hostb, remoteb, usera, passa, userb, passb



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/common/socket/util.rb', line 176

def Util.scpfromto host_a, remote_path_a, host_b, remote_path_b, user_a, password_a, user_b, password_b
	sfix = Util.suffix
	local_path="/tmp/localfile.#{sfix}"
	Net::SCP.start(host_a, user_a, :password=>password_a) do | scp |
		scp.download! remote_path_a, local_path do |ch, name, sent, total|
			print "."
		end
		puts ""
	end
	if(remote_path_b=~/(\/.*)\/[^\/]*/)
		puts "Make sure dir exist #{$1}"
		`mkdir -p #{$1}`
	end
	Net::SCP.start(host_b, user_b, :password=>password_b) do | scp |
		scp.upload! local_path, remote_path_b do |ch, name, sent, total|
			print "."
		end
		puts ""
	end
	File.delete(local_path)
end

.scpfromtod(host_a, remote_path_a, host_b, remote_path_b, user_a, password_a, user_b, password_b) ⇒ Object

===功能 scp文件夹从机器a到机器b 如果远程多层子目录不存在则自动的进行创建 ===example Util.scpfromtod hosta, remotea, hostb, remoteb, usera, passa, userb, passb



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/common/socket/util.rb', line 203

def Util.scpfromtod host_a, remote_path_a, host_b, remote_path_b, user_a, password_a, user_b, password_b
	sfix = Util.suffix
	dirname = `basename #{remote_path_b}`.chop!
	local_path="/tmp/localfile.#{sfix}"
	Net::SCP.start(host_a, user_a, :password=>password_a) do | scp |
		scp.download! remote_path_a, local_path, :recursive=>"yes" do |ch, name, sent, total|
			print "."
		end
		puts ""
	end
	if(remote_path_b=~/(\/.*)\/[^\/]*/)
		puts "Make sure dir exist #{$1}"
		`mkdir -p #{$1}`
	end
	Net::SCP.start(host_b, user_b, :password=>password_b) do | scp |
		scp.upload! local_path+"/#{dirname}", remote_path_b, :recursive=>"yes" do |ch, name, sent, total|
			print "."
		end
		puts ""
	end
	#Dir.rmdir(local_path)
	`rm -fr #{local_path}`
end

.suffix ⇒ Object

===功能 获取全局唯一的后缀 ===example su = Util.suffix



244
245
246
# File 'lib/common/socket/util.rb', line 244

def Util.suffix
	return "#{Time.now.tv_sec}#{Time.now.tv_usec}"
end

.upload(host, local_path, remote_path, user = USERNAME, password = PASSWORD) ⇒ Object

===功能 scp本机文件到远端 ===example Util.upload host, local, remote



165
166
167
168
169
# File 'lib/common/socket/util.rb', line 165

def Util.upload host, local_path, remote_path, user=USERNAME, password=PASSWORD
	Net::SCP.start(host, user, :password=>password) do | scp |
		scp.upload! local_path, remote_path
	end
end