Skip to content

Commit 67edb89

Browse files
chore: pass bootstrap.servers for local env (#61)
* chore: pass `bootstrap.servers` for local env * improve CONTRIBUTING.md * fix formatting
1 parent 669f9ca commit 67edb89

File tree

4 files changed

+99
-32
lines changed

4 files changed

+99
-32
lines changed

CONTRIBUTING.md

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,78 @@ To submit code contributions:
3737

3838
## Debugging
3939

40-
### Running Against Local Port Instance
40+
### Set Environment Variables
4141

42-
When debugging the Port Execution Agent locally against a local instance of Port, follow these steps:
42+
Create or update your `.env` file to include the following environment variables (replace with your actual values):
4343

44-
1. **Set Up Local Environment:**
45-
- Ensure you have a local instance of Port running. This will act as your development and testing environment.
44+
```env
45+
STREAMER_NAME=KAFKA
46+
KAFKA_CONSUMER_GROUP_ID=my_consumer_group
4647
47-
2. **Configure Environment Variables:**
48-
- Create or update your `.env` file to include the following environment variable:
49-
```env
50-
USING_LOCAL_PORT_INSTANCE=True
51-
```
48+
PORT_ORG_ID=your_organization_id_here
49+
PORT_CLIENT_ID=your_client_id_here
50+
PORT_CLIENT_SECRET=your_client_secret_here
51+
PORT_API_BASE_URL=your_api_base_url_here
52+
```
5253

53-
3. **Kafka Authentication:**
54-
- When `USING_LOCAL_PORT_INSTANCE` is set to `True`, the execution agent will not attempt to pull your local organization's kafka credentials.
54+
### Run with Python
5555

56-
4. **Running the Agent Locally:**
57-
- Start the execution agent as you normally would.
56+
**Prequisites:**
57+
58+
- Python 3.11
59+
- [Poetry](https://python-poetry.org/docs/#installing-manually)
60+
61+
**Install Dependencies:**
62+
63+
It is recommended to install the dependencies inside a virtual environment:
64+
65+
```bash
66+
VENV_PATH=".venv"
67+
python3.11 -m venv $VENV_PATH
68+
$VENV_PATH/bin/pip install -U pip setuptools
69+
$VENV_PATH/bin/pip install poetry
70+
source $VENV_PATH/bin/activate
71+
72+
poetry install
73+
```
74+
75+
**Run the Agent:**
76+
77+
Activate the virtual environment if not already activated:
78+
79+
```bash
80+
source .venv/bin/activate
81+
```
82+
83+
The execution must be from the `app` directory:
84+
85+
```bash
86+
cd app
87+
```
88+
89+
To start the Port Execution Agent, run the following command from the `app` directory:
90+
91+
```bash
92+
python main.py
93+
```
94+
95+
### Run with Docker
96+
97+
Before running the container, ensure you have built the Docker image:
98+
99+
```bash
100+
docker build -t port-execution-agent .
101+
```
102+
103+
Run the Port Execution Agent container with the following command:
104+
105+
```bash
106+
docker run \
107+
--name port-execution-agent \
108+
--env-file .env \
109+
--network host \
110+
port-execution-agent
111+
```
58112

59113
### General Troubleshooting (Optional)
60114

app/consumers/kafka_consumer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def __init__(
3434
"auto.offset.reset": settings.KAFKA_CONSUMER_AUTO_OFFSET_RESET,
3535
"enable.auto.commit": "false",
3636
}
37-
if not settings.USING_LOCAL_PORT_INSTANCE:
37+
if settings.USING_LOCAL_PORT_INSTANCE:
38+
logger.info("Using local Port instance for Kafka credentials")
39+
conf["bootstrap.servers"] = settings.KAFKA_CONSUMER_BOOTSTRAP_SERVERS
40+
else:
3841
logger.info("Getting Kafka credentials")
3942
brokers, username, password = get_kafka_credentials()
4043
conf["sasl.username"] = username

app/core/config.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@
22
from typing import Any, Optional
33

44
from dotenv import find_dotenv
5-
from pydantic import (
6-
AnyHttpUrl,
7-
BaseModel,
8-
BaseSettings,
9-
Field,
10-
parse_file_as,
11-
parse_obj_as,
12-
validator,
13-
)
5+
from pydantic import (AnyHttpUrl, BaseModel, BaseSettings, Field,
6+
parse_file_as, parse_obj_as, validator)
147

158

169
class ActionReport(BaseModel):
@@ -47,11 +40,32 @@ class Settings(BaseSettings):
4740
KAFKA_CONSUMER_SESSION_TIMEOUT_MS: int = 45000
4841
KAFKA_CONSUMER_AUTO_OFFSET_RESET: str = "earliest"
4942
KAFKA_CONSUMER_GROUP_ID: str = ""
43+
KAFKA_CONSUMER_BOOTSTRAP_SERVERS: str = ""
5044

5145
KAFKA_RUNS_TOPIC: str = ""
5246

5347
CONTROL_THE_PAYLOAD_CONFIG_PATH: Path = Path("./control_the_payload_config.json")
5448

49+
@validator("KAFKA_CONSUMER_BOOTSTRAP_SERVERS", always=True)
50+
def set_kafka_consumer_bootstrap_servers(
51+
cls, v: Optional[str], values: dict
52+
) -> str:
53+
using_local = values.get("USING_LOCAL_PORT_INSTANCE")
54+
if isinstance(v, str) and v:
55+
if using_local is False:
56+
raise ValueError(
57+
"KAFKA_CONSUMER_BOOTSTRAP_SERVERS works only with "
58+
"USING_LOCAL_PORT_INSTANCE=True"
59+
)
60+
61+
return v
62+
63+
elif using_local is True:
64+
raise ValueError(
65+
"KAFKA_CONSUMER_BOOTSTRAP_SERVERS must be set when "
66+
"USING_LOCAL_PORT_INSTANCE=True"
67+
)
68+
5569
@validator("KAFKA_RUNS_TOPIC", always=True)
5670
def set_kafka_runs_topic(cls, v: Optional[str], values: dict) -> str:
5771
if isinstance(v, str) and v:

app/invokers/webhook_invoker.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@
99
from core.consts import consts
1010
from flatten_dict import flatten, unflatten
1111
from invokers.base_invoker import BaseInvoker
12-
from port_client import report_run_response, report_run_status, run_logger_factory
12+
from port_client import (report_run_response, report_run_status,
13+
run_logger_factory)
1314
from pydantic import BaseModel, Field
1415
from requests import Response
15-
from utils import (
16-
decrypt_payload_fields,
17-
get_invocation_method_object,
18-
get_response_body,
19-
log_by_detail_level,
20-
response_to_dict,
21-
sign_sha_256,
22-
)
16+
from utils import (decrypt_payload_fields, get_invocation_method_object,
17+
get_response_body, log_by_detail_level, response_to_dict,
18+
sign_sha_256)
2319

2420
logging.basicConfig(level=settings.LOG_LEVEL)
2521
logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)