Skip to content

Commit 30a6067

Browse files
Deployment configuration
1 parent 60ee604 commit 30a6067

5 files changed

Lines changed: 69 additions & 94 deletions

File tree

codehub/cli/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
from pathlib import Path
44

5+
from codehub.cli.gcp.terraform import TerraformOutput
6+
57

68
ROOT = Path(__file__).parents[2]
79
PACKAGE_ROOT = Path(__file__).parents[1]
@@ -50,3 +52,13 @@ class CreateConfig:
5052
class OAuthConfig:
5153
client_id: str
5254
client_secret: str
55+
56+
57+
@dataclass
58+
class DeployConfig:
59+
name: str
60+
region: str
61+
helm_dir: str
62+
hub_dir: str
63+
k8s_dir: str
64+
cloud_state: TerraformOutput

codehub/cli/create.py

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44
import json
55
from typing import Optional
66
from codehub.cli.gcp.terraform import (
7-
TerraformOutput,
87
setup_terraform,
98
terraform_apply,
109
terraform_output,
1110
)
1211
from distutils.dir_util import copy_tree
13-
from codehub.cli.config import STRUCTURE, CreateConfig, OAuthConfig
12+
from codehub.cli.config import STRUCTURE, CreateConfig, DeployConfig, OAuthConfig
1413
from codehub.cli.gcp.helpers import authenticate_k8s_GKE
1514
from codehub.cli.k8s.create import create_k8s_resources
1615
from codehub.cli.helm.create import create_deploy
@@ -21,20 +20,18 @@
2120

2221

2322
def create_infrastructure(config: CreateConfig):
24-
_, _, k8s_dir, cloud_dir = __create_deploy_structure(config.name)
23+
helm_dir, hub_dir, k8s_dir, cloud_dir = __create_deploy_structure(config.name)
2524

2625
setup_terraform(
2726
config=config,
2827
cloud_dir=cloud_dir,
2928
)
3029

3130
cloud_state = terraform_apply(cloud_dir=cloud_dir)
32-
__create_k8s_resources(
33-
config.name,
34-
deploy_dir=k8s_dir,
35-
nfs_name=cloud_state.nfs_name,
36-
nfs_ip=cloud_state.nfs_ip,
31+
deploy_config = DeployConfig(
32+
config.name, config.region, helm_dir, hub_dir, k8s_dir, cloud_state
3733
)
34+
__create_k8s_resources(deploy_config)
3835

3936
return get_ip(config.name, k8s_dir)
4037

@@ -48,19 +45,14 @@ def create(config: CreateConfig):
4845
)
4946

5047
cloud_state = terraform_apply(cloud_dir=cloud_dir)
51-
__create_k8s_resources(
52-
config.name,
53-
deploy_dir=k8s_dir,
54-
nfs_name=cloud_state.nfs_name,
55-
nfs_ip=cloud_state.nfs_ip,
48+
deploy_config = DeployConfig(
49+
config.name, config.region, helm_dir, hub_dir, k8s_dir, cloud_state
5650
)
5751

52+
__create_k8s_resources(deploy_config)
53+
5854
__install_helm_chart(
59-
config.name,
60-
region=config.region,
61-
helm_deploy_dir=helm_dir,
62-
hub_deploy_dir=hub_dir,
63-
cloud_state=cloud_state,
55+
deploy_config,
6456
admins=config.admins,
6557
)
6658

@@ -77,16 +69,13 @@ def upgrade(name, admins, https=None, oauth_config: Optional[OAuthConfig] = None
7769
)
7870
cloud_state = terraform_output(cloud_dir=cloud_dir)
7971

80-
last_helm_install = read_yaml(os.path.join(old_deploy_dir, "helm", "chart.yaml"))[
81-
"install"
72+
region = read_yaml(os.path.join(old_deploy_dir, "helm", "chart.yaml"))["install"][
73+
"region"
8274
]
75+
deploy_config = DeployConfig(name, region, helm_dir, hub_dir, k8s_dir, cloud_state)
8376

8477
__upgrade_helm_chart(
85-
name,
86-
region=last_helm_install["region"],
87-
helm_deploy_dir=helm_dir,
88-
hub_deploy_dir=hub_dir,
89-
cloud_state=cloud_state,
78+
deploy_config,
9079
admins=admins,
9180
https=https,
9281
oauth_config=oauth_config,
@@ -130,10 +119,10 @@ def __upgrade_deploy_structure(name, old_deploy_dir):
130119
return helm_dir, hub_dir, k8s_dir, cloud_dir
131120

132121

133-
def __create_k8s_resources(name, deploy_dir, nfs_name, nfs_ip):
134-
authenticate_k8s_GKE(name)
122+
def __create_k8s_resources(deploy_config: DeployConfig):
123+
authenticate_k8s_GKE(deploy_config.name)
135124
try:
136-
create_k8s_resources(deploy_dir, nfs_name, nfs_ip)
125+
create_k8s_resources(deploy_config)
137126
except kubernetes.client.rest.ApiException as e:
138127
logger = logging.getLogger(__name__)
139128
# If the error is due to a resource already existing, we can continue
@@ -145,52 +134,31 @@ def __create_k8s_resources(name, deploy_dir, nfs_name, nfs_ip):
145134

146135

147136
def __install_helm_chart(
148-
name,
149-
region,
150-
helm_deploy_dir,
151-
hub_deploy_dir,
152-
cloud_state: TerraformOutput,
137+
deploy_config: DeployConfig,
153138
admins,
154139
):
155-
__create_deploy(name, helm_deploy_dir, hub_deploy_dir, cloud_state, admins)
156-
install_helm_chart(
157-
name,
158-
region=region,
159-
helm_deploy_dir=helm_deploy_dir,
160-
hub_deploy_dir=hub_deploy_dir,
161-
)
140+
__create_deploy(deploy_config, admins)
141+
install_helm_chart(deploy_config)
162142

163143

164144
def __upgrade_helm_chart(
165-
name,
166-
region,
167-
helm_deploy_dir,
168-
hub_deploy_dir,
169-
cloud_state: TerraformOutput,
145+
deploy_config: DeployConfig,
170146
admins,
171147
https=None,
172148
oauth_config: Optional[OAuthConfig] = None,
173149
):
174150
__create_deploy(
175-
name,
176-
helm_deploy_dir,
177-
hub_deploy_dir,
178-
cloud_state=cloud_state,
151+
deploy_config,
179152
admins=admins,
180153
https=https,
181154
oauth_config=oauth_config,
182155
)
183156

184-
upgrade_helm_chart(
185-
name, region, helm_deploy_dir=helm_deploy_dir, hub_deploy_dir=hub_deploy_dir
186-
)
157+
upgrade_helm_chart(deploy_config)
187158

188159

189160
def __create_deploy(
190-
name,
191-
helm_deploy_dir,
192-
hub_deploy_dir,
193-
cloud_state: TerraformOutput,
161+
deploy_config: DeployConfig,
194162
admins,
195163
https=None,
196164
oauth_config: Optional[OAuthConfig] = None,
@@ -200,12 +168,9 @@ def __create_deploy(
200168
contact_email = json.loads(f.read())["client_email"]
201169

202170
create_deploy(
203-
name,
171+
deploy_config,
204172
contact_email=contact_email,
205173
admins=admins,
206-
helm_deploy_dir=helm_deploy_dir,
207-
hub_deploy_dir=hub_deploy_dir,
208-
cloud_state=cloud_state,
209174
https=https,
210175
oauth_config=oauth_config,
211176
)

codehub/cli/helm/create.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22
from secrets import token_hex
33
from typing import Optional
44
from dotenv import load_dotenv
5-
from codehub.cli.gcp.terraform import TerraformOutput
6-
from codehub.cli.helpers import read_yaml, run_cmd, copy_file, fill_file_placeholders
7-
from codehub.cli.config import STRUCTURE, OAuthConfig
5+
from codehub.cli.helpers import read_yaml, copy_file, fill_file_placeholders
6+
from codehub.cli.config import STRUCTURE, DeployConfig, OAuthConfig
87

98

109
def create_deploy(
11-
cluster_name,
12-
hub_deploy_dir,
13-
helm_deploy_dir,
14-
cloud_state: TerraformOutput,
10+
deploy_config: DeployConfig,
1511
contact_email=None,
1612
https=None,
1713
oauth_config: Optional[OAuthConfig] = None,
1814
admins=[],
1915
):
2016
config_fps = __get_config_fps(https=https, oauth=oauth_config is not None)
21-
template_fp = __build_template(hub_deploy_dir, config_fps)
17+
template_fp = __build_template(deploy_config.hub_dir, config_fps)
2218

2319
placeholder_replacements = read_yaml(
2420
os.path.join(STRUCTURE["templates"]["hub"], "config_variables.yaml")
2521
)
2622
placeholder_replacements["SECRET_TOKEN"] = token_hex(32)
27-
placeholder_replacements["CLUSTER_NAME"] = cluster_name
23+
placeholder_replacements["CLUSTER_NAME"] = deploy_config.name
2824
placeholder_replacements["HOST_NAME"] = https
2925
placeholder_replacements["CONTACT_EMAIL"] = contact_email
3026
if oauth_config is not None:
@@ -44,6 +40,7 @@ def admin_to_yaml_str(admin):
4440
)
4541
placeholder_replacements["SUDOERS"] = " ".join(admins)
4642

43+
cloud_state = deploy_config.cloud_state
4744
placeholder_replacements["REGISTRY_HOSTNAME"] = cloud_state.docker_registry_hostname
4845
# The spaces after newline are important for the indentation in the yaml file
4946
placeholder_replacements["REGISTRY_PASSWORD"] = cloud_state.hub_sa_key.replace(
@@ -54,9 +51,9 @@ def admin_to_yaml_str(admin):
5451
if "DOCKER_IMAGE_TAG" not in placeholder_replacements:
5552
placeholder_replacements["DOCKER_IMAGE_TAG"] = "stable"
5653

57-
__add_config_content(hub_deploy_dir, template_fp, placeholder_replacements)
54+
__add_config_content(deploy_config.hub_dir, template_fp, placeholder_replacements)
5855

59-
__add_helm_chart_config(helm_deploy_dir)
56+
__add_helm_chart_config(deploy_config.helm_dir)
6057

6158

6259
def __get_config_fps(https=None, oauth=None):

codehub/cli/helm/install.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
import os
22
from codehub.cli.helpers import run_cmd, read_yaml, fill_file_placeholders
3-
from codehub.cli.config import STRUCTURE
3+
from codehub.cli.config import STRUCTURE, DeployConfig
44

55

6-
def install_helm_chart(cluster_name, region, helm_deploy_dir, hub_deploy_dir):
7-
__upgrade_or_install_helm_chart(
8-
cluster_name, region, helm_deploy_dir, hub_deploy_dir, upgrade=False
9-
)
6+
def install_helm_chart(deploy_config: DeployConfig):
7+
__upgrade_or_install_helm_chart(deploy_config, upgrade=False)
108

119

12-
def upgrade_helm_chart(cluster_name, region, helm_deploy_dir, hub_deploy_dir):
13-
__upgrade_or_install_helm_chart(
14-
cluster_name, region, helm_deploy_dir, hub_deploy_dir, upgrade=True
15-
)
10+
def upgrade_helm_chart(deploy_config: DeployConfig):
11+
__upgrade_or_install_helm_chart(deploy_config, upgrade=True)
1612

1713

18-
def __upgrade_or_install_helm_chart(
19-
cluster_name, region, helm_deploy_dir, hub_deploy_dir, upgrade=False
20-
):
14+
def __upgrade_or_install_helm_chart(deploy_config: DeployConfig, upgrade=False):
2115
chart_template_fp = os.path.join(STRUCTURE["templates"]["helm"], "chart.yaml")
22-
chart_deploy_fp = os.path.join(helm_deploy_dir, "chart.yaml")
16+
chart_deploy_fp = os.path.join(deploy_config.helm_dir, "chart.yaml")
2317

24-
placeholder_replacements = dict(REGION=region)
18+
placeholder_replacements = dict(REGION=deploy_config.region)
2519
fill_file_placeholders(chart_template_fp, chart_deploy_fp, placeholder_replacements)
2620

2721
helm_config = read_yaml(chart_deploy_fp)
2822
repo_config = helm_config["repo"]
2923
install_config = helm_config["install"]
3024

31-
config_file = os.path.join(hub_deploy_dir, "config.yaml")
25+
config_file = os.path.join(deploy_config.hub_dir, "config.yaml")
3226

3327
cmds = []
3428
cmds.append(
@@ -38,7 +32,7 @@ def __upgrade_or_install_helm_chart(
3832
"clusters",
3933
"get-credentials",
4034
f"--location={install_config['region']}",
41-
cluster_name,
35+
deploy_config.name,
4236
]
4337
)
4438

codehub/cli/k8s/create.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
from kubernetes import client as k8s_client
22
import os
33
from codehub.cli.helpers import read_yaml, fill_file_placeholders
4-
from codehub.cli.config import STRUCTURE
4+
from codehub.cli.config import STRUCTURE, DeployConfig
55

66

7-
def create_k8s_resources(k8s_deploy_path, nfs_name, nfs_ip):
8-
__create_cluster_role_binding(k8s_deploy_path)
7+
def create_k8s_resources(deploy_config: DeployConfig):
8+
k8s_dir = deploy_config.k8s_dir
9+
10+
__create_cluster_role_binding(k8s_dir)
911

1012
namespace = read_yaml(
1113
os.path.join(STRUCTURE["templates"]["k8s"], "namespace.yaml")
1214
)["metadata"]["name"]
1315

1416
placeholder_replacements = {"NAMESPACE": namespace}
1517

16-
__create_namespace(k8s_deploy_path, placeholder_replacements)
17-
__create_pvs(k8s_deploy_path, nfs_name, nfs_ip, placeholder_replacements)
18-
__create_pvcs(k8s_deploy_path, nfs_name, nfs_ip, placeholder_replacements)
18+
__create_namespace(k8s_dir, placeholder_replacements)
19+
__create_pvs(
20+
k8s_dir,
21+
deploy_config.cloud_state.nfs_name,
22+
deploy_config.cloud_state.nfs_ip,
23+
placeholder_replacements,
24+
)
25+
__create_pvcs(k8s_dir, placeholder_replacements)
1926

2027

2128
def __create_cluster_role_binding(k8s_deploy_path):
@@ -50,7 +57,7 @@ def __create_pvs(k8s_deploy_path, nfs_name, nfs_ip, placeholder_replacements):
5057
)
5158

5259

53-
def __create_pvcs(k8s_deploy_path, nfs_name, nfs_ip, placeholder_replacements):
60+
def __create_pvcs(k8s_deploy_path, placeholder_replacements):
5461
for pvc in ["pvc-personal.yaml", "pvc-shared.yaml"]:
5562
__create_k8s_resource_from_template(
5663
client_call=k8s_client.CoreV1Api().create_namespaced_persistent_volume_claim,

0 commit comments

Comments
 (0)