]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.rpmfactory/src/main/java/org/argeo/slc/rpmfactory/core/BuildInMock.java
Make build in mock less monolithic
[gpl/argeo-slc.git] / runtime / org.argeo.slc.rpmfactory / src / main / java / org / argeo / slc / rpmfactory / core / BuildInMock.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.rpmfactory.core;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 import org.apache.commons.exec.Executor;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.core.execution.tasks.SystemCall;
32
33 /** Build an RPM in mock. */
34 public class BuildInMock implements Runnable {
35 private final static Log log = LogFactory.getLog(BuildInMock.class);
36 private final static String NOARCH = "noarch";
37
38 private String rpmPackage = null;
39 private String branch = null;
40 private String arch = NOARCH;
41
42 private RpmFactory factory;
43 private Executor executor;
44
45 private String debuginfoDirName = "debuginfo";
46 private String mockExecutable = "/usr/bin/mock";
47
48 private List<String> preBuildCommands = new ArrayList<String>();
49
50 public void run() {
51 if (!factory.isDeveloperInstance()) {
52 // clean/init
53 SystemCall mockClean = createBaseMockCall();
54 mockClean.arg("--init");
55 mockClean.run();
56 }
57
58 // pre build commands
59 for (String preBuildCmd : preBuildCommands) {
60 SystemCall mockClean = createBaseMockCall();
61 mockClean.arg("--chroot").arg(preBuildCmd);
62 mockClean.run();
63 }
64
65 // actual build
66 SystemCall mockBuild = createBaseMockCall();
67 mockBuild.arg("--scm-enable");
68 mockBuild.arg("--scm-option").arg("package=" + rpmPackage);
69 mockBuild.arg("--no-clean");
70 //
71 //
72 mockBuild.run();
73 //
74
75 // copy RPMs to target directories
76 File stagingDir = factory
77 .getWorkspaceDir(factory.getStagingWorkspace());
78 File srpmDir = new File(stagingDir, "SRPMS");
79 srpmDir.mkdirs();
80 File archDir = null;
81 File debuginfoDir = null;
82 if (!arch.equals(NOARCH)) {
83 archDir = new File(stagingDir, arch);
84 debuginfoDir = new File(archDir, debuginfoDirName);
85 debuginfoDir.mkdirs();
86 }
87
88 Set<File> reposToRecreate = new HashSet<File>();
89 File resultDir = factory.getResultDir(arch);
90 if (resultDir.exists())
91 rpms: for (File file : resultDir.listFiles()) {
92 if (file.isDirectory())
93 continue rpms;
94
95 File[] targetDirs;
96 if (file.getName().contains(".src.rpm"))
97 targetDirs = new File[] { srpmDir };
98 else if (file.getName().contains("-debuginfo-"))
99 targetDirs = new File[] { debuginfoDir };
100 else if (!arch.equals(NOARCH)
101 && file.getName().contains("." + arch + ".rpm"))
102 targetDirs = new File[] { archDir };
103 else if (file.getName().contains(".noarch.rpm")) {
104 List<File> dirs = new ArrayList<File>();
105 for (String arch : factory.getArchs())
106 dirs.add(new File(stagingDir, arch));
107 targetDirs = dirs.toArray(new File[dirs.size()]);
108 } else if (file.getName().contains(".rpm"))
109 throw new SlcException("Don't know where to copy " + file);
110 else {
111 if (log.isTraceEnabled())
112 log.trace("Skip " + file);
113 continue rpms;
114 }
115
116 reposToRecreate.addAll(Arrays.asList(targetDirs));
117 copyToDirs(file, targetDirs);
118 }
119
120 // recreate changed repos
121 for (File repoToRecreate : reposToRecreate) {
122 SystemCall createrepo = new SystemCall();
123 createrepo.arg("createrepo");
124 // sqllite db
125 createrepo.arg("-d");
126 // debuginfo
127 if (!repoToRecreate.getName().equals(debuginfoDirName))
128 createrepo.arg("-x").arg(debuginfoDirName + "/*");
129 // quiet
130 createrepo.arg("-q");
131 createrepo.arg(repoToRecreate.getAbsolutePath());
132
133 createrepo.setExecutor(executor);
134 createrepo.run();
135 log.info("Updated repo " + repoToRecreate);
136 }
137
138 // index staging workspace
139 factory.indexWorkspace(factory.getStagingWorkspace());
140 }
141
142 /** Creates a mock call with all the common options such as config file etc. */
143 protected SystemCall createBaseMockCall() {
144 String mockCfg = factory.getMockConfig(arch);
145 File mockConfigFile = factory.getMockConfigFile(arch, branch);
146
147 // prepare mock call
148 SystemCall mock = new SystemCall();
149
150 if (arch != null)
151 mock.arg("setarch").arg(arch);
152 mock.arg(mockExecutable);
153 mock.arg("-v");
154 mock.arg("--configdir=" + mockConfigFile.getAbsoluteFile().getParent());
155 if (arch != null)
156 mock.arg("--arch=" + arch);
157 mock.arg("-r").arg(mockCfg);
158
159 mock.setLogCommand(true);
160 mock.setExecutor(executor);
161
162 return mock;
163 }
164
165 protected void copyToDirs(File file, File[] dirs) {
166 for (File dir : dirs) {
167 try {
168 FileUtils.copyFileToDirectory(file, dir);
169 if (log.isDebugEnabled())
170 log.debug(file + " => " + dir);
171 } catch (IOException e) {
172 throw new SlcException("Cannot copy " + file + " to " + dir, e);
173 }
174 }
175 }
176
177 public void setArch(String arch) {
178 this.arch = arch;
179 }
180
181 public void setRpmPackage(String rpmPackage) {
182 this.rpmPackage = rpmPackage;
183 }
184
185 public void setBranch(String branch) {
186 this.branch = branch;
187 }
188
189 public void setFactory(RpmFactory env) {
190 this.factory = env;
191 }
192
193 public void setExecutor(Executor executor) {
194 this.executor = executor;
195 }
196
197 public void setMockExecutable(String mockExecutable) {
198 this.mockExecutable = mockExecutable;
199 }
200
201 public void setPreBuildCommands(List<String> preBuildCommands) {
202 this.preBuildCommands = preBuildCommands;
203 }
204
205 }