diff --git a/autojenkins/jobs.py b/autojenkins/jobs.py index 35e9dbb..90746fd 100644 --- a/autojenkins/jobs.py +++ b/autojenkins/jobs.py @@ -14,7 +14,7 @@ class Jenkins(object): - def __init__(self, base_url='http://jenkins.pe.local'): + def __init__(self, base_url): self.ROOT = base_url def _build_url(self, command, *args): @@ -64,11 +64,11 @@ def create_copy(self, jobname, template_job, enable=True, **context): Create a job from a template job. """ config = self.get_config_xml(template_job) - with open('config.xml', 'w') as file: - file.write(config) + # with open('config.xml', 'w') as file: + # file.write(config) + # remove stupid quotes added by Jenkins - config = config.replace('"{{branch}}"', - '{{branch}}') + config = config.replace('"{{branch}}"', '{{branch}}') template_config = Template(config) config = template_config.render(**context) diff --git a/autojenkins/run.py b/autojenkins/run.py new file mode 100644 index 0000000..073041a --- /dev/null +++ b/autojenkins/run.py @@ -0,0 +1,59 @@ +import optparse + +from autojenkins import Jenkins + + +def create_opts_parser(): + usage = "Usage: %prog jobname [options]" + desc = 'Run autojenkins to create a job.' + parser = optparse.OptionParser(description=desc, usage=usage) + #parser.add_option('jobname', + # help='the name of a job in jenkins') + parser.add_option('--repo', + help='the repository name in github') + parser.add_option('--branch', + help='the branch name') + parser.add_option('--package', + help='the main python package (that contains manage.py)') + parser.add_option('-t', '--template', default='template', + help='the template job to copy from') + parser.add_option('-b', '--build', + action="store_true", dest="build", default=False, + help='start a build right after creation') + return parser + + +def run_jenkins(jobname, options): + is_not_none = lambda res, opt: res and getattr(options, opt) is not None + all_options_ok = reduce(is_not_none, ['repo', 'branch', 'package']) + if all_options_ok: + + print ("""Creating job '{0}' from template '{1}' for: + - repo: {2} + - branch: {3} + - package: {4} + """.format(jobname, options.template, options.repo, options.branch, + options.package)) + + jenkins = Jenkins('http://jenkins.pe.local') + response = jenkins.create_copy(jobname, options.template, + repo=options.repo, + branch=options.branch, + package=options.package) + print('Status: ' + response.status_code) + if response.status_code == 200 and options.build: + print('Triggering build.') + j.build(jobname) + return all_options_ok + + +if __name__ == '__main__': + parser = create_opts_parser() + (options, args) = parser.parse_args() + if len(args) == 1: + jobname = args[0] + else: + jobname = '{0}-{1}'.format(options.repo, options.branch) + ok = run_jenkins(jobname, options) + if not ok: + parser.print_help() diff --git a/autojenkins/test_run.py b/autojenkins/test_run.py index d71e627..b387433 100644 --- a/autojenkins/test_run.py +++ b/autojenkins/test_run.py @@ -2,7 +2,7 @@ if __name__ == '__main__': - j = Jenkins() + j = Jenkins('http://jenkins.pe.local') # dir = path.dirname(path.dirname(__file__)) # config_file = path.join(dir, 'templates/story-dev-job.xml') response = j.delete('aaa')