1212 from ddev .e2e .agent .interface import AgentInterface
1313
1414
15- def _invoke_check_with_retry (agent : AgentInterface , args : list [str ], * , retries : int = 3 , backoff : float = 0.5 ) -> None :
15+ def _invoke_check_with_retry (
16+ agent : AgentInterface ,
17+ args : list [str ],
18+ * ,
19+ env_vars : dict [str , str ] | None = None ,
20+ retries : int = 3 ,
21+ backoff : float = 0.5 ,
22+ ) -> None :
1623 """Invoke ``agent check`` with bounded retry to absorb transient autodiscovery-reload races."""
1724 import subprocess
1825 import time
1926
2027 for attempt in range (retries + 1 ):
2128 try :
22- agent .invoke (args )
29+ agent .invoke (args , env_vars = env_vars )
2330 return
2431 except subprocess .CalledProcessError :
2532 if attempt >= retries :
@@ -38,8 +45,23 @@ def _invoke_check_with_retry(agent: AgentInterface, args: list[str], *, retries:
3845@click .argument ('environment' )
3946@click .argument ('args' , required = True , nargs = - 1 )
4047@click .option ('--config-file' , hidden = True )
48+ @click .option (
49+ '--env' ,
50+ 'env_vars' ,
51+ multiple = True ,
52+ metavar = 'KEY=VALUE' ,
53+ help = 'Set an environment variable for this invocation only (may be repeated)' ,
54+ )
4155@click .pass_obj
42- def agent (app : Application , * , intg_name : str , environment : str , args : tuple [str , ...], config_file : str | None ):
56+ def agent (
57+ app : Application ,
58+ * ,
59+ intg_name : str ,
60+ environment : str ,
61+ args : tuple [str , ...],
62+ config_file : str | None ,
63+ env_vars : tuple [str , ...],
64+ ):
4365 """
4466 Invoke the Agent.
4567 """
@@ -60,6 +82,8 @@ def agent(app: Application, *, intg_name: str, environment: str, args: tuple[str
6082 agent_type = metadata .get (E2EMetadata .AGENT_TYPE , DEFAULT_AGENT_TYPE )
6183 agent = get_agent_interface (agent_type )(app , integration , environment , metadata , env_data .config_file )
6284
85+ invoke_env_vars = dict (entry .split ('=' , 1 ) for entry in env_vars ) if env_vars else None
86+
6387 full_args = list (args )
6488 trigger_run = False
6589 if full_args [0 ] == 'check' :
@@ -75,9 +99,9 @@ def agent(app: Application, *, intg_name: str, environment: str, args: tuple[str
7599 if config_file is None or not trigger_run :
76100 try :
77101 if trigger_run :
78- _invoke_check_with_retry (agent , full_args )
102+ _invoke_check_with_retry (agent , full_args , env_vars = invoke_env_vars )
79103 else :
80- agent .invoke (full_args )
104+ agent .invoke (full_args , env_vars = invoke_env_vars )
81105 except subprocess .CalledProcessError as e :
82106 app .abort (code = e .returncode )
83107
@@ -90,14 +114,14 @@ def agent(app: Application, *, intg_name: str, environment: str, args: tuple[str
90114 if not env_data .config_file .is_file ():
91115 try :
92116 env_data .write_config (config )
93- _invoke_check_with_retry (agent , full_args )
117+ _invoke_check_with_retry (agent , full_args , env_vars = invoke_env_vars )
94118 finally :
95119 env_data .config_file .unlink ()
96120 else :
97121 temp_config_file = env_data .config_file .parent / f'{ env_data .config_file .name } .bak.example'
98122 env_data .config_file .replace (temp_config_file )
99123 try :
100124 env_data .write_config (config )
101- _invoke_check_with_retry (agent , full_args )
125+ _invoke_check_with_retry (agent , full_args , env_vars = invoke_env_vars )
102126 finally :
103127 temp_config_file .replace (env_data .config_file )
0 commit comments