Commit dda79ea9 authored by Stavros-Anastasios Charismiadis's avatar Stavros-Anastasios Charismiadis
Browse files

Dockerize invoker, add start and clean scripts, update Postman collection

parent fc8724b1
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -13,3 +13,9 @@ __pycache__/
*.srl
*.log
*.bak
app.py
invoker_sdk_config.json
requirements.txt
discoverer.pkl
invoker_info/
logs/
 No newline at end of file

Dockerfile

0 → 100644
+24 −0
Original line number Diff line number Diff line
# Use a slim version of Python
FROM python:3.11-slim

# Set the working directory inside the container
WORKDIR /app

# Copy requirements and install them
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY app.py .

# Set environment variables
# This allows you to change the name without rebuilding the image
ENV PORT=8001
ENV FLASK_DEBUG=1
ENV USER_NAME="test"

# Expose the port the app runs on
EXPOSE 8001

# Run the application
CMD ["sh", "-c", "flask run --host=0.0.0.0 --port=$PORT"]
 No newline at end of file
+21 −8
Original line number Diff line number Diff line
@@ -27,22 +27,35 @@ A Postman collection is also provided, which can be imported directly to the Pos
## Getting started
To start the service, follow the steps below

1. Copy requirements_template.txt to requirements.txt and add opencapif_sdk
1. Copy requirements_template.txt to requirements.txt
    ```
     cp requirements_template.txt requirements.txt
    ```
2. Copy invoker_template.py to invoker.py
   
2. Copy app_template.py to app.py
    ```
     cp invoker_template.py invoker.py
     cp app_template.py app.py
    ```
3. Install requirements.txt
   
3. Copy invoker_sdk_config_template.json to invoker_sdk_config.json
    ```
     pip3 install -r requirements.txt
     cp invoker_sdk_config_template.json invoker_sdk_config.json
    ```
4. Fill the invoker_sdk_config.json with the correct credentials and information (check the "...")
   
4. Add opencapif_sdk in requirements.txt
    ```
     opencapif_sdk==0.1.24
    ```
5. Edit invoker_sdk_config.json with the correct configuration

6. To deploy the service, run the following command in an Ubuntu or MacOS terminal. To stop the invoker just press Ctrl+C or Command+C
```
    chmod +x start_invoker.sh
    ./start_invoker.sh <Participant name or username>
```

When the code is ready for testing, start the service using the following command
7. To remove the files of the invoker instance (invoker_info, logs and pkl file), execute the following command.
```
 python3 invoker.py
    chmod +x clean_files.sh
    ./clean_files.sh
```
 No newline at end of file

app_template.py

0 → 100644
+101 −0
Original line number Diff line number Diff line
import os
import json
import pickle
import requests
from flask import Flask, jsonify, request
# import OpenCAPIF SDK

app = Flask(__name__)
OBJ_FILE = "discoverer.pkl"

user_name = os.getenv("USER_NAME", "test")


@app.get("/onboard")
def onboard():
    response_message = "Onboard"
    ### Create the connection with CAPIF and set the invoker's config file

    ### Onboard the invoker

    ### Uncomment to print the Invoker ID
    # with open("./invoker_info/{0}/capif_api_security_context_details-{0}.json".format(user_name), "rb") as f:
    #     d = json.load(f)
    # response_message = "Invoker onboarded with ID: {}".format(d['api_invoker_id'])

    return response_message


@app.get("/discover")
def discover():
    response_message = "Discover"
    ### Create the connection with CAPIF as an onboarded invoker

    ### Discover APIs according to the filters

    ### Uncomment, fill the ... with the object and store discoverer object
    # with open(OBJ_FILE, 'wb') as f:
    #     pickle.dump( ... , f)

    ### Uncomment to read and return the discovered API
    # with open("./invoker_info/{0}/capif_api_security_context_details-{0}.json".format(user_name), "rb") as f:
    #     d = json.load(f)
    # response_message = d['registered_security_contexes']

    return response_message


@app.get("/getauth")
def getauth():
    response_message = "Get authorization to service API"
    ### Uncomment, fill the ... with a preferred variable name and fetch the discoverer object
    # if not os.path.exists(OBJ_FILE):
    #     return "No session found.", 404
    # with open(OBJ_FILE, 'rb') as f:
    #     ... = pickle.load(f)

    ### Create Security Context and get API token for the discovered API
    # jwt_token = ...

    ### Uncomment to return the token
    # response_message = "Get authorization to service API: {}".format(jwt_token)

    return response_message


@app.post("/access")
def access_service():
    response_message = "Access"
    ### Uncomment to read request body
    # request_data = request.get_json()
    # api_name = request_data['api_name']
    # resource_name = request_data['resource_name']
    # method = request_data['method']

    ### Uncomment, fill the ... with a preferred variable name and fetch the discoverer object
    # if not os.path.exists(OBJ_FILE):
    #     return "No session found.", 404
    # with open(OBJ_FILE, 'rb') as f:
    #     ... = pickle.load(f)

    ### Fetch the necessary information of the targeted API and add to a url parameter
    # service_path = ...
    # api_details = ...
    # service_interface =
    # url = ...

    ### Get API token for the discovered API and add it to a header
    # jwt_token = ...
    # headers = {
    #     'Authorization': 'Bearer {}'.format(jwt_token)
    # }

    ### Uncomment to make the request and return response text
    # response = requests.request(method, url, headers=headers)
    # response_message = response.text

    return response_message

if __name__ == "__main__":
    port = int(os.getenv("PORT", 8001))
    app.run(host='0.0.0.0', port=port, debug=True)
 No newline at end of file

clean_files.sh

0 → 100755
+3 −0
Original line number Diff line number Diff line
#!/bin/bash

sudo rm -R invoker_info logs discoverer.pkl
 No newline at end of file
Loading