Skip to content

Commit

Permalink
feat: add command that will update rollappd to latest drs commit (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
artemijspavlovs authored Nov 21, 2024
1 parent a4803d1 commit 93d16d2
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 96 deletions.
2 changes: 1 addition & 1 deletion cmd/da-light-client/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Cmd() *cobra.Command {
}

pterm.Info.Println("stopping existing system services, if any...")
err = servicemanager.Start([]string{"da-light-client"})
err = servicemanager.StartSystemServices([]string{"da-light-client"})
if err != nil {
pterm.Error.Println("failed to stop system services: ", err)
return
Expand Down
33 changes: 3 additions & 30 deletions cmd/relayer/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"strings"
"time"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"google.golang.org/api/iterator"
"google.golang.org/api/option"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
Expand All @@ -25,6 +23,7 @@ import (
dymintutils "github.com/dymensionxyz/roller/utils/dymint"
"github.com/dymensionxyz/roller/utils/errorhandling"
"github.com/dymensionxyz/roller/utils/filesystem"
firebaseutils "github.com/dymensionxyz/roller/utils/firebase"
"github.com/dymensionxyz/roller/utils/genesis"
"github.com/dymensionxyz/roller/utils/logging"
relayerutils "github.com/dymensionxyz/roller/utils/relayer"
Expand Down Expand Up @@ -136,38 +135,12 @@ func Cmd() *cobra.Command {

// Fetch DRS version information using the nested collection path
// Path format: versions/{version}/revisions/{revision}
drsDoc := client.Collection("versions").
Doc(drsVersion).
Collection("revisions").
OrderBy("timestamp", firestore.Desc).
Limit(1).
Documents(ctx)

doc, err := drsDoc.Next()
if err == iterator.Done {
pterm.Error.Printfln("DRS version not found for %s", drsVersion)
return
}
drsInfo, err := firebaseutils.GetLatestDrsVersionCommit(drsVersion)
if err != nil {
pterm.Error.Printfln("DRS version not found for %s", drsVersion)
return
}

var drsInfo dependencies.DrsVersionInfo
if err := doc.DataTo(&drsInfo); err != nil {
pterm.Error.Printfln("DRS version not found for %s", drsVersion)
pterm.Error.Println("failed to retrieve latest DRS version: ", err)
return
}

dep := dependencies.DefaultRelayerPrebuiltDependencies()
for _, v := range dep {
err := dependencies.InstallBinaryFromRelease(v)
if err != nil {
pterm.Error.Printfln("failed to install binary: %s", err)
return
}
}

rbi := dependencies.NewRollappBinaryInfo(
raResp.Rollapp.GenesisInfo.Bech32Prefix,
drsInfo.Commit,
Expand Down
18 changes: 18 additions & 0 deletions cmd/rollapp/drs/drs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package drs

import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/rollapp/drs/update"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "drs",
Short: "Commands related to drs(Dymension RollApp Standard).",
}

cmd.AddCommand(update.Cmd())

return cmd
}
142 changes: 142 additions & 0 deletions cmd/rollapp/drs/update/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package update

import (
"os"
"path/filepath"
"strings"

"github.com/pterm/pterm"
"github.com/spf13/cobra"

initconfig "github.com/dymensionxyz/roller/cmd/config/init"
"github.com/dymensionxyz/roller/cmd/consts"
"github.com/dymensionxyz/roller/utils/archives"
"github.com/dymensionxyz/roller/utils/dependencies"
dymintutils "github.com/dymensionxyz/roller/utils/dymint"
"github.com/dymensionxyz/roller/utils/filesystem"
firebaseutils "github.com/dymensionxyz/roller/utils/firebase"
rollapputils "github.com/dymensionxyz/roller/utils/rollapp"
"github.com/dymensionxyz/roller/utils/roller"
servicemanager "github.com/dymensionxyz/roller/utils/service_manager"
)

func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Update the drs(Dymension RollApp Standard).",
Run: func(cmd *cobra.Command, args []string) {
pterm.Info.Println("preparing update")
home, err := filesystem.ExpandHomePath(
cmd.Flag(initconfig.GlobalFlagNames.Home).Value.String(),
)
if err != nil {
pterm.Error.Println("failed to expand home directory")
return
}

rollerData, err := roller.LoadConfig(home)
if err != nil {
pterm.Error.Println("failed to load roller config file", err)
return
}

cv, err := dependencies.ExtractCommitFromBinaryVersion(consts.Executables.RollappEVM)
if err != nil {
pterm.Error.Println("Failed to get the current commit:", err)
return
}

drsVersion, err := rollapputils.ExtractDrsVersionFromBinary()
if err != nil {
pterm.Error.Println("Failed to extract drs version from binary:", err)
return
}

drsInfo, err := firebaseutils.GetLatestDrsVersionCommit(drsVersion)
if err != nil {
pterm.Error.Println("Failed to get the latest commit:", err)
return
}

if drsInfo.Commit[:6] == cv {
pterm.Info.Println("You are already using the latest version of DRS")
return
}

// if doesn't match, take latest as the reference
// download the latest, build into ~/.roller/tmp
raResp, err := rollapputils.GetMetadataFromChain(
rollerData.RollappID,
rollerData.HubData,
)
if err != nil {
pterm.Error.Println("failed to fetch rollapp information from hub: ", err)
return
}

raNewBinDir, err := os.MkdirTemp(os.TempDir(), "rollapp-drs-update")
if err != nil {
pterm.Error.Println(
"failed to create temporary directory for rollapp binary: ",
err,
)
return
}
defer os.RemoveAll(raNewBinDir)

Check failure on line 85 in cmd/rollapp/drs/update/update.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `os.RemoveAll` is not checked (errcheck)

rbi := dependencies.NewRollappBinaryInfo(
raResp.Rollapp.GenesisInfo.Bech32Prefix,
drsInfo.Commit,
strings.ToLower(raResp.Rollapp.VmType),
)

raDep := dependencies.DefaultRollappDependency(rbi)
// override the binary destination with the previously created tmp directory
// as the binary will be copied to the final destination after stopping the
// system services
tmpBinLocation := filepath.Join(
raNewBinDir,
"rollappd",
)

pterm.Info.Println("starting update")
raDep.Binaries[0].BinaryDestination = tmpBinLocation
err = dependencies.InstallBinaryFromRepo(raDep, raDep.DependencyName)
if err != nil {
pterm.Error.Println("failed to install rollapp binary: ", err)
return
}

// stop services
err = servicemanager.StopSystemServices([]string{"rollapp"})
if err != nil {
pterm.Error.Println("failed to stop rollapp services: ", err)
return
}

// repalce the current binary with the new one
err = archives.MoveBinaryIntoPlaceAndMakeExecutable(
tmpBinLocation,
consts.Executables.RollappEVM,
)
if err != nil {
pterm.Error.Println("failed to move rollapp binary: ", err)
return
}

// start services
err = servicemanager.StartSystemServices([]string{"rollapp"})
if err != nil {
pterm.Error.Println("failed to stop rollapp services: ", err)
return
}

// wait for healthy endpoint
dymintutils.WaitForHealthyRollApp("http://localhost:26657/health")

pterm.Success.Println("update complete")
},
}

return cmd
}
2 changes: 2 additions & 0 deletions cmd/rollapp/rollapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/roller/cmd/rollapp/config"
"github.com/dymensionxyz/roller/cmd/rollapp/drs"
initrollapp "github.com/dymensionxyz/roller/cmd/rollapp/init"
"github.com/dymensionxyz/roller/cmd/rollapp/keys"
"github.com/dymensionxyz/roller/cmd/rollapp/migrate"
Expand Down Expand Up @@ -32,6 +33,7 @@ func Cmd() *cobra.Command {
cmd.AddCommand(sequencer.Cmd())
cmd.AddCommand(keys.Cmd())
cmd.AddCommand(migrate.Cmd())
cmd.AddCommand(drs.Cmd())

sl := []string{"rollapp", "da-light-client"}
cmd.AddCommand(
Expand Down
38 changes: 26 additions & 12 deletions utils/archives/archives.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,8 @@ func ExtractTarGz(path string, data io.ReadCloser, dep dependencytypes.Dependenc
}

for _, bin := range dep.Binaries {
err := bash.ExecCommandWithInteractions(
"sudo",
"mv",
filepath.Join(path, bin.Binary),
bin.BinaryDestination,
)
if err != nil {
return err
}
err = bash.ExecCommandWithInteractions(
"sudo",
"chmod", "+x",
err := MoveBinaryIntoPlaceAndMakeExecutable(
path,
bin.BinaryDestination,
)
if err != nil {
Expand All @@ -76,3 +66,27 @@ func ExtractTarGz(path string, data io.ReadCloser, dep dependencytypes.Dependenc

return err
}

// MoveBinaryIntoPlaceAndMakeExecutable is a fantastic function name, ik
func MoveBinaryIntoPlaceAndMakeExecutable(
src, dest string,
) error {
err := bash.ExecCommandWithInteractions(
"sudo",
"mv",
src,
dest,
)
if err != nil {
return err
}
err = bash.ExecCommandWithInteractions(
"sudo",
"chmod", "+x",
dest,
)
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 93d16d2

Please sign in to comment.