Skip to content

Commit

Permalink
feat: update clean command
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Dec 5, 2023
1 parent 97bcfdc commit 1edc95b
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 38 deletions.
37 changes: 36 additions & 1 deletion cli/cmd/chains/chain/eth/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eth

import (
"strings"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)
Expand All @@ -13,4 +15,37 @@ It establishes a connection to the Ethereum network and allows the node in execu
SetRun(eth).
Build()

func eth(cmd *cobra.Command, args []string) {}
func eth(cmd *cobra.Command, args []string) {

cliContext := common.GetCliWithKurtosisContext()

err := common.ValidateArgs(args)

if err != nil {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cliContext.Spinner().StartWithMessage("Starting ETH Node", "green")

responseData, err := RunEth(cliContext)
if err != nil {
if strings.Contains(err.Error(), "already running") {
cliContext.Spinner().StopWithMessage("ETH Node Already Running")
cliContext.Logger().Error(common.CodeOf(err), err.Error())
cliContext.Context().Exit(0)
} else {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatalf(common.CodeOf(err), err.Error())
}
}

err = common.WriteServiceResponseData(responseData.ServiceName, *responseData, cliContext)

if err != nil {
cliContext.Spinner().Stop()
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cliContext.Spinner().StopWithMessage("ETH Node Started. Please find service details in current working directory(services.json)")
}
50 changes: 50 additions & 0 deletions cli/cmd/chains/chain/eth/run.go
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
package eth

import (
"github.com/hugobyte/dive-core/cli/common"
)

func RunEth(cli *common.Cli) (*common.DiveServiceResponse, error) {

enclaveContext, err := cli.Context().GetEnclaveContext(common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}
runConfig := common.GetStarlarkRunConfig(`{}`, common.DiveEthHardhatNodeScript, "start_eth_node")

response, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, runConfig)

if err != nil {
return nil, common.WrapCodeToError(err, common.KurtosisContextError, "Starlark Run Failed")
}

responseData, services, skippedInstructions, err := common.GetSerializedData(cli, response)

if err != nil {
err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

return nil, common.Errorc(common.KurtosisContextError, err.Error())

}

if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return nil, common.Errorc(common.KurtosisContextError, "Already Running")
}

ethResponseData := &common.DiveServiceResponse{}

result, err := ethResponseData.Decode([]byte(responseData))

if err != nil {
err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

return nil, common.Errorc(common.KurtosisContextError, err.Error())
}

return result, nil
}
34 changes: 28 additions & 6 deletions cli/cmd/chains/chain/icon/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ var IconDecentralizeCmd = common.NewDiveCommandBuilder().
Build()

func icon(cmd *cobra.Command, args []string) {
cliContext, err := common.GetCli()
if err != nil {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
err = common.ValidateArgs(args)

cliContext := common.GetCliWithKurtosisContext()

err := common.ValidateArgs(args)
if err != nil {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
Expand All @@ -57,7 +55,9 @@ func icon(cmd *cobra.Command, args []string) {
if err != nil {
cliContext.Logger().Error(common.InvalidCommandError, err.Error())
}

var response = &common.DiveServiceResponse{}

if decentralization {
response, err = RunIconNode(cliContext)

Expand Down Expand Up @@ -95,4 +95,26 @@ func icon(cmd *cobra.Command, args []string) {
cliContext.Spinner().StopWithMessage("Icon Node Started. Please find service details in current working directory(services.json)")
}

func iconDecentralization(cmd *cobra.Command, args []string) {}
func iconDecentralization(cmd *cobra.Command, args []string) {

cliContext := common.GetCliWithKurtosisContext()

err := common.ValidateArgs(args)

if err != nil {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cliContext.Spinner().StartWithMessage("Starting Icon Node Decentralization", "green")

params := GetDecentralizeParams(serviceName, nodeEndpoint, ksPath, ksPassword, networkID)

err = RunDecentralization(cliContext, params)

if err != nil {
cliContext.Logger().Error(common.KurtosisContextError, err.Error())

}

cliContext.Spinner().StopWithMessage("Decentralization Completed")
}
29 changes: 11 additions & 18 deletions cli/cmd/chains/chain/icon/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import (

func RunIconNode(cli *common.Cli) (*common.DiveServiceResponse, error) {

_, err := cli.Context().GetKurtosisContext()

if err != nil {
return nil, common.Errorc(common.KurtosisContextError, err.Error())

}

enclaveContext, err := cli.Context().GetEnclaveContext(common.DiveEnclave)

if err != nil {
Expand Down Expand Up @@ -43,12 +36,12 @@ func RunIconNode(cli *common.Cli) (*common.DiveServiceResponse, error) {
response, services, skippedInstructions, err := common.GetSerializedData(cli, iconData)

if err != nil {
for service := range services {
err = cli.Context().RemoveService(service, common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if err != nil {
return nil, common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

return nil, common.Errorc(common.KurtosisContextError, err.Error())
}

Expand All @@ -70,7 +63,7 @@ func RunIconNode(cli *common.Cli) (*common.DiveServiceResponse, error) {

func RunDecentralization(cli *common.Cli, params string) error {

cli.Spinner().SetSuffixMessage(" Starting Icon Node Decentralization", "green")
cli.Spinner().SetSuffixMessage("Starting Icon Node Decentralization", "green")

kurtosisEnclaveContext, err := cli.Context().GetEnclaveContext(common.DiveEnclave)

Expand All @@ -87,12 +80,12 @@ func RunDecentralization(cli *common.Cli, params string) error {
_, services, skippedInstructions, err := common.GetSerializedData(cli, data)
if err != nil {

for service := range services {
err = cli.Context().RemoveService(service, common.DiveEnclave)
if err != nil {
return common.Errorc(common.InvalidEnclaveContextError, err.Error())
}
err = cli.Context().RemoveServicesByServiceNames(services, common.DiveEnclave)
if err != nil {
return common.Errorc(common.InvalidEnclaveContextError, err.Error())
}

return common.Errorc(common.KurtosisContextError, err.Error())
}
if cli.Context().CheckSkippedInstructions(skippedInstructions) {
return common.Errorc(common.KurtosisContextError, "Decentralization Already Completed ")
Expand Down
8 changes: 2 additions & 6 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"os"

"github.com/hugobyte/dive-core/cli/cmd/bridge"
Expand Down Expand Up @@ -36,11 +35,8 @@ func run(cmd *cobra.Command, args []string) error {
}

func init() {
_, err := common.GetCli()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
common.GetCli()

}

func Execute() {
Expand Down
60 changes: 60 additions & 0 deletions cli/cmd/utility/clean.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
package utility

import (
"fmt"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)

var enclaveName = ""

var CleanCmd = common.NewDiveCommandBuilder().
SetUse("clean").
SetShort("Cleans up Kurtosis leftover artifacts").
SetLong("Destroys and removes any running encalves. If no enclaves running to remove it will throw an error").
AddBoolFlagP("all", "a", false, "To Clean All the Service in Enclave").
AddStringFlagWithShortHand(&enclaveName, "enclaveName", "e", common.DiveEnclave, "Please Provide enclave name to clean").
SetRun(clean).Build()

func clean(cmd *cobra.Command, args []string) {
cliContext := common.GetCliWithKurtosisContext()

err := common.ValidateArgs(args)

if err != nil {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cleanAll, err := cmd.Flags().GetBool("all")
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Error(common.InvalidCommandError, err.Error())
}

if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

err = cliContext.FileHandler().RemoveFiles([]string{common.DiveOutFile, common.ServiceFilePath})
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

enclaves, err := cliContext.Context().GetEnclaves()
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

if len(enclaves) == 0 {
cliContext.Logger().SetOutputToStdout()
cliContext.Logger().Info("No Enclaves Running")
cliContext.Context().Exit(0)

}

if cleanAll {
enclavesInfo, err := cliContext.Context().CleanEnclaves()
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

cliContext.Logger().Info(fmt.Sprintf("Enclaves Cleaned %v", enclavesInfo))

} else {
err = cliContext.Context().CleanEnclaveByName(enclaveName)
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
}
}
25 changes: 20 additions & 5 deletions cli/common/cli.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package common

import (
"fmt"
"os"
"path/filepath"
"sync"
)

var (
CliContext *Cli
cliContext *Cli

initOnce sync.Once
)
Expand Down Expand Up @@ -37,17 +39,30 @@ func initCli() (*Cli, error) {
}, nil
}

func GetCli() (*Cli, error) {
func GetCli() *Cli {

var err error
initOnce.Do(func() {
CliContext, err = initCli()
cliContext, err = initCli()
})

if err != nil {
return nil, WrapMessageToError(err, "Failed To Retrieve Cli")
fmt.Println("Failed to get CLI context:", err)
os.Exit(1)
}

return CliContext, nil
return cliContext
}

func GetCliWithKurtosisContext() *Cli {

_, err := cliContext.Context().GetKurtosisContext()

if err != nil {
fmt.Println(Errorc(KurtosisContextError, err.Error()))
os.Exit(1)
}
return cliContext
}

func (c *Cli) Logger() Logger {
Expand Down
26 changes: 26 additions & 0 deletions cli/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"

Expand Down Expand Up @@ -319,3 +320,28 @@ func GetSerializedData(cliContext *Cli, response chan *kurtosis_core_rpc_api_bin

return serializedOutputObj, services, skippedInstruction, nil
}

func (dc *diveContext) RemoveServicesByServiceNames(services map[string]string, enclaveName string) error {
enclaveCtx, err := dc.GetEnclaveContext(enclaveName)

if err != nil {
return WrapMessageToError(err, "Failed To Remove Services")
}

for serviceName := range services {
params := fmt.Sprintf(`{"service_name": "%s"}`, serviceName)
starlarkConfig := GetStarlarkRunConfig(params, "", "")

_, err = enclaveCtx.RunStarlarkScriptBlocking(dc.ctx, removeServiceStarlarkScript, starlarkConfig)
if err != nil {
return err
}

}

return nil
}

func (dc *diveContext) Exit(statusCode int) {
os.Exit(statusCode)
}
Loading

0 comments on commit 1edc95b

Please sign in to comment.