conductor-worker-python/pyexecplugins/Packer.py

43 lines
1.5 KiB
Python

from pyexecplugins.pyexecplugins import PyExecPlugin
import tempfile
import json
import subprocess
class Plugin(PyExecPlugin):
name = "Packer"
taskdef = {
"name" : "pypacker",
"retryCount" : 0,
"description" : "Executes packer.io command line for build and validate. It has been isolated in order to be able to start the worker only where OS dependencies are matched.",
"inputKeys" : ["command", "template"],
"outputKeys" : ["results"],
"ownerEmail" : "m.lettere@gmail.com"
}
def __init__(self, data=None):
super().__init__(data)
self.template = data["template"]
self.command = data.get("command", "build")
def execute(self):
fp = tempfile.NamedTemporaryFile(mode="w", delete=False)
fp.write(json.dumps(self.template))
fp.close()
completed = subprocess.run(["packer", self.command, fp.name], capture_output=True, text=True)
if(completed.returncode != 0):
raise Exception("packer.io failed: {} - out:{} - err:{}".format(
completed.returncode,
completed.stdout if completed.stdout != None else "",
completed.stderr if completed.stderr != None else "")
)
return {
'results' : [
{
'returncode' : completed.returncode,
'stdout' : completed.stdout,
'stderr': completed.stderr
}
]
}