Skip to content

Commit 17f9dec

Browse files
committed
iacconsolerc in agent
1 parent 7ee6be2 commit 17f9dec

2 files changed

Lines changed: 50 additions & 8 deletions

File tree

cmd/agent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ var agentCmd = &cobra.Command{
2727
Use: "agent",
2828
Short: "Run iacconsole-cli in agent mode",
2929
Long: `Connects to the IaCConsole server via WebSocket to receive and execute infrastructure commands.`,
30+
PreRun: func(cmd *cobra.Command, args []string) {
31+
initConfig()
32+
},
3033
Run: func(cmd *cobra.Command, args []string) {
3134
apiUrl := os.Getenv("IACCONSOLE_API_URL")
3235
wsURL, authHeader, accountID, err := utils.ParseAPIURL(apiUrl)

utils/agent_executor.go

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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

Comments
 (0)