]> SALOME platform Git repositories - modules/kernel.git/blob - bin/appliskel/kill_remote_containers.py
Salome HOME
Launch salome foreground when gui is activated.
[modules/kernel.git] / bin / appliskel / kill_remote_containers.py
1 #!/usr/bin/env python
2 #  -*- coding: utf-8 -*-
3 """
4 """
5 import sys,os,shutil,glob,socket
6 import optparse
7
8 import getAppliPath
9 appli_local=os.path.realpath(os.path.dirname(__file__))
10 APPLI=getAppliPath.relpath(appli_local,os.path.realpath(os.getenv('HOME')))
11
12 usage="""usage: %prog [options]
13
14 This procedure kill all containers that have been launched in a SALOME session on remote machines.
15 A SALOME session is identified by a machine name and a port number.
16
17 You need to have a well installed SALOME application with a CatalogResources.xml file.
18 This file is used (parsed) to collect all remote resources.
19 Remote resources are all the resources on remote machines not including the main local SALOME application.
20 """
21
22 try:
23   # cElementTree from Python 2.5+
24   import xml.etree.cElementTree as etree_
25 except ImportError:
26   try:
27     import xml.etree.ElementTree as etree_
28   except ImportError:
29     try:
30       import cElementTree as etree_
31     except ImportError:
32       try:
33         # normal ElementTree install
34         import elementtree.ElementTree as etree_
35       except ImportError:
36         raise
37
38 class ParseError(Exception):
39   pass
40
41 catalog_file=os.path.join(appli_local,"CatalogResources.xml")
42
43 SEP=":"
44 if sys.platform == "win32":SEP=";"
45
46 def get_hostname():
47   return socket.gethostname().split('.')[0]
48
49 class Resource:
50   """Define a SALOME resource
51      - components : the list of available components of the resource
52   """
53   def __init__(self,node):
54     self.node=node
55     self.components=[]
56     self.resource_dir=None
57
58   def get_rcp(self):
59     protocol= self.node.get("protocol")
60     if protocol and protocol[0]=='s':return "scp"
61     else:return "rcp"
62
63   def get_rsh(self):
64     protocol= self.node.get("protocol")
65     if protocol and protocol[0]=='s':return "ssh"
66     else:return "rsh"
67
68   def get_user(self):
69     userName= self.node.get("userName")
70     if not userName:
71       userName=os.getenv('USER')
72     return userName
73
74   def get_host(self):
75     hostname= self.node.get("hostname")
76     return hostname
77
78   def get_name(self):
79     name= self.node.get("name")
80     if name:return name
81     return self.get_host()
82
83   def get_appliPath(self):
84     appliPath= self.node.get("appliPath")
85     if appliPath is None:
86       appliPath=APPLI
87     return appliPath
88
89 def main():
90   parser = optparse.OptionParser(usage=usage)
91   parser.add_option('-p','--port', dest="port", 
92                             help="The SALOME session port (default NSPORT or 2810)")
93
94
95   options, args = parser.parse_args()
96
97   if not os.path.exists(catalog_file):
98     print "ERROR: the catalog file %s is mandatory" % catalog_file_base
99     sys.exit(1)
100
101   #Parse CatalogResource.xml
102   doc = etree_.parse(catalog_file)
103
104   rootNode = doc.getroot()
105   if rootNode.tag != "resources":
106     raise  ParseError("First level tag must be resources not %s" % rootNode.tag)
107
108   resources=[]
109
110   #Extract resources
111   for child in rootNode:
112     if child.tag != "machine":
113       raise  ParseError("Second level tag must be machine not %s" % child.tag)
114     resources.append(Resource(child))
115
116   local_names=("localhost",get_hostname())
117
118   for resource in resources:
119     if resource.get_host() in local_names:continue
120     command=resource.get_rsh() +" -l "+resource.get_user()+" "+resource.get_host()
121     command=command+ " " + os.path.join(resource.get_appliPath(),"runRemote.sh")
122     if options.port:
123       port=options.port
124     else:
125       port=os.getenv("NSPORT") or "2810"
126     command=command+ " " + get_hostname() + " " + port +" killSalomeWithPort.py " + port
127     print command
128     os.system(command)
129
130 if __name__ == '__main__':
131   main()
132