importOSB.py.erb 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from java.util import HashMap
  2. from java.util import HashSet
  3. from java.util import ArrayList
  4. from java.io import FileInputStream
  5. from com.bea.wli.sb.util import Refs
  6. from com.bea.wli.config.customization import Customization
  7. from com.bea.wli.sb.management.importexport import ALSBImportOperation
  8. import sys
  9. #=======================================================================================
  10. # Entry function to deploy project configuration and resources
  11. # into a ALSB domain
  12. #=======================================================================================
  13. def importToALSBDomain():
  14. try:
  15. SessionMBean = None
  16. print 'Attempting to import :', importJar, "on ALSB Admin Server"
  17. theBytes = readBinaryFile(importJar)
  18. print 'Read file', importJar
  19. sessionName = createSessionName()
  20. print 'Created session', sessionName
  21. SessionMBean = getSessionManagementMBean(sessionName)
  22. print 'SessionMBean started session'
  23. ALSBConfigurationMBean = findService(String("ALSBConfiguration.").concat(sessionName), "com.bea.wli.sb.management.configuration.ALSBConfigurationMBean")
  24. print "ALSBConfiguration MBean found", ALSBConfigurationMBean
  25. ALSBConfigurationMBean.uploadJarFile(theBytes)
  26. print 'Jar Uploaded'
  27. if project == "None":
  28. print 'No project specified, additive deployment performed'
  29. alsbJarInfo = ALSBConfigurationMBean.getImportJarInfo()
  30. alsbImportPlan = alsbJarInfo.getDefaultImportPlan()
  31. alsbImportPlan.setPassphrase(passphrase)
  32. alsbImportPlan.setPreserveExistingEnvValues(true)
  33. importResult = ALSBConfigurationMBean.importUploaded(alsbImportPlan)
  34. SessionMBean.activateSession(sessionName, "Complete import without customization using wlst")
  35. else:
  36. print 'ALSB project', project, 'will get overlaid'
  37. alsbJarInfo = ALSBConfigurationMBean.getImportJarInfo()
  38. alsbImportPlan = alsbJarInfo.getDefaultImportPlan()
  39. alsbImportPlan.setPassphrase(passphrase)
  40. operationMap=HashMap()
  41. operationMap = alsbImportPlan.getOperations()
  42. print
  43. print 'Default importPlan'
  44. printOpMap(operationMap)
  45. set = operationMap.entrySet()
  46. alsbImportPlan.setPreserveExistingEnvValues(true)
  47. #boolean
  48. abort = false
  49. #list of created ref
  50. createdRef = ArrayList()
  51. for entry in set:
  52. ref = entry.getKey()
  53. op = entry.getValue()
  54. #set different logic based on the resource type
  55. type = ref.getTypeId
  56. if type == Refs.SERVICE_ACCOUNT_TYPE or type == Refs.SERVICE_PROVIDER_TYPE:
  57. if op.getOperation() == ALSBImportOperation.Operation.Create:
  58. print 'Unable to import a service account or a service provider on a target system', ref
  59. abort = true
  60. # elif op.getOperation() == ALSBImportOperation.Operation.Create:
  61. else:
  62. #keep the list of created resources
  63. print 'ref: ',ref
  64. createdRef.add(ref)
  65. if abort == true :
  66. print 'This jar must be imported manually to resolve the service account and service provider dependencies'
  67. SessionMBean.discardSession(sessionName)
  68. raise
  69. print
  70. print 'Modified importPlan'
  71. printOpMap(operationMap)
  72. importResult = ALSBConfigurationMBean.importUploaded(alsbImportPlan)
  73. printDiagMap(importResult.getImportDiagnostics())
  74. if importResult.getFailed().isEmpty() == false:
  75. print 'One or more resources could not be imported properly'
  76. raise
  77. #customize if a customization file is specified
  78. #affects only the created resources
  79. if customFile != "None" :
  80. print 'Loading customization File', customFile
  81. print 'Customization applied to the created resources only', createdRef
  82. iStream = FileInputStream(customFile)
  83. customizationList = Customization.fromXML(iStream)
  84. filteredCustomizationList = ArrayList()
  85. setRef = HashSet(createdRef)
  86. # apply a filter to all the customizations to narrow the target to the created resources
  87. for customization in customizationList:
  88. print customization
  89. newcustomization = customization.clone(setRef)
  90. filteredCustomizationList.add(newcustomization)
  91. ALSBConfigurationMBean.customize(filteredCustomizationList)
  92. SessionMBean.activateSession(sessionName, "Complete import with customization using wlst")
  93. print "Deployment of : " + importJar + " successful"
  94. except:
  95. print "Unexpected error:", sys.exc_info()[0]
  96. if SessionMBean != None:
  97. SessionMBean.discardSession(sessionName)
  98. raise
  99. #=======================================================================================
  100. # Utility function to print the list of operations
  101. #=======================================================================================
  102. def printOpMap(map):
  103. set = map.entrySet()
  104. for entry in set:
  105. op = entry.getValue()
  106. print op.getOperation(),
  107. ref = entry.getKey()
  108. print ref
  109. print
  110. #=======================================================================================
  111. # Utility function to print the diagnostics
  112. #=======================================================================================
  113. def printDiagMap(map):
  114. set = map.entrySet()
  115. for entry in set:
  116. diag = entry.getValue().toString()
  117. print diag
  118. print
  119. #=======================================================================================
  120. # Utility function to read a binary file
  121. #=======================================================================================
  122. def readBinaryFile(fileName):
  123. file = open(fileName, 'rb')
  124. bytes = file.read()
  125. return bytes
  126. #=======================================================================================
  127. # Utility function to create an arbitrary session name
  128. #=======================================================================================
  129. def createSessionName():
  130. sessionName = String("SessionScript"+Long(System.currentTimeMillis()).toString())
  131. return sessionName
  132. #=======================================================================================
  133. # Utility function to load a session MBeans
  134. #=======================================================================================
  135. def getSessionManagementMBean(sessionName):
  136. SessionMBean = findService("SessionManagement", "com.bea.wli.sb.management.configuration.SessionManagementMBean")
  137. SessionMBean.createSession(sessionName)
  138. return SessionMBean
  139. # IMPORT script init
  140. try:
  141. # import the service bus configuration
  142. wlsUser = '<%= @wlsUser %>'
  143. password = '<%= @password %>'
  144. machine = '<%= @address %>'
  145. portNumber = '<%= @port %>'
  146. importJar = '<%= @artifact %>'
  147. customFile = '<%= @customFile %>'
  148. passphrase = "osb"
  149. project = '<%= @project %>'
  150. connect(wlsUser,password,'t3://'+machine+':'+portNumber)
  151. domainRuntime()
  152. importToALSBDomain()
  153. except:
  154. print "Unexpected error: ", sys.exc_info()[0]
  155. dumpStack()
  156. raise