Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add apollo configuration to load env file #11210

Merged
merged 16 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix:not overwrite env file
  • Loading branch information
huanshare committed Dec 3, 2024
commit 41d7d5492c77721d9142872492a13199390e0397
42 changes: 15 additions & 27 deletions api/extensions/configuration/apollo_configuration.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
import os
from abc import ABC

from configs import dify_config
from pydantic import parse_obj_as

import configs
from configs import DifyConfig, dify_config
from extensions.configuration.apollo.apollo_client import ApolloClient
from extensions.configuration.base_configuration import BaseConfiguration


class ApolloConfiguration(BaseConfiguration):
class ApolloConfiguration(BaseConfiguration, ABC):
"""Implementation for Apollo Configuration."""
huanshare marked this conversation as resolved.
Show resolved Hide resolved

def load_config_to_env_file(self):
client = ApolloClient(app_id=dify_config.APOLLO_APP_ID, cluster=dify_config.APOLLO_CLUSTER,
config_url=dify_config.APOLLO_CONFIG_URL, start_hot_update=False,
_notification_map={dify_config.APOLLO_NAMESPACE: -1})

# Get the path to the .env file
env_path = os.path.join(os.getcwd(), '.env')

apollo_config_dicts = client.get_all_dicts(dify_config.APOLLO_NAMESPACE)

# Retrieve the existing environment variables from the .env file
existing_vars = {}
if os.path.exists(env_path):
with open(env_path) as f:
for line in f:
key, value = line.strip().split('=', 1)
existing_vars[key.strip()] = value.strip()

# Update the existing variables with the new Apollo configuration
existing_vars.update(apollo_config_dicts)
def __init__(self):
super().__init__()
self.configuration_client = ApolloClient(app_id=dify_config.APOLLO_APP_ID, cluster=dify_config.APOLLO_CLUSTER,
config_url=dify_config.APOLLO_CONFIG_URL, start_hot_update=False,
_notification_map={dify_config.APOLLO_NAMESPACE: -1})

# Write the updated variables back to the .env file
with open(env_path, 'w') as f:
for key, value in existing_vars.items():
f.write(f"{key}={value}\n")
def load_configs(self):
# get all the config
apollo_config_dicts = self.configuration_client.get_all_dicts(dify_config.APOLLO_NAMESPACE)
configs.dify_config = parse_obj_as(DifyConfig, apollo_config_dicts)
7 changes: 5 additions & 2 deletions api/extensions/configuration/base_configuration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""Abstract interface for configuration center implementations."""

from abc import ABC, abstractmethod
from typing import Any


class BaseConfiguration(ABC):
"""Interface for configuration center."""

configuration_client: Any

def __init__(self): # noqa: B027
pass

# get configs from configuration center
@abstractmethod
def load_config_to_env_file(self):
def load_configs(self):
raise NotImplementedError
4 changes: 1 addition & 3 deletions api/extensions/ext_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class ConfigurationCenter:
def __init__(self):
self.configuration_runner = None
from configs import DifyConfig
configuration_type = configs.dify_config.CONFIGURATION_TYPE
if configuration_type:
configuration_factory = self.get_configuration_factory(configuration_type)
Expand All @@ -19,8 +18,7 @@ def __init__(self):
return

self.configuration_runner = configuration_factory()
self.configuration_runner.load_config_to_env_file()
configs.dify_config = DifyConfig()
self.configuration_runner.load_configs()

@staticmethod
def get_configuration_factory(configuration_type: str) -> type[BaseConfiguration]:
Expand Down
Loading