2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.rpmfactory
.core
;
19 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
25 import javax
.jcr
.Node
;
26 import javax
.jcr
.Repository
;
27 import javax
.jcr
.RepositoryException
;
28 import javax
.jcr
.Session
;
30 import org
.apache
.commons
.io
.FileUtils
;
31 import org
.argeo
.jcr
.JcrUtils
;
32 import org
.argeo
.slc
.SlcConstants
;
33 import org
.argeo
.slc
.SlcException
;
34 import org
.argeo
.slc
.core
.execution
.tasks
.SystemCall
;
35 import org
.argeo
.slc
.repo
.NodeIndexerVisitor
;
36 import org
.argeo
.slc
.rpmfactory
.RpmRepository
;
39 * Defines a build environment. This information is typically used by other
40 * components performing the various actions related to RPM build.
42 public class RpmFactory
{
43 private Repository rpmRepository
;
44 private Repository distRepository
;
47 private List
<RpmRepository
> repositories
= new ArrayList
<RpmRepository
>();
48 private List
<String
> archs
= new ArrayList
<String
>();
50 private String rpmBase
= "/mnt/slc/repos/rpm";
51 private String distBase
= "/mnt/slc/repos/dist";
52 private String mockVar
= "/var/lib/mock";
53 private String mockEtc
= "/etc/mock";
55 private String gitWorkspace
= "git";
57 private String localUrlBase
= "http://localhost:7070/";
59 private Boolean withTestingRepository
= false;
61 private String yumConfigMainSection
= "cachedir=/var/cache/yum\n"
62 + "debuglevel=1\n" + "reposdir=/dev/null\n"
63 + "logfile=/var/log/yum.log\n" + "retries=20\n" + "obsoletes=1\n"
64 + "gpgcheck=0\n" + "assumeyes=1\n" + "syslog_ident=mock\n"
65 + "syslog_device=\n" + "http_caching=none\n";
67 private String defaultMacroFiles
= "/usr/lib/rpm/macros:"
68 + "/usr/lib/rpm/ia32e-linux/macros:"
69 + "/usr/lib/rpm/redhat/macros:" + "/etc/rpm/macros.*:"
70 + "/etc/rpm/macros:" + "/etc/rpm/ia32e-linux/macros:"
72 private Map
<String
, String
> rpmmacros
= new HashMap
<String
, String
>();
75 private String proxiedReposBase
;
76 private String managedReposBase
;
78 private String stagingWorkspace
;
79 private String testingWorkspace
;
80 private String stableWorkspace
;
82 private File rpmFactoryBaseDir
;
83 private File mockConfDir
;
84 private File yumConfDir
;
88 proxiedReposBase
= localUrlBase
+ "repo/rpm/";
89 managedReposBase
= localUrlBase
+ "data/public/rpm/";
92 rpmFactoryBaseDir
.mkdirs();
93 mockConfDir
= new File(rpmFactoryBaseDir
.getPath() + "/conf/mock");
95 yumConfDir
= new File(rpmFactoryBaseDir
.getPath() + "/conf/yum");
98 // managed repositories
99 stagingWorkspace
= id
+ "-staging";
100 if (withTestingRepository
)
101 testingWorkspace
= id
+ "-testing";
102 stableWorkspace
= id
;
104 initDistWorkspace(stableWorkspace
);
106 initRpmWorkspace(stagingWorkspace
);
107 if (withTestingRepository
)
108 initRpmWorkspace(testingWorkspace
);
109 initRpmWorkspace(stableWorkspace
);
112 protected void initRpmWorkspace(String workspace
) {
113 Session session
= null;
115 session
= JcrUtils
.loginOrCreateWorkspace(rpmRepository
, workspace
);
116 JcrUtils
.addPrivilege(session
, "/", "anonymous", "jcr:read");
117 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
120 for (String arch
: archs
) {
121 Node archFolder
= JcrUtils
.mkfolders(session
, "/" + arch
);
123 if (!archFolder
.hasNode("repodata")) {
124 File workspaceDir
= getWorkspaceDir(workspace
);
125 // touch a file in order to make sue this is properly
127 File touch
= new File(workspaceDir
, ".touch");
128 touch
.createNewFile();
131 SystemCall createrepo
= new SystemCall();
132 createrepo
.arg("createrepo");
133 createrepo
.arg("-q");
134 File archDir
= new File(workspaceDir
, arch
);
135 createrepo
.arg(archDir
.getAbsolutePath());
139 } catch (Exception e
) {
140 throw new SlcException("Cannot initialize workspace " + workspace
,
143 JcrUtils
.logoutQuietly(session
);
147 protected void initGitWorkspace() {
148 Session session
= null;
150 session
= JcrUtils
.loginOrCreateWorkspace(rpmRepository
,
152 JcrUtils
.addPrivilege(session
, "/", "anonymous", "jcr:read");
153 JcrUtils
.addPrivilege(session
, "/", SlcConstants
.ROLE_SLC
,
155 } catch (Exception e
) {
156 throw new SlcException("Cannot initialize workspace "
159 JcrUtils
.logoutQuietly(session
);
163 protected void initDistWorkspace(String workspace
) {
164 Session session
= null;
167 .loginOrCreateWorkspace(distRepository
, workspace
);
168 JcrUtils
.addPrivilege(session
, "/", "anonymous", "jcr:read");
169 } catch (RepositoryException e
) {
170 throw new SlcException("Cannot initialize workspace " + workspace
,
173 JcrUtils
.logoutQuietly(session
);
177 public void destroy() {
181 public String
generateMockConfigFile(String arch
, String branch
) {
182 StringBuffer buf
= new StringBuffer();
184 buf
.append("config_opts['root'] = '" + getIdWithArch(arch
) + "'\n");
185 buf
.append("config_opts['target_arch'] = '" + arch
+ "'\n");
186 buf
.append("config_opts['legal_host_arches'] = ('" + arch
+ "',)\n");
187 buf
.append("config_opts['chroot_setup_cmd'] = 'groupinstall buildsys-build'\n");
188 // buf.append("config_opts['dist'] = 'el6'\n");
189 buf
.append("config_opts['plugin_conf']['yum_cache_enable'] = False\n");
191 buf
.append("config_opts['scm'] = False\n");
192 buf
.append("config_opts['scm_opts']['method'] = 'git'\n");
193 buf
.append("config_opts['scm_opts']['spec'] = 'SCM_PKG.spec'\n");
194 buf
.append("config_opts['scm_opts']['ext_src_dir'] = '"
195 + getSourcesDir().getAbsolutePath() + "'\n");
196 buf
.append("config_opts['scm_opts']['git_timestamps'] = True\n");
197 buf
.append("config_opts['scm_opts']['git_get'] = 'git clone "
198 + (branch
!= null ?
"-b " + branch
: "") + " "
199 + getGitBaseUrl() + "/SCM_PKG.git SCM_PKG'\n");
201 buf
.append("\nconfig_opts['yum.conf'] = \"\"\"\n");
202 buf
.append(generateYumConfigFile(arch
)).append('\n');
203 buf
.append("\"\"\"\n");
204 return buf
.toString();
207 public String
generateYumConfigFile(String arch
) {
208 StringBuffer buf
= new StringBuffer();
209 buf
.append("[main]\n");
210 buf
.append(yumConfigMainSection
).append('\n');
212 for (RpmRepository repository
: repositories
) {
213 buf
.append('[').append(repository
.getId()).append("]\n");
214 if (repository
instanceof ThirdPartyRpmRepository
) {
215 buf
.append("#baseurl=").append(repository
.getUrl())
216 .append(arch
).append('/').append("\n");
217 buf
.append("baseurl=").append(proxiedReposBase
)
218 .append(repository
.getId()).append('/').append(arch
)
219 .append('/').append("\n");
224 addManagedRepository(buf
, stagingWorkspace
, arch
);
225 if (withTestingRepository
)
226 addManagedRepository(buf
, testingWorkspace
, arch
);
227 addManagedRepository(buf
, stableWorkspace
, arch
);
228 return buf
.toString();
231 protected void addManagedRepository(StringBuffer buf
, String workspace
,
233 buf
.append('[').append(workspace
).append("]\n");
234 buf
.append("baseurl=").append(managedReposBase
).append(workspace
)
235 .append('/').append(arch
).append('/').append("\n");
238 /** Creates a mock config file. */
239 public File
getMockConfigFile(String arch
, String branch
) {
240 File mockSiteDefaultsFile
= new File(mockConfDir
, "site-defaults.cfg");
241 File mockLoggingFile
= new File(mockConfDir
, "logging.ini");
242 File mockConfigFile
= new File(mockConfDir
, getIdWithArch(arch
)
245 if (!mockSiteDefaultsFile
.exists())
246 mockSiteDefaultsFile
.createNewFile();
247 if (!mockLoggingFile
.exists())
248 FileUtils
.copyFile(new File(mockEtc
+ "/logging.ini"),
251 FileUtils
.writeStringToFile(mockConfigFile
,
252 generateMockConfigFile(arch
, branch
));
253 return mockConfigFile
;
254 } catch (IOException e
) {
255 throw new SlcException("Cannot write mock config file to "
256 + mockConfigFile
, e
);
260 /** Creates a yum config file. */
261 public File
getYumConfigFile(String arch
) {
262 File yumConfigFile
= new File(yumConfDir
, getIdWithArch(arch
) + ".conf");
264 FileUtils
.writeStringToFile(yumConfigFile
,
265 generateYumConfigFile(arch
));
266 return yumConfigFile
;
267 } catch (IOException e
) {
268 throw new SlcException("Cannot write yum config file to "
273 public File
getResultDir(String arch
) {
274 return new File(mockVar
+ "/" + getIdWithArch(arch
) + "/result");
277 public File
getWorkspaceDir(String workspace
) {
278 return new File(rpmBase
+ "/" + workspace
);
281 public File
getSourcesDir() {
282 return new File(distBase
+ "/" + stagingWorkspace
);
285 public String
getMockConfig(String arch
) {
286 return getIdWithArch(arch
);
289 public String
getIdWithArch(String arch
) {
290 return id
+ "-" + arch
;
293 public String
getGitBaseUrl() {
294 return managedReposBase
+ gitWorkspace
;
297 public void indexWorkspace(String workspace
) {
298 Session session
= null;
300 session
= rpmRepository
.login(workspace
);
301 session
.getRootNode().accept(
302 new NodeIndexerVisitor(new RpmIndexer()));
303 } catch (RepositoryException e
) {
304 throw new SlcException("Cannot index workspace " + workspace
, e
);
306 JcrUtils
.logoutQuietly(session
);
310 /** Write (topdir)/rpmmacros and (topdir)/rpmrc */
311 public void writeRpmbuildConfigFiles(File topdir
) {
312 writeRpmbuildConfigFiles(topdir
, new File(topdir
, "rpmmacros"),
313 new File(topdir
, "rpmrc"));
316 public void writeRpmbuildConfigFiles(File topdir
, File rpmmacroFile
,
319 List
<String
> macroLines
= new ArrayList
<String
>();
320 macroLines
.add("%_topdir " + topdir
.getCanonicalPath());
321 for (String macroKey
: rpmmacros
.keySet()) {
322 macroLines
.add(macroKey
+ " " + rpmmacros
.get(macroKey
));
324 FileUtils
.writeLines(rpmmacroFile
, macroLines
);
326 List
<String
> rpmrcLines
= new ArrayList
<String
>();
327 rpmrcLines
.add("include: /usr/lib/rpm/rpmrc");
328 rpmrcLines
.add("macrofiles: " + defaultMacroFiles
+ ":"
329 + rpmmacroFile
.getCanonicalPath());
330 FileUtils
.writeLines(rpmrcFile
, rpmrcLines
);
331 } catch (IOException e
) {
332 throw new SlcException("Cannot write rpmbuild config files", e
);
337 public Map
<String
, String
> getRpmmacros() {
341 public void setRpmmacros(Map
<String
, String
> rpmmacros
) {
342 this.rpmmacros
= rpmmacros
;
345 public String
getDefaultMacroFiles() {
346 return defaultMacroFiles
;
349 public void setDefaultMacroFiles(String defaultMacroFiles
) {
350 this.defaultMacroFiles
= defaultMacroFiles
;
353 public void setArchs(List
<String
> archs
) {
357 public List
<String
> getArchs() {
361 public void setRpmBase(String stagingBase
) {
362 this.rpmBase
= stagingBase
;
365 public void setId(String id
) {
369 public void setMockVar(String mockVar
) {
370 this.mockVar
= mockVar
;
373 public void setRpmRepository(Repository rpmRepository
) {
374 this.rpmRepository
= rpmRepository
;
377 public void setDistRepository(Repository distRepository
) {
378 this.distRepository
= distRepository
;
381 public void setLocalUrlBase(String localUrlBase
) {
382 this.localUrlBase
= localUrlBase
;
385 public void setYumConfigMainSection(String yumConfigMainSection
) {
386 this.yumConfigMainSection
= yumConfigMainSection
;
389 public void setRepositories(List
<RpmRepository
> repositories
) {
390 this.repositories
= repositories
;
393 public void setRpmFactoryBaseDir(File rpmFactoryBaseDir
) {
394 this.rpmFactoryBaseDir
= rpmFactoryBaseDir
;
397 public String
getStagingWorkspace() {
398 return stagingWorkspace
;
401 public String
getTestingWorkspace() {
402 return testingWorkspace
;
405 public String
getStableWorkspace() {
406 return stableWorkspace
;
409 public void setWithTestingRepository(Boolean withTestingRepository
) {
410 this.withTestingRepository
= withTestingRepository
;