diff --git a/cmd/relay/eth/actions/commonvars.go b/cmd/relay/eth/actions/commonvars.go new file mode 100644 index 0000000..4cc8f9b --- /dev/null +++ b/cmd/relay/eth/actions/commonvars.go @@ -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 +) diff --git a/cmd/relay/eth/actions/create.go b/cmd/relay/eth/actions/create.go index 8391e3d..ddb8a91 100644 --- a/cmd/relay/eth/actions/create.go +++ b/cmd/relay/eth/actions/create.go @@ -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" @@ -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 @@ -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 @@ -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()) diff --git a/cmd/relay/eth/actions/destroy.go b/cmd/relay/eth/actions/destroy.go index 49d0620..27ccd68 100644 --- a/cmd/relay/eth/actions/destroy.go +++ b/cmd/relay/eth/actions/destroy.go @@ -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" ) @@ -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) @@ -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 -} diff --git a/cmd/relay/eth/actions/logs.go b/cmd/relay/eth/actions/logs.go index aefe823..f6117f5 100644 --- a/cmd/relay/eth/actions/logs.go +++ b/cmd/relay/eth/actions/logs.go @@ -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" ) @@ -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) diff --git a/cmd/relay/eth/actions/status.go b/cmd/relay/eth/actions/status.go index 6c2a48a..5b67457 100644 --- a/cmd/relay/eth/actions/status.go +++ b/cmd/relay/eth/actions/status.go @@ -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" ) @@ -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) diff --git a/cmd/relay/eth/actions/listversions.go b/cmd/relay/eth/actions/versions.go similarity index 79% rename from cmd/relay/eth/actions/listversions.go rename to cmd/relay/eth/actions/versions.go index 3267f03..a1e5010 100644 --- a/cmd/relay/eth/actions/listversions.go +++ b/cmd/relay/eth/actions/versions.go @@ -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" ) @@ -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) diff --git a/cmd/relay/eth/common/common.go b/cmd/relay/eth/common/common.go new file mode 100644 index 0000000..7069163 --- /dev/null +++ b/cmd/relay/eth/common/common.go @@ -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 +} diff --git a/cmd/relay/eth/config/actions/apply.go b/cmd/relay/eth/config/actions/apply.go new file mode 100644 index 0000000..a50543e --- /dev/null +++ b/cmd/relay/eth/config/actions/apply.go @@ -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") +} diff --git a/cmd/relay/eth/config/actions/commonvars.go b/cmd/relay/eth/config/actions/commonvars.go new file mode 100644 index 0000000..0cfeed1 --- /dev/null +++ b/cmd/relay/eth/config/actions/commonvars.go @@ -0,0 +1,11 @@ +package actions + +var ( + subscriptions []string + updatePolicy string + currentVersion string + storage string + runtime string + forceRuntime bool + skipRecreate bool +) diff --git a/cmd/relay/eth/config/actions/configTest.go b/cmd/relay/eth/config/actions/configTest.go new file mode 100644 index 0000000..8355775 --- /dev/null +++ b/cmd/relay/eth/config/actions/configTest.go @@ -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 +} diff --git a/cmd/relay/eth/config/actions/diff.go b/cmd/relay/eth/config/actions/diff.go new file mode 100644 index 0000000..e174bd3 --- /dev/null +++ b/cmd/relay/eth/config/actions/diff.go @@ -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)) + }, +} diff --git a/cmd/relay/eth/config/actions/modify.go b/cmd/relay/eth/config/actions/modify.go new file mode 100644 index 0000000..1c3cc21 --- /dev/null +++ b/cmd/relay/eth/config/actions/modify.go @@ -0,0 +1,110 @@ +package actions + +import ( + "os" + + "github.com/marlinprotocol/ctl2/modules/util" + + "github.com/getlantern/deepcopy" + 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 ConfigModifyCmd = &cobra.Command{ + Use: "modify", + Short: "Modify state information of the project", + Long: `Modify state information of the project`, + 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("No existing modifications found.") + os.Exit(1) + } + } else { + log.Info("No existing modifications found. Sensing from set project config.") + deepcopy.Copy(&projectConfigMod, &projectConfig) + } + + if len(subscriptions) > 0 { + for _, v := range subscriptions { + if !util.IsValidSubscription(v) { + log.Error("Not a valid subscription line: ", v) + os.Exit(1) + } + } + projectConfigMod.Subscription = subscriptions + } + + if updatePolicy != "" { + if !util.IsValidUpdatePolicy(updatePolicy) { + log.Error("Not a valid update policy: ", updatePolicy) + os.Exit(1) + } + projectConfigMod.UpdatePolicy = updatePolicy + } + + if currentVersion != "" { + _, _, _, _, _, err = util.DecodeVersionString(currentVersion) + if err != nil { + log.Error("Error decoding version string: ", err.Error()) + os.Exit(1) + } + projectConfigMod.CurrentVersion = currentVersion + } + + if storage != "" { + projectConfigMod.Storage = storage + } + + if runtime != "" { + suitableRuntimes := util.GetRuntimes() + if !forceRuntime { + if suitable, ok := suitableRuntimes[runtime]; !ok || !suitable { + log.Error("Runtime provided for configuration: " + runtime + + " may not be supported by marlinctl or is not supported by your system." + + " If you think this is incorrect, override this check using --force-runtime.") + os.Exit(1) + } else { + log.Debug("Runtime provided for configuration: " + runtime + + " seems to be supported. Going ahead with configuring this.") + } + } else { + log.Warning("Skipped runtime suitability check due to forced runtime") + } + projectConfigMod.Runtime = runtime + projectConfigMod.ForcedRuntime = forceRuntime + } + viper.Set(modifiedProjectID, projectConfigMod) + + err = viper.WriteConfig() + if err != nil { + log.Error("Error while writing staging configs to disk: ", err.Error()) + os.Exit(1) + } + log.Info("Modifications registered") + }, +} + +func init() { + ConfigModifyCmd.Flags().StringSliceVar(&subscriptions, "subscriptions", []string{}, "Subscriptions - public, beta") + ConfigModifyCmd.Flags().StringVar(&updatePolicy, "update-policy", "", "Update policy - major, minor, patch, frozen") + ConfigModifyCmd.Flags().StringVar(¤tVersion, "current-version", "", "version to use") + ConfigModifyCmd.Flags().StringVar(&storage, "storage", "", "storage location to use") + ConfigModifyCmd.Flags().StringVar(&runtime, "runtime", "", "runtime to use") + ConfigModifyCmd.Flags().BoolVar(&forceRuntime, "forced-runtime", false, "forcefully set runtime to use") +} diff --git a/cmd/relay/eth/actions/common.go b/cmd/relay/eth/config/actions/reset.go similarity index 60% rename from cmd/relay/eth/actions/common.go rename to cmd/relay/eth/config/actions/reset.go index 6acff15..d8e846a 100644 --- a/cmd/relay/eth/actions/common.go +++ b/cmd/relay/eth/config/actions/reset.go @@ -4,6 +4,7 @@ import ( "errors" "os" + cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common" "github.com/marlinprotocol/ctl2/modules/registry" "github.com/marlinprotocol/ctl2/modules/util" "github.com/marlinprotocol/ctl2/types" @@ -12,49 +13,53 @@ import ( "github.com/spf13/viper" ) -var ( - enableBeta bool - runtime string - version string - instanceId string - projectId string = "relay_eth" - updatePolicy string - skipChecksum bool - forceRuntime bool - runtimeArgs map[string]string -) - -var ConfigTest = func(cmd *cobra.Command, args []string) error { - var marlinConfig types.Project - err := viper.UnmarshalKey("marlinctl", &marlinConfig) - if err != nil { - return err - } - if !viper.IsSet("relay_eth") { +// AppCmd represents the registry command +var ConfigResetCmd = &cobra.Command{ + Use: "reset", + Short: "Reset to default configs", + Long: `Reset to default configs`, + PreRunE: ConfigTest, + Run: func(cmd *cobra.Command, args []string) { + var marlinConfig types.Project + err := viper.UnmarshalKey(types.ProjectID_marlinctl, &marlinConfig) + if err != nil { + log.Error("Error while reading marlinctl configs: ", err.Error()) + os.Exit(1) + } 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, + err = SetupConfiguration(false, false, updPol.(string), defRun.(string), "latest") + if err != nil { + log.Error("Error while resetting project to default config values ", err.Error()) + os.Exit(1) + } else { + log.Info("Successfully reset project to default config values") + } } - } else { - log.Debug("Project config found. Not creating defaults.") - } - return nil + if viper.IsSet(cmn.ProjectID + "_modified") { + err := util.RemoveConfigEntry(cmn.ProjectID + "_modified") + if err != nil { + log.Error("Error while removing modifications relating to the project from config file: " + err.Error()) + os.Exit(1) + } + } + }, } -func setupConfiguration(enableBeta bool, forceRuntime bool, updatePolicy string, runtime string, version string) error { +func SetupConfiguration(enableBeta bool, forceRuntime bool, updatePolicy string, runtime string, version string) error { if !util.IsValidUpdatePolicy(updatePolicy) { return errors.New("Unknown update policy: " + updatePolicy) } var projectConfig types.Project - err := viper.UnmarshalKey(projectId, &projectConfig) + err := viper.UnmarshalKey(cmn.ProjectID, &projectConfig) if err != nil { return err } @@ -84,7 +89,7 @@ func setupConfiguration(enableBeta bool, forceRuntime bool, updatePolicy string, var currentVersion = "0.0.0" if version != "latest" { - versions, err := registry.GlobalRegistry.GetVersions(projectId, releaseSubscriptions, version, updatePolicy, runtime) + versions, err := registry.GlobalRegistry.GetVersions(cmn.ProjectID, releaseSubscriptions, version, updatePolicy, runtime) if err != nil { log.Error("Error while fetching from global registry: ", err) os.Exit(1) @@ -103,13 +108,14 @@ func setupConfiguration(enableBeta bool, forceRuntime bool, updatePolicy string, } } - viper.Set(projectId, types.Project{ + viper.Set(cmn.ProjectID, types.Project{ Subscription: releaseSubscriptions, UpdatePolicy: updatePolicy, CurrentVersion: currentVersion, - Storage: viper.GetString("homedir") + "/projects/" + projectId, + Storage: viper.GetString("homedir") + "/projects/" + cmn.ProjectID, Runtime: runtime, ForcedRuntime: false, + AdditionalInfo: nil, }) return viper.WriteConfig() diff --git a/cmd/relay/eth/config/config.go b/cmd/relay/eth/config/config.go new file mode 100644 index 0000000..7c9be38 --- /dev/null +++ b/cmd/relay/eth/config/config.go @@ -0,0 +1,35 @@ +/* +Copyright © 2020 MARLIN TEAM + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package config + +import ( + "github.com/marlinprotocol/ctl2/cmd/relay/eth/config/actions" + "github.com/spf13/cobra" +) + +// AppCmd represents the registry command +var ConfigCmd = &cobra.Command{ + Use: "config", + Short: "Configure state information of the project", + Long: `Configure state information of the project`, +} + +func init() { + ConfigCmd.AddCommand(actions.ConfigModifyCmd) + ConfigCmd.AddCommand(actions.ConfigDiffCmd) + ConfigCmd.AddCommand(actions.ConfigApplyCmd) + ConfigCmd.AddCommand(actions.ConfigResetCmd) +} diff --git a/cmd/relay/eth/config/configTest.go b/cmd/relay/eth/config/configTest.go new file mode 100644 index 0000000..8294807 --- /dev/null +++ b/cmd/relay/eth/config/configTest.go @@ -0,0 +1,7 @@ +package config + +import ( + "github.com/marlinprotocol/ctl2/cmd/relay/eth/config/actions" +) + +var ConfigTest = actions.ConfigTest diff --git a/cmd/relay/eth/eth.go b/cmd/relay/eth/eth.go index d80d6a9..e326b59 100644 --- a/cmd/relay/eth/eth.go +++ b/cmd/relay/eth/eth.go @@ -17,6 +17,7 @@ package eth import ( "github.com/marlinprotocol/ctl2/cmd/relay/eth/actions" + "github.com/marlinprotocol/ctl2/cmd/relay/eth/config" "github.com/spf13/cobra" ) @@ -33,4 +34,5 @@ func init() { EthCmd.AddCommand(actions.DestroyCmd) EthCmd.AddCommand(actions.VersionsCmd) EthCmd.AddCommand(actions.LogsCmd) + EthCmd.AddCommand(config.ConfigCmd) } diff --git a/go.mod b/go.mod index 76a41a7..94d0f73 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/dustin/go-humanize v1.0.0 github.com/getlantern/deepcopy v0.0.0-20160317154340-7f45deb8130a github.com/go-openapi/strfmt v0.19.11 // indirect + github.com/google/go-cmp v0.5.2 github.com/hpcloud/tail v1.0.0 github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf github.com/jedib0t/go-pretty v4.3.0+incompatible @@ -16,12 +17,15 @@ require ( github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d github.com/mitchellh/go-homedir v1.1.0 github.com/prometheus/common v0.4.0 + github.com/r3labs/diff/v2 v2.9.1 github.com/schollz/progressbar/v3 v3.7.3 github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v1.1.1 github.com/spf13/viper v1.7.1 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad + gopkg.in/d4l3k/messagediff.v1 v1.2.1 gopkg.in/fsnotify.v1 v1.4.7 // indirect gopkg.in/src-d/go-git.v4 v4.13.1 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect + gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index 448842b..ead12fc 100644 --- a/go.sum +++ b/go.sum @@ -113,7 +113,9 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -248,6 +250,9 @@ github.com/prometheus/common v0.15.0 h1:4fgOnadei3EZvgRwxJ7RMpG1k1pOZth5Pc13tysp github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/r3labs/diff v1.1.0 h1:V53xhrbTHrWFWq3gI4b94AjgEJOerO1+1l0xyHOBi8M= +github.com/r3labs/diff/v2 v2.9.1 h1:PE0m2ueSI0a0ymwTu1Js0iS8PO9yLvQqw/44s3VJlgg= +github.com/r3labs/diff/v2 v2.9.1/go.mod h1:I8noH9Fc2fjSaMxqF3G2lhDdC0b+JXCfyx85tWFM9kc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= @@ -293,11 +298,14 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI= +github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk= github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= @@ -441,6 +449,7 @@ google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9Ywl google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= +google.golang.org/appengine v1.6.6/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= google.golang.org/genproto v0.0.0-20190418145605-e7d98fc518a7/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE= @@ -458,6 +467,8 @@ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLks gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/d4l3k/messagediff.v1 v1.2.1 h1:70AthpjunwzUiarMHyED52mj9UwtAnE89l1Gmrt3EU0= +gopkg.in/d4l3k/messagediff.v1 v1.2.1/go.mod h1:EUzikiKadqXWcD1AzJLagx0j/BeeWGtn++04Xniyg44= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= @@ -475,6 +486,7 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/modules/util/util.go b/modules/util/util.go index b7c94b3..14b42ad 100644 --- a/modules/util/util.go +++ b/modules/util/util.go @@ -4,7 +4,6 @@ import ( "bytes" "crypto/md5" "encoding/hex" - "encoding/json" "errors" "fmt" "io" @@ -26,6 +25,7 @@ import ( "github.com/schollz/progressbar/v3" git "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/yaml.v2" ) func GetUser() (*user.User, error) { @@ -48,12 +48,21 @@ func GetUser() (*user.User, error) { } func RemoveConfigEntry(key string) error { + // viper yaml parser is bad -> it makes nil entries do weird things like + // map[interface{}]interface{}. Need a better yaml parser. + // Using Beats authors' workaround given here: https://github.com/go-yaml/yaml/issues/139 + // Make sure whenever you call this function, viper is in sync with disk + // NVM - this yaml issue is also solved automatically with marshalling + // IT IS WEIRD. ISSUES MAY OCCUR AT A LATER DAY + configMap := viper.AllSettings() delete(configMap, key) - encodedConfig, err := json.MarshalIndent(configMap, "", " ") + + encodedConfig, err := yaml.Marshal(configMap) if err != nil { return err } + err = viper.ReadConfig(bytes.NewReader(encodedConfig)) if err != nil { return err @@ -349,6 +358,14 @@ func IsValidUpdatePolicy(updatePolicy string) bool { return false } +func IsValidSubscription(subscription string) bool { + var subscriptions = map[string]bool{"public": true, "beta": true, "alpha": true, "dev": true} + if found, ok := subscriptions[subscription]; found && ok { + return true + } + return false +} + func DecodeVersionString(verString string) (int, int, int, string, int, error) { dashSplit := strings.Split(verString, "-") if len(dashSplit) < 1 { @@ -440,3 +457,18 @@ func ExpandTilde(path string) string { } return path } + +func PrintPrettyDiff(message string) { + lines := strings.Split(message, "\n") + for _, l := range lines { + if len(l) < 1 { + continue + } else if l[0] == []byte("-")[0] { + fmt.Println(text.FgRed.Sprintf(l)) + } else if l[0] == []byte("+")[0] { + fmt.Println(text.FgGreen.Sprintf(l)) + } else { + fmt.Println(l) + } + } +} diff --git a/types/marlinctl_configs.go b/types/types.go similarity index 79% rename from types/marlinctl_configs.go rename to types/types.go index b9f1b5b..33a1082 100644 --- a/types/marlinctl_configs.go +++ b/types/types.go @@ -22,3 +22,9 @@ type ReleaseJSON struct { JSONVersion int `json:"json_version"` Data interface{} `json:"data"` } + +const ( + ProjectID_marlinctl = "marlinctl" + ProjectID_beacon = "beacon" + ProjectID_relay_eth = "relay_eth" +)