|
20 | 20 | get_all_available_tracks, |
21 | 21 | get_terraform_tracks_from_modules, |
22 | 22 | parse_track_yaml, |
| 23 | + remove_tracks_from_terraform_modules, |
23 | 24 | terraform_binary, |
24 | 25 | validate_track_can_be_deployed, |
25 | 26 | ) |
@@ -76,9 +77,7 @@ def deploy( |
76 | 77 | distinct_tracks = tmp_tracks |
77 | 78 |
|
78 | 79 | add_tracks_to_terraform_modules( |
79 | | - tracks=distinct_tracks - get_terraform_tracks_from_modules(), |
80 | | - remote=remote, |
81 | | - production=production, |
| 80 | + tracks=distinct_tracks - get_terraform_tracks_from_modules() |
82 | 81 | ) |
83 | 82 | else: |
84 | 83 | # Run generate first. |
@@ -137,10 +136,70 @@ def deploy( |
137 | 136 |
|
138 | 137 | for track in distinct_tracks: |
139 | 138 | if track.require_build_container: |
140 | | - # TODO: Ansible build containers here |
141 | | - |
142 | | - # TODO: Set the "build_container" OpenTofu variable to false in module.tf |
143 | | - pass |
| 139 | + run_ansible_playbook( |
| 140 | + remote=remote, |
| 141 | + production=production, |
| 142 | + track=track.name, |
| 143 | + path=os.path.join( |
| 144 | + find_ctf_root_directory(), "challenges", track.name, "ansible" |
| 145 | + ), |
| 146 | + playbook="build.yaml", |
| 147 | + execute_common=False, |
| 148 | + ) |
| 149 | + |
| 150 | + remove_tracks_from_terraform_modules( |
| 151 | + {track}, remote=remote, production=production |
| 152 | + ) |
| 153 | + add_tracks_to_terraform_modules( |
| 154 | + { |
| 155 | + Track( |
| 156 | + name=track.name, |
| 157 | + remote=track.remote, |
| 158 | + production=track.production, |
| 159 | + require_build_container=False, |
| 160 | + ) |
| 161 | + } |
| 162 | + ) |
| 163 | + |
| 164 | + try: |
| 165 | + subprocess.run( |
| 166 | + args=[terraform_binary(), "apply", "-auto-approve"], |
| 167 | + cwd=os.path.join(find_ctf_root_directory(), ".deploy"), |
| 168 | + check=True, |
| 169 | + ) |
| 170 | + except subprocess.CalledProcessError: |
| 171 | + LOG.warning( |
| 172 | + 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." |
| 173 | + ) |
| 174 | + |
| 175 | + if ( |
| 176 | + input("Do you want to clean and start over? [Y/n] ").lower() or "y" |
| 177 | + ) != "y": |
| 178 | + exit(code=1) |
| 179 | + |
| 180 | + force = True |
| 181 | + destroy( |
| 182 | + tracks=tracks, production=production, remote=remote, force=force |
| 183 | + ) |
| 184 | + |
| 185 | + distinct_tracks = generate( |
| 186 | + tracks=tracks, production=production, remote=remote |
| 187 | + ) |
| 188 | + |
| 189 | + subprocess.run( |
| 190 | + args=[terraform_binary(), "apply", "-auto-approve"], |
| 191 | + cwd=os.path.join(find_ctf_root_directory(), ".deploy"), |
| 192 | + check=True, |
| 193 | + ) |
| 194 | + except KeyboardInterrupt: |
| 195 | + LOG.warning( |
| 196 | + "CTRL+C was detected during Terraform deployment. Destroying everything..." |
| 197 | + ) |
| 198 | + force = True |
| 199 | + destroy( |
| 200 | + tracks=tracks, production=production, remote=remote, force=force |
| 201 | + ) |
| 202 | + exit(code=0) |
144 | 203 |
|
145 | 204 | if not os.path.exists( |
146 | 205 | path=( |
@@ -244,31 +303,39 @@ def deploy( |
244 | 303 | ) |
245 | 304 |
|
246 | 305 |
|
247 | | -def run_ansible_playbook(remote: str, production: bool, track: str, path: str) -> None: |
| 306 | +def run_ansible_playbook( |
| 307 | + remote: str, |
| 308 | + production: bool, |
| 309 | + track: str, |
| 310 | + path: str, |
| 311 | + playbook: str = "deploy.yaml", |
| 312 | + execute_common: bool = True, |
| 313 | +) -> None: |
248 | 314 | extra_args = [] |
249 | 315 | if remote: |
250 | 316 | extra_args += ["-e", f"ansible_incus_remote={remote}"] |
251 | 317 |
|
252 | 318 | if production: |
253 | 319 | extra_args += ["-e", "nsec_production=true"] |
254 | 320 |
|
255 | | - LOG.info(msg=f"Running common yaml with ansible for track {track}...") |
256 | | - ansible_args = [ |
257 | | - "ansible-playbook", |
258 | | - os.path.join("..", "..", "..", ".deploy", "common.yaml"), |
259 | | - "-i", |
260 | | - "inventory", |
261 | | - ] + extra_args |
262 | | - subprocess.run( |
263 | | - args=ansible_args, |
264 | | - cwd=path, |
265 | | - check=True, |
266 | | - ) |
| 321 | + if execute_common: |
| 322 | + LOG.info(msg=f"Running common yaml with ansible for track {track}...") |
| 323 | + ansible_args = [ |
| 324 | + "ansible-playbook", |
| 325 | + os.path.join("..", "..", "..", ".deploy", "common.yaml"), |
| 326 | + "-i", |
| 327 | + "inventory", |
| 328 | + ] + extra_args |
| 329 | + subprocess.run( |
| 330 | + args=ansible_args, |
| 331 | + cwd=path, |
| 332 | + check=True, |
| 333 | + ) |
267 | 334 |
|
268 | | - LOG.info(msg=f"Running deploy.yaml with ansible for track {track}...") |
| 335 | + LOG.info(msg=f"Running {playbook} with ansible for track {track}...") |
269 | 336 | ansible_args = [ |
270 | 337 | "ansible-playbook", |
271 | | - "deploy.yaml", |
| 338 | + playbook, |
272 | 339 | "-i", |
273 | 340 | "inventory", |
274 | 341 | ] + extra_args |
|
0 commit comments