#!/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