-
Notifications
You must be signed in to change notification settings - Fork 10
/
autocreator.py
executable file
·91 lines (77 loc) · 3.54 KB
/
autocreator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
from mako.template import Template
from mako.lookup import TemplateLookup
import argparse
import yaml
from os import walk
from jenkins import JenkinsUtils
from mako import exceptions
def serveTemplate(templateName, templateLoc, **kwargs):
mylookup = TemplateLookup(directories=templateLoc)
try:
template = mylookup.get_template(templateName)
return template.render(**kwargs)
except:
print exceptions.text_error_template().render()
return None
def parse_args():
parser = argparse.ArgumentParser(description="Jenkins command line autocreator")
parser.add_argument("-d", "--dataLocation", help="The location where your jenkins job data lives", required=True)
parser.add_argument("-job", "--jobType", help="The types of jobs you wish to create", required=True)
parser.add_argument("-url", "--jenkinsURL", help="The URL of your jenkins master", required=True)
parser.add_argument("-a", "--append", help="Optional parameter that will append the value to the end of your job names")
parser.add_argument("-http", "--http", help="Use this to use http instead of https. Defaults to https", action="store_true", default=False)
parser.add_argument("-u", "--username", help="Optional paramater for the username to authenticate against jenkins", required=False, default=None)
parser.add_argument("-p", "--password", help="Optional paramater for the password to authenticate against jenkins", required=False, default=None)
args = parser.parse_args()
return args
def getJobsToBuild(dataDirectory):
files = []
for (dirpath, dirnames, filenames) in walk(dataDirectory):
files.extend(filenames)
break
for fileName in files:
if "default" in fileName:
files.remove(fileName)
return files
# Features to add
# The ability to only build a single job (or list of a few jobs) from a job type instead of all the jobs of that type
# Pass port number separately as a separate optional parameter as this may make doing different jenkins auths easier
# Add logging and a verbosity option. No output right now about what is happening
# Maybe support subdirectories of different "types" so something like acceptance/staging, or acceptance/performance, etc
# Overwrite the job name from within the yml
# Hash the yml and store it locally so we can tell if a job has changed so we dont have to rebuild it
cliArgs = parse_args()
jobType = cliArgs.jobType
dataLoc = cliArgs.dataLocation
if cliArgs.http == True:
connectionType = "http"
else:
connectionType = "https"
if cliArgs.append == None:
appendJobName = ""
else:
appendJobName = cliArgs.append
jobsLoc = dataLoc + "/" + jobType
dataLoc = [dataLoc, jobsLoc]
jenkins = JenkinsUtils(cliArgs.jenkinsURL, connectionType, cliArgs.username, cliArgs.password)
for job in getJobsToBuild(jobsLoc):
jobData = serveTemplate(job, dataLoc)
try:
jobYML = yaml.load(jobData)
except:
print "Yaml failed to parse for file: " + job
continue
jobName = ((jobType + "_" + job).replace(".yml", "") + appendJobName)
print "\nProcessing the template: " + jobName
if jobName.endswith("_build_flow"):
jenkinsXML = serveTemplate('build_flow.txt', 'templates', **jobYML)
print "Creating job: " + jobName
response = jenkins.post_jenkins_job(jenkinsXML, jobName)
else:
jenkinsXML = serveTemplate('jenkins.txt', 'templates', **jobYML)
print "Creating job: " + jobName
response = jenkins.post_jenkins_job(jenkinsXML, jobName)
if response.status != 200:
print "Unable to create job: " + jobName
print " Response code received: " + str(response.status)