66 "log"
77 "os"
88 "os/exec"
9+ "path/filepath"
910 "strings"
1011 "sync"
1112 "time"
@@ -28,17 +29,55 @@ func ExecuteAgentCommand(conn *websocket.Conn, cmd AgentCommand, state *State) {
2829 state .ParsedDimensions [dp .Key ] = dp .Value
2930 }
3031
31- // 2. Setup backend config first to set StateS3Path
32+ // Convert dimensions to DimensionsFlags format for compatibility
33+ state .DimensionsFlags = make ([]string , 0 , len (cmd .Dimensions ))
34+ for _ , dp := range cmd .Dimensions {
35+ state .DimensionsFlags = append (state .DimensionsFlags , dp .Key + ":" + dp .Value )
36+ }
37+
38+ // 2. Setup paths from viper configs (like in exec mode)
39+ unitPath , err := filepath .Abs (state .GetStringFromViperByOrgOrDefault ("units_path" ) + "/" + state .OrgName + "/" + state .UnitName )
40+ if err != nil {
41+ log .Printf ("Error resolving unit path: %v" , err )
42+ sendComplete (conn , cmd .ID , 1 , err .Error ())
43+ return
44+ }
45+ state .UnitPath = unitPath
46+
47+ if sharedModulesPath := state .GetStringFromViperByOrgOrDefault ("shared_modules_path" ); sharedModulesPath != "" {
48+ absPath , err := filepath .Abs (sharedModulesPath )
49+ if err != nil {
50+ log .Printf ("Error resolving shared modules path: %v" , err )
51+ sendComplete (conn , cmd .ID , 1 , err .Error ())
52+ return
53+ }
54+ state .SharedModulesPath = absPath
55+ }
56+
57+ if inventoryPath := state .GetStringFromViperByOrgOrDefault ("inventory_path" ); inventoryPath != "" {
58+ absPath , err := filepath .Abs (inventoryPath + "/" + state .OrgName )
59+ if err != nil {
60+ log .Printf ("Error resolving inventory path: %v" , err )
61+ sendComplete (conn , cmd .ID , 1 , err .Error ())
62+ return
63+ }
64+ state .InventoryPath = absPath
65+ }
66+
67+ // 3. Parse unit manifest (needed before SetupBackendConfig)
68+ state .ParseUnitManifest ("unit_manifest.json" )
69+
70+ // 4. Setup backend config (depends on UnitManifest)
3271 backendConfig := state .SetupBackendConfig ()
3372
34- // 3. Setup paths - handle error gracefully
73+ // 5. Prepare temp directory - handle error gracefully
3574 if err := state .PrepareTemp (); err != nil {
3675 log .Printf ("Error preparing temp directory: %v" , err )
3776 sendComplete (conn , cmd .ID , 1 , err .Error ())
3877 return
3978 }
4079
41- // 4 . Generate variables - handle errors gracefully
80+ // 6 . Generate variables - handle errors gracefully
4281 if err := state .GenerateVarsByDims (); err != nil {
4382 log .Printf ("Error generating vars by dimensions: %v" , err )
4483 sendComplete (conn , cmd .ID , 1 , err .Error ())
@@ -63,7 +102,7 @@ func ExecuteAgentCommand(conn *websocket.Conn, cmd AgentCommand, state *State) {
63102 return
64103 }
65104
66- // 5 . Prepare execution
105+ // 7 . Prepare execution
67106 cmdToExec := state .GetStringFromViperByOrgOrDefault ("cmd_to_exec" )
68107 if cmdToExec == "" {
69108 cmdToExec = "tofu"
@@ -77,7 +116,7 @@ func ExecuteAgentCommand(conn *websocket.Conn, cmd AgentCommand, state *State) {
77116 }
78117 args = append (args , cmd .ExtraArgs ... )
79118
80- // 6 . Spawn process
119+ // 8 . Spawn process
81120 log .Printf ("Agent executing: %s %s" , cmdToExec , strings .Join (args , " " ))
82121 child := exec .Command (cmdToExec , args ... )
83122 child .Dir = state .CmdWorkTempDir
@@ -86,13 +125,13 @@ func ExecuteAgentCommand(conn *websocket.Conn, cmd AgentCommand, state *State) {
86125 stdout , _ := child .StdoutPipe ()
87126 stderr , _ := child .StderrPipe ()
88127
89- err : = child .Start ()
128+ err = child .Start ()
90129 if err != nil {
91130 sendComplete (conn , cmd .ID , 1 , err .Error ())
92131 return
93132 }
94133
95- // 7 . Stream output
134+ // 9 . Stream output
96135 var mu sync.Mutex
97136 done := make (chan bool )
98137 go streamPipe (conn , & mu , cmd .ID , "stdout" , stdout , done )
@@ -111,7 +150,7 @@ func ExecuteAgentCommand(conn *websocket.Conn, cmd AgentCommand, state *State) {
111150 }
112151 }
113152
114- // 8 . Cleanup
153+ // 10 . Cleanup
115154 if exitCode == 0 && (cmd .Action == "apply" || cmd .Action == "destroy" ) {
116155 os .RemoveAll (state .CmdWorkTempDir )
117156 }
0 commit comments