Salome HOME
Updated copyright comment
[modules/kernel.git] / bin / appliskel / kill_remote_containers.py
1 #!/usr/bin/env python3
2 #  -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 """
26 """
27 import sys,os,shutil,glob,socket
28 import argparse
29 from salome_utils import getUserName
30
31 import getAppliPath
32 appli_local=os.path.realpath(os.path.dirname(__file__))
33 APPLI=getAppliPath.relpath(appli_local,os.path.realpath(os.path.expanduser("~")))
34
35 usage="""%(prog)s [options]
36
37 This procedure kill all containers that have been launched in a SALOME session on remote machines.
38 A SALOME session is identified by a machine name and a port number.
39
40 You need to have a well installed SALOME application with a CatalogResources.xml file.
41 This file is used (parsed) to collect all remote resources.
42 Remote resources are all the resources on remote machines not including the main local SALOME application.
43 """
44
45 try:
46   # cElementTree from Python 2.5+
47   import xml.etree.cElementTree as etree_
48 except ImportError:
49   try:
50     import xml.etree.ElementTree as etree_
51   except ImportError:
52     try:
53       import cElementTree as etree_
54     except ImportError:
55       try:
56         # normal ElementTree install
57         import elementtree.ElementTree as etree_
58       except ImportError:
59         raise
60
61 class ParseError(Exception):
62   pass
63
64 catalog_file=os.path.join(appli_local,"CatalogResources.xml")
65
66 SEP=":"
67 if sys.platform == "win32":SEP=";"
68
69 def get_hostname():
70   return socket.gethostname().split('.')[0]
71
72 class Resource:
73   """Define a SALOME resource
74      - components : the list of available components of the resource
75   """
76   def __init__(self,node):
77     self.node=node
78     self.components=[]
79     self.resource_dir=None
80
81   def get_rcp(self):
82     protocol= self.node.get("protocol")
83     if protocol and protocol[0]=='s':return "scp"
84     else:return "rcp"
85
86   def get_rsh(self):
87     protocol= self.node.get("protocol")
88     if protocol and protocol[0]=='s':return "ssh"
89     else:return "rsh"
90
91   def get_user(self):
92     userName= self.node.get("userName")
93     if not userName:
94       userName=getUserName()
95     return userName
96
97   def get_host(self):
98     hostname= self.node.get("hostname")
99     return hostname
100
101   def get_name(self):
102     name= self.node.get("name")
103     if name:return name
104     return self.get_host()
105
106   def get_appliPath(self):
107     appliPath= self.node.get("appliPath")
108     if appliPath is None:
109       appliPath=APPLI
110     return appliPath
111
112 def main():
113   parser = argparse.ArgumentParser(usage=usage)
114   parser.add_argument('-p','--port', dest="port",
115                       help="The SALOME session port (default NSPORT or 2810)")
116
117   args = parser.parse_args()
118
119   if not os.path.exists(catalog_file):
120     print("ERROR: the catalog file %s is mandatory" % catalog_file)
121     sys.exit(1)
122
123   #Parse CatalogResource.xml
124   doc = etree_.parse(catalog_file)
125
126   rootNode = doc.getroot()
127   if rootNode.tag != "resources":
128     raise  ParseError("First level tag must be resources not %s" % rootNode.tag)
129
130   resources=[]
131
132   #Extract resources
133   for child in rootNode:
134     if child.tag != "machine":
135       raise  ParseError("Second level tag must be machine not %s" % child.tag)
136     resources.append(Resource(child))
137
138   local_names=("localhost",get_hostname())
139
140   for resource in resources:
141     if resource.get_host() in local_names:continue
142     command=resource.get_rsh() +" -l "+resource.get_user()+" "+resource.get_host()
143     command=command+ " " + os.path.join(resource.get_appliPath(),"runRemote.sh")
144     if args.port:
145       port=args.port
146     else:
147       port=os.getenv("NSPORT") or "2810"
148     command=command+ " " + get_hostname() + " " + port +" killSalomeWithPort.py " + port
149     print(command)
150     return subprocess.call(command, shell=True)
151
152
153 if __name__ == '__main__':
154   main()
155