Salome HOME
[PYTHON 3] 1st draft
[modules/kernel.git] / bin / appliskel / kill_remote_containers.py
1 #!/usr/bin/env python
2 #  -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2016  CEA/DEN, EDF R&D, 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 optparse
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.getenv('HOME')))
34
35 usage="""usage: %prog [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 = optparse.OptionParser(usage=usage)
114   parser.add_option('-p','--port', dest="port",
115                             help="The SALOME session port (default NSPORT or 2810)")
116
117
118   options, args = parser.parse_args()
119
120   if not os.path.exists(catalog_file):
121     print("ERROR: the catalog file %s is mandatory" % catalog_file_base)
122     sys.exit(1)
123
124   #Parse CatalogResource.xml
125   doc = etree_.parse(catalog_file)
126
127   rootNode = doc.getroot()
128   if rootNode.tag != "resources":
129     raise  ParseError("First level tag must be resources not %s" % rootNode.tag)
130
131   resources=[]
132
133   #Extract resources
134   for child in rootNode:
135     if child.tag != "machine":
136       raise  ParseError("Second level tag must be machine not %s" % child.tag)
137     resources.append(Resource(child))
138
139   local_names=("localhost",get_hostname())
140
141   for resource in resources:
142     if resource.get_host() in local_names:continue
143     command=resource.get_rsh() +" -l "+resource.get_user()+" "+resource.get_host()
144     command=command+ " " + os.path.join(resource.get_appliPath(),"runRemote.sh")
145     if options.port:
146       port=options.port
147     else:
148       port=os.getenv("NSPORT") or "2810"
149     command=command+ " " + get_hostname() + " " + port +" killSalomeWithPort.py " + port
150     print(command)
151     os.system(command)
152
153 if __name__ == '__main__':
154   main()
155