417049d3b4b505ffc2d7fc6dd50549ba2c1ab11b
[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 = "verify,merge,periodic,distribution,integration,sonar"
61 templates += ",clm"  # ensure we always create a clm job for all projects
62 templates += ",validate-autorelease"  # Autorelease validate template
63
64 ##################
65 # Handle Streams #
66 ##################
67 streams = OrderedDict()
68 if cfg.get("STREAMS"):  # this is a list of single-key dicts
69     for stream_dict in cfg.get("STREAMS"):
70         streams.update(stream_dict)
71 else:
72     streams = {"boron": jjblib.STREAM_DEFAULTS["boron"]}
73
74 first_stream = streams.iterkeys().next()  # Keep master branch at top.
75 sonar_branch = streams[first_stream]["branch"]
76 # Create YAML to list branches to create jobs for
77 str_streams = "stream:\n"
78 for stream, options in streams.items():
79     str_streams += ("        - %s:\n"
80                     "            branch: '%s'\n" %
81                     (stream, options["branch"]))
82     str_streams += "            jdk: %s\n" % options["jdks"].split(',')[0].strip()  # noqa
83     str_streams += "            jdks:\n"
84     for jdk in options["jdks"].split(","):
85         str_streams += "                - %s\n" % jdk.strip()
86
87     # Disable autorelease validate job unless project is participating
88     # in autorelease, JJB does not allow flipping a boolean so we have to
89     # flip it here via not operator since the JJB configuration for disabling
90     # a Jenkins Job is "disabled: bool".
91     str_streams += "            disable_autorelease: %s\n" % (not options.get(
92         "autorelease", False))
93
94 ###############
95 # Handle JDKS #
96 ###############
97 if cfg.get('JDKS'):
98     jdks = cfg.get('JDKS')
99 else:
100     jdks = "openjdk7"
101 use_jdks = ""
102 for jdk in jdks.split(","):
103     use_jdks += "                - %s\n" % jdk
104
105 ##############
106 # Handle POM #
107 ##############
108 if cfg.get('POM'):
109     pom = cfg.get('POM')
110 else:
111     pom = "pom.xml"
112
113 ####################
114 # Handle MVN_GOALS #
115 ####################
116 if cfg.get('MVN_GOALS'):
117     mvn_goals = cfg.get('MVN_GOALS')
118 else:
119     mvn_goals = ("clean install "
120                  "-V "  # Show Maven / Java version before building
121                  "-Dmaven.repo.local=/tmp/r "
122                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
123
124 ###################
125 # Handle MVN_OPTS #
126 ###################
127 if cfg.get('MVN_OPTS'):
128     mvn_opts = cfg.get('MVN_OPTS')
129 else:
130     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
131
132 #######################
133 # Handle DEPENDENCIES #
134 #######################
135 if cfg.get('DEPENDENCIES'):
136     dependencies = cfg.get('DEPENDENCIES')
137     if dependencies.find("odlparent") < 0:  # Add odlparent if not listed
138         dependencies = "odlparent," + dependencies
139     disabled = "false"
140 else:
141     dependencies = None
142     if project.project != "odlparent":  # Odlparent does not depend on itself
143         dependencies = "odlparent"  # All other projects depend on odlparent
144     disabled = "false"
145
146 if dependencies:
147     email_prefix = (email_prefix + " " +
148                 " ".join(['[%s]' % d for d in dependencies.split(",")]))  # noqa
149     dependent_jobs = ",".join(
150         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
151
152 ############################
153 # Handle ARCHIVE_ARTIFACTS #
154 ############################
155
156 always_archive = "**/target/surefire-reports/*-output.txt"
157
158 archive_artifacts = cfg.get('ARCHIVE_ARTIFACTS', '')
159 archive_artifacts = ("- archive-artifacts:\n"
160                      "            artifacts: '%s, %s'" %
161                      (always_archive, archive_artifacts))
162
163 ##############################
164 # Create configuration start #
165 ##############################
166
167 # Create project directory if it doesn't exist
168 if not os.path.exists(project_dir):
169     os.makedirs(project_dir)
170
171 print("project: %s\n"
172       "streams: %s\n"
173       "goals: %s\n"
174       "options: %s\n"
175       "dependencies: %s\n"
176       "artifacts: %s" %
177       (project,
178        str_streams,
179        mvn_goals,
180        mvn_opts,
181        dependencies,
182        archive_artifacts,))
183
184 # Create initial project YAML file
185 use_templates = templates.split(",")
186 use_templates.insert(0, "project")
187 job_templates_yaml = ""
188 for t in use_templates:
189     if t == "project":  # This is not a job type but is used for templating
190         pass
191     elif t == "sonar":
192         job_templates_yaml = job_templates_yaml + \
193             "        - '%s-%s'\n" % (project, t)
194     else:
195         job_templates_yaml = job_templates_yaml + \
196             "        - '%s-%s-{stream}'\n" % (project, t)
197
198 with open(project_file, "w") as outfile:
199     for t in use_templates:
200         template_file = "jjb-templates/%s.yaml" % t
201         with open(template_file, "r") as infile:
202             for line in infile:
203                 if not re.match("\s*#", line):
204                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
205                     line = re.sub("PROJECT_SHORTNAME", project.project, line)
206                     line = re.sub("PROJECT_PATH", project.path, line)
207                     line = re.sub("JENKINS_SETTINGS", jenkins_settings, line)
208                     line = re.sub("DISABLED", disabled, line)
209                     line = re.sub("STREAMS", str_streams, line)
210                     line = re.sub("POM", pom, line)
211                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
212                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
213                     line = re.sub("DEPENDENCIES", dependent_jobs, line)
214                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
215                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
216                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
217                     # The previous command may have created superfluous lines.
218                     # If a line has no non-whitespace, it has to be '\n' only.
219                     line = re.sub(r'^\s+\n', "", line)
220                 outfile.write(line)
221         outfile.write("\n")