Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
fangchenli committed Nov 5, 2023
1 parent f3775d7 commit 842de7b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
18 changes: 13 additions & 5 deletions src/_nebari/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
import pathlib
import re
import sys
import typing
from typing import Any, Dict, List, Union

import pydantic

from _nebari.utils import yaml


def set_nested_attribute(data: typing.Any, attrs: typing.List[str], value: typing.Any):
def set_nested_attribute(data: Any, attrs: List[str], value: Any):
"""Takes an arbitrary set of attributes and accesses the deep
nested object config to set value
"""

def _get_attr(d: typing.Any, attr: str):
def _get_attr(d: Any, attr: str):
if isinstance(d, list) and re.fullmatch(r"\d+", attr):
return d[int(attr)]
elif hasattr(d, "__getitem__"):
return d[attr]
else:
return getattr(d, attr)

def _set_attr(d: typing.Any, attr: str, value: typing.Any):
def _set_attr(d: Any, attr: str, value: Any):
if isinstance(d, list) and re.fullmatch(r"\d+", attr):
d[int(attr)] = value
elif hasattr(d, "__getitem__"):
Expand Down Expand Up @@ -63,6 +63,13 @@ def set_config_from_environment_variables(
return config


def dump_nested_model(model_dict: Dict[str, Union[pydantic.BaseModel, str]]):
result = {}
for key, value in model_dict.items():
result[key] = value.model_dump() if isinstance(value, pydantic.BaseModel) else value
return result


def read_configuration(
config_filename: pathlib.Path,
config_schema: pydantic.BaseModel,
Expand All @@ -88,14 +95,15 @@ def read_configuration(

def write_configuration(
config_filename: pathlib.Path,
config: typing.Union[pydantic.BaseModel, typing.Dict],
config: Union[pydantic.BaseModel, Dict],
mode: str = "w",
):
"""Write the nebari configuration file to disk"""
with config_filename.open(mode) as f:
if isinstance(config, pydantic.BaseModel):
yaml.dump(config.model_dump(), f)
else:
config = dump_nested_model(config)
yaml.dump(config, f)


Expand Down
3 changes: 2 additions & 1 deletion src/_nebari/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import tempfile
from pathlib import Path
from typing import Any, Dict

import pydantic
import requests
Expand Down Expand Up @@ -45,7 +46,7 @@ def render_config(
region: str = None,
disable_prompt: bool = False,
ssl_cert_email: str = None,
):
) -> Dict[str, Any]:
config = {
"provider": cloud_provider.value,
"namespace": namespace,
Expand Down
10 changes: 5 additions & 5 deletions src/_nebari/stages/infrastructure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,17 @@ def _check_input(cls, data: Any) -> Any:


class LocalProvider(schema.Base):
kube_context: typing.Optional[str] = None
node_selectors: typing.Dict[str, KeyValueDict] = {
kube_context: Optional[str] = None
node_selectors: Dict[str, KeyValueDict] = {
"general": KeyValueDict(key="kubernetes.io/os", value="linux"),
"user": KeyValueDict(key="kubernetes.io/os", value="linux"),
"worker": KeyValueDict(key="kubernetes.io/os", value="linux"),
}


class ExistingProvider(schema.Base):
kube_context: typing.Optional[str] = None
node_selectors: typing.Dict[str, KeyValueDict] = {
kube_context: Optional[str] = None
node_selectors: Dict[str, KeyValueDict] = {
"general": KeyValueDict(key="kubernetes.io/os", value="linux"),
"user": KeyValueDict(key="kubernetes.io/os", value="linux"),
"worker": KeyValueDict(key="kubernetes.io/os", value="linux"),
Expand Down Expand Up @@ -694,7 +694,7 @@ def tf_objects(self) -> List[Dict]:

def input_vars(self, stage_outputs: Dict[str, Dict[str, Any]]):
if self.config.provider == schema.ProviderEnum.local:
return LocalInputVars(kube_context=self.config.local.kube_context).dict()
return LocalInputVars(kube_context=self.config.local.kube_context).model_dump()
elif self.config.provider == schema.ProviderEnum.existing:
return ExistingInputVars(
kube_context=self.config.existing.kube_context
Expand Down
13 changes: 7 additions & 6 deletions src/_nebari/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pathlib
import re
import typing
from typing import Optional

import questionary
import rich
Expand Down Expand Up @@ -84,17 +85,17 @@ class GitRepoEnum(str, enum.Enum):
class InitInputs(schema.Base):
cloud_provider: ProviderEnum = ProviderEnum.local
project_name: schema.project_name_pydantic = ""
domain_name: typing.Optional[str] = None
namespace: typing.Optional[schema.namespace_pydantic] = "dev"
domain_name: Optional[str] = None
namespace: Optional[schema.namespace_pydantic] = "dev"
auth_provider: AuthenticationEnum = AuthenticationEnum.password
auth_auto_provision: bool = False
repository: typing.Optional[schema.github_url_pydantic] = None
repository: Optional[schema.github_url_pydantic] = None
repository_auto_provision: bool = False
ci_provider: CiEnum = CiEnum.none
terraform_state: TerraformStateEnum = TerraformStateEnum.remote
kubernetes_version: typing.Union[str, None] = None
region: typing.Union[str, None] = None
ssl_cert_email: typing.Union[schema.email_pydantic, None] = None
kubernetes_version: Optional[str] = None
region: Optional[str] = None
ssl_cert_email: Optional[schema.email_pydantic] = None
disable_prompt: bool = False
output: pathlib.Path = pathlib.Path("nebari-config.yaml")

Expand Down

0 comments on commit 842de7b

Please sign in to comment.