Browse code

Package builder: refactor PackageBuilder class

Remove PackageBuilderChroot and PackageBuilderContainer subclasses
since there is no more differencies between them.

Change-Id: Ibaeb69db1853249e11b53419c20322b15b83a37f
Reviewed-on: http://photon-jenkins.eng.vmware.com:8082/6145
Tested-by: gerrit-photon <photon-checkins@vmware.com>
Reviewed-by: Anish Swaminathan <anishs@vmware.com>

Alexey Makhalov authored on 2018/11/09 07:21:41
Showing 5 changed files
... ...
@@ -9,8 +9,8 @@ from SpecData import SPECS
9 9
 from StringUtils import StringUtils
10 10
 from Sandbox import Chroot, Container
11 11
 
12
-class PackageBuilderBase(object):
13
-    def __init__(self, mapPackageToCycles, pkgBuildType):
12
+class PackageBuilder(object):
13
+    def __init__(self, mapPackageToCycles, sandboxType):
14 14
         # will be initialized in buildPackageFunction()
15 15
         self.logName = None
16 16
         self.logPath = None
... ...
@@ -18,13 +18,14 @@ class PackageBuilderBase(object):
18 18
         self.package = None
19 19
         self.version = None
20 20
         self.doneList = None
21
+        self.sandboxType = sandboxType
22
+        self.sandbox = None
21 23
         self.mapPackageToCycles = mapPackageToCycles
22 24
         self.listNodepsPackages = ["glibc", "gmp", "zlib", "file", "binutils", "mpfr",
23 25
                                    "mpc", "gcc", "ncurses", "util-linux", "groff", "perl",
24 26
                                    "texinfo", "rpm", "openssl", "go"]
25
-        self.pkgBuildType = pkgBuildType
26 27
 
27
-    def buildPackageFunction(self, pkg, doneList):
28
+    def build(self, pkg, doneList):
28 29
         packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
29 30
         #do not build if RPM is already built
30 31
         #test only if the package is in the testForceRPMS with rpmCheck
... ...
@@ -42,6 +43,54 @@ class PackageBuilderBase(object):
42 42
             self.logger.exception(e)
43 43
             raise e
44 44
 
45
+    def _buildPackage(self):
46
+        try:
47
+            self.sandbox.create(self.package + "-" + self.version)
48
+
49
+            tUtils = ToolChainUtils(self.logName, self.logPath)
50
+            if self.sandbox.hasToolchain():
51
+                tUtils.installCustomToolChainRPMS(self.sandbox, self.package, self.version)
52
+            else:
53
+                tUtils.installToolChainRPMS(self.sandbox, self.package, self.version)
54
+
55
+            listDependentPackages, listTestPackages, listInstalledPackages, listInstalledRPMs = (
56
+                self._findDependentPackagesAndInstalledRPM(self.sandbox))
57
+
58
+            pkgUtils = PackageUtils(self.logName, self.logPath)
59
+
60
+            if listDependentPackages:
61
+                self.logger.debug("Installing the build time dependent packages......")
62
+                for pkg in listDependentPackages:
63
+                    packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
64
+                    self._installPackage(pkgUtils, packageName, packageVersion, self.sandbox, self.logPath,listInstalledPackages, listInstalledRPMs)
65
+                for pkg in listTestPackages:
66
+                    flag = False
67
+                    packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
68
+                    for depPkg in listDependentPackages:
69
+                        depPackageName, depPackageVersion = StringUtils.splitPackageNameAndVersion(depPkg)
70
+                        if depPackageName == packageName:
71
+                            flag = True
72
+                            break;
73
+                    if flag == False:
74
+                        self._installPackage(pkgUtils, packageName,packageVersion, self.sandbox, self.logPath,listInstalledPackages, listInstalledRPMs)
75
+                pkgUtils.installRPMSInOneShot(self.sandbox)
76
+                self.logger.debug("Finished installing the build time dependent packages....")
77
+
78
+            pkgUtils.adjustGCCSpecs(self.sandbox, self.package, self.version)
79
+            pkgUtils.buildRPMSForGivenPackage(self.sandbox, self.package, self.version,
80
+                                              self.logPath)
81
+            self.logger.debug("Successfully built the package: " + self.package)
82
+        except Exception as e:
83
+            self.logger.error("Failed while building package: " + self.package)
84
+            self.logger.debug("Sandbox: " + self.sandbox.getID() +
85
+                              " not deleted for debugging.")
86
+            logFileName = os.path.join(self.logPath, self.package + ".log")
87
+            fileLog = os.popen('tail -n 100 ' + logFileName).read()
88
+            self.logger.info(fileLog)
89
+            raise e
90
+        if self.sandbox:
91
+            self.sandbox.destroy()
92
+
45 93
     def _buildPackagePrepareFunction(self, package, version, doneList):
46 94
         self.package = package
47 95
         self.version = version
... ...
@@ -53,6 +102,15 @@ class PackageBuilderBase(object):
53 53
         self.logger = Logger.getLogger(self.logName, self.logPath, constants.logLevel)
54 54
         self.doneList = doneList
55 55
 
56
+        if self.sandboxType == "chroot":
57
+            sandbox = Chroot(self.logger)
58
+        elif self.sandboxType == "container":
59
+            sandbox = Container(self.logger)
60
+        else:
61
+            raise Exception("Unknown sandbox type: " + sandboxType)
62
+
63
+        self.sandbox = sandbox
64
+
56 65
     def _findPackageNameAndVersionFromRPMFile(self, rpmfile):
57 66
         rpmfile = os.path.basename(rpmfile)
58 67
         releaseindex = rpmfile.rfind("-")
... ...
@@ -142,115 +200,3 @@ class PackageBuilderBase(object):
142 142
             listTestPackages=list(set(testPackages))
143 143
             listDependentPackages = list(set(listDependentPackages))
144 144
         return listDependentPackages, listTestPackages, listInstalledPackages, listInstalledRPMs
145
-
146
-class PackageBuilderContainer(PackageBuilderBase):
147
-    def __init__(self, mapPackageToCycles, pkgBuildType):
148
-        PackageBuilderBase.__init__(self, mapPackageToCycles, pkgBuildType)
149
-
150
-    def _buildPackage(self):
151
-        #should initialize a logger based on package name
152
-        containerTaskName = "build-" + self.package + "-" + self.version
153
-        container = None
154
-        try:
155
-            container = Container(self.logger)
156
-            container.create(containerTaskName)
157
-
158
-            tcUtils = ToolChainUtils(self.logName, self.logPath)
159
-            tcUtils.installCustomToolChainRPMS(container, self.package, self.version)
160
-
161
-            listDependentPackages, listTestPackages, listInstalledPackages, listInstalledRPMs = (
162
-                self._findDependentPackagesAndInstalledRPM(container))
163
-
164
-            pkgUtils = PackageUtils(self.logName, self.logPath)
165
-
166
-            if listDependentPackages:
167
-                self.logger.debug("Installing the build time dependent packages......")
168
-                for pkg in listDependentPackages:
169
-                    packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
170
-                    self._installPackage(pkgUtils, packageName, packageVersion, container, self.logPath,listInstalledPackages, listInstalledRPMs)
171
-                for pkg in listTestPackages:
172
-                    flag = False
173
-                    packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
174
-                    for depPkg in listDependentPackages:
175
-                        depPackageName, depPackageVersion = StringUtils.splitPackageNameAndVersion(depPkg)
176
-                        if depPackageName == packageName:
177
-                            flag = True
178
-                            break;
179
-                    if flag == False:
180
-                        self._installPackage(pkgUtils, packageName,packageVersion, container, self.logPath,listInstalledPackages, listInstalledRPMs)
181
-                pkgUtils.installRPMSInOneShot(container)
182
-                self.logger.debug("Finished installing the build time dependent packages....")
183
-
184
-            self.logger.debug("BuildContainer-buildPackage: Start building the package: " +
185
-                             self.package)
186
-            pkgUtils.adjustGCCSpecs(container, self.package, self.version)
187
-            pkgUtils.buildRPMSForGivenPackage(container, self.package, self.version, self.logPath)
188
-            self.logger.debug("BuildContainer-buildPackage: Successfully built the package: " +
189
-                             self.package)
190
-        except Exception as e:
191
-            self.logger.error("Failed while building package:" + self.package)
192
-            if container is not None:
193
-                self.logger.debug("Container " + container.getID() +
194
-                                  " retained for debugging.")
195
-            logFileName = os.path.join(self.logPath, self.package + ".log")
196
-            fileLog = os.popen('tail -n 20 ' + logFileName).read()
197
-            self.logger.debug(fileLog)
198
-            raise e
199
-
200
-        # Remove the container
201
-        if container:
202
-            container.destroy()
203
-
204
-class PackageBuilderChroot(PackageBuilderBase):
205
-    def __init__(self, mapPackageToCycles, pkgBuildType):
206
-        PackageBuilderBase.__init__(self, mapPackageToCycles, pkgBuildType)
207
-
208
-    def _buildPackage(self):
209
-        chroot = None
210
-        try:
211
-            chroot = Chroot(self.logger)
212
-            chroot.create(self.package + "-" + self.version)
213
-
214
-            tUtils = ToolChainUtils(self.logName, self.logPath)
215
-            tUtils.installToolChainRPMS(chroot, self.package,
216
-                                        self.version, self.logPath,
217
-                                        usePublishedRPMS=True,
218
-                                        availablePackages=self.doneList)
219
-
220
-            listDependentPackages, listTestPackages, listInstalledPackages, listInstalledRPMs = (
221
-                self._findDependentPackagesAndInstalledRPM(chroot))
222
-
223
-            pkgUtils = PackageUtils(self.logName, self.logPath)
224
-
225
-            if listDependentPackages:
226
-                self.logger.debug("Installing the build time dependent packages......")
227
-                for pkg in listDependentPackages:
228
-                    packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
229
-                    self._installPackage(pkgUtils, packageName, packageVersion, chroot, self.logPath,listInstalledPackages, listInstalledRPMs)
230
-                for pkg in listTestPackages:
231
-                    flag = False
232
-                    packageName, packageVersion = StringUtils.splitPackageNameAndVersion(pkg)
233
-                    for depPkg in listDependentPackages:
234
-                        depPackageName, depPackageVersion = StringUtils.splitPackageNameAndVersion(depPkg)
235
-                        if depPackageName == packageName:
236
-                            flag = True
237
-                            break;
238
-                    if flag == False:
239
-                        self._installPackage(pkgUtils, packageName,packageVersion, chroot, self.logPath,listInstalledPackages, listInstalledRPMs)
240
-                pkgUtils.installRPMSInOneShot(chroot)
241
-                self.logger.debug("Finished installing the build time dependent packages....")
242
-
243
-            pkgUtils.adjustGCCSpecs(chroot, self.package, self.version)
244
-            pkgUtils.buildRPMSForGivenPackage(chroot, self.package, self.version,
245
-                                              self.logPath)
246
-            self.logger.debug("Successfully built the package:" + self.package)
247
-        except Exception as e:
248
-            self.logger.error("Failed while building package:" + self.package)
249
-            self.logger.debug("Chroot: " + chroot.getPath() +
250
-                              " not deleted for debugging.")
251
-            logFileName = os.path.join(self.logPath, self.package + ".log")
252
-            fileLog = os.popen('tail -n 100 ' + logFileName).read()
253
-            self.logger.info(fileLog)
254
-            raise e
255
-        if chroot:
256
-            chroot.destroy()
... ...
@@ -221,15 +221,15 @@ class PackageManager(object):
221 221
             if chroot:
222 222
                 chroot.destroy()
223 223
             raise e
224
-        self.logger.debug("createBuildContainer: " + chroot.getPath())
224
+        self.logger.debug("createBuildContainer: " + chroot.getID())
225 225
 
226 226
         # Create photon build container using toolchain chroot
227 227
         chroot.unmountAll()
228 228
         #TODO: Coalesce logging
229 229
         cmdUtils = CommandUtils()
230
-        cmd = "cd " + chroot.getPath() + " && tar -czf ../tcroot.tar.gz ."
230
+        cmd = "cd " + chroot.getID() + " && tar -czf ../tcroot.tar.gz ."
231 231
         cmdUtils.runCommandInShell(cmd, logfn=self.logger.debug)
232
-        cmd = "mv " + chroot.getPath() + "/../tcroot.tar.gz ."
232
+        cmd = "mv " + chroot.getID() + "/../tcroot.tar.gz ."
233 233
         cmdUtils.runCommandInShell(cmd, logfn=self.logger.debug)
234 234
         #TODO: Container name, docker file name from constants.
235 235
         self.dockerClient.images.build(tag=constants.buildContainerImage,
... ...
@@ -23,6 +23,12 @@ class Sandbox(object):
23 23
     def put(self, src, dest):
24 24
         pass
25 25
 
26
+    def getID(self):
27
+        pass
28
+
29
+    def hasToolchain(self):
30
+        return False
31
+
26 32
 class Chroot(Sandbox):
27 33
     def __init__(self, logger):
28 34
         Sandbox.__init__(self, logger)
... ...
@@ -32,7 +38,7 @@ class Chroot(Sandbox):
32 32
                                    " " + constants.rpmPath)
33 33
         self.chrootCmdPrefix = None
34 34
 
35
-    def getPath(self):
35
+    def getID(self):
36 36
         return self.chrootID
37 37
 
38 38
     def create(self, chrootName):
... ...
@@ -179,7 +185,7 @@ class Container(Sandbox):
179 179
         # Prepare an empty chroot environment to let docker use the BUILD folder.
180 180
         # This avoids docker using overlayFS which will cause make check failure.
181 181
 
182
-#            chroot.getPath() + constants.topDirPath + "/BUILD": {'bind': constants.topDirPath + "/BUILD",
182
+#            chroot.getID() + constants.topDirPath + "/BUILD": {'bind': constants.topDirPath + "/BUILD",
183 183
 #                                                         'mode': 'rw'},
184 184
             constants.dockerUnixSocket: {'bind': constants.dockerUnixSocket, 'mode': 'rw'}
185 185
             }
... ...
@@ -239,3 +245,6 @@ class Container(Sandbox):
239 239
         copyCmd = "docker cp " + src + " " + self.containerID.short_id + ":" + dest
240 240
         CommandUtils.runCommandInShell(copyCmd)
241 241
 
242
+    def hasToolchain(self):
243
+        return True
244
+
... ...
@@ -156,13 +156,13 @@ class ToolChainUtils(object):
156 156
 
157 157
         self.logger.debug(packages)
158 158
         cmd = (self.rpmCommand + " -i -v --nodeps --noorder --force --root " +
159
-               chroot.getPath() +" --define \'_dbpath /var/lib/rpm\' "+ rpmFiles)
159
+               chroot.getID() +" --define \'_dbpath /var/lib/rpm\' "+ rpmFiles)
160 160
         retVal = CommandUtils.runCommandInShell(cmd, logfn=self.logger.debug)
161 161
         if retVal != 0:
162 162
             self.logger.debug("Command Executed:" + cmd)
163 163
             self.logger.error("Installing tool chain  failed")
164 164
             raise Exception("RPM installation failed")
165
-        self.logger.debug("Successfully installed default Tool Chain RPMS in Chroot:" + chroot.getPath())
165
+        self.logger.debug("Successfully installed default toolchain RPMS in Chroot:" + chroot.getID())
166 166
         if packageName:
167 167
             self.installCustomToolChainRPMS(chroot, packageName, packageVersion)
168 168
 
... ...
@@ -197,4 +197,3 @@ class ToolChainUtils(object):
197 197
             self.logger.debug("Command Executed:" + cmd)
198 198
             self.logger.error("Installing custom toolchains failed")
199 199
             raise Exception("RPM installation failed")
200
-
... ...
@@ -1,6 +1,5 @@
1 1
 import threading
2
-from PackageBuilder import PackageBuilderChroot
3
-from PackageBuilder import PackageBuilderContainer
2
+from PackageBuilder import PackageBuilder
4 3
 import Scheduler
5 4
 import ThreadPool
6 5
 
... ...
@@ -24,14 +23,10 @@ class WorkerThread(threading.Thread):
24 24
             doneList = Scheduler.Scheduler.getDoneList()
25 25
             if pkg is None:
26 26
                 break
27
-            if self.pkgBuildType == "chroot":
28
-                pkgBuilder = PackageBuilderChroot(self.mapPackageToCycle,
29
-                                                  self.pkgBuildType)
30
-            elif self.pkgBuildType == "container":
31
-                pkgBuilder = PackageBuilderContainer(self.mapPackageToCycle,
32
-                                                     self.pkgBuildType)
27
+            pkgBuilder = PackageBuilder(self.mapPackageToCycle,
28
+                                              self.pkgBuildType)
33 29
             try:
34
-                pkgBuilder.buildPackageFunction(pkg, doneList)
30
+                pkgBuilder.build(pkg, doneList)
35 31
             except Exception as e:
36 32
                 self.logger.exception(e)
37 33
                 buildThreadFailed = True