Add CLM job type
[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 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 import argparse
17 import os
18 import re
19
20 parser = argparse.ArgumentParser()
21 parser.add_argument("project", help="project")
22 parser.add_argument("-d", "--dependencies",
23                     help=("Project dependencies\n\n"
24                           "A comma-seperated (no spaces) list of projects "
25                           "your project depends on. "
26                           "This is used to create an integration job that "
27                           "will trigger when a dependent project-merge job "
28                           "is built successfully.\n\n"
29                           "Example: aaa,controller,yangtools"))
30 parser.add_argument("-t", "--templates", help="Job templates to use")
31 parser.add_argument("-b", "--branches", help="Git Branches to build")
32 parser.add_argument("-j", "--jdks", help="JDKs to build against (for verify jobs)")  # noqa
33 parser.add_argument("-p", "--pom", help="Path to pom.xml to use in Maven build"
34                                         "(Default: pom.xml")
35 parser.add_argument("-g", "--mvn-goals", help="Maven Goals")
36 parser.add_argument("-o", "--mvn-opts", help="Maven Options")
37 parser.add_argument("-a", "--archive-artifacts",
38                     help="Comma-seperated list of patterns of artifacts to "
39                          "archive on build completion. "
40                          "See: http://ant.apache.org/manual/Types/fileset.html")  # noqa
41 parser.add_argument("-z", "--no-cfg", action="store_true",
42                     help=("Disable initializing the project.cfg file."))
43 args = parser.parse_args()
44
45 project = args.project
46 project_dir = os.path.join("jjb", project)
47 project_file = os.path.join(project_dir, "%s.yaml" % project)
48 templates = args.templates  # Defaults to all templates
49 branches = args.branches    # Defaults to "master,stable/helium" if not passed
50 jdks = args.jdks            # Defaults to openjdk7
51 pom = args.pom              # Defaults to pom.xml
52 mvn_goals = args.mvn_goals  # Defaults to "clean install" if not passsed
53 mvn_opts = args.mvn_opts    # Defaults to blank if not passed
54 dependencies = args.dependencies
55 dependent_jobs = ""
56 disabled = "true"   # Always disabled unless project has dependencies
57 email_prefix = "[%s]" % project
58 archive_artifacts = args.archive_artifacts
59
60 # The below 2 variables are used to determine if we should generate a CFG file
61 # for a project automatically.
62 #
63 # no_cfg - is a commandline parameter that can be used by scripts such as the
64 #          jjb-autoupdate-project script to explicitly disable generating CFG
65 #          files.
66 # make_cfg - is a internal variable used to decide if we should try to
67 #            auto generate the CFG file for a project based on optional
68 #            variables passed by the user on the commandline.
69 no_cfg = args.no_cfg
70 make_cfg = False  # Set to true if we need to generate initial CFG file
71 cfg_string = []
72
73 if not templates:
74     templates = "verify,merge,daily,integration,sonar"
75 else:
76     make_cfg = True
77     cfg_string.append("JOB_TEMPLATES: %s" % templates)
78 templates += ",clm"  # ensure we always create a clm job for all projects
79
80 if not branches:
81     branches = "master,stable/helium"
82     sonar_branch = "master"
83 else:
84     make_cfg = True
85     cfg_string.append("BRANCHES: %s" % branches)
86     # For projects who use a different development branch than master
87     sonar_branch = branches.split(",")[0]
88 # Create YAML to list branches to create jobs for
89 streams = "stream:\n"
90 for branch in branches.split(","):
91     streams = streams + ("        - %s:\n"
92                          "            branch: '%s'\n" %
93                          (branch.replace('/', '-'),
94                           branch))
95
96 if not jdks:
97     jdks = "openjdk7"
98 else:
99     make_cfg = True
100     cfg_string.append("JDKS: %s" % jdks)
101 use_jdks = ""
102 for jdk in jdks.split(","):
103     use_jdks += "                - %s\n" % jdk
104
105 if not pom:
106     pom = "pom.xml"
107 else:
108     make_cfg = True
109     cfg_string.append("POM: %s" % pom)
110
111 if not mvn_goals:
112     mvn_goals = ("clean install "
113                  "-V "  # Show Maven / Java version before building
114                  "-Dmaven.repo.local=/tmp/r "
115                  "-Dorg.ops4j.pax.url.mvn.localRepository=/tmp/r ")
116 else:  # User explicitly set MAVEN_OPTS so create CFG
117     make_cfg = True
118     cfg_string.append("MAVEN_GOALS: %s" % mvn_goals)
119
120 if not mvn_opts:
121     mvn_opts = "-Xmx1024m -XX:MaxPermSize=256m"
122 else:  # User explicitly set MAVEN_OPTS so create CFG
123     make_cfg = True
124     cfg_string.append("MAVEN_OPTS: %s" % mvn_opts)
125
126 if not dependencies:
127     dependencies = "odlparent"  # All projects depend on odlparent
128 if dependencies:
129     if dependencies.find("odlparent") < 0:  # If odlparent is not listed add it
130         dependencies = "odlparent," + dependencies
131     make_cfg = True
132     disabled = "false"
133     email_prefix = (email_prefix + " " +
134                     " ".join(['[%s]' % d for d in dependencies.split(",")]))
135     dependent_jobs = ",".join(
136         ['%s-merge-{stream}' % d for d in dependencies.split(",")])
137     cfg_string.append("DEPENDENCIES: %s" % dependencies)
138
139 if not archive_artifacts:
140     archive_artifacts = ""
141 else:
142     cfg_string.append("ARCHIVE: %s" % archive_artifacts)
143     archive_artifacts = ("- archive-artifacts:\n"
144                          "            artifacts: '%s'" % archive_artifacts)
145
146 ##############################
147 # Create configuration start #
148 ##############################
149
150 # Create project directory if it doesn't exist
151 if not os.path.exists(project_dir):
152     os.makedirs(project_dir)
153
154 print("project: %s\n"
155       "branches: %s\n"
156       "goals: %s\n"
157       "options: %s\n"
158       "dependencies: %s\n"
159       "artifacts: %s" %
160       (project,
161        branches,
162        mvn_goals,
163        mvn_opts,
164        dependencies,
165        archive_artifacts,))
166
167 # Create initial project CFG file
168 if not no_cfg and make_cfg:
169     print("Creating %s.cfg file" % project)
170     cfg_file = os.path.join(project_dir, "%s.cfg" % project)
171     with open(cfg_file, "w") as outstream:
172         cfg = "\n".join(cfg_string)
173         outstream.write(cfg)
174
175 # Create initial project YAML file
176 use_templates = templates.split(",")
177 use_templates.insert(0, "project")
178 job_templates_yaml = ""
179 for t in use_templates:
180     if t == "project":  # This is not a job type but is used for templating
181         pass
182     elif t == "sonar" or t == "clm":
183         job_templates_yaml = job_templates_yaml + \
184             "        - '%s-%s'\n" % (project, t)
185     else:
186         job_templates_yaml = job_templates_yaml + \
187             "        - '%s-%s-{stream}'\n" % (project, t)
188
189 with open(project_file, "w") as outfile:
190     for t in use_templates:
191         template_file = "jjb-templates/%s.yaml" % t
192         with open(template_file, "r") as infile:
193             for line in infile:
194                 if not re.match("\s*#", line):
195                     line = re.sub("JOB_TEMPLATES", job_templates_yaml, line)
196                     line = re.sub("PROJECT", project, line)
197                     line = re.sub("DISABLED", disabled, line)
198                     line = re.sub("STREAMS", streams, line)
199                     line = re.sub("JDKS", use_jdks, line)
200                     line = re.sub("POM", pom, line)
201                     line = re.sub("MAVEN_GOALS", mvn_goals, line)
202                     line = re.sub("MAVEN_OPTS", mvn_opts, line)
203                     line = re.sub("DEPENDENCIES", dependent_jobs, line)
204                     line = re.sub("EMAIL_PREFIX", email_prefix, line)
205                     line = re.sub("SONAR_BRANCH", sonar_branch, line)
206                     line = re.sub("ARCHIVE_ARTIFACTS", archive_artifacts, line)
207                 outfile.write(line)
208         outfile.write("\n")