Guest User

test2

a guest
Feb 21st, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. import sys
  2. import os
  3. from java.lang import System
  4. import sys
  5. import os
  6. from java.lang import System
  7. #Python Script to manage Servers in weblogic server.
  8. #This script takes input from command line and executes it.
  9. #It can be used to check status,stop,start of applications in weblogic server using weblogic wlst tool.
  10. #Company: TechPaste Solutions
  11. import getopt
  12. #========================
  13. #Usage Section
  14. #========================
  15. def usage():
  16.  
  17. print "Usage:"
  18. print "java weblogic.WLST manageServers.py -u username -p password -a adminUrl [:] -n ServerName -c [stop:start:restart:status:stopall:startall:statusall]\n"
  19. sys.exit(2)
  20. #========================
  21. #Connect To Domain
  22. #========================
  23.  
  24. def connectToDomain():
  25. try:
  26. if username != "":
  27. connect(username, password, adminUrl)
  28. print 'Successfully connected to the domain\n'
  29. # else:
  30. # connect(userConfigFile=UCF, userKeyFile=UKF, url=admurl)
  31. # print 'Successfully connected to the domain\n'
  32.  
  33. except:
  34. print 'The domain is unreacheable. Please try again\n'
  35. exit()
  36.  
  37. #==============================================
  38. #Checking Server Status
  39. #==============================================
  40. def _serverstatus(ServerName):
  41. try:
  42. # print 'Server reached:'+ServerName
  43. cd('domainRuntime:/ServerLifeCycleRuntimes/'+ServerName);
  44. # print 'Cd successful';
  45. # ls();
  46. serverState = cmo.getState()
  47. # print 'Server State'+serverState;
  48. if serverState == "RUNNING":
  49. print 'Server ' + ServerName + ' is :\033[1;32m' + serverState + '\033[0m';
  50. elif serverState == "STARTING":
  51. print 'Server ' + ServerName + ' is :\033[1;33m' + serverState + '\033[0m';
  52. elif serverState == "UNKNOWN":
  53. print 'Server ' + ServerName + ' is :\033[1;34m' + serverState + '\033[0m';
  54. else:
  55. print 'Server ' + ServerName + ' is :\033[1;31m' + serverState + '\033[0m';
  56. return serverState
  57. except:
  58. print 'Not able to get the' + serverState +'server status. Please try again\n';
  59. print 'Please check logged in user has full access to complete the requested operation on ' +ServerName+ '\n';
  60. exit()
  61.  
  62. #==============================================
  63. #Start Server Block
  64. #==============================================
  65.  
  66. def _startServer(ServerName):
  67.  
  68. try:
  69. cd('domainRuntime:/ServerLifeCycleRuntimes/'+ServerName);
  70. cmo.start();
  71. state=_serverstatus(ServerName);
  72. while (state!='RUNNING'):
  73. state=_serverstatus(ServerName);
  74. java.lang.Thread.sleep(5000);
  75. except:
  76. print 'Error in getting current status of ' +ServerName+ '\n';
  77. print 'Please check logged in user has full access to complete the start operation on ' +ServerName+ '\n';
  78. exit()
  79. #==============================================
  80. #Stop Server Block
  81. #==============================================
  82.  
  83. def _stopServer(ServerName):
  84.  
  85. try:
  86. cd('domainRuntime:/ServerLifeCycleRuntimes/'+ServerName);
  87. cmo.forceShutdown();
  88. state=_serverstatus(ServerName);
  89. while (state!='SHUTDOWN'):
  90. state=_serverstatus(ServerName);
  91. java.lang.Thread.sleep(5000);
  92. except:
  93. print 'Error in getting current status of ' +ServerName+ '\n';
  94. print 'Please check logged in user has full access to complete the stop operation on ' +ServerName+ '\n';
  95. exit()
  96.  
  97. #===============================
  98. #Input Values Validation Section
  99. #===============================
  100.  
  101. if __name__=='__main__' or __name__== 'main':
  102.  
  103. try:
  104. opts, args = getopt.getopt(sys.argv[1:], "u:p:a:n:c:", ["username=", "password=", "adminUrl=", "ServerName=", "command="])
  105.  
  106. except getopt.GetoptError, err:
  107. print str(err)
  108. usage()
  109.  
  110. username = ''
  111. password = ''
  112. adminUrl = ''
  113. ServerName = ''
  114. command = ''
  115.  
  116. for opt, arg in opts:
  117. if opt == "-u":
  118. username = arg
  119. elif opt == "-p":
  120. password = arg
  121. elif opt == "-a":
  122. adminUrl = arg
  123. elif opt == "-n":
  124. ServerName = arg
  125. elif opt == "-c":
  126. command = arg
  127.  
  128. if username == "":
  129. print "Missing \"-u username\" parameter.\n"
  130. usage()
  131. elif password == "":
  132. print "Missing \"-p password\" parameter.\n"
  133. usage()
  134. elif adminUrl == "":
  135. print "Missing \"-a adminUrl\" parameter.\n"
  136. usage()
  137. elif ServerName == "":
  138. print "Missing \"-n ServerName\" parameter.\n"
  139. usage()
  140. elif command == "":
  141. print "Missing \"-c command\" parameter.\n"
  142. usage()
  143. #========================
  144. #Main Control Block For Operations
  145. #========================
  146.  
  147. def lifecycleMain():
  148. try:
  149. if command =='status' :
  150. _serverstatus(ServerName);
  151. elif command =='stop':
  152. state=_serverstatus(ServerName);
  153. if state!='SHUTDOWN' :
  154. print 'Trying To Shutdown Server:' + ServerName + '...';
  155. _stopServer(ServerName);
  156. elif command =='start':
  157. state=_serverstatus(ServerName);
  158. if state!='RUNNING' :
  159. print 'Trying To Start Server:' + ServerName + '...';
  160. _startServer(ServerName);
  161. elif command =='restart':
  162. state=_serverstatus(ServerName);
  163. if state!='SHUTDOWN' :
  164. print 'Trying To Shutdown Server:' + ServerName + '...';
  165. _stopServer(ServerName);
  166. state=_serverstatus(ServerName);
  167. if state!='RUNNING' :
  168. print 'Trying To Start Server:' + ServerName + '...';
  169. _startServer(ServerName);
  170. elif command == "stopall":
  171. try:
  172. cd('/Servers')
  173. allServers=ls('/Servers', returnMap='true')
  174. for p_server in allServers:
  175. if p_server == 'AdminServer':
  176. continue
  177. else:
  178. _stopServer(p_server);
  179. except Exception, e:
  180. print "Error Occured"
  181. elif command == "startall":
  182. try:
  183. cd('/Servers')
  184. allServers=ls('/Servers', returnMap='true')
  185. for p_server in allServers:
  186. if p_server == 'AdminServer':
  187. continue
  188. else:
  189. _startServer(p_server);
  190. except Exception, e:
  191. print "Error Occured"
  192. elif command == "statusall":
  193. try:
  194. cd('/Servers')
  195. allServers=ls('/Servers', returnMap='true')
  196. for p_server in allServers:
  197. _serverstatus(p_server);
  198. except Exception, e:
  199. print "Error Occured"
  200. else:
  201. print 'Not able to understand the command supplied.'
  202. usage();
  203. except:
  204. print 'Error during lifecycle operation of ' +ServerName+ '\n'
  205. exit();
  206. #========================
  207. #Execute Block
  208. #========================
  209.  
  210. connectToDomain();
  211. lifecycleMain();
  212. disconnect();
  213. exit();
Add Comment
Please, Sign In to add comment