inputs: convert to host service

Create a `InputService` class with an abstract method called `map`,
meant to be implemented by all inputs. An `unmap` method may be
optionally overridden by inputs to cleanup resources.
Instantiate a `host.ServiceManager` in the `Stage.run` section and
pass the to the host side input code so it can be used to spawn the
input services.
Convert all existing inputs to the new service framework.
This commit is contained in:
Christian Kellner 2021-06-07 12:50:13 +02:00 committed by Tom Gundersen
parent 08bc9ab7d8
commit 1ed85dc790
6 changed files with 122 additions and 121 deletions

View file

@ -14,12 +14,10 @@ like `rpm.` to avoid namespace clashes. This is enforced via
schema validation.
"""
import json
import sys
import subprocess
from osbuild.objectstore import StoreClient
from osbuild import inputs
SCHEMA = r"""
@ -77,16 +75,12 @@ SCHEMA = r"""
"""
def main():
args = json.load(sys.stdin)
refs = args["refs"]
target = args["target"]
class FilesInput(inputs.InputService):
store = StoreClient(connect_to=args["api"]["store"])
source = store.source("org.osbuild.files")
def map(self, store, _origin, refs, target, _options):
for checksum in refs:
try:
source = store.source("org.osbuild.files")
for checksum in refs:
subprocess.run(
[
"ln",
@ -95,21 +89,20 @@ def main():
],
check=True,
)
except subprocess.CalledProcessError as e:
json.dump({"error": e.output}, sys.stdout)
return 1
reply = {
"path": target,
"data": {
"refs": refs
reply = {
"path": target,
"data": {
"refs": refs
}
}
}
return reply
json.dump(reply, sys.stdout)
return 0
def main():
service = FilesInput.from_args(sys.argv[1:])
service.main()
if __name__ == '__main__':
r = main()
sys.exit(r)
main()

View file

@ -7,31 +7,38 @@ it to the stage.
"""
import json
import os
import sys
import uuid
from osbuild import inputs
SCHEMA = """
"additionalProperties": true
"""
class NoopInput(inputs.InputService):
def map(self, _store, _origin, refs, target, _options):
uid = str(uuid.uuid4())
path = os.path.join(target, uid)
os.makedirs(path)
reply = {
"path": target,
"data": {
"refs": refs
}
}
return reply
def main():
args = json.load(sys.stdin)
refs = args["refs"]
target = args["target"]
uid = str(uuid.uuid4())
path = os.path.join(target, uid)
os.makedirs(path)
data = {"path": path, "data": {"refs": refs}}
json.dump(data, sys.stdout)
return 0
service = NoopInput.from_args(sys.argv[1:])
service.main()
if __name__ == '__main__':
r = main()
sys.exit(r)
main()

View file

@ -17,7 +17,7 @@ import json
import sys
import subprocess
from osbuild.objectstore import StoreClient
from osbuild import inputs
SCHEMA = """
@ -99,31 +99,31 @@ def export(checksums, cache, output):
}
}
json.dump(reply, sys.stdout)
return reply
class OSTreeInput(inputs.InputService):
def map(self, store, origin, refs, target, _options):
if origin == "org.osbuild.pipeline":
for ref, options in refs.items():
source = store.read_tree(ref)
with open(os.path.join(source, "compose.json"), "r") as f:
compose = json.load(f)
commit_id = compose["ostree-commit"]
reply = export({commit_id: options}, source, target)
else:
source = store.source("org.osbuild.ostree")
reply = export(refs, source, target)
return reply
def main():
args = json.load(sys.stdin)
refs = args["refs"]
target = args["target"]
origin = args["origin"]
store = StoreClient(connect_to=args["api"]["store"])
if origin == "org.osbuild.pipeline":
for ref, options in refs.items():
source = store.read_tree(ref)
with open(os.path.join(source, "compose.json"), "r") as f:
compose = json.load(f)
commit_id = compose["ostree-commit"]
export({commit_id: options}, source, target)
else:
source = store.source("org.osbuild.ostree")
export(refs, source, target)
return 0
service = OSTreeInput.from_args(sys.argv[1:])
service.main()
if __name__ == '__main__':
r = main()
sys.exit(r)
main()

View file

@ -8,11 +8,9 @@ in read only mode. If the id is `null` or the empty
string it returns an empty tree.
"""
import json
import sys
from osbuild.objectstore import StoreClient
from osbuild import inputs
SCHEMA = """
@ -52,33 +50,29 @@ SCHEMA = """
"""
def error(msg):
json.dump({"error": msg}, sys.stdout)
sys.exit(1)
class TreeInput(inputs.InputService):
def map(self, store, _origin, refs, target, _options):
# input verification *must* have been done via schema
# verification. It is expected that origin is a pipeline
# and we have exactly one reference, i.e. a pipeline id
pid, _ = refs.popitem()
if pid:
path = store.read_tree_at(pid, target)
if not path:
raise ValueError(f"Unknown pipeline '{pid}'")
reply = {"path": target}
return reply
def main():
args = json.load(sys.stdin)
refs = args["refs"]
target = args["target"]
# input verification *must* have been done via schema
# verification. It is expected that origin is a pipeline
# and we have exactly one reference, i.e. a pipeline id
pid, _ = refs.popitem()
store = StoreClient(connect_to=args["api"]["store"])
if pid:
path = store.read_tree_at(pid, target)
if not path:
error(f"Could not find pipeline with id '{pid}'")
json.dump({"path": target}, sys.stdout)
return 0
service = TreeInput.from_args(sys.argv[1:])
service.main()
if __name__ == '__main__':
r = main()
sys.exit(r)
main()