aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Copeland <ben.copeland@linaro.org>2017-11-01 12:48:04 +0000
committerBen Copeland <ben.copeland@linaro.org>2017-11-01 15:04:40 +0000
commit53487203891386b14c884cea2de1c5919b091591 (patch)
tree9c3f6d5568679766f0771e5e17d6f09e05623fd9
parent82afb8df337094a3ea38cfb285fe79fc5b218dbe (diff)
downloadci-yadp-builder-53487203891386b14c884cea2de1c5919b091591.tar.gz
yadp_builder_v2: Python2 version
Due to having to installing a lot of dependencies on our Jenkins Master, here is a Python2 version of the script. The script still adds a class constructor, but due to bad Python2 support had to build the script out differently to our Python3 version. Change-Id: I02627514c04d46e87571a471055263b14a329369
-rwxr-xr-xyadp_builder_v2.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/yadp_builder_v2.py b/yadp_builder_v2.py
new file mode 100755
index 0000000..3c815e7
--- /dev/null
+++ b/yadp_builder_v2.py
@@ -0,0 +1,86 @@
+import argparse
+import yaml
+import os.path
+from jinja2 import FileSystemLoader, Environment
+import jenkins
+import logging
+
+
+class Loader(yaml.Loader):
+
+ def __init__(self, *args, **kwargs):
+ super(Loader, self).__init__(*args, **kwargs)
+ self.add_constructor('!include', type(self).construct_include)
+ try:
+ self._root = os.path.split(self)[0]
+ except AttributeError:
+ self._root = os.path.curdir
+
+ def construct_include(self, node):
+ """Include file referenced at node."""
+
+ filename = os.path.abspath(os.path.join(
+ self._root, self.construct_scalar(node)
+ ))
+ extension = os.path.splitext(filename)[1].lstrip('.')
+
+ with open(filename, 'r') as f:
+ if extension in ('yaml', 'yml'):
+ return yaml.load(f, Loader)
+ else:
+ return ''.join(f.readlines())
+
+
+def jinja2_from_template(directory, template_name, data):
+ loader = FileSystemLoader(directory)
+ env = Environment(loader=loader)
+ template = env.get_template(template_name)
+ return template.render(hosts=data)
+
+
+def get_parser():
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-u', '--username', type=str, required=True,
+ help='Username for Jenkins server')
+ parser.add_argument('-p', '--password', type=str, required=True,
+ help='Password for Jenkins server')
+ parser.add_argument('-s', '--server', type=str,
+ default='http://localhost:8080',
+ help='Jenkins server URL. e.g. http://localhost:8080')
+ parser.add_argument('-i', '--inventory', type=str, default='hosts.yml',
+ help='specify inventory host path')
+ parser.add_argument('-l', '--loglevel', default='INFO',
+ help="Setting logging level, default: %(default)s")
+ parser.add_argument('--dryrun', action='store_true',
+ help='Do not publish to Jenkins')
+ return parser
+
+
+if __name__ == '__main__':
+ parser = get_parser()
+ args = parser.parse_args()
+ logging.basicConfig(level=args.loglevel)
+ with open(args.inventory, 'r') as f:
+ data = yaml.load(f, Loader)
+ logging.debug(data)
+ template_output = jinja2_from_template(
+ './templates',
+ 'configure-yadocker-cloud.groovy.j2', data)
+ server = jenkins.Jenkins(args.server, username=args.username,
+ password=args.password)
+ if args.dryrun:
+ with open('/tmp/configure-yadocker-cloud.groovy', 'w') as fw:
+ fw.write(template_output)
+ template_output = jinja2_from_template(
+ './templates',
+ 'configure-yadocker-cloud.groovy-dryrun.j2', data)
+
+ publishdry = server.run_script(template_output)
+ if 'error' in publishdry:
+ logging.info(publishdry)
+ exit(1)
+ else:
+ logging.info(publishdry)
+ else:
+ publish = server.run_script(template_output)
+ logging.info(publish)