Skip to content

Commit

Permalink
First take on command-line runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Carles Barrobés committed Nov 1, 2011
1 parent 2ae640f commit 3e8f1fd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
10 changes: 5 additions & 5 deletions autojenkins/jobs.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
59 changes: 59 additions & 0 deletions 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()
2 changes: 1 addition & 1 deletion autojenkins/test_run.py
Expand Up @@ -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')
Expand Down

0 comments on commit 3e8f1fd

Please sign in to comment.