osbuild-mpp: Add url option to mpp-embed
Allow the user to pass a URL to be embeded as org.osbuild.curl input Signed-off-by: Ygal Blum <ygal.blum@gmail.com>
This commit is contained in:
parent
f4bfce05e3
commit
42552e0436
1 changed files with 31 additions and 16 deletions
|
|
@ -251,13 +251,15 @@ Example:
|
||||||
|
|
||||||
Embedding data and files so they can be used in inputs:
|
Embedding data and files so they can be used in inputs:
|
||||||
|
|
||||||
This directive allows to generate `org.osbuild.inline` sources on the fly. They can
|
This directive allows to generate `org.osbuild.inline` and `org.osbuild.curl`
|
||||||
be generated by reading a file (via the `path` parameter) or by directly providing
|
sources on the fly. `org.osbuild.inline` sources can be generated by reading
|
||||||
the data (via the `text` parameter). The reference to the inline source will be
|
a file (via the `path` parameter) or by directly providing the data (via the `text` parameter).
|
||||||
added to the array of references of the corresponding input. Any JSON specified
|
`org.osbuild.curl` resources can be generated by fetching a public URL (via the `url` parameter)
|
||||||
via the `options` parameter will be passed as value for the reference. Additionally,
|
The reference to the inline source will be added to the array of references of the
|
||||||
a dictionary called `embedded` will be created and within a mapping from the `id` to
|
corresponding input. Any JSON specified via the `options` parameter will be passed
|
||||||
the checksum so that the source can be used in e.g. `mpp-format-string` directvies.
|
as value for the reference. Additionally, a dictionary called `embedded` will be
|
||||||
|
created and within a mapping from the `id` to the checksum so that the source can
|
||||||
|
be used in e.g. `mpp-format-string` directvies.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
|
|
@ -311,6 +313,7 @@ import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
from typing import Dict, Optional
|
from typing import Dict, Optional
|
||||||
|
|
||||||
import rpm
|
import rpm
|
||||||
|
|
@ -1406,28 +1409,40 @@ class ManifestFileV2(ManifestFile):
|
||||||
def embed_data(ip, mpp):
|
def embed_data(ip, mpp):
|
||||||
uid = mpp["id"]
|
uid = mpp["id"]
|
||||||
path = mpp.get("path")
|
path = mpp.get("path")
|
||||||
|
url = mpp.get("url")
|
||||||
text = mpp.get("text")
|
text = mpp.get("text")
|
||||||
|
|
||||||
if path and text:
|
input_count = bool(text) + bool(path) + bool(url)
|
||||||
raise ValueError(f"Cannot specify both 'path' and 'text' for '{uid}'")
|
if input_count == 0:
|
||||||
|
raise ValueError(f"At least one of 'path', 'url' or 'text' must be specified for '{uid}'")
|
||||||
|
if input_count > 1:
|
||||||
|
raise ValueError(f"Only one of 'path', 'url' or 'text' may be specified for '{uid}'")
|
||||||
|
|
||||||
if path:
|
if path:
|
||||||
f, _ = self.find_and_open_file(path, [], mode="rb")
|
f, _ = self.find_and_open_file(path, [], mode="rb")
|
||||||
with f:
|
with f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
elif url:
|
||||||
|
response = urllib.request.urlopen(url)
|
||||||
|
data = response.fp.read()
|
||||||
else:
|
else:
|
||||||
data = bytes(text, "utf-8")
|
data = bytes(text, "utf-8")
|
||||||
|
|
||||||
encoded = base64.b64encode(data).decode("utf-8")
|
|
||||||
checksum = hashlib.sha256(data).hexdigest()
|
checksum = hashlib.sha256(data).hexdigest()
|
||||||
digest = "sha256:" + checksum
|
digest = "sha256:" + checksum
|
||||||
|
|
||||||
source = element_enter(self.sources, "org.osbuild.inline", {})
|
if url:
|
||||||
items = element_enter(source, "items", {})
|
source = element_enter(self.sources, "org.osbuild.curl", {})
|
||||||
items[digest] = {
|
items = element_enter(source, "items", {})
|
||||||
"encoding": "base64",
|
items[digest] = url
|
||||||
"data": encoded
|
else:
|
||||||
}
|
encoded = base64.b64encode(data).decode("utf-8")
|
||||||
|
source = element_enter(self.sources, "org.osbuild.inline", {})
|
||||||
|
items = element_enter(source, "items", {})
|
||||||
|
items[digest] = {
|
||||||
|
"encoding": "base64",
|
||||||
|
"data": encoded
|
||||||
|
}
|
||||||
|
|
||||||
refs = element_enter(ip, "references", {})
|
refs = element_enter(ip, "references", {})
|
||||||
refs[digest] = mpp.get("options", {})
|
refs[digest] = mpp.get("options", {})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue