Back to index

salome-kernel  6.5.0
kill_remote_containers.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #  -*- coding: utf-8 -*-
00003 # Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00004 #
00005 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00006 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00007 #
00008 # This library is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU Lesser General Public
00010 # License as published by the Free Software Foundation; either
00011 # version 2.1 of the License.
00012 #
00013 # This library is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 # Lesser General Public License for more details.
00017 #
00018 # You should have received a copy of the GNU Lesser General Public
00019 # License along with this library; if not, write to the Free Software
00020 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00021 #
00022 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00023 #
00024 
00025 """
00026 """
00027 import sys,os,shutil,glob,socket
00028 import optparse
00029 
00030 import getAppliPath
00031 appli_local=os.path.realpath(os.path.dirname(__file__))
00032 APPLI=getAppliPath.relpath(appli_local,os.path.realpath(os.getenv('HOME')))
00033 
00034 usage="""usage: %prog [options]
00035 
00036 This procedure kill all containers that have been launched in a SALOME session on remote machines.
00037 A SALOME session is identified by a machine name and a port number.
00038 
00039 You need to have a well installed SALOME application with a CatalogResources.xml file.
00040 This file is used (parsed) to collect all remote resources.
00041 Remote resources are all the resources on remote machines not including the main local SALOME application.
00042 """
00043 
00044 try:
00045   # cElementTree from Python 2.5+
00046   import xml.etree.cElementTree as etree_
00047 except ImportError:
00048   try:
00049     import xml.etree.ElementTree as etree_
00050   except ImportError:
00051     try:
00052       import cElementTree as etree_
00053     except ImportError:
00054       try:
00055         # normal ElementTree install
00056         import elementtree.ElementTree as etree_
00057       except ImportError:
00058         raise
00059 
00060 class ParseError(Exception):
00061   pass
00062 
00063 catalog_file=os.path.join(appli_local,"CatalogResources.xml")
00064 
00065 SEP=":"
00066 if sys.platform == "win32":SEP=";"
00067 
00068 def get_hostname():
00069   return socket.gethostname().split('.')[0]
00070 
00071 class Resource:
00072   """Define a SALOME resource
00073      - components : the list of available components of the resource
00074   """
00075   def __init__(self,node):
00076     self.node=node
00077     self.components=[]
00078     self.resource_dir=None
00079 
00080   def get_rcp(self):
00081     protocol= self.node.get("protocol")
00082     if protocol and protocol[0]=='s':return "scp"
00083     else:return "rcp"
00084 
00085   def get_rsh(self):
00086     protocol= self.node.get("protocol")
00087     if protocol and protocol[0]=='s':return "ssh"
00088     else:return "rsh"
00089 
00090   def get_user(self):
00091     userName= self.node.get("userName")
00092     if not userName:
00093       userName=os.getenv('USER')
00094     return userName
00095 
00096   def get_host(self):
00097     hostname= self.node.get("hostname")
00098     return hostname
00099 
00100   def get_name(self):
00101     name= self.node.get("name")
00102     if name:return name
00103     return self.get_host()
00104 
00105   def get_appliPath(self):
00106     appliPath= self.node.get("appliPath")
00107     if appliPath is None:
00108       appliPath=APPLI
00109     return appliPath
00110 
00111 def main():
00112   parser = optparse.OptionParser(usage=usage)
00113   parser.add_option('-p','--port', dest="port", 
00114                             help="The SALOME session port (default NSPORT or 2810)")
00115 
00116 
00117   options, args = parser.parse_args()
00118 
00119   if not os.path.exists(catalog_file):
00120     print "ERROR: the catalog file %s is mandatory" % catalog_file_base
00121     sys.exit(1)
00122 
00123   #Parse CatalogResource.xml
00124   doc = etree_.parse(catalog_file)
00125 
00126   rootNode = doc.getroot()
00127   if rootNode.tag != "resources":
00128     raise  ParseError("First level tag must be resources not %s" % rootNode.tag)
00129 
00130   resources=[]
00131 
00132   #Extract resources
00133   for child in rootNode:
00134     if child.tag != "machine":
00135       raise  ParseError("Second level tag must be machine not %s" % child.tag)
00136     resources.append(Resource(child))
00137 
00138   local_names=("localhost",get_hostname())
00139 
00140   for resource in resources:
00141     if resource.get_host() in local_names:continue
00142     command=resource.get_rsh() +" -l "+resource.get_user()+" "+resource.get_host()
00143     command=command+ " " + os.path.join(resource.get_appliPath(),"runRemote.sh")
00144     if options.port:
00145       port=options.port
00146     else:
00147       port=os.getenv("NSPORT") or "2810"
00148     command=command+ " " + get_hostname() + " " + port +" killSalomeWithPort.py " + port
00149     print command
00150     os.system(command)
00151 
00152 if __name__ == '__main__':
00153   main()
00154