Salome HOME
Merge branch 'V7_main'
[modules/kernel.git] / bin / appliskel / kill_remote_containers.py
1 #!/usr/bin/env python
2 #  -*- coding: utf-8 -*-
3 # Copyright (C) 2007-2014  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
30 import getAppliPath
31 appli_local=os.path.realpath(os.path.dirname(__file__))
32 APPLI=getAppliPath.relpath(appli_local,os.path.realpath(os.getenv('HOME')))
33
34 usage="""usage: %prog [options]
35
36 This procedure kill all containers that have been launched in a SALOME session on remote machines.
37 A SALOME session is identified by a machine name and a port number.
38
39 You need to have a well installed SALOME application with a CatalogResources.xml file.
40 This file is used (parsed) to collect all remote resources.
41 Remote resources are all the resources on remote machines not including the main local SALOME application.
42 """
43
44 try:
45   # cElementTree from Python 2.5+
46   import xml.etree.cElementTree as etree_
47 except ImportError:
48   try:
49     import xml.etree.ElementTree as etree_
50   except ImportError:
51     try:
52       import cElementTree as etree_
53     except ImportError:
54       try:
55         # normal ElementTree install
56         import elementtree.ElementTree as etree_
57       except ImportError:
58         raise
59
60 class ParseError(Exception):
61   pass
62
63 catalog_file=os.path.join(appli_local,"CatalogResources.xml")
64
65 SEP=":"
66 if sys.platform == "win32":SEP=";"
67
68 def get_hostname():
69   return socket.gethostname().split('.')[0]
70
71 class Resource:
72   """Define a SALOME resource
73      - components : the list of available components of the resource
74   """
75   def __init__(self,node):
76     self.node=node
77     self.components=[]
78     self.resource_dir=None
79
80   def get_rcp(self):
81     protocol= self.node.get("protocol")
82     if protocol and protocol[0]=='s':return "scp"
83     else:return "rcp"
84
85   def get_rsh(self):
86     protocol= self.node.get("protocol")
87     if protocol and protocol[0]=='s':return "ssh"
88     else:return "rsh"
89
90   def get_user(self):
91     userName= self.node.get("userName")
92     if not userName:
93       userName=os.getenv('USER')
94     return userName
95
96   def get_host(self):
97     hostname= self.node.get("hostname")
98     return hostname
99
100   def get_name(self):
101     name= self.node.get("name")
102     if name:return name
103     return self.get_host()
104
105   def get_appliPath(self):
106     appliPath= self.node.get("appliPath")
107     if appliPath is None:
108       appliPath=APPLI
109     return appliPath
110
111 def main():
112   parser = optparse.OptionParser(usage=usage)
113   parser.add_option('-p','--port', dest="port", 
114                             help="The SALOME session port (default NSPORT or 2810)")
115
116
117   options, args = parser.parse_args()
118
119   if not os.path.exists(catalog_file):
120     print "ERROR: the catalog file %s is mandatory" % catalog_file_base
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 options.port:
145       port=options.port
146     else:
147       port=os.getenv("NSPORT") or "2810"
148     command=command+ " " + get_hostname() + " " + port +" killSalomeWithPort.py " + port
149     print command
150     os.system(command)
151
152 if __name__ == '__main__':
153   main()
154