deploySwaggerUI.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #------------------------------------------------------------------------------
  2. # Swagger UI applications deployment/undeployment script
  3. # See usage below
  4. #
  5. # This is a servicable file. This file may be changed or overwritten when a
  6. # new fixpack is installed. If you need to modify this file, make a copy and
  7. # make changes to the copy only.
  8. #------------------------------------------------------------------------------
  9. import sys
  10. #------------------------------------------------------------------------------
  11. # Get the directory, as a string, where WAS is installed.
  12. #------------------------------------------------------------------------------
  13. def getWASHome(cell, node):
  14. varMap = AdminConfig.getid("/Cell:" + cell + "/Node:" + node + "/VariableMap:/")
  15. entries = AdminConfig.list("VariableSubstitutionEntry", varMap)
  16. eList = entries.splitlines()
  17. for entry in eList:
  18. name = AdminConfig.showAttribute(entry, "symbolicName")
  19. if name == "WAS_INSTALL_ROOT":
  20. value = AdminConfig.showAttribute(entry, "value")
  21. return value
  22. #failover
  23. return java.lang.System.getenv('WAS_HOME')
  24. #------------------------------------------------------------------------------
  25. # Get the WAS systemApps directory.
  26. #
  27. # The WAS systemApps directory is always located at <WAS_HOME>/systemApps
  28. #------------------------------------------------------------------------------
  29. def getSystemAppsDir(cell, node):
  30. fileSep = getFileSep(node)
  31. return getWASHome(cell, node) + fileSep + "systemApps"
  32. #------------------------------------------------------------------------------
  33. # Get the directory, as a string, of the SwaggerUI.ear application.
  34. #
  35. # The isclite.ear is located in <WAS_HOME>/systemApps/SwaggerUI.ear
  36. #------------------------------------------------------------------------------
  37. def getSwaggerUIDir(cell, node):
  38. fileSep = getFileSep(node)
  39. return getSystemAppsDir(cell, node) + fileSep + "SwaggerUI.ear"
  40. #------------------------------------------------------------------------------
  41. # Get the directory, as a string, of the RESTAPIDocs.ear application.
  42. #
  43. # The isclite.ear is located in <WAS_HOME>/systemApps/RESTAPIDocs.ear
  44. #------------------------------------------------------------------------------
  45. def getRESTAPIDocsDir(cell, node):
  46. fileSep = getFileSep(node)
  47. return getSystemAppsDir(cell, node) + fileSep + "RESTAPIDocs.ear"
  48. #------------------------------------------------------------------------------
  49. # Get the file separator character
  50. #
  51. # This gets the file separator for the node in which we plan to install the
  52. # console. Therefore we can't just use the value that python on Java gives
  53. # us. Instead, check the platform of the node, and use "\" for windows and
  54. # "/" for everything else.
  55. #------------------------------------------------------------------------------
  56. def getFileSep(node):
  57. os = AdminTask.getNodePlatformOS("-nodeName " + node)
  58. if os == 'windows':
  59. return '\\'
  60. else:
  61. return '/'
  62. #------------------------------------------------------------------------------
  63. # Get a tuple containing the cell, node, server name, and type
  64. #------------------------------------------------------------------------------
  65. def getCellNodeServer():
  66. servers = AdminConfig.list("Server").splitlines()
  67. for serverId in servers:
  68. serverName = serverId.split("(")[0]
  69. server = serverId.split("(")[1] #remove name( from id
  70. server = server.split("/")
  71. cell = server[1]
  72. node = server[3]
  73. return (cell, node, serverName)
  74. return None
  75. #------------------------------------------------------------------------------
  76. # Install SwaggerUI.ear and RESTAPIDocs.ear
  77. #------------------------------------------------------------------------------
  78. def doInstall(targets):
  79. topology = getCellNodeServer()
  80. if topology == None:
  81. sys.stderr.write("Could not find suitable server\n")
  82. sys.exit(105)
  83. else:
  84. cell = topology[0]
  85. node = topology[1]
  86. server = topology[2]
  87. swaggerUIDir = getSwaggerUIDir(cell, node)
  88. restAPIDocsDir = getRESTAPIDocsDir(cell, node)
  89. sysAppDir = getSystemAppsDir(cell, node)
  90. try:
  91. print "Deploying SwggerUI.ear"
  92. AdminApp.install(swaggerUIDir, ['-appname', 'SwaggerUI', '-zeroEarCopy', '-installed.ear.destination', '$(WAS_INSTALL_ROOT)/systemApps', '-CtxRootForWebMod', [['.*', '.*', '/ibm/api/explorer']], '-MapModulesToServers', [[ 'SwaggerUI', 'SwaggerUI.war,WEB-INF/web.xml', targets]]])
  93. print "Deploying RESTAPIDocs.ear"
  94. AdminApp.install(restAPIDocsDir, ['-appname', 'RESTAPIDocs', '-zeroEarCopy', '-installed.ear.destination', '$(WAS_INSTALL_ROOT)/systemApps', '-CtxRootForWebMod', [['.*', '.*', '/ibm/api/docs']], '-MapModulesToServers', [[ 'RESTAPIDocs', 'RESTAPIDocs.war,WEB-INF/web.xml', targets]]])
  95. AdminConfig.save()
  96. except:
  97. error = str(sys.exc_info()[1])
  98. if error.count("7279E") > 0: # catch WASX7279E (app with given name already exists)
  99. print "Swagger UI is already installed."
  100. else:
  101. print "Exception occurred during installing Swagger UI(" + targetCell +", " + targetNode + ", " + targetServer + "):", error
  102. return 0
  103. return 1
  104. #------------------------------------------------------------------------------
  105. # Print script usage
  106. #------------------------------------------------------------------------------
  107. def printUsage():
  108. print "Usage:"
  109. print " To install SwaggerUI on a single server:"
  110. print " wsadmin deploySwaggerUI.py install 'WebSphere:cell=cellName,node=nodeName,server=serverName'"
  111. print " wsadmin deploySwaggerUI.py install 'WebSphere:cell=cellName,cluster=clusterName'"
  112. print " To remove SwaggerUI on a cluster:"
  113. print " wsadmin deploySwaggerUI.py remove"
  114. print ""
  115. print "For example:"
  116. print " wsadmin deploySwaggerUI.py install 'WebSphere:cell=myCell,node=myCellNode01,server=server1'"
  117. print " wsadmin deploySwaggerUI.py install 'WebSphere:cell=myCell04,cluster=cluster1'"
  118. print ""
  119. #------------------------------------------------------------------------------
  120. # Uninstall the admin console
  121. #------------------------------------------------------------------------------
  122. def doRemove():
  123. AdminApp.uninstall("SwaggerUI")
  124. AdminApp.uninstall("RESTAPIDocs")
  125. AdminConfig.save()
  126. #------------------------------------------------------------------------------
  127. # Main entry point
  128. #------------------------------------------------------------------------------
  129. print "Starting..."
  130. if len(sys.argv) < 1 or len(sys.argv) > 2:
  131. sys.stderr.write("Invalid number of arguments\n")
  132. printUsage()
  133. sys.exit(101)
  134. else:
  135. mode = sys.argv[0]
  136. if mode == "install" and len(sys.argv) == 2:
  137. print "Installing Swagger UI applications..."
  138. doInstall(sys.argv[1])
  139. elif mode == "remove" and len(sys.argv) == 1:
  140. print "Removing Swagger UI applications..."
  141. doRemove()
  142. else:
  143. sys.stderr.write("Invalid command: " + mode + "\n")
  144. printUsage()
  145. sys.exit(102)