-
Notifications
You must be signed in to change notification settings - Fork 1
/
runsrv
241 lines (198 loc) · 6.03 KB
/
runsrv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
61
62
63
64
65
66
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env ruby
require "bundler/setup"
require_relative '../test/test_helper/platform_require'
require_relative '../test/test_helper/helper'
require 'net/ping/tcp'
module AssMaintainer::InfoBaseTest
module Tmp
SRVINFO = File.join Dir.tmpdir, "ass_maintainer_infobase_test.srvinfo"
end
module Server
PORT = '11540'
REGPORT = '11541'
RANGE = '11560:11591'
RAGENT_ADMIN = 'ragent'
RAGENT_PASS = '0000'
RMNGR_ADMIN = 'rmngr'
RMNGR_PASS = '1111'
class Ragent
BIN = 'ragent.exe'
attr_reader :platform_require, :port, :regport, :range
def initialize(platform_require, port = nil, regport = nil, range = nil)
@platform_require = platform_require
@port = port || PORT
@regport = regport || REGPORT
@range = range || RANGE
end
def thick
@thick ||= Helper.thicks(platform_require).last
end
def exists?
path && File.file?(path)
end
def path
return unless thick
@path ||= thick.path.class.new("#{thick.path.dirname}/#{BIN}")
end
def version
return unless exists?
thick.version
end
def srvinfo
return unless exists?
thick.path.class.new(@srvinfo).realpath.to_s
end
def args
['-port', port,
'-regport', regport,
'-range', range,
'-agent',
'-d', srvinfo]
end
def cmd
path.to_s
end
def running?
tcp_ping.ping?
end
def tcp_ping
@tcp_ping ||= Net::Ping::TCP.new(host, port)
end
def host
'localhost'
end
def run(srvinfo, make_admins_)
@srvinfo = srvinfo
fail "service on the tcp port `#{port}' is already running" if running?
fail "`#{BIN}' v#{version} isn't instaled!" unless exists?
Process.spawn "\"#{cmd}\" #{args.join(' ')}"
sleep(3)
make_admins if make_admins_
end
def runtime_new
Module.new do
is_ole_runtime :agent
end
end
def run_ole_runtime
require 'ass_ole'
self.class.like_ole_runtime runtime_new
ole_runtime_get.run "localhost:#{port}", platform_require
end
def make_admins
run_ole_runtime
make_rmngr_admin
make_ragent_admin
end
def make_ragent_admin
authenticateAgent('', '')
regAgentAdmin ragent_admin_get
end
def ragent_admin_get
ai = createClusterAdminInfo
ai.Name = RAGENT_ADMIN
ai.Password = RAGENT_PASS
ai.PasswordAuthAllowed = true
ai
end
def make_rmngr_admin
cl = cluster_get
Authenticate(cl, '', '')
regClusterAdmin(cl, cluster_admin_get)
end
def cluster_get
getClusters[0]
end
def cluster_admin_get
ai = createClusterAdminInfo
ai.Name = RMNGR_ADMIN
ai.Password = RMNGR_PASS
ai.PasswordAuthAllowed = true
ai
end
end
require 'clamp'
class CMD < Clamp::Command
def prepare_srv_info
FileUtils.rm_r Tmp::SRVINFO if File.exist? Tmp::SRVINFO
FileUtils.mkdir_p Tmp::SRVINFO
Tmp::SRVINFO
end
def exists?
ragent.exists?
end
def running?
ragent.running?
end
def ragent
@ragent ||= Ragent
.new(PLATFORM_REQUIRE, port, regport, range)
end
alias_method :ra, :ragent
def srvinfo_get
return prepare_srv_info if srvinfo == Tmp::SRVINFO
srvinfo || prepare_srv_info
end
def errno_enotempty_mess
"rm -r #{srvinfo}: Device or resource busy\n"\
"Cause of this: \n"\
" 1) other ragent.exe is running and locking srvinfo directory\n"\
" 2) other rmngr.exe is running and locking srvinfo directory\n"\
"Kill suitable ragent.exe and rmngr.exe processes and try again:\n"\
" #{netstat.join("\n ")}"
end
def netstat
`netstat -abn -p tcp`.force_encoding('ASCII-8BIT')
.split("\r\n"*2)
.map {|i| i.gsub(/\s+/, ' ')}
.select {|i| i =~ %r{LISTENING.+r(agent|mngr)\.exe}}
.sort_by {|i| i.split(" ").last}
end
def rmngr_host
`hostname`.strip
end
def ragent_usr
"#{Server::RAGENT_ADMIN}:#{Server::RAGENT_PASS}@" if make_admins?
end
def rmngr_usr
"#{Server::RMNGR_ADMIN}:#{Server::RMNGR_PASS}@" if make_admins?
end
def success_mess
"for pass ESRV_ENV to test execute:\n"\
"export ESRV_ENV=\"--ragent #{ragent_usr}#{ra.host}:#{ra.port} \\\n"\
" --rmngr #{rmngr_usr}#{rmngr_host}:#{ra.regport} \\\n"\
" --dbms YOUR_DBMS \\\n"\
" --dbsrv user:pass@your_dbms_host:port\""
end
def execute
signal_usage_error 'Run in cygwin or windows only!' if Helper.linux?
ragent.run(srvinfo_get, make_admins?)
puts success_mess
rescue RuntimeError => e
raise Clamp::ExecutionError.new(e.message, '', 1)
rescue Errno::ENOTEMPTY => e
raise Clamp::ExecutionError.new(errno_enotempty_mess, '', 1)
end
option '--port', 'PORT', 'ragent.exe tcp port', default: PORT do |s|
Integer(s)
end
option '--regport', 'PORT', 'rmngr.exe tcp port', default: REGPORT do |s|
Integer(s)
end
option '--range', 'FROM:TO', 'rphost.exe tcp ports range', default: RANGE do |s|
from, to = s.split(':')
"#{Integer(from)}:#{Integer(to)}"
end
parameter '[SRVINFO]', 'path to ragent.exe srvinfo directory', default: Tmp::SRVINFO do |s|
signal_usage_error "Path `#{s}' not found or isn't directory!" unless File.directory? s
s
end
option '--plist', :flag, 'show netstat for rphost.exe and rmngr.exe and exit' do
$stderr.puts netstat.join("\n")
exit 0
end
option '--[no-]make-admins', :flag, 'default admins for ragent.exe and rmngr.exe will be maked'
end
end
end
AssMaintainer::InfoBaseTest::Server::CMD.run