@@ -106,7 +106,9 @@ def add_tracks_to_terraform_modules(tracks: set[Track]):
106106 build_container = {{ 'true' if track.require_build_container else 'false' }}
107107 {% if track.production %}deploy = "production"{% endif %}
108108 {% if track.remote %}incus_remote = "{{ track.remote }}"{% endif %}
109- depends_on = [module.common]
109+ {% for ov in output_variables %}
110+ {{ ov }} = module.common.{{ ov }}
111+ {% endfor %}
110112 }
111113 {% endfor %}
112114 """
@@ -115,6 +117,7 @@ def add_tracks_to_terraform_modules(tracks: set[Track]):
115117 fd .write (
116118 template .render (
117119 tracks = tracks - get_terraform_tracks_from_modules (),
120+ output_variables = get_common_modules_output_variables (),
118121 )
119122 )
120123
@@ -138,6 +141,84 @@ def create_terraform_modules_file(remote: str, production: bool = False):
138141 fd .write (template .render (production = production , remote = remote ))
139142
140143
144+ def get_common_modules_output_variables () -> set [str ]:
145+ output_variables : set [str ] = set ()
146+ output_variable_regex : re .Pattern = re .compile (
147+ r'^output\s*"([a-zA-Z_\-]+)"\s*{' , re .MULTILINE
148+ )
149+ variable_regex : re .Pattern = re .compile (
150+ r'^variable\s*"([a-zA-Z_\-]+)"\s*{' , re .MULTILINE
151+ )
152+
153+ variables : set [str ] = set ()
154+
155+ for file in os .listdir (
156+ path := os .path .join (find_ctf_root_directory (), ".deploy" , "common" )
157+ ):
158+ if file == "versions.tf" :
159+ continue
160+
161+ with open (os .path .join (path , file ), "r" ) as f :
162+ match file :
163+ case "variables.tf" :
164+ for i in variable_regex .findall (f .read ()):
165+ variables .add (i )
166+ case _:
167+ for i in output_variable_regex .findall (f .read ()):
168+ output_variables .add (i )
169+
170+ for variable in output_variables - variables :
171+ LOG .error (
172+ msg
173+ := f'Variable "{ variable } " could not be found in "variables.tf". This could cause an issue when creating/destroying an environment.'
174+ )
175+
176+ if (
177+ input (f'Do you want to add "{ variable } " to "variables.tf"? [y/N] ' ).lower ()
178+ or "n"
179+ ) == "n" :
180+ raise Exception (msg )
181+
182+ try :
183+ print ("Do CTRL+C to cancel..." )
184+ while not (default := input ("What is the default value? " )):
185+ print ("Do CTRL+C to cancel..." )
186+
187+ var_type = input ("What is the type? [string] " ) or "string"
188+
189+ with open (os .path .join (path , "variables.tf" ), "a" ) as f :
190+ f .write ("\n " )
191+ template = jinja2 .Environment ().from_string (
192+ source = textwrap .dedent (
193+ text = """\
194+ variable "{{variable}}" {
195+ default = "{{default}}"
196+ type = {{type}}
197+ }
198+ """
199+ )
200+ )
201+ f .write (
202+ template .render (variable = variable , default = default , type = var_type )
203+ )
204+ variables .add (variable )
205+ except KeyboardInterrupt :
206+ LOG .warning (
207+ f'Cancelling the addition of the "{ variable } " to "variables.tf".'
208+ )
209+
210+ raise Exception (msg )
211+
212+ if len (output_variables - variables ) != 0 :
213+ LOG .critical (
214+ msg
215+ := f'Some output variables were not found in "variables.tf": { ", " .join (output_variables - variables )} '
216+ )
217+ raise Exception (msg )
218+
219+ return output_variables & variables
220+
221+
141222def get_terraform_tracks_from_modules () -> set [Track ]:
142223 with open (
143224 file = os .path .join (find_ctf_root_directory (), ".deploy" , "modules.tf" ), mode = "r"
0 commit comments