grim/local-pipelines

nearly working

2016-06-28, Gary Kramlich
639b3366dbcd
Parents e6e9f8ad528d
Children e921b0627ab8
nearly working
--- a/pipelines/pipeline.py Tue Jun 28 16:14:20 2016 -0500
+++ b/pipelines/pipeline.py Tue Jun 28 16:52:30 2016 -0500
@@ -1,7 +1,9 @@
# vi:et:ts=4 sw=4 sts=4
+import fnmatch
import os
import subprocess
+import tempfile
class Pipeline(object):
@@ -10,18 +12,29 @@
self.build_path = os.path.abspath(path)
self.branch = branch
- def _determine_image(self):
- image = self.config.get("image", "")
+ self.steps = self._get_steps()
+
+ def _get_steps(self):
+ if "branches" in self.config["pipelines"]:
+ for pattern, steps in self.config["pipelines"]["branches"].iteritems():
+ if fnmatch.fnmatch(self.branch, pattern):
+ return steps
+
+ return self.config["pipelines"]["default"]
+
- if "default" in self.config:
- if "image" in self.config["default"]:
- image = self.config["default"]
+ def _determine_image(self, step):
+ return step.get("image", self.config.get("image"))
+
+ def _build_script(self, step):
+ fd, filename = tempfile.mkstemp(prefix='pipeline', suffix='.sh')
- print(self.config)
+ with os.fdopen(fd, "w") as ofp:
+ ofp.writelines(step['script'])
- return image
+ return filename
- def command(self):
+ def get_command(self, step):
cmd = [
"docker",
"run",
@@ -35,22 +48,31 @@
"-w /opt/atlassian/bitbucketci/agent/build",
"--label com.atlassian.pipelines.agent=\"local\"",
"{image}",
- "-i",
+ # TODO didn't work for me... "-i",
"/tmp/{script_filename}",
]
return " ".join(cmd).format(
- script_filename='TODO',
+ script_filename=self._build_script(step),
build_path=self.build_path,
- image=self._determine_image(),
+ image=self._determine_image(step),
)
- def __repr__(self):
- return self.command()
+ def _commands(self):
+ commands = []
+ for step in self.steps:
+ commands.append(self.get_command(step))
+
+ return commands
def run(self):
- print(self)
- proc = subprocess.Popen(self.command().split())
+ for command in self._commands():
+ print('+ {}'.format(command))
+ proc = subprocess.Popen(command.split())
- return proc.wait()
+ return_code = proc.wait()
+ if return_code != 0:
+ return return_code
+ return 0
+