Module: Env

Included in:
EnvTest, MCBasePatternHelper, Stub
Defined in:
lib/common/socket/env.rb

Overview

This class supply enviroment related local/remote operation e.g. modify configration, operate process…

Author

chenjie

Data

2009-4-30

Instance Method Summary collapse

Instance Method Details

#bak_conf(host = HOST, user = USERNAME, password = PASSWORD, conf_path = CONFPATH, context_key) ⇒ Object

bak_conf “conf1”



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/common/socket/env.rb', line 116

def bak_conf host=HOST, user=USERNAME, password=PASSWORD, conf_path=CONFPATH, context_key
  conf_dir=`dirname #{conf_path}`
  conf_name=`basename #{conf_path}`
  conf_dir.chop!
  conf_name.chop!
       now = Time.now.to_datetime
       bak_cmd = "cd #{conf_dir} && cp #{conf_name} #{conf_name}.#{now}"
       if Util.is_localhost? host
          system "#{bak_cmd}"
       else
    Util.exe_ssh_cmd! host, bak_cmd, user, password
       end
       Context.set "#{context_key}", "#{conf_name}.#{now}"
       $log.debug "bak conf which context key is #{context_key}"
end

#copy_conf(host = HOST, user = USERNAME, password = PASSWORD, source_conf, target_conf) ⇒ Object

copy_conf “/home/space/space/conf/conf1”, “/home/space/space/conf/conf2”



167
168
169
170
171
172
173
174
175
# File 'lib/common/socket/env.rb', line 167

def copy_conf host=HOST, user=USERNAME, password=PASSWORD, source_conf, target_conf
       copy_cmd = "cp #{source_conf} #{target_conf}"
       if Util.is_localhost?(host) then
            system "#{copy_cmd}"
       else
    Util.exe_ssh_cmd! host, copy_cmd, user, password
       end
       $log.debug "Copy conf from #{source_conf} to #{target_conf}."
end

#get_conf(host = HOST, user = USERNAME, password = PASSWORD, conf_path = CONFPATH, key) ⇒ Object

功能

获取配置文件的value

参数

  • host

  • conf_path

  • key

return if key existed, return conf value string; if not, return empty string “”

Example:

get_conf “name”

检查的方法:

assert_body pattern



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/common/socket/env.rb', line 85

def get_conf host=HOST, user=USERNAME, password=PASSWORD, conf_path=CONFPATH, key
  if Util.is_localhost? host
    cm = ConfModifier.new conf_path
    ret = cm.get_conf key
    Context.set("IOBODY", ret)
    return ret
        else
      #lib_path = Util.get_lib_path host
          #cmd_str = "cd #{lib_path} && ruby conf_modifier.rb get_conf #{conf_path} #{key}"
    #value = Util.exe_ssh_cmd! host, cmd_str, user, password
    #Context.set("IOBODY", value)
    #return value
    Util.change_in_local(host, conf_path, user, password) { | local_path |
      cm = ConfModifier.new local_path
      ret = cm.get_conf key
      Context.set("IOBODY", ret)
    }
    return ret
        end
end

#get_thread_num(host, module_name, user = USERNAME, password = PASSWORD) ⇒ Object

功能

获取模块进程线程数

参数

  • host

  • module_name

return thead number or 0(process not running)

example

get_thread_num host, “friend”



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/common/socket/env.rb', line 259

def get_thread_num host, module_name, user=USERNAME, password=PASSWORD
  if Util.is_localhost? host
  str = `pstree | grep #{module_name}`
               mdata = str.scan /.*---(\d+).*/
               if mdata.size > 0 then
                       return mdata[0][0].to_i
               else
                       return 0
               end
  else
        cmd_str = "pstree | grep #{module_name}"
  str = Util.exe_ssh_cmd! host, cmd_str, user, password
        mdata = str.scan /.*---(\d+).*/
        if mdata.size > 0 then
            return mdata[0][0].to_i
        else
            return 0
        end   
  end
end

#is_process_running?(host, module_name, user = USERNAME, password = PASSWORD) ⇒ Boolean

example

is_process_running? host, “friend”

Returns:

  • (Boolean)


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/common/socket/env.rb', line 232

def is_process_running? host, module_name, user=USERNAME, password=PASSWORD
    if Util.is_localhost?(host) then
value = `pstree | grep #{module_name} | wc -l`
if value.to_i > 0 then
                             return true
                     else
                             return false
                     end
    else
cmd_str = "pstree | grep #{module_name} | wc -l"
value = Util.exe_ssh_cmd! host, cmd_str, user, password
if value.to_i > 0 then
        return true
    else
        return false
  end
    end
end

#kill_process(host, module_name, user = USERNAME, password = PASSWORD) ⇒ Object

example

kill_process host, “friend”



217
218
219
220
221
222
223
224
225
# File 'lib/common/socket/env.rb', line 217

def kill_process host, module_name, user=USERNAME, password=PASSWORD
  if Util.is_localhost?(host) then
`killall -9 #{module_name}`
    else
cmd_str = "killall -9 #{module_name}"
Util.exe_ssh_cmd! host, cmd_str, user, password
    end
    sleep 1
end

#modify_conf(host = HOST, user = USERNAME, password = PASSWORD, conf_path = CONFPATH, hash) ⇒ Object

功能

修改配置文件

参数

  • host

  • conf_path

  • hash 欲修改的配置hash

example

hh = “path”=>newpath modify_conf hh



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/common/socket/env.rb', line 59

def modify_conf host=HOST, user=USERNAME, password=PASSWORD, conf_path=CONFPATH, hash
  if Util.is_localhost? host
      cm = ConfModifier.new conf_path
        cm.modifyall hash
        cm.writeconf
        else
          hash.each{|k,v|
             Env.modify_conf_single host, user, password, conf_path, k, v
          }
        end
end

#modify_conf_single(host = HOST, user = USERNAME, password = PASSWORD, conf_path = CONFPATH, key, new_value) ⇒ Object

功能

修改配置文件

参数

  • host host ip(example: 127.0.0.1 stands for localhost)

  • conf_path 配置文件路径

  • key 欲修改的key

  • new_value 新的value值

example

modify_conf_single “name”, newname



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/common/socket/env.rb', line 33

def modify_conf_single host=HOST, user=USERNAME, password=PASSWORD, conf_path=CONFPATH, key, new_value
  if Util.is_localhost? host
        cm = ConfModifier.new conf_path
        cm.modify key, new_value
        cm.writeconf
  else
        #lib_path = Util.get_lib_path host
          #cmd_str = "cd #{lib_path} && ruby conf_modifier.rb modify_conf_single #{conf_path} #{key} #{new_value.to_s}"
    #Util.exe_ssh_cmd! host, cmd_str, user, password
    Util.change_in_local(host, conf_path, user, password) { | local_path |
      cm = ConfModifier.new local_path
      cm.modify key, new_value
      cm.writeconf
    }
        end
end

#recover_conf(host = HOST, user = USERNAME, password = PASSWORD, conf_path = CONFPATH, context_key) ⇒ Object

recover_conf “conf1”



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/common/socket/env.rb', line 142

def recover_conf host=HOST, user=USERNAME, password=PASSWORD, conf_path=CONFPATH, context_key
  conf_dir=`dirname #{conf_path}`
  conf_name=`basename #{conf_path}`
  conf_dir.chop!
  conf_name.chop!
       bak_conf = Context.get "#{context_key}"
       recover_cmd = "cd #{conf_dir} && mv #{bak_conf} #{conf_name}"
       if Util.is_localhost?(host) then
            system "#{recover_cmd}"
       else
    Util.exe_ssh_cmd! host, recover_cmd, user, password
       end
       Context.delete "#{context_key}"
       $log.debug "Recover conf which context key is #{context_key}."
end

#start_process(host, module_name, super_path = "/home/space/space/", child_path = "./bin/", user = USERNAME, password = PASSWORD) ⇒ Object

功能

启动模块进程, 运用脚本

参数

  • host

  • module_name module name, e.g.: appui

  • script_path default “/home/space/space”

  • script_name default “loadspace.sh”

return true/false

Example

start_process '10.241.14.56', 'mlstar', '/home/space/mlstar/'


194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/common/socket/env.rb', line 194

def start_process host, module_name, super_path="/home/space/space/", child_path="./bin/", user=USERNAME, password=PASSWORD

    if Util.is_localhost? host
    `killall -9 #{module_name}`
    `cd #{super_path}; nohup #{child_path}#{module_name} >/dev/null 2>&1 &`
    else
    cmd_str = "killall -9 #{module_name};cd #{super_path}; nohup #{child_path}#{module_name} >/dev/null 2>&1 &"
    Util.exe_ssh_cmd! host, cmd_str, user, password
        end

  sleep 2
        value = is_process_running? host, module_name, user, password
        return value
end