Module: OatsAgent

Defined in:
lib/oats_agent.rb,
lib/oats_agent/ragent.rb,
lib/oats_agent/rclient.rb,
lib/oats_agent/version.rb,
lib/oats_agent/commandline_options.rb

Defined Under Namespace

Modules: CommandlineOptions Classes: Ragent, Rclient

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.fkill(proc) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oats_agent.rb', line 15

def fkill(proc)
  if proc.instance_of? Hash
    pname = proc['cmd']
    pid = proc['pid']
    msg = " PID #{pid}: #{pname}"
  else
    pid = proc
  end
  $log.warn "Killing" + msg
  if RUBY_PLATFORM =~ /(mswin|mingw)/
    killed = Process.kill('KILL',pid.to_i)
    killed = 0 if RUBY_VERSION =~ /^1.9/ and killed.empty?
    $log.warn "Failed to kill" + msg if killed == 0
  else
    out = `kill -9 #{pid} 2>&1`
    $log.warn out unless out == ''
  end
end

.kill_matching(cmd_string) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oats_agent.rb', line 34

def kill_matching(cmd_string)
  procs = []
  if RUBY_PLATFORM =~ /(mswin|mingw)/
    WIN32OLE.connect("winmgmts://").ExecQuery("select * from win32_process").each do |process|
      #          puts [process.Commandline, process.ProcessId, process.name].inspect
      procs.push 'pid' => process.ProcessId, 'cmd' => process.CommandLine if process.Commandline =~ /#{cmd_string}/
    end
    # Looking for port is too slow on windows
    #        lines = "netstat -#{RUBY_PLATFORM =~ /(mswin|mingw)/ ?  'a' : 'n' } -o"
    #        list = `#{lines}`.split(/\n/)
    #        list.each do |p|
    #          p =~ /:(\d+)\s+.*\s+LISTENING\s+(\d+)/
    #          next unless $1 and $1 == port
    #          procs.push({ 'pid' => $2, 'cmd' => 'LISTENING' }) 
    #        end
  else
    ps_cmd = "ps -ew -o pid=,ppid=,command="
    ps_list = `#{ps_cmd}`
    list = ps_list.split(/\n/)
    list.each do |p|
      next unless p =~ /#{cmd_string}/
      p =~ /(\d+)\s+(\d+)\s+(.*)/
      procs.push({ 'pid' => $1, 'ppid' => $2, 'cmd' => $3 }) 
    end
  end
  procs.each { |p| fkill(p) }
end

.spawn(options) ⇒ Object

Initiates process to run agent in the background

Options Hash Keys:
  required: agent_nickname, agent_port, 
  optional: oats_user, test_directory, repository_version


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/oats_agent.rb', line 67

def spawn(options)
  nick = options["nickname"] || ENV['HOSTNAME']
  port = options["port"] || 3010
  port = port.to_s
  user = options["user"]
  repo_version = options["repository_version"]
  dir_tests = options["test_directory"] || ENV['OATS_TESTS']

  archive_dir = File.expand_path "results_archive", ENV['HOME']
  log_dir = "#{archive_dir}/#{nick}/agent_logs"
  log_file = "#{log_dir}/agent_#{Time.new.to_i}.log"
  config_file = "#{log_dir}/config-agent.txt"
  agent_log_file = "#{log_dir}/agent.log"
  params =  "-n #{nick} -p #{port}"
  
  agent_params = params.dup
  agent_params +=  "-r #{repo_version}" if repo_version
  agent_params +=  "-u #{user}" if user
  if options["agent_host"] and options["agent_host"] != ENV['HOSTNAME']
    if RUBY_PLATFORM =~ /(mswin|mingw)/
      cmd = "psexec.exe -d -i -n 10 -w " + archive_dir +
        ' -u qa -p ' + 'passwd' + ' \\\\' + options["agent_host"] +
        ' ruby oats_agent ' + agent_params.join(' ')
    else
      #  options['agent_host'] = ENV['HOSTNAME']
      cmd = "ssh " + options["agent_host"] + ' oats_agent ' + agent_params
    end
    $log.info "Issuing remote host request: #{cmd}"
    out = `#{cmd}`
    $log.info out unless out == ''
    return
  end
  
  
  FileUtils.mkdir_p(log_dir) unless File.exists?(log_dir)
  ENV['OATS_AGENT_LOGFILE'] = log_file

  ruby_cmd = File.expand_path('../oats_agent/start.rb', __FILE__) + ' ' + params
  
  # Need these off when called by OCC, otherwise the OCC values are inherited
  %w(RUBYOPT BUNDLE_BIN_PATH BUNDLE_GEMFILE).each { |e| ENV[e] = nil }
  kill_matching ruby_cmd
  return if options["kill_agent"]

  if File.directory?(dir_tests + '/.svn') and ENV['OATS_TESTS_SVN_REPOSITORY']
    svn_out =nil
    $log.info "Requested OATS Version: #{repo_version}" if repo_version
    code_version = nil
   
    3.times do
      code_version = `svn info #{dir_tests} | sed -n 's/Last Changed Rev: *//p'`
      code_version = nil if code_version == ''
      break if code_version.nil? or (repo_version and  code_version >= repo_version)
      cmd = "svn update #{dir_tests} 2>&1"
      svn_out = `#{cmd}`
      svn_out.chomp!
      $log.info svn_out
      case svn_out 
      when/^At revision/
        code_version = svn_out.sub(/At revision *(\d+).*/,'\1')
      when/Cleanup/
      when/Could not resolve hostname/
        break
      end
      if code_version == ''
        code_version = nil 
        sleep 3
      else
        break
      end
    end
    if svn_out.nil? and ENV['OATS_TESTS_SVN_REPOSITORY']
      $log.error "Could not update the code version " +( code_version || '') + ( repo_version ? "to #{repo_version}" : '')
      exit 2
    end

  elsif ENV["$OATS_TESTS_GIT_REPOSITORY"]
    dir_tests = archive_dir + ENV['OATS_AGENT_NICKNAME'] + '/oats_tests'
    `git clone git_rep dir_tests if File.directory?(dir_tests)`
    Dir.chdir dir_tests
    origin = ENV["$OATS_TESTS_GIT_REPOSITORY"] || 'origin'

    if repo_version
      2.times do
        # may detach HEAD, but it is OK
        out = `git checkout #{repo_version} 2>&1`
        break if status == 0
        if out == "fatal: reference is not a tree: #{repo_version}"
          $log.info "Need to pull requested version: #{repo_version} "
        else
          $log.info "$out"
        end
        $log.info `git pull #{origin} master` # fast-forward master from origin
      end
    else
      $log.info `git pull #{origin} master` # fast-forward master from origin
    end
    code_version = `git rev-list HEAD -1` # last commit in checked out version
    if code_version =~ /#{repo_version}/
      $log.info "Could not update the code version #{code_version} to #{repo_version}" 
      exit 2
    end
    $log.info "Using OATS code version: #{code_version}" unless repo_version
  else
    code_version = repo_version
    $log.info "Setting OATS code version to the requested version: #{code_version}" if code_version 
  end if dir_tests
  ENV['OATS_TESTS_CODE_VERSION'] = code_version

  msg = ''
  msg += "User: #{user} " if user
  msg += "Repo version: #{repo_version} " if repo_version
  $log.info "#{msg}Starting: #{ruby_cmd}"
  if RUBY_PLATFORM =~ /(mswin|mingw)/
    archiv = ENV['HOME'] + '/results_archive'
    cmd = "psexec.exe -d -i -n 10 -w #{archiv} ruby #{ruby_cmd} 2>&1"
  else
    cmd = "#{ruby_cmd} >/dev/null 2>&1 &" 
  end
  out = `#{cmd}`
  $log.info out unless out == ''
  File.open(config_file, 'w') {|f| f.puts(nick +' '+ port) }

  10.times do
    if File.exist? log_file
      FileUtils.rm_f agent_log_file
      FileUtils.ln log_file, agent_log_file
      break
    end
    sleep 1
  end

end