Convert CLM job to regular JJB template
[releng/builder.git] / scripts / jjb-init-project.py
1 #!/usr/bin/python
2
3 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
4 ##############################################################################
5 # Copyright (c) 2014, 2015 The Linux Foundation and others.
6 #
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Eclipse Public License v1.0
9 # which accompanies this distribution, and is available at
10 # http://www.eclipse.org/legal/epl-v10.html
11 #
12 # Contributors:
13 #   Thanh Ha (The Linux Foundation) - Initial implementation
14 ##############################################################################
15
16 from collections import OrderedDict
17 import os
18 import re
19
20 import yaml
21
22 import jjblib
23
24
25 args = jjblib.parse_jjb_args()
26
27
28 project = jjblib.Project(args.project)
29 if project.meta_project is not None:
30     project_dir = os.path.join("jjb", project.meta_project, project.project)
31     jenkins_settings = "%s-%s-settings" % (project.meta_project,
32                                            project.project)
33 else:
34     project_dir = os.path.join("jjb", project.project)
35     jenkins_settings = "%s-settings" % project.project
36
37 project_file = os.path.join(project_dir, "%s.yaml" % project)
38 dependent_jobs = ""
39 disabled = "true"   # Always disabled unless project has dependencies
40 email_prefix = "[%s]" % project
41
42
43 if not args.conf:
44     jjblib.create_template_config(project_dir, args)
45     project_conf = os.path.join(project_dir, "%s.cfg" % args.project)
46 else:
47     project_conf = args.conf
48
49 cfg = dict()  # Needed to skip missing project.cfg files
50 if os.path.isfile(project_conf):
51     stream = open(project_conf, "r")
52     cfg = yaml.load(stream)
53
54 ####################
55 # Handle Templates #
56 ####################
57 if cfg.get("JOB_TEMPLATES"):
58     templates = cfg.get("JOB_TEMPLATES")
59 else:
60     templates = (
61         "verify,merge,periodic,distribution,distribution-check,integration,"
62         "sonar")
63 templates += ",validate-autorelease"  # Autorelease validate template
64
65 ##################
66 # Handle Streams #
67 ##################
68 streams = OrderedDict()
69 if cfg.get("STREAMS"):  # this is a list of single-key dicts
70     for stream_dict in cfg.get("STREAMS"):
71         streams.update(stream_dict)
72 else:
73     streams = {"boron": jjblib.STREAM_DEFAULTS["boron"]}
74
75 first_stream = next(iter(streams))  # Keep master branch at top.
76 sonar_branch = streams[first_stream]["branch"]
77 # Create YAML to list branches to create jobs for
78 str_streams = "stream:\n"
79 for stream, options in streams.items():
80     str_streams += ("        - %s:\n"
81                     "            branch: '%s'\n" %
82                     (stream, options["branch"]))
83     str_streams += "            jdk: %s\n" % options["jdks"].split(',')[0].strip()  # noqa
84     str_streams += "            jdks:\n"
85     for jdk in options["jdks"].split(","):
86         str_streams += "                - %s\n" % jdk.strip()
87
88     # Disable autorelease validate job unless project is participating
89     # in autorelease, JJB does not allow flipping a boolean so we have to
90     # flip it here via not operator since the JJB configuration for disabling
91     # a Jenkins Job is "disabled: bool".
92     str_streams += "            disable_autorelease: %s\n" % (not options.get(
93         "autorelease", False))
94
95     # Disable the distribution-check job unless project enables it
96     str_streams += "            disable_distribution_check: %s\n" % (
97         not options.get("distribution-check", True))
98
99 ###############
100 # Handle JDKS #
101 ###############
102 if cfg.get('JDKS'):
103     jdks = cfg.get('JDKS')
104 else:
105     jdks = "openjdk7"
106 use_jdks = ""
107 for jdk in jdks.split(","):
108     use_jdks += "                - %s\n" % jdk
109
110 ##############
111 # Handle POM #
112 ##############
113 if cfg.get('POM'):
114     pom = cfg.get('POM')
115 else:
116     pom = "pom.xml"
117
118 ####################
119 # Handle MVN_GOALS #
120 ####################
121 if cfg.get('MVN_GOALS'):
122     mvn_goals = cfg.get('MVN_GOALS')
123 else:
124     mvn_goals = ("clean install "
125                  "-Dmaven.repo.local=/tmp/r "
126                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
127
128 ###################
129 # Handle MVN_OPTS #
130 ###################
131 if cfg.get('MVN_OPTS'):
132     mvn_opts = cfg.get('MVN_OPTS')
133 else:
134     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
135
136 #######################
137 # Handle DEPENDENCIES #
138 #######################
139 if cfg.get('DEPENDENCIES'):
140     dependencies = cfg.get('DEPENDENCIES')
141     if dependencies.find("odlparent") < 0:  # Add odlparent if not listed
142         dependencies = "odlparent," + dependencies
143     disabled = "false"
144 else:
145     dependencies = None
146     if project.project != "odlparent":  # Odlparent does not depend on itself
147         dependencies = "odlparent"  # All other projects depend on odlparent
148     disabled = "false"
149
150 if dependencies:
151     email_prefix = (email_prefix + " " +
152                 " ".join(['[%s]' % d for d in dependencies.split(",")]))  # noqa
153     dependent_jobs = ",".join(
154         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
155
156 ############################
157 # Handle ARCHIVE_ARTIFACTS #
158 ############################
159
160 always_archive = "**/target/surefire-reports/*-output.txt"
161
162 archive_artifacts = cfg.get('ARCHIVE_ARTIFACTS', '')
163 archive_artifacts = ("- archive-artifacts:\n"
164                      "            artifacts: '%s, %s'" %
165                      (always_archive, archive_artifacts))
166
167 ##############################
168 # Create configuration start #
169 ##############################
170
171 # Create project directory if it doesn't exist
172 if not os.path.exists(project_dir):
173     os.makedirs(project_dir)
174
175 print("project: %s\n"
176       "streams: %s\n"
177       "goals: %s\n"
178       "options: %s\n"
179       "dependencies: %s\n"
180       "artifacts: %s" %
181       (project,
182        str_streams,
183        mvn_goals,
184        mvn_opts,
185        dependencies,
186        archive_artifacts,))
187
188 # Create initial project YAML file
189 use_templates = templates.split(",")
190 use_templates.insert(0, "project")
191 job_templates_yaml = ""
192 for t in use_templates:
193     if t == "project":  # This is not a job type but is used for templating
194         pass
195     elif t == "sonar":
196         job_templates_yaml = job_templates_yaml + \
197             "        - '%s-%s'\n" % (project, t)
198     else:
199         job_templates_yaml = job_templates_yaml + \
200             "        - '%s-%s-{stream}'\n" % (project, t)
201
202 with open(project_file, "w") as outfile:
203     for t in use_templates:
204         template_file = "jjb-templates/%s.yaml" % t
205         with open(template_file, "r") as infile:
206             for line in infile:
207                 if not re.match("\s*#", line):
208                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
209                     line = re.sub("PROJECT_SHORTNAME", project.project, line)
210                     line = re.sub("PROJECT_PATH", project.path, line)
211                     line = re.sub("JENKINS_SETTINGS", jenkins_settings, line)
212                     line = re.sub("DISABLED", disabled, line)
213                     line = re.sub("STREAMS", str_streams, line)
214                     line = re.sub("POM", pom, line)
215                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
216                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
217                     line = re.sub("DEPENDENCIES", dependent_jobs, line)
218                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
219                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
220                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
221                     # The previous command may have created superfluous lines.
222                     # If a line has no non-whitespace, it has to be '\n' only.
223                     line = re.sub(r'^\s+\n', "", line)
224                 outfile.write(line)
225         outfile.write("\n")