1111from ctf .destroy import destroy
1212from ctf .generate import generate
1313from ctf .logger import LOG
14+ from ctf .models import Track
1415from ctf .utils import (
1516 add_tracks_to_terraform_modules ,
1617 check_git_lfs ,
1718 find_ctf_root_directory ,
18- get_all_available_tracks ,
19- get_terraform_tracks_from_modules ,
2019 parse_track_yaml ,
20+ remove_tracks_from_terraform_modules ,
2121 terraform_binary ,
22- validate_track_can_be_deployed ,
2322)
2423
2524app = typer .Typer ()
@@ -54,21 +53,10 @@ def deploy(
5453 ] = False ,
5554):
5655 ENV ["INCUS_REMOTE" ] = remote
57- if redeploy :
58- distinct_tracks = set (
59- track
60- for track in get_all_available_tracks ()
61- if validate_track_can_be_deployed (track = track ) and track in tracks
62- )
63-
64- add_tracks_to_terraform_modules (
65- tracks = distinct_tracks - get_terraform_tracks_from_modules (),
66- remote = remote ,
67- production = production ,
68- )
69- else :
70- # Run generate first.
71- distinct_tracks = generate (tracks = tracks , production = production , remote = remote )
56+ # Run generate first.
57+ distinct_tracks = generate (
58+ tracks = tracks , production = production , remote = remote , redeploy = redeploy
59+ )
7260
7361 # Check if Git LFS is installed on the system as it is required for deployment.
7462 if not check_git_lfs ():
@@ -84,7 +72,7 @@ def deploy(
8472 "git" ,
8573 "lfs" ,
8674 "pull" ,
87- f"--include={ ',' .join ([os .path .join ('challenges' , track , 'ansible' , '*' ) for track in distinct_tracks ])} " ,
75+ f"--include={ ',' .join ([os .path .join ('challenges' , track . name , 'ansible' , '*' ) for track in distinct_tracks ])} " ,
8876 ],
8977 check = True ,
9078 )
@@ -103,8 +91,9 @@ def deploy(
10391 if (input ("Do you want to clean and start over? [Y/n] " ).lower () or "y" ) != "y" :
10492 exit (code = 1 )
10593
106- force = True
107- destroy (tracks = tracks , production = production , remote = remote , force = force )
94+ destroy (tracks = tracks , production = production , remote = remote , force = True )
95+
96+ distinct_tracks = generate (tracks = tracks , production = production , remote = remote )
10897
10998 subprocess .run (
11099 args = [terraform_binary (), "apply" , "-auto-approve" ],
@@ -115,22 +104,81 @@ def deploy(
115104 LOG .warning (
116105 "CTRL+C was detected during Terraform deployment. Destroying everything..."
117106 )
118- force = True
119- destroy (tracks = tracks , production = production , remote = remote , force = force )
107+ destroy (tracks = tracks , production = production , remote = remote , force = True )
120108 exit (code = 0 )
121109
122110 for track in distinct_tracks :
111+ if track .require_build_container :
112+ run_ansible_playbook (
113+ remote = remote ,
114+ production = production ,
115+ track = track .name ,
116+ path = os .path .join (
117+ find_ctf_root_directory (), "challenges" , track .name , "ansible"
118+ ),
119+ playbook = "build.yaml" ,
120+ execute_common = False ,
121+ )
122+
123+ remove_tracks_from_terraform_modules (
124+ {track }, remote = remote , production = production
125+ )
126+ add_tracks_to_terraform_modules (
127+ {
128+ Track (
129+ name = track .name ,
130+ remote = track .remote ,
131+ production = track .production ,
132+ require_build_container = False ,
133+ )
134+ }
135+ )
136+
137+ try :
138+ subprocess .run (
139+ args = [terraform_binary (), "apply" , "-auto-approve" ],
140+ cwd = os .path .join (find_ctf_root_directory (), ".deploy" ),
141+ check = True ,
142+ )
143+ except subprocess .CalledProcessError :
144+ LOG .warning (
145+ f"The project could not deploy due to instable state. It is often due to CTRL+C while deploying as { os .path .basename (terraform_binary ())} was not able to save the state of each object created."
146+ )
147+
148+ if (
149+ input ("Do you want to clean and start over? [Y/n] " ).lower () or "y"
150+ ) != "y" :
151+ exit (code = 1 )
152+
153+ destroy (tracks = tracks , production = production , remote = remote , force = True )
154+
155+ distinct_tracks = generate (
156+ tracks = tracks , production = production , remote = remote
157+ )
158+
159+ subprocess .run (
160+ args = [terraform_binary (), "apply" , "-auto-approve" ],
161+ cwd = os .path .join (find_ctf_root_directory (), ".deploy" ),
162+ check = True ,
163+ )
164+ except KeyboardInterrupt :
165+ LOG .warning (
166+ "CTRL+C was detected during Terraform deployment. Destroying everything..."
167+ )
168+ destroy (tracks = tracks , production = production , remote = remote , force = True )
169+ exit (code = 0 )
170+
123171 if not os .path .exists (
124172 path = (
125173 path := os .path .join (
126- find_ctf_root_directory (), "challenges" , track , "ansible"
174+ find_ctf_root_directory (), "challenges" , track . name , "ansible"
127175 )
128176 )
129177 ):
130178 continue
131179
132180 run_ansible_playbook (
133- remote = remote , production = production , track = track , path = path
181+ remote = remote , production = production , track = track . name , path = path
134182 )
135183
136184 if not production :
@@ -154,7 +202,7 @@ def deploy(
154202
155203 if remote == "local" :
156204 LOG .debug (msg = f"Parsing track.yaml for track { track } " )
157- track_yaml = parse_track_yaml (track_name = track )
205+ track_yaml = parse_track_yaml (track_name = track . name )
158206
159207 for service in track_yaml ["services" ]:
160208 if service .get ("dev_port_mapping" ):
@@ -175,12 +223,12 @@ def deploy(
175223 "device" ,
176224 "add" ,
177225 machine_name ,
178- f"proxy-{ track } -{ service ['dev_port_mapping' ]} -to-{ service ['port' ]} " ,
226+ f"proxy-{ track . name } -{ service ['dev_port_mapping' ]} -to-{ service ['port' ]} " ,
179227 "proxy" ,
180228 f"listen=tcp:0.0.0.0:{ service ['dev_port_mapping' ]} " ,
181229 f"connect=tcp:127.0.0.1:{ service ['port' ]} " ,
182230 "--project" ,
183- track ,
231+ track . name ,
184232 ],
185233 cwd = path ,
186234 check = True ,
@@ -212,7 +260,7 @@ def deploy(
212260 msg = f"Running `incus project switch { tracks_list [track_index - 1 ]} `"
213261 )
214262 subprocess .run (
215- args = ["incus" , "project" , "switch" , tracks_list [track_index - 1 ]],
263+ args = ["incus" , "project" , "switch" , tracks_list [track_index - 1 ]. name ],
216264 check = True ,
217265 env = ENV ,
218266 )
@@ -222,7 +270,14 @@ def deploy(
222270 )
223271
224272
225- def run_ansible_playbook (remote : str , production : bool , track : str , path : str ) -> None :
273+ def run_ansible_playbook (
274+ remote : str ,
275+ production : bool ,
276+ track : str ,
277+ path : str ,
278+ playbook : str = "deploy.yaml" ,
279+ execute_common : bool = True ,
280+ ) -> None :
226281 extra_args = []
227282 if STATE ["verbose" ]:
228283 extra_args .append ("-vvv" )
@@ -232,23 +287,24 @@ def run_ansible_playbook(remote: str, production: bool, track: str, path: str) -
232287 if production :
233288 extra_args += ["-e" , "nsec_production=true" ]
234289
235- LOG .info (msg = f"Running common yaml with ansible for track { track } ..." )
236- ansible_args = [
237- "ansible-playbook" ,
238- os .path .join (find_ctf_root_directory (), ".deploy" , "common.yaml" ),
239- "-i" ,
240- "inventory" ,
241- ] + extra_args
242- subprocess .run (
243- args = ansible_args ,
244- cwd = path ,
245- check = True ,
246- )
290+ if execute_common :
291+ LOG .info (msg = f"Running common yaml with ansible for track { track } ..." )
292+ ansible_args = [
293+ "ansible-playbook" ,
294+ os .path .join (".." , ".." , ".." , ".deploy" , "common.yaml" ),
295+ "-i" ,
296+ "inventory" ,
297+ ] + extra_args
298+ subprocess .run (
299+ args = ansible_args ,
300+ cwd = path ,
301+ check = True ,
302+ )
247303
248- LOG .info (msg = f"Running deploy.yaml with ansible for track { track } ..." )
304+ LOG .info (msg = f"Running { playbook } with ansible for track { track } ..." )
249305 ansible_args = [
250306 "ansible-playbook" ,
251- "deploy.yaml" ,
307+ playbook ,
252308 "-i" ,
253309 "inventory" ,
254310 ] + extra_args
0 commit comments