Skip to content

Commit

Permalink
added reset, diff, modify config commands
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Jan 8, 2021
1 parent 8fc3be6 commit 750d0e1
Show file tree
Hide file tree
Showing 20 changed files with 453 additions and 71 deletions.
12 changes: 12 additions & 0 deletions cmd/relay/eth/actions/commonvars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package actions

var (
enableBeta bool
runtime string
version string
instanceId string
updatePolicy string
skipChecksum bool
forceRuntime bool
runtimeArgs map[string]string
)
10 changes: 6 additions & 4 deletions cmd/relay/eth/actions/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
cfg "github.com/marlinprotocol/ctl2/cmd/relay/eth/config"
"github.com/marlinprotocol/ctl2/modules/registry"
projectRunners "github.com/marlinprotocol/ctl2/modules/runner/relay_eth"
"github.com/marlinprotocol/ctl2/modules/util"
Expand All @@ -32,7 +34,7 @@ var raDiscoveryAddrs, raHeartbeatAddrs, raDataDir, raDiscoveryPort, raPubsubPort
var CreateCmd = &cobra.Command{
Use: "create",
Short: `Create an ethrelay on local system`,
PreRunE: ConfigTest,
PreRunE: cfg.ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
if len(runtimeArgs) == 0 {
runtimeArgs["DiscoveryAddrs"] = raDiscoveryAddrs
Expand All @@ -47,12 +49,12 @@ var CreateCmd = &cobra.Command{

}
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
return
}
versionToRun, err := registry.GlobalRegistry.GetVersionToRun(projectId, updatePolicy, version)
versionToRun, err := registry.GlobalRegistry.GetVersionToRun(cmn.ProjectID, updatePolicy, version)
if err != nil {
log.Error("Error while getting version to run: ", err)
return
Expand Down Expand Up @@ -84,7 +86,7 @@ var CreateCmd = &cobra.Command{

projectConfig.CurrentVersion = versionToRun.Version

viper.Set(projectId, projectConfig)
viper.Set(cmn.ProjectID, projectConfig)
err = viper.WriteConfig()
if err != nil {
log.Error("Failure while updating config for current version: ", err.Error())
Expand Down
32 changes: 5 additions & 27 deletions cmd/relay/eth/actions/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ limitations under the License.
package actions

import (
"encoding/json"
"errors"
"io/ioutil"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
cfg "github.com/marlinprotocol/ctl2/cmd/relay/eth/config"
projectRunners "github.com/marlinprotocol/ctl2/modules/runner/relay_eth"
"github.com/marlinprotocol/ctl2/types"
)
Expand All @@ -34,16 +33,16 @@ var DestroyCmd = &cobra.Command{
Use: "destroy",
Short: "Destroy any running eth relay",
Long: `Destroy any running eth relay`,
PreRunE: ConfigTest,
PreRunE: cfg.ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
os.Exit(1)
}

runnerId, version, err := getResourceMetaData(projectConfig, instanceId)
runnerId, version, err := cmn.GetResourceMetaData(projectConfig, instanceId)
if err != nil {
log.Error("Error while fetching resource information: ", err)
os.Exit(1)
Expand Down Expand Up @@ -80,24 +79,3 @@ var DestroyCmd = &cobra.Command{
func init() {
DestroyCmd.Flags().StringVarP(&instanceId, "instance-id", "i", "001", "instance-id of the resource")
}

func getResourceMetaData(projectConfig types.Project, instanceId string) (string, string, error) {
resFileLocation := projectRunners.GetResourceFileLocation(projectConfig.Storage, instanceId)
if _, err := os.Stat(resFileLocation); os.IsNotExist(err) {
return "", "", errors.New("Cannot locate resource: " + resFileLocation)
}
file, err := ioutil.ReadFile(resFileLocation)
if err != nil {
return "", "", err
}
var resourceMetaData = struct {
Runner string `json:"Runner"`
Version string `json:"Version"`
}{}
err = json.Unmarshal([]byte(file), &resourceMetaData)
if err != nil {
return "", "", err
}
log.Debug("Resource metadata: ", resourceMetaData)
return resourceMetaData.Runner, resourceMetaData.Version, nil
}
8 changes: 5 additions & 3 deletions cmd/relay/eth/actions/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
cfg "github.com/marlinprotocol/ctl2/cmd/relay/eth/config"
projectRunners "github.com/marlinprotocol/ctl2/modules/runner/relay_eth"
"github.com/marlinprotocol/ctl2/types"
)
Expand All @@ -31,16 +33,16 @@ var LogsCmd = &cobra.Command{
Use: "logs",
Short: "Tail logs for eth relay",
Long: `Tail logs for eth relay`,
PreRunE: ConfigTest,
PreRunE: cfg.ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
os.Exit(1)
}

runnerId, version, err := getResourceMetaData(projectConfig, instanceId)
runnerId, version, err := cmn.GetResourceMetaData(projectConfig, instanceId)
if err != nil {
log.Error("Error while fetching resource information: ", err)
os.Exit(1)
Expand Down
8 changes: 5 additions & 3 deletions cmd/relay/eth/actions/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
cfg "github.com/marlinprotocol/ctl2/cmd/relay/eth/config"
projectRunners "github.com/marlinprotocol/ctl2/modules/runner/relay_eth"
"github.com/marlinprotocol/ctl2/types"
)
Expand All @@ -31,16 +33,16 @@ var StatusCmd = &cobra.Command{
Use: "status",
Short: "Get status of eth relay",
Long: `Get status of eth relay`,
PreRunE: ConfigTest,
PreRunE: cfg.ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
os.Exit(1)
}

runnerId, version, err := getResourceMetaData(projectConfig, instanceId)
runnerId, version, err := cmn.GetResourceMetaData(projectConfig, instanceId)
if err != nil {
log.Error("Error while fetching resource information: ", err)
os.Exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
cfg "github.com/marlinprotocol/ctl2/cmd/relay/eth/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -31,15 +33,15 @@ var VersionsCmd = &cobra.Command{
Use: "versions",
Short: "List versions for eth relay",
Long: `List versions for eth relay`,
PreRunE: ConfigTest,
PreRunE: cfg.ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(projectId, &projectConfig)
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project config: ", err)
os.Exit(1)
}
versions, err := registry.GlobalRegistry.GetVersions(projectId, projectConfig.Subscription, "0.0.0", "major", projectConfig.Runtime)
versions, err := registry.GlobalRegistry.GetVersions(cmn.ProjectID, projectConfig.Subscription, "0.0.0", "major", projectConfig.Runtime)

if err != nil {
log.Error("Error encountered while listing versions: ", err)
Expand Down
37 changes: 37 additions & 0 deletions cmd/relay/eth/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package common

import (
"encoding/json"
"errors"
"io/ioutil"
"os"

projectRunners "github.com/marlinprotocol/ctl2/modules/runner/relay_eth"
"github.com/marlinprotocol/ctl2/types"
"github.com/prometheus/common/log"
)

const (
ProjectID string = types.ProjectID_relay_eth
)

func GetResourceMetaData(projectConfig types.Project, instanceId string) (string, string, error) {
resFileLocation := projectRunners.GetResourceFileLocation(projectConfig.Storage, instanceId)
if _, err := os.Stat(resFileLocation); os.IsNotExist(err) {
return "", "", errors.New("Cannot locate resource: " + resFileLocation)
}
file, err := ioutil.ReadFile(resFileLocation)
if err != nil {
return "", "", err
}
var resourceMetaData = struct {
Runner string `json:"Runner"`
Version string `json:"Version"`
}{}
err = json.Unmarshal([]byte(file), &resourceMetaData)
if err != nil {
return "", "", err
}
log.Debug("Resource metadata: ", resourceMetaData)
return resourceMetaData.Runner, resourceMetaData.Version, nil
}
45 changes: 45 additions & 0 deletions cmd/relay/eth/config/actions/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package actions

import (
"os"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// AppCmd represents the registry command
var ConfigApplyCmd = &cobra.Command{
Use: "apply",
Short: "Apply project config modifications",
Long: `Apply project config modifications`,
PreRunE: ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project configs: ", err.Error())
os.Exit(1)
}

modifiedProjectID := cmn.ProjectID + "_modified"
var projectConfigMod types.Project
if viper.IsSet(modifiedProjectID) {
err = viper.UnmarshalKey(modifiedProjectID, &projectConfigMod)
if err != nil {
log.Error("Error while unmarshalling modifications: ", err.Error())
os.Exit(1)
}
} else {
log.Info("No existing modifications found.")
os.Exit(1)
}

},
}

func init() {
ConfigApplyCmd.Flags().BoolVar(&skipRecreate, "skip-recreate", false, "Skip recreating project's running resources while updating configs")
}
11 changes: 11 additions & 0 deletions cmd/relay/eth/config/actions/commonvars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package actions

var (
subscriptions []string
updatePolicy string
currentVersion string
storage string
runtime string
forceRuntime bool
skipRecreate bool
)
32 changes: 32 additions & 0 deletions cmd/relay/eth/config/actions/configTest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package actions

import (
cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var ConfigTest = func(cmd *cobra.Command, args []string) error {
var marlinConfig types.Project
err := viper.UnmarshalKey(types.ProjectID_marlinctl, &marlinConfig)
if err != nil {
return err
}
if !viper.IsSet(cmn.ProjectID) {
log.Debug("Setting up default config for running relay_eth.")
updPol, ok1 := marlinConfig.AdditionalInfo["defaultprojectupdatepolicy"]
defRun, ok2 := marlinConfig.AdditionalInfo["defaultprojectruntime"]
if ok1 && ok2 {
SetupConfiguration(false,
false,
updPol.(string),
defRun.(string),
"latest")
}
} else {
log.Debug("Project config found. Not creating defaults.")
}
return nil
}
45 changes: 45 additions & 0 deletions cmd/relay/eth/config/actions/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package actions

import (
"os"

"github.com/google/go-cmp/cmp"
cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
"github.com/marlinprotocol/ctl2/modules/util"
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// AppCmd represents the registry command
var ConfigDiffCmd = &cobra.Command{
Use: "diff",
Short: "Show difference between current project configs and modifications",
Long: `Show difference between current project configs and modifications`,
PreRunE: ConfigTest,
Run: func(cmd *cobra.Command, args []string) {
var projectConfig types.Project
err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig)
if err != nil {
log.Error("Error while reading project configs: ", err.Error())
os.Exit(1)
}

modifiedProjectID := cmn.ProjectID + "_modified"
var projectConfigMod types.Project
if viper.IsSet(modifiedProjectID) {
err = viper.UnmarshalKey(modifiedProjectID, &projectConfigMod)
if err != nil {
log.Error("Error while unmarshalling modifications: ", err.Error())
os.Exit(1)
}
} else {
log.Info("No existing modifications found.")
os.Exit(1)
}

log.Info("Difference:")
util.PrintPrettyDiff(cmp.Diff(projectConfig, projectConfigMod))
},
}
Loading

0 comments on commit 750d0e1

Please sign in to comment.