Skip to content

Commit 479eac4

Browse files
committed
Add new operation new cmd command
1 parent c9c1aad commit 479eac4

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

cmd/operator.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log/slog"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
12+
"github.com/spf13/cobra"
13+
"github.com/spf13/pflag"
14+
"github.com/spf13/viper"
15+
16+
"github.com/trento-project/agent/internal/agent"
17+
"github.com/trento-project/agent/internal/operations/operator"
18+
"github.com/trento-project/agent/pkg/utils"
19+
)
20+
21+
func NewOperatorCmd() *cobra.Command {
22+
operatorCmd := &cobra.Command{ //nolint
23+
Use: "operator",
24+
Short: "Run operator related commands",
25+
Hidden: true,
26+
}
27+
28+
operatorCmd.AddCommand(NewOperatorRunCmd())
29+
operatorCmd.AddCommand(NewOperatorListCmd())
30+
31+
return operatorCmd
32+
}
33+
34+
func NewOperatorRunCmd() *cobra.Command {
35+
runCmd := &cobra.Command{ //nolint
36+
Use: "run",
37+
Short: "Run operator",
38+
Run: runOperator,
39+
PersistentPreRunE: func(agentCmd *cobra.Command, _ []string) error {
40+
agentCmd.Flags().VisitAll(func(f *pflag.Flag) {
41+
err := viper.BindPFlag(f.Name, f)
42+
if err != nil {
43+
panic(fmt.Errorf("error during cli init: %w", err))
44+
}
45+
})
46+
47+
return agent.InitConfig("agent")
48+
},
49+
}
50+
51+
runCmd.Flags().StringP("operator", "o", "", "The operator to use")
52+
runCmd.Flags().StringP("arguments", "a", "", "The used operator arguments")
53+
err := runCmd.MarkFlagRequired("operator")
54+
if err != nil {
55+
panic(err)
56+
}
57+
58+
return runCmd
59+
}
60+
61+
func NewOperatorListCmd() *cobra.Command {
62+
listCmd := &cobra.Command{ //nolint
63+
Use: "list",
64+
Short: "List the available operators",
65+
Run: listOperators,
66+
PersistentPreRunE: func(agentCmd *cobra.Command, _ []string) error {
67+
agentCmd.Flags().VisitAll(func(f *pflag.Flag) {
68+
err := viper.BindPFlag(f.Name, f)
69+
if err != nil {
70+
panic(fmt.Errorf("error during cli init: %w", err))
71+
}
72+
})
73+
74+
return agent.InitConfig("agent")
75+
},
76+
}
77+
78+
return listCmd
79+
}
80+
81+
func runOperator(cmd *cobra.Command, _ []string) {
82+
var operatorName = viper.GetString("operator")
83+
var arguments = viper.GetString("arguments")
84+
var logger = utils.NewDefaultLogger(
85+
viper.GetString("log-level"),
86+
)
87+
88+
slog.SetDefault(logger)
89+
slog.Info("Operation", "operator", operatorName, "arguments", arguments)
90+
91+
opArgs := make(operator.Arguments)
92+
err := json.Unmarshal([]byte(arguments), &opArgs)
93+
if err != nil {
94+
logger.Error("error unmarshalling arguments", "err", err)
95+
os.Exit(1)
96+
}
97+
98+
registry := operator.StandardRegistry()
99+
operatorBuilder, err := registry.GetOperatorBuilder(operatorName)
100+
if err != nil {
101+
logger.Error("error building operator", "err", err)
102+
os.Exit(1)
103+
}
104+
105+
ctx, cancel := context.WithCancel(cmd.Context())
106+
107+
signals := make(chan os.Signal, 1)
108+
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
109+
cancelled := false
110+
go func() {
111+
<-signals
112+
slog.Info("Caught signal!")
113+
cancelled = true
114+
cancel()
115+
116+
}()
117+
118+
op := operatorBuilder("", opArgs)
119+
report := op.Run(ctx)
120+
121+
if cancelled {
122+
slog.Info("Operation cancelled")
123+
return
124+
}
125+
126+
if report.Error != nil {
127+
logger.Error(report.Error.Error())
128+
os.Exit(0)
129+
}
130+
131+
diff, err := json.Marshal(report.Success.Diff)
132+
if err != nil {
133+
logger.Error("error marshalling diff output", "err", err)
134+
os.Exit(1)
135+
}
136+
137+
logger.Info("Operation succedded",
138+
"phase", report.Success.LastPhase,
139+
"diff", string(diff),
140+
)
141+
}
142+
143+
func listOperators(*cobra.Command, []string) {
144+
var logger = utils.NewDefaultLogger(
145+
viper.GetString("log-level"),
146+
)
147+
148+
slog.SetDefault(logger)
149+
150+
registry := operator.StandardRegistry()
151+
operators := registry.AvailableOperators()
152+
153+
slog.Info("Available operators:")
154+
155+
for _, o := range operators {
156+
slog.Info(o)
157+
}
158+
}

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ that can help you deploy, provision and operate infrastructure for SAP Applicati
4545
rootCmd.AddCommand(NewFactsCmd())
4646
rootCmd.AddCommand(NewVersionCmd())
4747
rootCmd.AddCommand(NewGenerateCmd())
48+
rootCmd.AddCommand(NewOperatorCmd())
4849

4950
return rootCmd
5051
}

0 commit comments

Comments
 (0)