Skip to content

Commit

Permalink
added recreate, restart, config apply and config show for relay_eth
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Jan 8, 2021
1 parent 750d0e1 commit a17db6d
Show file tree
Hide file tree
Showing 12 changed files with 406 additions and 7 deletions.
16 changes: 9 additions & 7 deletions cmd/relay/eth/actions/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ var CreateCmd = &cobra.Command{
return
}

projectConfig.CurrentVersion = versionToRun.Version

viper.Set(cmn.ProjectID, projectConfig)
err = viper.WriteConfig()
if err != nil {
log.Error("Failure while updating config for current version: ", err.Error())
return
if updatePolicy == "" && version == "" {
projectConfig.CurrentVersion = versionToRun.Version

viper.Set(cmn.ProjectID, projectConfig)
err = viper.WriteConfig()
if err != nil {
log.Error("Failure while updating config for current version: ", err.Error())
return
}
}
},
}
Expand Down
75 changes: 75 additions & 0 deletions cmd/relay/eth/actions/recreate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright © 2020 MARLIN TEAM <info@marlin.pro>
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 actions

import (
"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"
)

// AppCmd represents the registry command
var RecreateCmd = &cobra.Command{
Use: "recreate",
Short: "Trigger recreate for the service",
Long: `Trigger recreate fot the service`,
PreRunE: cfg.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 config: ", err)
os.Exit(1)
}

runnerId, version, err := cmn.GetResourceMetaData(projectConfig, instanceId)
if err != nil {
log.Error("Error while fetching resource information: ", err)
os.Exit(1)
}

runner, err := projectRunners.GetRunnerInstance(runnerId, version, projectConfig.Storage, struct{}{}, true, true, instanceId)
if err != nil {
log.Error("Cannot get runner: ", err.Error())
os.Exit(1)
}

err = runner.PreRunSanity()
if err != nil {
log.Error("Failure during pre run sanity: ", err.Error())
return
}

err = runner.Recreate()
if err != nil {
log.Error("Failure during recreating application: ", err.Error())
return
}

log.Info("Restarted")
},
}

func init() {
RecreateCmd.Flags().StringVarP(&instanceId, "instance-id", "i", "001", "instance-id of the resource")
}
73 changes: 73 additions & 0 deletions cmd/relay/eth/actions/restart.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright © 2020 MARLIN TEAM <info@marlin.pro>
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 actions

import (
"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"
)

// AppCmd represents the registry command
var RestartCmd = &cobra.Command{
Use: "restart",
Short: "Trigger restart for the service",
Long: `Trigger restart fot the service`,
PreRunE: cfg.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 config: ", err)
os.Exit(1)
}

runnerId, version, err := cmn.GetResourceMetaData(projectConfig, instanceId)
if err != nil {
log.Error("Error while fetching resource information: ", err)
os.Exit(1)
}

runner, err := projectRunners.GetRunnerInstance(runnerId, version, projectConfig.Storage, struct{}{}, true, true, instanceId)
if err != nil {
log.Error("Cannot get runner: ", err.Error())
os.Exit(1)
}

err = runner.PreRunSanity()
if err != nil {
log.Error("Failure during pre run sanity: ", err.Error())
return
}

err = runner.Restart()
if err != nil {
log.Error("Failure during restart: ", err.Error())
return
}
},
}

func init() {
RestartCmd.Flags().StringVarP(&instanceId, "instance-id", "i", "001", "instance-id of the resource")
}
14 changes: 14 additions & 0 deletions cmd/relay/eth/config/actions/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package actions
import (
"os"

"github.com/marlinprotocol/ctl2/modules/util"

cmn "github.com/marlinprotocol/ctl2/cmd/relay/eth/common"
"github.com/marlinprotocol/ctl2/types"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -37,6 +39,18 @@ var ConfigApplyCmd = &cobra.Command{
os.Exit(1)
}

viper.Set(cmn.ProjectID, projectConfigMod)
err = viper.WriteConfig()
if err != nil {
log.Error("Error while writing configs to disk: ", err.Error())
os.Exit(1)
}
err = util.RemoveConfigEntry(modifiedProjectID)
if err != nil {
log.Error("Error while removing staging configs from disk: ", err.Error())
os.Exit(1)
}
log.Info("Configs have been updated. These will take effect next time you create an application instance.")
},
}

Expand Down
36 changes: 36 additions & 0 deletions cmd/relay/eth/config/actions/show.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package actions

import (
"encoding/json"
"os"

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 ConfigShowCmd = &cobra.Command{
Use: "show",
Short: "Show current project configuration",
Long: `Show current project configuration`,
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)
}
s, err := json.MarshalIndent(projectConfig, "", " ")
if err != nil {
log.Error("Error while decoding json: ", err.Error())
os.Exit(1)
}
log.Info("Current config:")
util.PrintPrettyDiff(string(s))
},
}
1 change: 1 addition & 0 deletions cmd/relay/eth/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var ConfigCmd = &cobra.Command{
}

func init() {
ConfigCmd.AddCommand(actions.ConfigShowCmd)
ConfigCmd.AddCommand(actions.ConfigModifyCmd)
ConfigCmd.AddCommand(actions.ConfigDiffCmd)
ConfigCmd.AddCommand(actions.ConfigApplyCmd)
Expand Down
2 changes: 2 additions & 0 deletions cmd/relay/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ var EthCmd = &cobra.Command{

func init() {
EthCmd.AddCommand(actions.CreateCmd)
EthCmd.AddCommand(actions.RestartCmd)
EthCmd.AddCommand(actions.RecreateCmd)
EthCmd.AddCommand(actions.StatusCmd)
EthCmd.AddCommand(actions.DestroyCmd)
EthCmd.AddCommand(actions.VersionsCmd)
Expand Down
9 changes: 9 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ It can spawn up beacons, gateways, relays on various platforms and runtimes.`,
}
log.SetLevel(lvl)

// showCmdTree(cmd.Root(), "")

err = readConfig()
var configuredRegistries []types.Registry
err = viper.UnmarshalKey("registries", &configuredRegistries)
Expand Down Expand Up @@ -91,6 +93,13 @@ It can spawn up beacons, gateways, relays on various platforms and runtimes.`,
},
}

func showCmdTree(cmd *cobra.Command, lvl string) {
fmt.Println(lvl + "-" + cmd.Use)
for _, c := range cmd.Commands() {
showCmdTree(c, lvl+" |")
}
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
61 changes: 61 additions & 0 deletions modules/runner/beacon/linux-amd64.supervisor.runner01.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,67 @@ func (r *linux_amd64_supervisor_runner01) Create(runtimeArgs map[string]string)
return nil
}

func (r *linux_amd64_supervisor_runner01) Restart() error {
available, resData, err := r.fetchResourceInformation(GetResourceFileLocation(r.Storage, r.InstanceId))
if err != nil {
return err
}
if !available {
return errors.New("resource by id " + r.InstanceId + " doesn't exist. Can't return status.")
}

_, err1 := exec.Command("supervisorctl", "restart", resData.BeaconProgram).Output()

if err1 == nil {
log.Info("Triggered restart")
} else {
log.Warning("Triggered restart, however supervisor did return some errors. ", err1.Error())
}

return nil
}

func (r *linux_amd64_supervisor_runner01) Recreate() error {
available, resData, err := r.fetchResourceInformation(GetResourceFileLocation(r.Storage, r.InstanceId))
if err != nil {
return err
}
if !available {
return errors.New("resource by id " + r.InstanceId + " doesn't exist. Can't return status.")
}
err = r.Destroy()
if err != nil {
return err
}

err = r.PostRun()
if err != nil {
return err
}

err = r.Prepare()
if err != nil {
return err
}

ref := reflect.ValueOf(resData)
typeOfref := ref.Type()
runtimeArgs := make(map[string]string)

for i := 0; i < ref.NumField(); i++ {
var name = typeOfref.Field(i).Name
if name != "StartTime" {
runtimeArgs[name] = ref.Field(i).String()
}
}

err = r.Create(runtimeArgs)
if err != nil {
return err
}
return nil
}

func (r *linux_amd64_supervisor_runner01) Destroy() error {
available, resData, err := r.fetchResourceInformation(GetResourceFileLocation(r.Storage, r.InstanceId))
if err != nil {
Expand Down
Loading

0 comments on commit a17db6d

Please sign in to comment.