Salome HOME
#20171 [CEA 20071] Handle missing netstat
[modules/kernel.git] / bin / nameserver.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23
24 import os, sys, re, socket
25 #import commands
26 from server import Server
27 from salome_utils import getHostName
28 from launchConfigureParser import verbose
29
30 # -----------------------------------------------------------------------------
31
32 class NamingServer(Server):
33     #XTERM = ""
34     #USER = os.getenv('USER')
35     #if USER is None:
36     #  USER = 'anonymous'
37     #os.system("mkdir -m 777 -p /tmp/logs")
38     #LOGDIR = "/tmp/logs/" + USER
39
40     def initNSArgs(self):
41         from salome_utils import getLogDir
42         upath = getLogDir()
43         try:
44             os.makedirs(upath, mode=0o777)
45         except:
46             pass
47
48         if verbose(): print("Name Service... ", end =' ')
49         hname = getHostName()
50
51         with open(os.environ["OMNIORB_CONFIG"]) as f:
52           ss = re.findall("NameService=corbaname::" + hname + ":\d+", f.read())
53           if verbose(): print("ss = ", ss, end=' ')
54         sl = ss[0]
55         ll = sl.split(':')
56         aPort = ll[-1]
57         #aPort=(ss.join().split(':'))[2];
58         #aPort=re.findall("\d+", ss[0])[0]
59
60         # \begin{E.A.}
61         # put the log files of omniNames in different directory with port reference,
62         # it is cleaner on linux and it is a fix for salome since it is impossible to
63         # remove the log files if the corresponding omniNames has not been killed.
64         # \end{E.A.}
65
66         upath = os.path.join(upath, "omniNames_%s"%(aPort))
67         try:
68           os.mkdir(upath)
69         except:
70           # print("Can't create " + upath)
71           pass
72
73         #os.system("touch " + upath + "/dummy")
74         for fname in os.listdir(upath):
75           try:
76             os.remove(upath + "/" + fname)
77           except:
78             pass
79         #os.system("rm -f " + upath + "/omninames* " + upath + "/dummy " + upath + "/*.log")
80
81         #aSedCommand="s/.*NameService=corbaname::" + hname + ":\([[:digit:]]*\)/\1/"
82         # print("sed command = ", aSedCommand)
83         #aPort = commands.getoutput("sed -e\"" + aSedCommand + "\"" + os.environ["OMNIORB_CONFIG"])
84         # print("port=", aPort)
85         if sys.platform == "win32":
86           # print("start omniNames -start " + aPort + " -logdir " + upath)
87           self.CMD = ['omniNames', '-start' , aPort , '-nohostname', '-logdir' , os.path.realpath(upath), '-errlog', os.path.realpath(os.path.join(upath,'omniNameErrors.log'))]
88           #os.system("start omniNames -start " + aPort + " -logdir " + upath)
89         else:
90           # get ip address on default interface (for instance eth0) to limit listening on this interface (cyber security request)
91           from subprocess import check_output
92           ips = check_output(['hostname', '--all-ip-addresses'])
93           if ips.strip():
94             ipDefault = ips.split()[0].decode()
95           else:
96             ip = check_output(['hostname', '--ip-address'])
97             ipDefault = ip.split()[-1].decode()
98           self.CMD = ['omniNames','-start' , aPort]
99           self.CMD += ['-logdir' , upath, '-errlog', upath+'/omniNameErrors.log']
100
101         if verbose(): print("... ok")
102         if verbose(): print("to list contexts and objects bound into the context with the specified name : showNS ")
103
104
105     def initArgs(self):
106         Server.initArgs(self)
107         if sys.platform == "win32":
108           env_ld_library_path = ['env', 'LD_LIBRARY_PATH=' + os.getenv("PATH")]
109         elif sys.platform == "darwin":
110           env_ld_library_path = ['env', 'DYLD_LIBRARY_PATH=' + os.getenv("DYLD_LIBRARY_PATH"), 'DYLD_FALLBACK_LIBRARY_PATH=' + os.getenv("DYLD_FALLBACK_LIBRARY_PATH")]
111         else:
112           env_ld_library_path = ['env', 'LD_LIBRARY_PATH=' + os.getenv("LD_LIBRARY_PATH")]
113         self.CMD = ['xterm', '-e']+ env_ld_library_path + ['python']
114         self.initNSArgs()
115
116 # In LifeCycleCORBA, FactoryServer is started with rsh on the requested
117 #    computer if this Container does not exist. Default is localhost.
118 #    Others Containers are started with start_impl method of FactoryServer Container.
119 # For using rsh it is necessary to have in the ${HOME} directory a .rhosts file
120 # Warning : on RedHat the file /etc/hosts contains by default a line like :
121 # 127.0.0.1               bordolex bordolex.paris1.matra-dtv.fr localhost.localdomain localhost
122 #   (bordolex is the station name). omniNames on bordolex will be accessible from other
123 #   computers only if the computer name is removed on that line like :
124 #   127.0.0.1               bordolex.paris1.matra-dtv.fr localhost.localdomain localhost
125
126 # To start dynamically Containers on several computers you need to
127 # put in the ${OMNIORB_CONFIG} file a computer name instead of "localhost"
128 # example : ORBInitRef NameService=corbaname::dm2s0017
129
130 # If you need to use several omniNames running on the same computer, you have to :
131 #1. put in your ${OMNIORB_CONFIG} file a computer name and port number
132 # example : ORBInitRef NameService=corbaname::dm2s0017:1515
133 #2. start omninames with this port number like in orbmodile.py
134 # example : omniNames -start 1515 -logdir ${BaseDir}/logs/${Username} &