|
| 1 | +#!/usr/bin/groovy |
| 2 | + |
| 3 | +import java.security.MessageDigest |
| 4 | +import org.apache.commons.io.FileUtils |
| 5 | +import org.apache.commons.io.FileExistsException |
| 6 | + |
| 7 | +/** |
| 8 | + * Short name of the product to upgrade. |
| 9 | + */ |
| 10 | +ProductName = 'hornetq' |
| 11 | + |
| 12 | +/** |
| 13 | + * Path of the directory in which the libraries of the new product version are available. |
| 14 | + */ |
| 15 | +SourceLibsPath = "${SILVERPEAS_HOME}/bin/jar/hornetq" |
| 16 | + |
| 17 | +/** |
| 18 | + * Libraries of tier-party product embedded in the JBoss AS are distributed into several |
| 19 | + * locations in a JBoss installation (JBOSS_HOME): common/libs, client/, and server/<profile>/deploy |
| 20 | + * for RAs. |
| 21 | + * Below are defined the descriptors on the libraries to upgrade. |
| 22 | + */ |
| 23 | +def JBossCommonLibs = [ |
| 24 | + path: "${JBOSS_HOME}/common/lib", |
| 25 | + libs: ['hornetq-core.jar', 'hornetq-bootstrap.jar', 'hornetq-jboss-as-integration.jar', |
| 26 | + 'hornetq-jms.jar', 'hornetq-logging.jar', 'netty.jar'], |
| 27 | + copied: [] |
| 28 | +] |
| 29 | + |
| 30 | +def JBossClientLibs = [ |
| 31 | + path: "${JBOSS_HOME}/client", |
| 32 | + libs: ['hornetq-core-client.jar', 'hornetq-jms-client.jar', 'netty.jar'], |
| 33 | + copied: [] |
| 34 | +] |
| 35 | + |
| 36 | +def JBossRAs = [ |
| 37 | + path: "${JBOSS_HOME}/server/${JBOSS_SERVER_PROFILE}/deploy", |
| 38 | + libs: ['jms-ra.rar'], |
| 39 | + copied: [] |
| 40 | +] |
| 41 | + |
| 42 | +/** |
| 43 | + * The descriptors carrying the information about the libraries implied in the upgrade. |
| 44 | + */ |
| 45 | +JBossLibsInUpdate = [JBossCommonLibs, JBossClientLibs, JBossRAs] |
| 46 | + |
| 47 | +void die(String msg) { |
| 48 | + throw new RuntimeException(msg) |
| 49 | +} |
| 50 | + |
| 51 | +String md5sum(String f) { |
| 52 | + MessageDigest digest = MessageDigest.getInstance("MD5") |
| 53 | + new File(f).withInputStream() { input -> |
| 54 | + byte[] buffer = new byte[8192] |
| 55 | + int read = 0 |
| 56 | + while( (read = input.read(buffer)) > 0) { |
| 57 | + digest.update(buffer, 0, read); |
| 58 | + } |
| 59 | + } |
| 60 | + byte[] md5sum = digest.digest() |
| 61 | + return new BigInteger(1, md5sum).toString(16).padLeft(32, '0') |
| 62 | +} |
| 63 | + |
| 64 | +def setUpBackup() { |
| 65 | + BACKUP_DIR = new File("${FileUtils.getTempDirectoryPath()}/${ProductName}") |
| 66 | + RESTORE_ERROR = false |
| 67 | + if (BACKUP_DIR.exists()) |
| 68 | + try { |
| 69 | + FileUtils.forceDelete(BACKUP_DIR) |
| 70 | + } catch(Exception ex) { |
| 71 | + die "ERROR: cannot remove a previous temporary directory: ${ex.message}. Stop the upgrade" |
| 72 | + } |
| 73 | + if (!BACKUP_DIR.mkdirs()) |
| 74 | + die 'ERROR: cannot create temporary directory! Stop the upgrade' |
| 75 | +} |
| 76 | + |
| 77 | +def tearDownBackup() { |
| 78 | + if (!RESTORE_ERROR) |
| 79 | + try { |
| 80 | + FileUtils.forceDelete(BACKUP_DIR) |
| 81 | + } catch(Exception ex) { |
| 82 | + println "WARNING: ${ex.message}" |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +def copyLibs(descriptor) { |
| 87 | + descriptor.libs.each { lib -> |
| 88 | + try { |
| 89 | + FileUtils.moveToDirectory(new File("${descriptor.path}/${lib}"), BACKUP_DIR, false) |
| 90 | + } catch(FileExistsException ex) { |
| 91 | + } |
| 92 | + descriptor.copied << lib |
| 93 | + File source = new File("${SourceLibsPath}/${lib}") |
| 94 | + if (source.isFile()) |
| 95 | + FileUtils.copyFileToDirectory(source, new File("${descriptor.path}")) |
| 96 | + else if (source.isDirectory()) |
| 97 | + FileUtils.copyDirectoryToDirectory(source, new File("${descriptor.path}")) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +def restoreLibs(descriptor) { |
| 102 | + descriptor.copied.each { lib -> |
| 103 | + try { |
| 104 | + File source = new File("${BACKUP_DIR.path}/${lib}") |
| 105 | + if (source.isFile()) |
| 106 | + FileUtils.copyFileToDirectory(source, new File("${descriptor.path}")) |
| 107 | + else (source.isDirectory) |
| 108 | + FileUtils.copyDirectoryToDirectory(source, new File("${descriptor.path}")) |
| 109 | + } catch(Exception ex) { |
| 110 | + println "WARNING: cannot restore ${lib}. ${ex.message}" |
| 111 | + RESTORE_ERROR = true |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +String libToCheck = JBossLibsInUpdate[0].libs[0] |
| 117 | +String currentMD5 = md5sum "${SourceLibsPath}/${libToCheck}" |
| 118 | +String newerMD5 = md5sum "${JBossLibsInUpdate[0].path}/${libToCheck}" |
| 119 | +if (currentMD5 != newerMD5) { |
| 120 | + try { |
| 121 | + println 'Old HornetQ version detected: upgrade it...' |
| 122 | + setUpBackup() |
| 123 | + try { |
| 124 | + JBossLibsInUpdate.each { descriptor -> |
| 125 | + copyLibs descriptor |
| 126 | + } |
| 127 | + } catch(Exception ex) { |
| 128 | + println "ERROR: ${ex.message}. Restores the original ${ProductName}..." |
| 129 | + JBossLibsInUpdate.each { descriptor -> |
| 130 | + restoreLibs descriptor |
| 131 | + } |
| 132 | + if (RESTORE_ERROR) |
| 133 | + println "INFO: Some error has occured while restoring the original ${ProductName} in JBoss.\n" + |
| 134 | + "Theses libraries are in ${BACKUP_DIR.path}, please restore them by hand" |
| 135 | + die "An error occured while upgrading ${ProductName}. Stop the upgrade." |
| 136 | + } finally { |
| 137 | + tearDownBackup() |
| 138 | + } |
| 139 | + } catch(Exception ex) { |
| 140 | + println ex.message |
| 141 | + return 1 |
| 142 | + } |
| 143 | +} |
| 144 | +return 0 |
0 commit comments