Michael DÜrre | 7bb133e | 2022-01-18 10:06:31 +0100 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | import os |
| 3 | import argparse |
| 4 | import subprocess |
| 5 | import re |
| 6 | import shutil |
| 7 | import tempfile |
| 8 | from lib.pomfile import PomFile |
| 9 | |
| 10 | DEFAULT_PARENT_GROUPID="org.onap.ccsdk.parent" |
Michael Dürre | d3168e3 | 2022-06-13 05:23:15 +0200 | [diff] [blame] | 11 | DEFAULT_PARENT_VERSION="2.4.0-SNAPSHOT" |
Michael DÜrre | 7bb133e | 2022-01-18 10:06:31 +0100 | [diff] [blame] | 12 | DEFAULT_STRICT=True |
| 13 | USE_OLD_SERVLET_API=True |
| 14 | |
| 15 | class OdlParentMigrator: |
| 16 | |
| 17 | def __init__(self,odlSourcePath, odlParentPath=None, groupId=DEFAULT_PARENT_GROUPID, version=DEFAULT_PARENT_VERSION, strict=DEFAULT_STRICT): |
| 18 | self.odlSourcePath=odlSourcePath |
| 19 | self.mvnbin = "/usr/bin/mvn" |
| 20 | self.version = version |
| 21 | self.groupId = groupId |
| 22 | self.strict = strict |
| 23 | if odlParentPath is None: |
| 24 | odlParentPath = os.path.abspath(os.path.dirname(os.path.realpath(__file__))+"/../odlparent") |
| 25 | self.odlParentPath=odlParentPath |
| 26 | self.parentPath =os.path.abspath(odlParentPath+'/../') |
| 27 | |
| 28 | |
| 29 | def getMvnRepoVersion(self, groupId, artifactId): |
| 30 | path="{}/system/{}/{}".format(self.odlSourcePath,groupId.replace('.','/'),artifactId) |
| 31 | if not os.path.exists(path): |
| 32 | return None |
| 33 | folders =[f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))] |
| 34 | if len(folders)<1: |
| 35 | return None |
| 36 | return folders[0] |
| 37 | |
| 38 | def getMvnRepoVersions(self, groupId, artifactId): |
| 39 | path="{}/system/{}/{}".format(self.odlSourcePath,groupId.replace('.','/'),artifactId) |
| 40 | if not os.path.exists(path): |
| 41 | return None |
| 42 | folders =[f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))] |
| 43 | if len(folders)<1: |
| 44 | return None |
| 45 | return folders |
| 46 | |
| 47 | def migrateInstalledOdlBom(self) -> bool: |
| 48 | success = True |
| 49 | print("migrating installed-odl-bom") |
| 50 | root=os.path.abspath(self.odlParentPath+'/..') |
| 51 | self.exec(('cd {src}/system && '+ |
| 52 | '{root}/tools/mkbom.sh {groupId} {artifactId} {version}> '+ |
| 53 | '{root}/installed-odl-bom/pom.xml && cd -').format( |
| 54 | root=root,src=self.odlSourcePath, |
| 55 | parent=self.odlParentPath,groupId=self.groupId, |
| 56 | artifactId='installed-odl-bom', version=self.version)) |
| 57 | if USE_OLD_SERVLET_API: |
| 58 | pom = PomFile('{}/installed-odl-bom/pom.xml'.format(root)) |
| 59 | success = pom.setDependencyManagementVersion('javax.servlet','javax.servlet-api','3.1.0') |
| 60 | |
| 61 | print("done") |
| 62 | return success |
| 63 | |
| 64 | def migrateDependenciesBom(self) -> bool: |
| 65 | success = True |
| 66 | print("migrating dependencies-bom") |
| 67 | |
| 68 | print("done" if success else "failed") |
| 69 | return success |
| 70 | |
| 71 | def migrateSetupProperties(self) -> bool: |
| 72 | success = True |
| 73 | print("migrating setup") |
| 74 | mdsalVersion=self.getMvnRepoVersion('org.opendaylight.mdsal','mdsal-binding-api') |
| 75 | odlBundleVersion=self.getMvnRepoVersion('org.opendaylight.odlparent','features-odlparent') |
| 76 | mdsalItVersion=self.getMvnRepoVersion('org.opendaylight.controller','features-controller') |
| 77 | yangVersion = self.getMvnRepoVersion('org.opendaylight.yangtools','yang-common') |
| 78 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/binding-parent.properties', |
| 79 | 'odlparent.version=.*','odlparent.version={}'.format(mdsalVersion)) |
| 80 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/bundle-parent.properties', |
| 81 | 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion)) |
| 82 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/feature-repo-parent.properties', |
| 83 | 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion)) |
| 84 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/karaf4-parent.properties', |
| 85 | 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion)) |
| 86 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/mdsal-it-parent.properties', |
| 87 | 'odlparent.version=.*','odlparent.version={}'.format(mdsalItVersion)) |
| 88 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/odlparent-lite.properties', |
| 89 | 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion)) |
| 90 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/odlparent.properties', |
| 91 | 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion)) |
| 92 | self.replaceInFile(self.odlParentPath+'/setup/src/main/properties/single-feature-parent.properties', |
| 93 | 'odlparent.version=.*','odlparent.version={}'.format(odlBundleVersion)) |
| 94 | |
| 95 | |
| 96 | templatePom = PomFile(self.odlParentPath+'/setup/src/main/resources/pom-template.xml') |
| 97 | x = templatePom.setXmlValue('/project/properties/odl.controller.mdsal.version',mdsalVersion) |
| 98 | success = success and x |
| 99 | x = templatePom.setXmlValue('/project/properties/odl.mdsal.version',odlBundleVersion) |
| 100 | success = success and x |
| 101 | x = templatePom.setXmlValue('/project/properties/odl.mdsal.model.version',odlBundleVersion) |
| 102 | success = success and x |
| 103 | x = templatePom.setXmlValue('/project/properties/odl.netconf.restconf.version',mdsalVersion) |
| 104 | success = success and x |
| 105 | x = templatePom.setXmlValue('/project/properties/odl.netconf.netconf.version',mdsalVersion) |
| 106 | success = success and x |
| 107 | x = templatePom.setXmlValue('/project/properties/odl.netconf.sal.rest.docgen.version',mdsalVersion) |
| 108 | success = success and x |
| 109 | |
| 110 | x = templatePom.setXmlValue('/project/properties/commons.codec.version', |
| 111 | self.getMvnRepoVersion('commons-codec','commons-codec')) |
| 112 | success = success and x |
| 113 | x = templatePom.setXmlValue('/project/properties/commons.lang3.version', |
| 114 | self.getMvnRepoVersion('org.apache.commons','commons-lang3')) |
| 115 | success = success and x |
| 116 | x = templatePom.setXmlValue('/project/properties/commons.lang.version', |
| 117 | self.getMvnRepoVersion('commons-lang','commons-lang')) |
| 118 | success = success and x |
| 119 | x = templatePom.setXmlValue('/project/properties/commons.net.version', |
| 120 | self.getMvnRepoVersion('commons-net','commons-net')) |
| 121 | success = success and x |
| 122 | x = templatePom.setXmlValue('/project/properties/eclipse.persistence.version', |
| 123 | self.getMvnRepoVersion('org.eclipse.persistence','org.eclipse.persistence.core')) |
| 124 | success = success and x |
| 125 | x = templatePom.setXmlValue('/project/properties/gson.version', |
| 126 | self.getMvnRepoVersion('com.google.code.gson','gson')) |
| 127 | success = success and x |
| 128 | x = templatePom.setXmlValue('/project/properties/guava.version', |
| 129 | self.getMvnRepoVersion('com.google.guava','guava')) |
| 130 | success = success and x |
| 131 | x = templatePom.setXmlValue('/project/properties/jackson.version', |
| 132 | self.getMvnRepoVersion('com.fasterxml.jackson.core','jackson-core')) |
| 133 | success = success and x |
| 134 | x = templatePom.setXmlValue('/project/properties/javassist.version', |
| 135 | self.getMvnRepoVersion('org.javassist','javassist')) |
| 136 | success = success and x |
| 137 | x = templatePom.setXmlValue('/project/properties/jersey.version', |
| 138 | self.getMvnRepoVersion('org.glassfish.jersey.core','jersey-common')) |
| 139 | success = success and x |
| 140 | x = templatePom.setXmlValue('/project/properties/jersey.client.version', |
| 141 | self.getMvnRepoVersion('org.glassfish.jersey.core','jersey-client')) |
| 142 | success = success and x |
| 143 | x = templatePom.setXmlValue('/project/properties/org.json.version', |
| 144 | self.getMvnRepoVersion('org.json','json')) |
| 145 | success = success and x |
| 146 | x = templatePom.setXmlValue('/project/properties/netty.version', |
| 147 | self.getMvnRepoVersion('io.netty','netty-common')) |
| 148 | success = success and x |
| 149 | x = templatePom.setXmlValue('/project/properties/slf4j.version', |
| 150 | self.getMvnRepoVersion('org.slf4j','slf4j-api')) |
| 151 | success = success and x |
| 152 | x = templatePom.setXmlValue('/project/properties/derby.version', |
| 153 | self.getMvnRepoVersion('org.apache.derby','derby')) |
| 154 | success = success and x |
| 155 | x = templatePom.setXmlValue('/project/properties/jetty.version', |
| 156 | self.getMvnRepoVersion('org.eclipse.jetty','jetty-http')) |
| 157 | success = success and x |
| 158 | print("done" if success else "failed") |
| 159 | return success |
| 160 | |
| 161 | def migrateDependenciesOdlBom(self): |
| 162 | success = True |
| 163 | print("migrating dependencies-odl-bom") |
| 164 | bgpVersion = self.getMvnRepoVersion('org.opendaylight.bgpcep','topology-api') |
| 165 | controllerVersion = self.getMvnRepoVersion('org.opendaylight.controller', 'blueprint') |
| 166 | mdsalVersion=self.getMvnRepoVersion('org.opendaylight.mdsal','mdsal-binding-api') |
| 167 | netconfVersion = self.getMvnRepoVersion('org.opendaylight.netconf','ietf-netconf') |
| 168 | |
| 169 | pomFile = PomFile(os.path.abspath(self.parentPath+'/dependencies-odl-bom/pom.xml')) |
| 170 | x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=bgp-artifacts]/version',bgpVersion) |
| 171 | success = success and x |
| 172 | x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=controller-artifacts]/version',controllerVersion) |
| 173 | success = success and x |
| 174 | x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=mdsal-artifacts]/version',mdsalVersion) |
| 175 | success = success and x |
| 176 | x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=netconf-artifacts]/version',netconfVersion) |
| 177 | success = success and x |
| 178 | x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-binding-broker-impl]/version',netconfVersion, True) |
| 179 | success = success and x |
| 180 | # at the moment not possible because of dependent variable in path after value to set |
| 181 | # x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-binding-broker-impl,type=test-jar]/version',netconfVersion) |
| 182 | # success = success and x |
| 183 | x = pomFile.setXmlValue('/project/dependencyManagement/dependencies/dependency[artifactId=sal-test-model]/version',netconfVersion) |
| 184 | success = success and x |
| 185 | print("done" if success else "failed") |
| 186 | return success |
| 187 | |
| 188 | def setParentValues(self): |
| 189 | print("setting all other parents") |
| 190 | # find all pom.xml files with parent to set |
| 191 | pomfiles=[os.path.abspath(self.parentPath+'/pom.xml'), |
| 192 | os.path.abspath(self.parentPath+'/dependencies-bom/pom.xml'), |
| 193 | os.path.abspath(self.odlParentPath+'/pom.xml'), |
| 194 | os.path.abspath(self.odlParentPath+'/setup/pom.xml'), |
| 195 | os.path.abspath(self.parentPath+'/springboot/pom.xml'), |
| 196 | os.path.abspath(self.parentPath+'/springboot/spring-boot-setup/pom.xml')] |
| 197 | |
| 198 | success=True |
| 199 | for file in pomfiles: |
| 200 | pomfile = PomFile(file) |
| 201 | if pomfile.hasParent(): |
| 202 | x = pomfile.setXmlValue('/project/parent/groupId',self.groupId) |
| 203 | success = success and x |
| 204 | x = pomfile.setXmlValue('/project/parent/version',self.version) |
| 205 | success = success and x |
| 206 | |
| 207 | # find all pom.xml files with groupId and version to set |
| 208 | pomfiles=PomFile.findAll(os.path.abspath(self.odlParentPath+'/..')) |
| 209 | for file in pomfiles: |
| 210 | pomfile = PomFile(file) |
| 211 | x = pomfile.setXmlValue('/project/groupId',self.groupId) |
| 212 | success = success and x |
| 213 | x = pomfile.setXmlValue('/project/version',self.version) |
| 214 | success = success and x |
| 215 | |
| 216 | # set only groupId for odl template |
| 217 | pomfile = PomFile(self.odlParentPath+'/setup/src/main/resources/pom-template.xml') |
| 218 | x = pomfile.setXmlValue('/project/groupId',self.groupId) |
| 219 | success = success and x |
| 220 | print("done" if success else "failed") |
| 221 | return success |
| 222 | |
| 223 | def execMaven(self, command): |
| 224 | print(self.execToStdOut(self.mvnbin,command)) |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | ''' |
| 230 | Perform the pure-Python equivalent of in-place `sed` substitution: e.g., |
| 231 | `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`. |
| 232 | ''' |
| 233 | def replaceInFile(self, filename, pattern, replacement): |
| 234 | |
| 235 | # For efficiency, precompile the passed regular expression. |
| 236 | pattern_compiled = re.compile(pattern) |
| 237 | |
| 238 | # For portability, NamedTemporaryFile() defaults to mode "w+b" (i.e., binary |
| 239 | # writing with updating). This is usually a good thing. In this case, |
| 240 | # however, binary writing imposes non-trivial encoding constraints trivially |
| 241 | # resolved by switching to text writing. Let's do that. |
| 242 | with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file: |
| 243 | with open(filename) as src_file: |
| 244 | for line in src_file: |
| 245 | tmp_file.write(pattern_compiled.sub(replacement, line)) |
| 246 | |
| 247 | # Overwrite the original file with the munged temporary file in a |
| 248 | # manner preserving file attributes (e.g., permissions). |
| 249 | shutil.copystat(filename, tmp_file.name) |
| 250 | shutil.move(tmp_file.name, filename) |
| 251 | |
| 252 | def exec(self, bin, params=""): |
| 253 | output = subprocess.Popen( |
| 254 | bin+" "+params, shell=True, stdout=subprocess.PIPE).stdout.read() |
| 255 | return output |
| 256 | def execToStdOut(self, bin, params=""): |
| 257 | process = subprocess.Popen( |
| 258 | (bin+" "+params).split(' '), shell=False) |
| 259 | process.communicate() |
| 260 | |
| 261 | def run(self): |
| 262 | print("starting ONAP odl parent migration") |
| 263 | print("odl src={}".format(self.odlSourcePath)) |
| 264 | print("target ={}".format(self.odlParentPath)) |
| 265 | x = self.migrateInstalledOdlBom() |
| 266 | if self.strict and not x: |
| 267 | exit(1) |
| 268 | x = self.migrateDependenciesBom() |
| 269 | if self.strict and not x: |
| 270 | exit(1) |
| 271 | x = self.migrateDependenciesOdlBom() |
| 272 | if self.strict and not x: |
| 273 | exit(1) |
| 274 | x = self.migrateSetupProperties() |
| 275 | if self.strict and not x: |
| 276 | exit(1) |
| 277 | x = self.setParentValues() |
| 278 | if self.strict and not x: |
| 279 | exit(1) |
| 280 | # self.execMaven('clean install -f {}'.format(self.odlParentPath+'/setup')) |
| 281 | # self.execMaven('clean install -f {}'.format(self.parentPath)) |
| 282 | |
| 283 | parser = argparse.ArgumentParser(description='ONAP odl parent migration tool') |
| 284 | parser.add_argument('--src', type=str, required=True, help='the source folder where odl is located') |
| 285 | parser.add_argument('--group-id', type=str, required=False,default=DEFAULT_PARENT_GROUPID, help='groupid for the parents') |
| 286 | parser.add_argument('--version', type=str, required=False,default=DEFAULT_PARENT_VERSION, help='version') |
| 287 | parser.add_argument('--non-strict', action='store_false' if DEFAULT_STRICT else 'store_true', help='determine stopping script if something cannot be set') |
| 288 | args = parser.parse_args() |
| 289 | |
| 290 | migrator = OdlParentMigrator(args.src,None,args.group_id, args.version, args.non_strict) |
| 291 | migrator.run() |