Skip to content

Commit 256e36e

Browse files
Hub configuration
1 parent 25febf7 commit 256e36e

5 files changed

Lines changed: 74 additions & 65 deletions

File tree

codehub/cli/config.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
from dataclasses import dataclass
22
import os
33
from pathlib import Path
4-
5-
from codehub.cli.gcp.terraform import TerraformOutput
4+
from typing import Optional
65

76

87
ROOT = Path(__file__).parents[2]
@@ -39,6 +38,33 @@
3938
CLUSTER_MAX_LEN = 14
4039

4140

41+
@dataclass
42+
class CloudState:
43+
nfs_ip: str
44+
nfs_name: str
45+
cluster_id: str
46+
hub_sa_key: str
47+
cluster_endpoint: str
48+
cluster_cert: str
49+
gcp_token: str
50+
docker_registry_hostname: str
51+
docker_image: str
52+
53+
54+
@dataclass
55+
class OAuthConfig:
56+
client_id: str
57+
client_secret: str
58+
59+
60+
@dataclass
61+
class HubConfig:
62+
admins: list[str]
63+
https: Optional[str] = None
64+
oauth_config: Optional[OAuthConfig] = None
65+
contact_email: Optional[str] = None
66+
67+
4268
@dataclass
4369
class CreateConfig:
4470
name: str
@@ -49,9 +75,9 @@ class CreateConfig:
4975

5076

5177
@dataclass
52-
class OAuthConfig:
53-
client_id: str
54-
client_secret: str
78+
class UpgradeConfig:
79+
name: str
80+
hub_config: HubConfig
5581

5682

5783
@dataclass
@@ -61,4 +87,4 @@ class DeployConfig:
6187
helm_dir: str
6288
hub_dir: str
6389
k8s_dir: str
64-
cloud_state: TerraformOutput
90+
cloud_state: CloudState

codehub/cli/create.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
import os
33
import logging
44
import json
5-
from typing import Optional
65
from codehub.cli.gcp.terraform import (
76
setup_terraform,
87
terraform_apply,
98
terraform_output,
109
)
1110
from distutils.dir_util import copy_tree
12-
from codehub.cli.config import STRUCTURE, CreateConfig, DeployConfig, OAuthConfig
11+
from codehub.cli.config import (
12+
STRUCTURE,
13+
CreateConfig,
14+
DeployConfig,
15+
HubConfig,
16+
UpgradeConfig,
17+
)
1318
from codehub.cli.gcp.helpers import authenticate_k8s_GKE
1419
from codehub.cli.k8s.create import create_k8s_resources
1520
from codehub.cli.helm.create import create_deploy
@@ -48,39 +53,40 @@ def create(config: CreateConfig):
4853
deploy_config = DeployConfig(
4954
config.name, config.region, helm_dir, hub_dir, k8s_dir, cloud_state
5055
)
56+
hub_config = HubConfig(config.admins)
5157

5258
__create_k8s_resources(deploy_config)
5359

54-
__create_deploy(deploy_config, admins=config.admins)
60+
__create_deploy(deploy_config, hub_config)
5561
install_helm_chart(deploy_config, upgrade=False)
5662

5763
wait_for_hub_to_get_ready(k8s_dir)
5864

5965
return get_ip(config.name, k8s_dir)
6066

6167

62-
def upgrade(name, admins, https=None, oauth_config: Optional[OAuthConfig] = None):
63-
old_deploy_dir = get_latest_deployment(name=name)
68+
def upgrade(config: UpgradeConfig):
69+
old_deploy_dir = get_latest_deployment(config.name)
6470

6571
helm_dir, hub_dir, k8s_dir, cloud_dir = __upgrade_deploy_structure(
66-
name, old_deploy_dir
72+
config.name, old_deploy_dir
6773
)
6874
cloud_state = terraform_output(cloud_dir=cloud_dir)
6975

7076
region = read_yaml(os.path.join(old_deploy_dir, "helm", "chart.yaml"))["install"][
7177
"region"
7278
]
73-
deploy_config = DeployConfig(name, region, helm_dir, hub_dir, k8s_dir, cloud_state)
79+
deploy_config = DeployConfig(
80+
config.name, region, helm_dir, hub_dir, k8s_dir, cloud_state
81+
)
7482

7583
__create_deploy(
7684
deploy_config,
77-
admins=admins,
78-
https=https,
79-
oauth_config=oauth_config,
85+
config.hub_config,
8086
)
8187
install_helm_chart(deploy_config, upgrade=True)
8288

83-
authenticate_k8s_GKE(name)
89+
authenticate_k8s_GKE(config.name)
8490
wait_for_hub_to_get_ready(k8s_dir)
8591

8692

@@ -134,18 +140,9 @@ def __create_k8s_resources(deploy_config: DeployConfig):
134140

135141
def __create_deploy(
136142
deploy_config: DeployConfig,
137-
admins,
138-
https=None,
139-
oauth_config: Optional[OAuthConfig] = None,
143+
hub_config: HubConfig,
140144
):
141-
contact_email = None
142145
with open(STRUCTURE["secrets"]["gcp"]["sa"], "rt") as f:
143-
contact_email = json.loads(f.read())["client_email"]
146+
hub_config.contact_email = json.loads(f.read())["client_email"]
144147

145-
create_deploy(
146-
deploy_config,
147-
contact_email=contact_email,
148-
admins=admins,
149-
https=https,
150-
oauth_config=oauth_config,
151-
)
148+
create_deploy(deploy_config, hub_config)

codehub/cli/entrypoint.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import click
33
import logging
44
import sys
5-
from codehub.cli.config import CreateConfig, OAuthConfig
5+
from codehub.cli.config import CreateConfig, HubConfig, OAuthConfig, UpgradeConfig
66
from codehub.cli.create import create, upgrade, scale, create_infrastructure
77
from codehub.cli.delete import delete
88
from codehub.cli.helpers import check_commands, validate_cluster_name, check_credentials
@@ -61,10 +61,14 @@ def upgradecluster(name, admin, https=None, client_id=None, client_secret=None):
6161
logger = logging.getLogger(__name__)
6262
logger.info(f"Updating cluster '{name}'")
6363

64-
oauth_config: Optional[OAuthConfig] = None
64+
hub_config = HubConfig([i.lower() for i in admin])
65+
6566
if https is not None:
67+
hub_config.https = https
6668
if client_id and client_secret:
67-
oauth_config = OAuthConfig(client_id=client_id, client_secret=client_secret)
69+
hub_config.oauth_config = OAuthConfig(
70+
client_id=client_id, client_secret=client_secret
71+
)
6872
# None or both need to be passed
6973
elif client_id or client_secret:
7074
raise ValueError(
@@ -74,14 +78,9 @@ def upgradecluster(name, admin, https=None, client_id=None, client_secret=None):
7478
+ "`--client-secret` <github-client-secret>"
7579
)
7680

77-
admins = [i.lower() for i in admin]
81+
config = UpgradeConfig(name, hub_config)
7882

79-
res = upgrade(
80-
name=name,
81-
admins=admins,
82-
https=https,
83-
oauth_config=oauth_config,
84-
)
83+
res = upgrade(config)
8584

8685
logger.debug(f"{upgrade.__module__}.{upgrade.__name__} output:")
8786
logger.debug(res)

codehub/cli/gcp/terraform.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@
33
import os
44
from typing import Any, List, Optional
55
from distutils.dir_util import copy_tree
6-
from codehub.cli.config import STRUCTURE, CreateConfig
6+
from codehub.cli.config import STRUCTURE, CreateConfig, CloudState
77
from codehub.cli.helpers import (
88
fill_file_placeholders,
99
run_cmd,
1010
run_cmd_passthrough_stdout,
1111
)
1212

1313

14-
@dataclasses.dataclass
15-
class TerraformOutput:
16-
nfs_ip: str
17-
nfs_name: str
18-
cluster_id: str
19-
hub_sa_key: str
20-
cluster_endpoint: str
21-
cluster_cert: str
22-
gcp_token: str
23-
docker_registry_hostname: str
24-
docker_image: str
25-
26-
2714
def setup_terraform(
2815
*,
2916
config: CreateConfig,
@@ -55,7 +42,7 @@ def terraform_apply(
5542
*,
5643
cloud_dir: str,
5744
additional_vars: Optional[dict[str, Any]] = None,
58-
) -> TerraformOutput:
45+
) -> CloudState:
5946
terraform_cmd = _terraform_cmd(cloud_dir)
6047
run_cmd(terraform_cmd + ["init"])
6148

@@ -84,13 +71,13 @@ def terraform_destroy(*, cloud_dir: str):
8471
)
8572

8673

87-
def terraform_output(*, cloud_dir: str) -> TerraformOutput:
74+
def terraform_output(*, cloud_dir: str) -> CloudState:
8875
command = _terraform_cmd(cloud_dir) + ["output", "-json"]
8976
terraform_output: dict[str, dict[str, Any]] = json.loads(
9077
run_cmd(command, verbose=False)
9178
)
9279
output = {key: val_dict["value"] for key, val_dict in terraform_output.items()}
93-
return TerraformOutput(**output)
80+
return CloudState(**output)
9481

9582

9683
def _get_project_from_sa(gcp_sa_path: str) -> str:

codehub/cli/helm/create.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import os
22
from secrets import token_hex
3-
from typing import Optional
43
from dotenv import load_dotenv
54
from codehub.cli.helpers import read_yaml, copy_file, fill_file_placeholders
6-
from codehub.cli.config import STRUCTURE, DeployConfig, OAuthConfig
5+
from codehub.cli.config import STRUCTURE, DeployConfig, HubConfig
76

87

98
def create_deploy(
109
deploy_config: DeployConfig,
11-
contact_email=None,
12-
https=None,
13-
oauth_config: Optional[OAuthConfig] = None,
14-
admins=[],
10+
hub_config: HubConfig,
1511
):
16-
config_fps = __get_config_fps(https=https, oauth=oauth_config is not None)
12+
oauth_config = hub_config.oauth_config
13+
config_fps = __get_config_fps(
14+
https=hub_config.https, oauth=oauth_config is not None
15+
)
1716
template_fp = __build_template(deploy_config.hub_dir, config_fps)
1817

1918
placeholder_replacements = read_yaml(
2019
os.path.join(STRUCTURE["templates"]["hub"], "config_variables.yaml")
2120
)
2221
placeholder_replacements["SECRET_TOKEN"] = token_hex(32)
2322
placeholder_replacements["CLUSTER_NAME"] = deploy_config.name
24-
placeholder_replacements["HOST_NAME"] = https
25-
placeholder_replacements["CONTACT_EMAIL"] = contact_email
23+
placeholder_replacements["HOST_NAME"] = hub_config.https
24+
placeholder_replacements["CONTACT_EMAIL"] = hub_config.contact_email
2625
if oauth_config is not None:
2726
placeholder_replacements["GITHUB_OAUTH_CLIENT_ID"] = oauth_config.client_id
2827
placeholder_replacements["GITHUB_OAUTH_CLIENT_SECRET"] = (
2928
oauth_config.client_secret
3029
)
3130

31+
admins = hub_config.admins.copy()
3232
if "admin" not in admins:
3333
admins.append("admin")
3434

0 commit comments

Comments
 (0)