Skip to content

Commit

Permalink
fix: remove systemd services on reset (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Sep 9, 2024
1 parent 2a7c5df commit d00987b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
40 changes: 38 additions & 2 deletions cmd/relayer/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"

Expand All @@ -17,6 +18,7 @@ import (
"github.com/dymensionxyz/roller/relayer"
"github.com/dymensionxyz/roller/sequencer"
globalutils "github.com/dymensionxyz/roller/utils"
"github.com/dymensionxyz/roller/utils/bash"
configutils "github.com/dymensionxyz/roller/utils/config"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/config/yamlconfig"
Expand Down Expand Up @@ -47,11 +49,19 @@ func Cmd() *cobra.Command {
// TODO: refactor
var need genesisutils.AppState
j, _ := genesis.AppState.MarshalJSON()
json.Unmarshal(j, &need)
err = json.Unmarshal(j, &need)
if err != nil {
pterm.Error.Println("failed to retrieve base denom from genesis file")
return
}
rollappDenom := need.Bank.Supply[0].Denom

rollerConfigFilePath := filepath.Join(home, consts.RollerConfigFileName)
globalutils.UpdateFieldInToml(rollerConfigFilePath, "base_denom", rollappDenom)
err = globalutils.UpdateFieldInToml(rollerConfigFilePath, "base_denom", rollappDenom)
if err != nil {
pterm.Error.Println("failed to set base denom in roller.toml")
return
}

rollappConfig, err := tomlconfig.LoadRollerConfig(home)
if err != nil {
Expand All @@ -64,6 +74,7 @@ func Cmd() *cobra.Command {
hd, err := tomlconfig.LoadHubData(home)
if err != nil {
pterm.Error.Println("failed to load hub data from roller.toml")
return
}

rollappChainData, err := tomlconfig.LoadRollappMetadataFromChain(
Expand Down Expand Up @@ -99,6 +110,31 @@ func Cmd() *cobra.Command {
return
}

pterm.Info.Println("removing old systemd services")
for _, svc := range consts.RelayerSystemdServices {
svcFileName := fmt.Sprintf("%s.service", svc)
pterm.Info.Printf("removing %s", svcFileName)
svcFilePath := filepath.Join("/etc/systemd/system/", svcFileName)
c := exec.Command("sudo", "systemctl", "stop", svcFileName)
_, err := bash.ExecCommandWithStdout(c)
if err != nil {
pterm.Error.Printf("failed to remove systemd services: %v\n", err)
return
}
c = exec.Command("sudo", "systemctl", "disable", svcFileName)
_, err = bash.ExecCommandWithStdout(c)
if err != nil {
pterm.Error.Printf("failed to remove systemd services: %v\n", err)
return
}
c = exec.Command("sudo", "rm", svcFilePath)
_, err = bash.ExecCommandWithStdout(c)
if err != nil {
pterm.Error.Printf("failed to remove systemd services: %v\n", err)
return
}
}

err = os.MkdirAll(relayerHome, 0o755)
if err != nil {
pterm.Error.Printf("failed to create %s: %v\n", relayerHome, err)
Expand Down
3 changes: 1 addition & 2 deletions cmd/relayer/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/utils"
"github.com/dymensionxyz/roller/relayer"
"github.com/dymensionxyz/roller/utils/config/tomlconfig"
"github.com/dymensionxyz/roller/utils/errorhandling"
"github.com/spf13/cobra"
)

func Cmd() *cobra.Command {
Expand Down
23 changes: 23 additions & 0 deletions cmd/rollapp/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ func runInit(cmd *cobra.Command, env string, raID string) error {
return err
}

pterm.Info.Println("removing old systemd services")
for _, svc := range consts.RollappSystemdServices {
svcFileName := fmt.Sprintf("%s.service", svc)
pterm.Info.Printf("removing %s", svcFileName)

svcFilePath := filepath.Join("/etc/systemd/system/", svcFileName)
c := exec.Command("sudo", "systemctl", "stop", svcFileName)
_, err := bash.ExecCommandWithStdout(c)
if err != nil {
return err
}
c = exec.Command("sudo", "systemctl", "disable", svcFileName)
_, err = bash.ExecCommandWithStdout(c)
if err != nil {
return err
}
c = exec.Command("sudo", "rm", svcFilePath)
_, err = bash.ExecCommandWithStdout(c)
if err != nil {
return err
}
}

// nolint:gofumpt
err = os.MkdirAll(home, 0o755)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions cmd/services/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"runtime"
"strings"

"github.com/dymensionxyz/roller/cmd/consts"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
"github.com/pterm/pterm"
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/consts"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
)

func RollappCmd() *cobra.Command {
Expand Down Expand Up @@ -48,7 +49,7 @@ func RelayerCmd() *cobra.Command {
pterm.Info.Printf(
"run %s to join the eibc market\n",
pterm.DefaultBasicText.WithStyle(pterm.FgYellow.ToStyle()).
Sprintf("roller eibc run"),
Sprintf("roller eibc init"),
)
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/services/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"runtime"
"strings"

servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
"github.com/pterm/pterm"
"github.com/spf13/cobra"

servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
)

func Cmd(services []string) *cobra.Command {
Expand All @@ -32,7 +33,7 @@ func stopSystemdServices(services []string) error {
)
}
for _, service := range services {
err := servicemanager.RestartSystemdService(fmt.Sprintf("%s.service", service))
err := servicemanager.StopSystemdService(fmt.Sprintf("%s.service", service))
if err != nil {
return fmt.Errorf("failed to stop %s systemd service: %v", service, err)
}
Expand Down

0 comments on commit d00987b

Please sign in to comment.