From 64d43c5f181c0347a024f15ed8aa15dc745c8e58 Mon Sep 17 00:00:00 2001 From: Marko Semet Date: Tue, 15 Sep 2020 13:55:12 +0200 Subject: [PATCH] Add plugin script shellless --- .gitignore | 1 + plugins/elements/script_shellless.py | 49 ++++++++++++++++++++++++++ plugins/elements/script_shellless.yaml | 7 ++++ project.conf | 6 ++++ 4 files changed, 63 insertions(+) create mode 100644 .gitignore create mode 100644 plugins/elements/script_shellless.py create mode 100644 plugins/elements/script_shellless.yaml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/plugins/elements/script_shellless.py b/plugins/elements/script_shellless.py new file mode 100644 index 0000000..4b89a83 --- /dev/null +++ b/plugins/elements/script_shellless.py @@ -0,0 +1,49 @@ +from buildstream import Element, ElementError, Scope + + +class ScriptShelllessElement(Element): + __root_read_only:bool + __commands:list + + BST_FORBID_RDEPENDS = True + + def configure(self, node): + # Get data + self.__root_read_only = self.node_get_member(node, bool, 'root_read_only', None) + self.__commands = self.node_get_member(node, list, 'commands', None) + + def preflight(self): + pass + + def get_unique_key(self): + return {'version': 6, "root_read_only": self.__root_read_only, "commands": self.__commands} + + def configure_sandbox(self, sandbox): + sandbox.set_environment(self.get_environment()) + + def stage(self, sandbox): + for build_dep in self.dependencies(Scope.BUILD, recurse=False): + with self.timed_activity("Staging {} at /".format(build_dep.name), silent_nested=True): + build_dep.stage_dependency_artifacts(sandbox, Scope.RUN, path="/") + for build_dep in self.dependencies(Scope.BUILD, recurse=False): + with self.timed_activity("Integrating {}".format(build_dep.name), silent_nested=True): + for dep in build_dep.dependencies(Scope.RUN): + dep.integrate(sandbox) + + def assemble(self, sandbox): + for command in self.__commands: + self.status("Running command", detail=" ".join(command)) + #self.status(sandbox.get_virtual_directory()) + exitcode = sandbox.run(command, SandboxFlags.ROOT_READ_ONLY if self.__root_read_only else 0, cwd="/") + print("Command: %s Exit: %i" % (repr(" ".join(command)), exitcode)) + if exitcode != 0: + raise ElementError("Command '{}' failed with exitcode {}".format(" ".join(command), exitcode)) + + # Return where the result can be collected from + return "/" + + + +# Plugin entry point +def setup(): + return ScriptShelllessElement \ No newline at end of file diff --git a/plugins/elements/script_shellless.yaml b/plugins/elements/script_shellless.yaml new file mode 100644 index 0000000..de6d2de --- /dev/null +++ b/plugins/elements/script_shellless.yaml @@ -0,0 +1,7 @@ +variables: + cwd: / + +config: + root-read-only: False + # List of list of arguments + commands: [] \ No newline at end of file diff --git a/project.conf b/project.conf index 1b3091f..ad72e73 100644 --- a/project.conf +++ b/project.conf @@ -6,3 +6,9 @@ format-version: 17 # Subdirectory where elements are stored element-path: elements + +plugins: + - origin: local + path: plugins/elements + elements: + script_shellless: 0