Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Margo support in maestro CLI #625

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions cli/cmd/margo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/

package cmd

import (
"encoding/json"
"fmt"

"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/model"
"github.com/eclipse-symphony/symphony/cli/config"
"github.com/eclipse-symphony/symphony/cli/utils"
"github.com/eclipse-symphony/symphony/hydra"
"github.com/eclipse-symphony/symphony/hydra/margo/v0"
"github.com/spf13/cobra"
)

var (
appPackagePath string
)

var MargoCmd = &cobra.Command{
Use: "margo",
Short: "Margo commands",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("\n%sPlease use either 'margo run' or 'margo list'%s\n\n", utils.ColorRed(), utils.ColorReset())
},
}

var MargoRunCmd = &cobra.Command{
Use: "run",
Short: "run a Margo application package",
Run: func(cmd *cobra.Command, args []string) {
//check context
c := config.GetMaestroConfig(configFile)
ctx := c.DefaultContext
if configContext != "" {
ctx = configContext
}

if ctx == "" {
ctx = "default"
}
//check target
targets, err := utils.Get(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "targets", "", "", target)
if err != nil && targets != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
if len(targets) == 0 {
fmt.Printf("\n%s Target '%s' is not found%s\n\n", utils.ColorRed(), target, utils.ColorReset())
return
}
reader := margo.MargoSolutionReader{}
solution, err := reader.Parse(hydra.AppPackageDescription{
Path: appPackagePath,
Type: "margo",
Version: "v0",
})
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
solutionContainerName := solution.ObjectMeta.Name

solutionContainers, err := utils.Get(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "solutioncontainers", "", "", solutionContainerName)
if err != nil && solutionContainers != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
if len(solutionContainers) != 0 {
fmt.Printf("\n%s Solution '%s' already exists%s\n\n", utils.ColorRed(), solutionContainerName, utils.ColorReset())
return
}

//create solution container
solutionContainer := model.SolutionContainerState{
ObjectMeta: model.ObjectMeta{
Name: solutionContainerName,
},
Spec: &model.SolutionContainerSpec{},
}
solutionContainerData, err := json.Marshal(solutionContainer)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
err = utils.Upsert(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "solution-containers", solutionContainerName, solutionContainerData)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
solutionName := solutionContainerName + "-v-v1"
solution.ObjectMeta.Name = solutionName
solution.Spec.RootResource = solutionContainerName

//create solution
solutionData, err := json.Marshal(solution)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
err = utils.Upsert(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "solutions", solutionName, solutionData)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}

instanceName := solutionName + "-instance"
//check instance
instances, err := utils.Get(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "instances", "", "", instanceName)
if err != nil && instances != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
if len(instances) > 0 {
fmt.Printf("\n%s Solution instance '%s' already exists%s\n\n", utils.ColorRed(), instanceName, utils.ColorReset())
return
}
//create instance
instance := Instance{
Metadata: model.ObjectMeta{
Name: instanceName,
},
Spec: model.InstanceSpec{
Solution: solutionContainerName + ":v1",
Target: model.TargetSelector{
Name: target,
},
},
}
instanceData, err := json.Marshal(instance)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
err = utils.Upsert(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "instances", instanceName, instanceData)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
fmt.Printf("\n%s Solution instance created successfully%s\n\n", utils.ColorGreen(), utils.ColorReset())
},
}

func init() {
MargoRunCmd.Flags().StringVarP(&appPackagePath, "package", "p", "", "Margo application package definition path")
MargoRunCmd.Flags().StringVarP(&target, "target", "t", "", "Target to run the solution on")
MargoRunCmd.MarkFlagRequired("package")
MargoRunCmd.MarkFlagRequired("target")
MargoCmd.AddCommand(MargoRunCmd)
RootCmd.AddCommand(MargoCmd)
}
123 changes: 123 additions & 0 deletions cli/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/

package cmd

import (
"encoding/json"
"fmt"

"github.com/eclipse-symphony/symphony/api/pkg/apis/v1alpha1/model"
"github.com/eclipse-symphony/symphony/cli/config"
"github.com/eclipse-symphony/symphony/cli/utils"
"github.com/spf13/cobra"
)

var (
solutionPath string
target string
instanceName string
)

var RunCmd = &cobra.Command{
Use: "run",
Short: "run a Margo application package",
Run: func(cmd *cobra.Command, args []string) {
//check context
c := config.GetMaestroConfig(configFile)
ctx := c.DefaultContext
if configContext != "" {
ctx = configContext
}

if ctx == "" {
ctx = "default"
}
//check target
targets, err := utils.Get(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "targets", "", "", target)
if err != nil && targets != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
if len(targets) == 0 {
fmt.Printf("\n%s Target '%s' is not found%s\n\n", utils.ColorRed(), target, utils.ColorReset())
return
}
//check instance
instances, err := utils.Get(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "instances", "", "", instanceName)
if err != nil && instances != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
if len(instances) > 0 {
fmt.Printf("\n%s Solution instance '%s' already exists%s\n\n", utils.ColorRed(), instanceName, utils.ColorReset())
return
}
//check solution
solutionData, err := utils.GetArtifactFile(solutionPath)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
var solution Solution
err = json.Unmarshal(solutionData, &solution)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
solutionName := solution.Metadata.Name
solutions, err := utils.Get(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "solutions", "", "", solutionName)
if err != nil && solutions != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
if len(solutions) != 0 {
fmt.Printf("\n%s Solution '%s' already exists%s\n\n", utils.ColorRed(), solutionName, utils.ColorReset())
return
}
//create solution
err = utils.Upsert(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "solutions", solutionName, solutionData)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
//create instance
instance := Instance{
Metadata: model.ObjectMeta{
Name: instanceName,
},
Spec: model.InstanceSpec{
Solution: solutionName,
Target: model.TargetSelector{
Name: target,
},
},
}
instanceData, err := json.Marshal(instance)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
err = utils.Upsert(c.Contexts[ctx].Url, c.Contexts[ctx].User, c.Contexts[ctx].Secret, "instances", instanceName, instanceData)
if err != nil {
fmt.Printf("\n%s %s%s\n\n", utils.ColorRed(), err.Error(), utils.ColorReset())
return
}
fmt.Printf("\n%s Solution instance created successfully%s\n\n", utils.ColorGreen(), utils.ColorReset())
},
}

func init() {
RunCmd.Flags().StringVarP(&solutionPath, "solution", "s", "", "Symphony solution file path")
RunCmd.Flags().StringVarP(&target, "target", "t", "", "Target to run the solution on")
RunCmd.Flags().StringVarP(&instanceName, "name", "n", "", "Name of the solution instance")
RunCmd.Flags().StringVarP(&configFile, "config", "c", "", "Maestro CLI config file")
RunCmd.Flags().StringVarP(&configContext, "context", "", "", "Maestro CLI configuration context")
RunCmd.MarkFlagRequired("solution")
RunCmd.MarkFlagRequired("target")
RunCmd.MarkFlagRequired("name")
RootCmd.AddCommand(RunCmd)
}
10 changes: 5 additions & 5 deletions cli/cmd/samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var DescribeCmd = &cobra.Command{
},
}

var RunCmd = &cobra.Command{
var SampleRunCmd = &cobra.Command{
Use: "run",
Short: "run a Symphony sample",
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -339,14 +339,14 @@ var RemoveCmd = &cobra.Command{
}

func init() {
RunCmd.Flags().StringArrayVarP(&setSwitches, "set", "s", nil, "set sample parameter as key=value")
RunCmd.Flags().StringVarP(&sampleConfigFile, "config", "c", "", "Maestro CLI config file")
RunCmd.Flags().StringVarP(&sampleConfigContext, "context", "", "", "Maestro CLI configuration context")
SampleRunCmd.Flags().StringArrayVarP(&setSwitches, "set", "s", nil, "set sample parameter as key=value")
SampleRunCmd.Flags().StringVarP(&sampleConfigFile, "config", "c", "", "Maestro CLI config file")
SampleRunCmd.Flags().StringVarP(&sampleConfigContext, "context", "", "", "Maestro CLI configuration context")
RemoveCmd.Flags().StringVarP(&sampleConfigFile, "config", "c", "", "Maestro CLI config file")
RemoveCmd.Flags().StringVarP(&sampleConfigContext, "context", "", "", "Maestro CLI configuration context")
DescribeCmd.Flags().StringVarP(&sampleConfigFile, "config", "c", "", "Maestro CLI config file")
DescribeCmd.Flags().StringVarP(&sampleConfigContext, "context", "", "", "Maestro CLI configuration context")
SamplesCmd.AddCommand(RunCmd)
SamplesCmd.AddCommand(SampleRunCmd)
SamplesCmd.AddCommand(RemoveCmd)
SamplesCmd.AddCommand(DescribeCmd)
SamplesCmd.AddCommand(ListCmd)
Expand Down
18 changes: 18 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,39 @@ replace github.com/eclipse-symphony/symphony/api => ../api

replace github.com/eclipse-symphony/symphony/coa => ../coa

replace github.com/eclipse-symphony/symphony/hydra => ../hydra

require github.com/spf13/cobra v1.8.1

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/eclipse-symphony/symphony/coa v0.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/text v0.21.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
helm.sh/helm/v3 v3.15.4 // indirect
k8s.io/apimachinery v0.30.3 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)

require (
Expand All @@ -33,6 +50,7 @@ require (

require (
github.com/eclipse-symphony/symphony/api v0.0.0
github.com/eclipse-symphony/symphony/hydra v0.0.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.2
github.com/mattn/go-runewidth v0.0.15 // indirect
Expand Down
Loading
Loading