-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
515 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"github.com/trento-project/workbench/pkg/operator" | ||
) | ||
|
||
var arguments string | ||
|
||
var executeCmd = &cobra.Command{ | ||
Use: "execute", | ||
Short: "execute an operator providing operator name and arguments", | ||
Long: ` | ||
workbench execute <operator name> --arguments <json object> | ||
Example | ||
worbench execute saptunesolutionapply --arguments "{"solution": "HANA"}" | ||
`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
logger := logrus.StandardLogger() | ||
if verbose { | ||
logger.SetLevel(logrus.DebugLevel) | ||
} | ||
|
||
solution := args[0] | ||
if solution != "saptunesolutionapply" { | ||
return fmt.Errorf("solution %s provided as argument, is invalid", solution) | ||
} | ||
|
||
opArgs := make(operator.OperatorArguments) | ||
err := json.Unmarshal([]byte(arguments), &opArgs) | ||
if err != nil { | ||
return fmt.Errorf("could not unmarhsal %s into arguments", arguments) | ||
} | ||
|
||
op := operator.NewSaptuneApplySolution(opArgs, "test-cli", operator.OperatorOptions[operator.SaptuneApplySolution]{ | ||
Check failure on line 43 in cmd/execute.go GitHub Actions / static-analysis
|
||
BaseOperatorOptions: []operator.BaseOption{operator.WithLogger(logger)}, | ||
Check failure on line 44 in cmd/execute.go GitHub Actions / static-analysis
Check failure on line 44 in cmd/execute.go GitHub Actions / static-analysis
Check failure on line 44 in cmd/execute.go GitHub Actions / test
|
||
}) | ||
|
||
report := op.Run(context.Background()) | ||
if report.Error != nil { | ||
return fmt.Errorf("operation execution error, phase: %s, reason: %s", | ||
report.Error.ErrorPhase, | ||
report.Error.Message, | ||
) | ||
} | ||
|
||
logger.Infof("exeuction succeded in phase: %s, diff: before: %s, after: %s", | ||
report.Success.LastPhase, | ||
report.Success.Diff["before"], | ||
report.Success.Diff["after"], | ||
) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var verbose bool | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "workbench", | ||
Short: "Workbench allow run trento operator directly", | ||
} | ||
|
||
func Execute() { | ||
executeCmd.Flags().StringVarP(&arguments, "arguments", "a", "", "arguments as json object (required)") | ||
executeCmd.MarkFlagRequired("arguments") | ||
|
||
rootCmd.AddCommand(executeCmd) | ||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") | ||
|
||
if err := rootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"github.com/trento-project/workbench/cmd" | ||
) | ||
|
||
func main() { | ||
fmt.Println("hello moto") | ||
cmd.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package operator | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
) | ||
|
||
type phaser interface { | ||
plan(ctx context.Context) error | ||
commit(ctx context.Context) error | ||
rollback(ctx context.Context) error | ||
verify(ctx context.Context) error | ||
operationDiff(ctx context.Context) map[string]any | ||
} | ||
|
||
type Executor struct { | ||
currentPhase OPERATION_PHASES | ||
phaser phaser | ||
operationID string | ||
} | ||
|
||
func (e *Executor) Run(ctx context.Context) *ExecutionReport { | ||
e.currentPhase = PLAN | ||
err := e.phaser.plan(ctx) | ||
if err != nil { | ||
return executionReportWithError(err, e.currentPhase, e.operationID) | ||
} | ||
|
||
e.currentPhase = COMMIT | ||
|
||
err = e.phaser.commit(ctx) | ||
if err != nil { | ||
e.currentPhase = ROLLBACK | ||
rollbackError := e.phaser.rollback(ctx) | ||
if rollbackError != nil { | ||
return executionReportWithError( | ||
wrapRollbackError(err, rollbackError), | ||
e.currentPhase, | ||
e.operationID, | ||
) | ||
|
||
} | ||
return executionReportWithError(err, e.currentPhase, e.operationID) | ||
} | ||
|
||
e.currentPhase = VERIFY | ||
err = e.phaser.verify(ctx) | ||
if err != nil { | ||
e.currentPhase = ROLLBACK | ||
rollbackError := e.phaser.rollback(ctx) | ||
if rollbackError != nil { | ||
return executionReportWithError( | ||
wrapRollbackError(err, rollbackError), | ||
e.currentPhase, | ||
e.operationID, | ||
) | ||
} | ||
return executionReportWithError(err, e.currentPhase, e.operationID) | ||
} | ||
|
||
diff := e.phaser.operationDiff(ctx) | ||
|
||
return executionReportWithSuccess(diff, e.currentPhase, e.operationID) | ||
} | ||
|
||
func wrapRollbackError(phaseError error, rollbackError error) error { | ||
return errors.Join(rollbackError, phaseError) | ||
} |
Oops, something went wrong.