Commit ac377681 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Merge branch 'fix/common-proto' into 'develop'

Adapted Service to common proto folder

See merge request teraflow-h2020/controller!128
parents d96fe734 d3e95f32
Loading
Loading
Loading
Loading

src/context/requirements.txt

deleted100644 → 0
+0 −72
Original line number Diff line number Diff line
#
# This file is autogenerated by pip-compile with python 3.9
# To update, run:
#
#    pip-compile src/context/requirements.in
#
aniso8601==9.0.1
    # via flask-restful
attrs==21.2.0
    # via pytest
certifi==2021.5.30
    # via requests
charset-normalizer==2.0.6
    # via requests
click==8.0.1
    # via flask
flask==2.0.1
    # via flask-restful
flask-restful==0.3.9
    # via -r src/context/requirements.in
grpcio==1.41.0
    # via
    #   -r src/context/requirements.in
    #   grpcio-health-checking
grpcio-health-checking==1.41.0
    # via -r src/context/requirements.in
idna==3.2
    # via requests
iniconfig==1.1.1
    # via pytest
itsdangerous==2.0.1
    # via flask
jinja2==3.0.1
    # via flask
markupsafe==2.0.1
    # via jinja2
packaging==21.0
    # via pytest
pluggy==1.0.0
    # via pytest
prometheus-client==0.11.0
    # via -r src/context/requirements.in
protobuf==3.18.0
    # via grpcio-health-checking
py==1.10.0
    # via pytest
py-cpuinfo==8.0.0
    # via pytest-benchmark
pyparsing==2.4.7
    # via packaging
pytest==6.2.5
    # via
    #   -r src/context/requirements.in
    #   pytest-benchmark
pytest-benchmark==3.4.1
    # via -r src/context/requirements.in
pytz==2021.1
    # via flask-restful
redis==3.5.3
    # via -r src/context/requirements.in
requests==2.26.0
    # via -r src/context/requirements.in
six==1.16.0
    # via
    #   flask-restful
    #   grpcio
toml==0.10.2
    # via pytest
urllib3==1.26.7
    # via requests
werkzeug==2.0.1
    # via flask
+5 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ build service:
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile .
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  after_script:
@@ -30,6 +30,8 @@ build service:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"' 
    - changes:
      - src/common/**/*.py
      - proto/*.proto
      - src/$IMAGE_NAME/**/*.{py,in,yml}
      - src/$IMAGE_NAME/Dockerfile
      - src/$IMAGE_NAME/tests/*.py
@@ -64,6 +66,8 @@ unit test service:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH)'
    - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "develop"' 
    - changes:
      - src/common/**/*.py
      - proto/*.proto
      - src/$IMAGE_NAME/**/*.{py,in,yml}
      - src/$IMAGE_NAME/Dockerfile
      - src/$IMAGE_NAME/tests/*.py
+34 −16
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM python:3-slim
FROM python:3.9-slim

# Install dependencies
RUN apt-get --yes --quiet --quiet update && \
@@ -28,25 +28,43 @@ RUN GRPC_HEALTH_PROBE_VERSION=v0.2.0 && \
    chmod +x /bin/grpc_health_probe

# Get generic Python packages
RUN python3 -m pip install --upgrade pip setuptools wheel pip-tools
RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install --upgrade setuptools wheel
RUN python3 -m pip install --upgrade pip-tools

# Set working directory
# Get common Python packages
# Note: this step enables sharing the previous Docker build steps among all the Python components
WORKDIR /var/teraflow
COPY common_requirements.in common_requirements.in
RUN pip-compile --quiet --output-file=common_requirements.txt common_requirements.in
RUN python3 -m pip install -r common_requirements.txt

# Create module sub-folders
RUN mkdir -p /var/teraflow/service
# Add common files into working directory
WORKDIR /var/teraflow/common
COPY src/common/. ./
RUN rm -rf proto

# Create proto sub-folder, copy .proto files, and generate Python code
RUN mkdir -p /var/teraflow/common/proto
WORKDIR /var/teraflow/common/proto
RUN touch __init__.py
COPY proto/*.proto ./
RUN python3 -m grpc_tools.protoc -I=. --python_out=. --grpc_python_out=. *.proto
RUN rm *.proto
RUN find . -type f -exec sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' {} \;

# Get Python packages per module
COPY service/requirements.in service/requirements.in
RUN pip-compile --output-file=service/requirements.txt service/requirements.in
RUN python3 -m pip install -r service/requirements.txt
# Create component sub-folders, get specific Python packages
RUN mkdir -p /var/teraflow/service
WORKDIR /var/teraflow/service
COPY src/service/requirements.in requirements.in
RUN pip-compile --quiet --output-file=requirements.txt requirements.in
RUN python3 -m pip install -r requirements.txt

# Add files into working directory
COPY common/. common
COPY context/. context
COPY device/. device
COPY monitoring/. monitoring
COPY service/. service
# Add component files into working directory
WORKDIR /var/teraflow
COPY src/context/. context/
COPY src/device/. device/
COPY src/service/. service/

# Start service service
# Start the service
ENTRYPOINT ["python", "-m", "service.service"]
+2 −2
Original line number Diff line number Diff line
@@ -15,10 +15,10 @@
import grpc, logging
from common.Constants import ServiceNameEnum
from common.Settings import get_service_host, get_service_port_grpc
from common.proto.context_pb2 import Empty, Service, ServiceId
from common.proto.service_pb2_grpc import ServiceServiceStub
from common.tools.client.RetryDecorator import retry, delay_exponential
from common.tools.grpc.Tools import grpc_message_to_json_string
from service.proto.context_pb2 import Empty, Service, ServiceId
from service.proto.service_pb2_grpc import ServiceServiceStub

LOGGER = logging.getLogger(__name__)
MAX_RETRIES = 15

src/service/genproto.sh

deleted100755 → 0
+0 −49
Original line number Diff line number Diff line
#!/bin/bash -eu
#
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Make folder containing the script the root folder for its execution
cd $(dirname $0)

rm -rf proto/*.py
rm -rf proto/__pycache__
tee proto/__init__.py << EOF > /dev/null
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

EOF

python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto context.proto
python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto service.proto
python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto kpi_sample_types.proto

rm proto/context_pb2_grpc.py
rm proto/kpi_sample_types_pb2_grpc.py

sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/context_pb2.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/service_pb2.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/service_pb2_grpc.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/kpi_sample_types_pb2.py
Loading