Skip to content

Commit

Permalink
Feature/SK-932 | Add fedn controller start (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
sowmyasris authored Jul 11, 2024
1 parent c47a636 commit 49e53d2
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/code-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
--exclude-dir='docs'
--exclude-dir='flower-client'
--exclude='tests.py'
--exclude='controller_cmd.py'
--exclude='README.rst'
'^[ \t]+(import|from) ' -I .
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ services:
- mongo
entrypoint: [ "sh", "-c" ]
command:
- "/venv/bin/pip install --no-cache-dir -e . && /venv/bin/python fedn/network/api/server.py"
- "/venv/bin/pip install --no-cache-dir -e . && /venv/bin/fedn controller start"
ports:
- 8092:8092

Expand Down
1 change: 1 addition & 0 deletions fedn/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .session_cmd import session_cmd # noqa: F401
from .status_cmd import status_cmd # noqa: F401
from .validation_cmd import validation_cmd # noqa: F401
from .controller_cmd import controller_cmd # noqa: F401
18 changes: 18 additions & 0 deletions fedn/cli/controller_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import click

from .main import main


@main.group("controller")
@click.pass_context
def controller_cmd(ctx):
""":param ctx:"""
pass


@controller_cmd.command("start")
@click.pass_context
def controller_cmd(ctx):
from fedn.network.api.server import start_server_api

start_server_api()
9 changes: 2 additions & 7 deletions fedn/cli/main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import importlib.metadata

from fedn.utils.dist import get_version
import click

CONTEXT_SETTINGS = dict(
# Support -h as a shortcut for --help
help_option_names=["-h", "--help"],
)

# Dynamically get the version of the package
try:
version = importlib.metadata.version("fedn")
except importlib.metadata.PackageNotFoundError:
version = "unknown"
version=get_version("fedn")


@click.group(context_settings=CONTEXT_SETTINGS)
Expand Down
14 changes: 11 additions & 3 deletions fedn/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import yaml

from fedn.utils.dist import get_package_path

SECRET_KEY = os.environ.get("FEDN_JWT_SECRET_KEY", False)
FEDN_JWT_CUSTOM_CLAIM_KEY = os.environ.get("FEDN_JWT_CUSTOM_CLAIM_KEY", False)
FEDN_JWT_CUSTOM_CLAIM_VALUE = os.environ.get("FEDN_JWT_CUSTOM_CLAIM_VALUE", False)
Expand All @@ -20,9 +22,15 @@ def get_environment_config():
"""Get the configuration from environment variables."""
global STATESTORE_CONFIG
global MODELSTORAGE_CONFIG

STATESTORE_CONFIG = os.environ.get("STATESTORE_CONFIG", "/workspaces/fedn/config/settings-reducer.yaml.template")
MODELSTORAGE_CONFIG = os.environ.get("MODELSTORAGE_CONFIG", "/workspaces/fedn/config/settings-reducer.yaml.template")
if not os.environ.get("STATESTORE_CONFIG", False):
STATESTORE_CONFIG = get_package_path() + "/common/settings-controller.yaml.template"
else:
STATESTORE_CONFIG = os.environ.get("STATESTORE_CONFIG")

if not os.environ.get("MODELSTORAGE_CONFIG", False):
MODELSTORAGE_CONFIG = get_package_path() + "/common/settings-controller.yaml.template"
else:
MODELSTORAGE_CONFIG = os.environ.get("MODELSTORAGE_CONFIG")


def get_statestore_config(file=None):
Expand Down
24 changes: 24 additions & 0 deletions fedn/common/settings-controller.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
network_id: fedn-network
controller:
host: localhost
port: 8092
debug: True

statestore:
type: MongoDB
mongo_config:
username: fedn_admin
password: password
host: localhost
port: 6534

storage:
storage_type: S3
storage_config:
storage_hostname: localhost
storage_port: 9000
storage_access_key: fedn_admin
storage_secret_key: password
storage_bucket: fedn-models
context_bucket: fedn-context
storage_secure_mode: False
11 changes: 9 additions & 2 deletions fedn/network/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from fedn.network.api.v1 import _routes

custom_url_prefix = os.environ.get("FEDN_CUSTOM_URL_PREFIX", False)
# statestore_config,modelstorage_config,network_id,control=set_statestore_config()
api = API(statestore, control)
app = Flask(__name__)
for bp in _routes:
Expand Down Expand Up @@ -625,8 +626,14 @@ def list_combiners_data():
if custom_url_prefix:
app.add_url_rule(f"{custom_url_prefix}/list_combiners_data", view_func=list_combiners_data, methods=["POST"])

if __name__ == "__main__":

def start_server_api():
config = get_controller_config()
port = config["port"]
debug = config["debug"]
app.run(debug=debug, port=port, host="0.0.0.0")
host = "0.0.0.0"
app.run(debug=debug, port=port, host=host)


if __name__ == "__main__":
start_server_api()
1 change: 0 additions & 1 deletion fedn/network/api/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
statestore_config = get_statestore_config()
modelstorage_config = get_modelstorage_config()
network_id = get_network_config()

statestore = MongoStateStore(network_id, statestore_config["mongo_config"])
statestore.set_storage_backend(modelstorage_config)
control = Control(statestore=statestore)
3 changes: 1 addition & 2 deletions fedn/network/api/v1/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import pymongo
from pymongo.database import Database

from fedn.network.api.shared import statestore_config, network_id
from fedn.network.api.shared import statestore_config,network_id

api_version = "v1"

mc = pymongo.MongoClient(**statestore_config["mongo_config"])
mc.server_info()
mdb: Database = mc[network_id]
Expand Down
17 changes: 17 additions & 0 deletions fedn/utils/dist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import importlib.metadata

import fedn


def get_version(pacakge):
# Dynamically get the version of the package
try:
version = importlib.metadata.version("fedn")
except importlib.metadata.PackageNotFoundError:
version = "unknown"
return version


def get_package_path():
# Get the path of the package
return fedn.__path__[0]

0 comments on commit 49e53d2

Please sign in to comment.