33import logging
44import json
55from codehub .cli .gcp .terraform import (
6- TerraformOutput ,
76 setup_terraform ,
87 terraform_apply ,
98 terraform_output ,
109)
1110from distutils .dir_util import copy_tree
12- from codehub .cli .config import STRUCTURE
11+ from codehub .cli .config import (
12+ STRUCTURE ,
13+ CreateConfig ,
14+ DeployConfig ,
15+ HubConfig ,
16+ UpgradeConfig ,
17+ )
1318from codehub .cli .gcp .helpers import authenticate_k8s_GKE
1419from codehub .cli .k8s .create import create_k8s_resources
1520from codehub .cli .helm .create import create_deploy
16- from codehub .cli .helm .install import install_helm_chart , upgrade_helm_chart
21+ from codehub .cli .helm .install import install_helm_chart
1722from codehub .cli .helpers import get_cloud_dir , get_latest_deployment , read_yaml
18- from codehub .cli .manage import wait_for_hub_to_get_ready , get_ip
23+ from codehub .cli .manage import wait_for_hub_to_get_ready
1924import kubernetes .client .rest # Add this import for the exception handling
2025
2126
22- def create_infrastructure (name , region , zone , machine_type ) :
23- _ , _ , k8s_dir , cloud_dir = __create_deploy_structure (name )
27+ def create_infrastructure (config : CreateConfig ) -> DeployConfig :
28+ helm_dir , hub_dir , k8s_dir , cloud_dir = __create_deploy_structure (config . name )
2429
2530 setup_terraform (
26- cluster_name = name ,
27- region = region ,
28- zone = zone ,
29- machine_type = machine_type ,
31+ config = config ,
3032 cloud_dir = cloud_dir ,
3133 )
3234
33- cloud_config = terraform_apply (cloud_dir = cloud_dir )
34- __create_k8s_resources (
35- name ,
36- deploy_dir = k8s_dir ,
37- nfs_name = cloud_config .nfs_name ,
38- nfs_ip = cloud_config .nfs_ip ,
35+ cloud_state = terraform_apply (cloud_dir = cloud_dir )
36+ deploy_config = DeployConfig (
37+ config .name , config .region , helm_dir , hub_dir , k8s_dir , cloud_state
3938 )
39+ __create_k8s_resources (deploy_config )
4040
41- return get_ip ( name , k8s_dir )
41+ return deploy_config
4242
4343
44- def create (name , admins , region , zone , machine_type ):
45- helm_dir , hub_dir , k8s_dir , cloud_dir = __create_deploy_structure (name )
44+ def create (config : CreateConfig ) -> DeployConfig :
45+ deploy_config = create_infrastructure (config )
46+ hub_config = HubConfig (config .admins )
4647
47- setup_terraform (
48- cluster_name = name ,
49- region = region ,
50- zone = zone ,
51- machine_type = machine_type ,
52- cloud_dir = cloud_dir ,
53- )
48+ __create_k8s_resources (deploy_config )
5449
55- cloud_state = terraform_apply (cloud_dir = cloud_dir )
56- __create_k8s_resources (
57- name ,
58- deploy_dir = k8s_dir ,
59- nfs_name = cloud_state .nfs_name ,
60- nfs_ip = cloud_state .nfs_ip ,
61- )
62-
63- __install_helm_chart (
64- name ,
65- region = region ,
66- helm_deploy_dir = helm_dir ,
67- hub_deploy_dir = hub_dir ,
68- cloud_state = cloud_state ,
69- admins = admins ,
70- )
50+ __create_deploy (deploy_config , hub_config )
51+ install_helm_chart (deploy_config , upgrade = False )
7152
72- wait_for_hub_to_get_ready (k8s_dir )
53+ wait_for_hub_to_get_ready (deploy_config . k8s_dir )
7354
74- return get_ip ( name , k8s_dir )
55+ return deploy_config
7556
7657
77- def upgrade (name , admins , https = None , client_id = None , client_secret = None ):
78- old_deploy_dir = get_latest_deployment (name = name )
58+ def upgrade (config : UpgradeConfig ):
59+ old_deploy_dir = get_latest_deployment (config . name )
7960
8061 helm_dir , hub_dir , k8s_dir , cloud_dir = __upgrade_deploy_structure (
81- name , old_deploy_dir
62+ config . name , old_deploy_dir
8263 )
8364 cloud_state = terraform_output (cloud_dir = cloud_dir )
8465
85- last_helm_install = read_yaml (os .path .join (old_deploy_dir , "helm" , "chart.yaml" ))[
86- "install "
66+ region = read_yaml (os .path .join (old_deploy_dir , "helm" , "chart.yaml" ))[ "install" ] [
67+ "region "
8768 ]
69+ deploy_config = DeployConfig (
70+ config .name , region , helm_dir , hub_dir , k8s_dir , cloud_state
71+ )
8872
89- __upgrade_helm_chart (
90- name ,
91- region = last_helm_install ["region" ],
92- helm_deploy_dir = helm_dir ,
93- hub_deploy_dir = hub_dir ,
94- cloud_state = cloud_state ,
95- admins = admins ,
96- https = https ,
97- client_id = client_id ,
98- client_secret = client_secret ,
73+ __create_deploy (
74+ deploy_config ,
75+ config .hub_config ,
9976 )
77+ install_helm_chart (deploy_config , upgrade = True )
10078
101- authenticate_k8s_GKE (name )
79+ authenticate_k8s_GKE (config . name )
10280 wait_for_hub_to_get_ready (k8s_dir )
10381
10482
@@ -136,10 +114,10 @@ def __upgrade_deploy_structure(name, old_deploy_dir):
136114 return helm_dir , hub_dir , k8s_dir , cloud_dir
137115
138116
139- def __create_k8s_resources (name , deploy_dir , nfs_name , nfs_ip ):
140- authenticate_k8s_GKE (name )
117+ def __create_k8s_resources (deploy_config : DeployConfig ):
118+ authenticate_k8s_GKE (deploy_config . name )
141119 try :
142- create_k8s_resources (deploy_dir , nfs_name , nfs_ip )
120+ create_k8s_resources (deploy_config )
143121 except kubernetes .client .rest .ApiException as e :
144122 logger = logging .getLogger (__name__ )
145123 # If the error is due to a resource already existing, we can continue
@@ -150,72 +128,11 @@ def __create_k8s_resources(name, deploy_dir, nfs_name, nfs_ip):
150128 raise
151129
152130
153- def __install_helm_chart (
154- name ,
155- region ,
156- helm_deploy_dir ,
157- hub_deploy_dir ,
158- cloud_state : TerraformOutput ,
159- admins ,
160- ):
161- __create_deploy (name , helm_deploy_dir , hub_deploy_dir , cloud_state , admins )
162- install_helm_chart (
163- name ,
164- region = region ,
165- helm_deploy_dir = helm_deploy_dir ,
166- hub_deploy_dir = hub_deploy_dir ,
167- )
168-
169-
170- def __upgrade_helm_chart (
171- name ,
172- region ,
173- helm_deploy_dir ,
174- hub_deploy_dir ,
175- cloud_state : TerraformOutput ,
176- admins ,
177- https = None ,
178- client_id = None ,
179- client_secret = None ,
180- ):
181- __create_deploy (
182- name ,
183- helm_deploy_dir ,
184- hub_deploy_dir ,
185- cloud_state = cloud_state ,
186- admins = admins ,
187- https = https ,
188- client_id = client_id ,
189- client_secret = client_secret ,
190- )
191-
192- upgrade_helm_chart (
193- name , region , helm_deploy_dir = helm_deploy_dir , hub_deploy_dir = hub_deploy_dir
194- )
195-
196-
197131def __create_deploy (
198- name ,
199- helm_deploy_dir ,
200- hub_deploy_dir ,
201- cloud_state : TerraformOutput ,
202- admins ,
203- https = None ,
204- client_id = None ,
205- client_secret = None ,
132+ deploy_config : DeployConfig ,
133+ hub_config : HubConfig ,
206134):
207- contact_email = None
208135 with open (STRUCTURE ["secrets" ]["gcp" ]["sa" ], "rt" ) as f :
209- contact_email = json .loads (f .read ())["client_email" ]
210-
211- create_deploy (
212- name ,
213- contact_email = contact_email ,
214- admins = admins ,
215- helm_deploy_dir = helm_deploy_dir ,
216- hub_deploy_dir = hub_deploy_dir ,
217- cloud_state = cloud_state ,
218- https = https ,
219- client_id = client_id ,
220- client_secret = client_secret ,
221- )
136+ hub_config .contact_email = json .loads (f .read ())["client_email" ]
137+
138+ create_deploy (deploy_config , hub_config )
0 commit comments