Skip to content

Commit

Permalink
fix review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
MP91 committed Sep 13, 2023
1 parent 0ad1ebb commit eb05585
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 273 deletions.
41 changes: 10 additions & 31 deletions .devcontainer/scripts/post-create.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,15 @@ echo "### Install python requirements ###"
echo "#######################################################"
# Update pip before installing requirements
pip3 install --upgrade pip
REQUIREMENTS="runtime_k3d/src/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
REQUIREMENTS="runtime_kanto/src/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
# Dependencies for the app
REQUIREMENTS="runtime_local/src/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
for package in runtime_k3d runtime_kanto runtime_local desired_state_generator; do

REQUIREMENTS="$package/src/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
REQUIREMENTS="$package/test/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi

echo "#######################################################"
echo "### Install test requirements ###"
echo "#######################################################"
# Update pip before installing requirements
pip3 install --upgrade pip
REQUIREMENTS="runtime_k3d/test/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
REQUIREMENTS="runtime_kanto/test/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
# Dependencies for the app
REQUIREMENTS="runtime_local/test/requirements.txt"
if [ -f $REQUIREMENTS ]; then
pip3 install -r $REQUIREMENTS
fi
done
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ build
logs
results
.coverage
tmp
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Function | Description
:---|:---
`$pathInWorkspaceOrPackage( <relative_path> )` | Resolves a path dynamically either to the local project workspace, if the file is available or falls back to a file in the package repository. If none of these files is available an exception is raised.

## Generators
## Deployment spec generators

This package contains the following generators:

Expand Down
122 changes: 60 additions & 62 deletions desired_state_generator/src/gen_desired_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@
import hashlib
import json
import re
from typing import Any, Dict, List
from typing import Any, Dict, List, Optional

import requests
import velocitas_lib
import velocitas_lib.services

VSS_SOURCE_DEFAULT = "vss-source-default-vss"
VSS_SOURCE_CUSTOM = "vss-source-custom-vss"
DATABROKER_ID = "data-broker-grpc"
GRPC_INTERFACE_ID = "dataprovider-proto-grpc"
DATABROKER_PACKAGE_ID = "vehicledatabroker"
MQTT_PACKAGE_ID = "mqtt-broker"

VSS_INTERFACE = "vehicle-signal-interface"
PUBSUB_INTERFACE = "pubsub"
GRPC_INTERFACE = "grpc-interface"


def is_uri(path: str) -> bool:
"""Check if the provided path is a URI.
Expand All @@ -36,10 +46,11 @@ def is_uri(path: str) -> bool:


def parse_vehicle_signal_interface(config: Dict[str, Any]) -> List[str]:
"""Parses the vehicle signal interface config.
"""Parse the vehicle signal interface config.
Args:
config: The json-config of the interface, as defined in the appManifest.json.
config (Dict[str, Any]): The json-config of the interface,
as defined in the appManifest.json.
Returns:
List[str]: A list of requirements defined by the config.
Expand All @@ -52,15 +63,12 @@ def parse_vehicle_signal_interface(config: Dict[str, Any]) -> List[str]:
version = ""
if vss_release_prefix in src:
version = src.removeprefix(vss_release_prefix).split("/")[0]
requirements.append(f"vss-source-default-vss:{version}")
elif is_uri(src):
version = get_md5_from_uri(src)
requirements.append(f"vss-source-custom-vss:{version}")
requirements.append(f"{VSS_SOURCE_DEFAULT}:{version}")
else:
version = get_md5_for_file(src)
requirements.append(f"vss-source-custom-vss:{version}")
version = get_md5_from_file_content(src)
requirements.append(f"{VSS_SOURCE_CUSTOM}:{version}")

requirements.append(f"data-broker-grpc:{get_package_version('vehicledatabroker')}")
requirements.append(f"{DATABROKER_ID}:{get_package_version(DATABROKER_PACKAGE_ID)}")

datapoints = config["datapoints"]["required"]
for datapoint in datapoints:
Expand All @@ -72,45 +80,34 @@ def parse_vehicle_signal_interface(config: Dict[str, Any]) -> List[str]:


def parse_grpc_interface(config: Dict[str, Any]) -> str:
"""Parses the grpc interface config.
"""Parse the grpc interface config.
Args:
config: The json-config of the interface, as defined in the appManifest.json.
config (Dict[str, Any]): The json-config of the interface,
as defined in the appManifest.json.
Returns:
str: The requirement with md5-hash of the proto-file as version.
"""
src = str(config["src"])

return f"dataprovider-proto-grpc:{get_md5_from_uri(src)}"
return f"{GRPC_INTERFACE_ID}:{get_md5_from_file_content(src)}"


def get_md5_from_uri(src: str) -> str:
"""Get the md5-hash of a file defined by an URI.
def get_md5_from_file_content(src: str) -> str:
"""Get the md5-hash of the contents of a file defined by a source.
Args:
str: The URI of the file.
src (str): The source of the file. Can either be a local file-path or an URI
Returns:
str: The md5-hash of the file.
"""
md5 = hashlib.md5(usedforsecurity=False)
with requests.get(src, timeout=30) as source:
for chunk in source.iter_content(chunk_size=4096):
md5.update(chunk)

return md5.hexdigest()
file_path = src
if is_uri(src):
file_path = "./tmp"
velocitas_lib.download_file(src, file_path)


def get_md5_for_file(file_path: str):
"""Get the md5-hash of a local file defined by a path.
Args:
str: The local path to the file.
Returns:
str: The md5-hash of the file.
"""
md5 = hashlib.md5(usedforsecurity=False)
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
Expand All @@ -120,58 +117,43 @@ def get_md5_for_file(file_path: str):


def get_package_version(package_id: str) -> str:
"""Get the version of the used mqtt-broker.
"""Get the version of the provided package.
Args:
package_id (str): The ID of the package
Returns:
str: The version of the mqtt-broker defined in the runtime.json.
str: The version of the package defined in the runtime.json.
"""
config = velocitas_lib.services.get_specific_service(package_id).config
version = config.image.split(":")[-1]
return version


def parse_interfaces(interfaces: List[Dict[str, Any]]) -> List[str]:
"""Parses the defined interfaces.
"""Parse the defined interfaces.
Args:
interfaces: The json-array of interfaces, as defined in the appManifest.json.
interfaces (List[Dict[str, Any]])): The json-array of interfaces,
as defined in the appManifest.json.
Returns:
List[str]: A list of requirements defined by the interface definitions.
"""
requirements = []
for interface in interfaces:
interface_type = interface["type"]
if interface_type == "vehicle-signal-interface":
if interface_type == VSS_INTERFACE:
requirements += parse_vehicle_signal_interface(interface["config"])
elif interface_type == "pubsub":
requirements.append(f"mqtt:{get_package_version('mqtt-broker')}")
elif interface_type == "grpc-interface":
elif interface_type == PUBSUB_INTERFACE:
requirements.append(f"mqtt:{get_package_version(MQTT_PACKAGE_ID)}")
elif interface_type == GRPC_INTERFACE:
requirements.append(parse_grpc_interface(interface["config"]))

return requirements


def main():
parser = argparse.ArgumentParser("generate-desired-state")
parser.add_argument(
"-o",
"--output-file-path",
type=str,
required=False,
help="Path to the folder where the manifest should be placed.",
)
parser.add_argument(
"-s",
"--source",
type=str,
required=True,
help="The URL of the image including the tag.",
)
args = parser.parse_args()

output_file_path = args.output_file_path
source = args.source
def main(source: str, output_file_path: Optional[str] = None):
imageName = source.split(":")[0].split("/")[-1]
version = source.split(":")[1]

Expand Down Expand Up @@ -202,4 +184,20 @@ def main():


if __name__ == "__main__":
main()
parser = argparse.ArgumentParser("generate-desired-state")
parser.add_argument(
"-o",
"--output-file-path",
type=str,
required=False,
help="Path to the folder where the manifest should be placed.",
)
parser.add_argument(
"-s",
"--source",
type=str,
required=True,
help="The URL of the image including the tag.",
)
args = parser.parse_args()
main(args.source, args.output_file_path)
40 changes: 0 additions & 40 deletions desired_state_generator/src/install_deps.py

This file was deleted.

1 change: 0 additions & 1 deletion desired_state_generator/src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
requests==2.31.0
git+https://github.com/eclipse-velocitas/velocitas-lib.git@v0.0.2
1 change: 1 addition & 0 deletions desired_state_generator/test/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest==7.2.1
27 changes: 14 additions & 13 deletions desired_state_generator/test/test_gen_desired_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
import sys
from pathlib import Path

sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
from gen_desired_state import get_md5_for_file, get_md5_from_uri, is_uri

import pytest

def test_get_md5_for_file():
hash = get_md5_for_file(f"{Path.cwd()}/LICENSE")
# generated with https://emn178.github.io/online-tools/md5_checksum.html
assert hash == "86d3f3a95c324c9479bd8986968f4327"


def test_get_md5_for_uri():
hash = get_md5_from_uri(
"https://raw.githubusercontent.com/eclipse-velocitas/devenv-runtimes/main/LICENSE"
)
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "src"))
from gen_desired_state import get_md5_from_file_content, is_uri


@pytest.mark.parametrize(
"src",
[
f"{Path.cwd()}/LICENSE",
"https://raw.githubusercontent.com/eclipse-velocitas/devenv-runtimes/main/LICENSE",
],
)
def test_get_md5_for_file(src):
hash = get_md5_from_file_content(src)
# generated with https://emn178.github.io/online-tools/md5_checksum.html
assert hash == "86d3f3a95c324c9479bd8986968f4327"

Expand Down
Loading

0 comments on commit eb05585

Please sign in to comment.