Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

downloading node config if not there #3

Merged
merged 1 commit into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions cmd/node_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ the latest version of go-spacemesh if not already installed.`,
Run: func(cmd *cobra.Command, args []string) {
common.InitDotDir()

fmt.Println("Starting node...")

// Check if node is running according to state file
sp := common.NewStateProvider()
if running := sp.NodeIsRunning(); running {
Expand All @@ -41,7 +39,6 @@ the latest version of go-spacemesh if not already installed.`,
}
fmt.Println("Download Finished")
fmt.Println("Unpacking...")
fmt.Printf("Creating directory %s", common.BinDirectory())

err := util.Unzip(zipFileName, common.BinDirectory())
cobra.CheckErr(err)
Expand All @@ -51,6 +48,12 @@ the latest version of go-spacemesh if not already installed.`,
fmt.Println("\nDone")
}

// Check if node config is there
if _, err := os.Stat(common.NodeConfigFile()); os.IsNotExist(err) {
fmt.Println("Node config not found. Downloading default config.")
common.InitNodeConfig(common.NodeConfigFile())
}

fmt.Println("Starting Node...")

nodePath := common.NodeBin()
Expand All @@ -59,6 +62,7 @@ the latest version of go-spacemesh if not already installed.`,
"--listen", "/ip4/0.0.0.0/tcp/7513", // TODO(jonZlotnik): passthrough port flag
"--config", common.NodeConfigFile(),
"--data-folder", common.NodeDataDirectory(),
"--smeshing-opts-datadir", common.NodeDataDirectory(),
"--grpc", "node",
"--grpc-port", "9092",
)
Expand All @@ -71,7 +75,7 @@ the latest version of go-spacemesh if not already installed.`,
err = nodeProc.Start()
cobra.CheckErr(err)
sp.UpdateNodePid(nodeProc.Process.Pid)
fmt.Printf("Just launched %d, exiting\n", nodeProc.Process.Pid)
fmt.Printf("Launched with PID: %d\n", nodeProc.Process.Pid)
},
}

Expand Down
42 changes: 38 additions & 4 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package common

import (
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"

"github.com/spacemeshos/smcli/util"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,7 +52,6 @@ func SystemType() string {
// │ └── config.json
// ├── logs
// │ └── go-spacemesh.log
// ├── networks.json
// ├── config.yaml
// └── state.json

Expand Down Expand Up @@ -87,9 +90,6 @@ func ConfigFile() string {
func StateFile() string {
return filepath.Join(DotDirectory(), "state.json")
}
func NetworksFile() string {
return filepath.Join(DotDirectory(), "networks.json")
}
func LogDirectory() string {
return filepath.Join(DotDirectory(), "logs")
}
Expand All @@ -109,3 +109,37 @@ func InitDotDir() {
func OpenNodeLogFile() (*os.File, error) {
return os.OpenFile(LogFile(), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0660)
}

func InitNodeConfig(filePath string) {

type NetworkDiscoveryResp []struct {
NetName string `json:"netName"`
NetID int `json:"netID"`
Conf string `json:"conf"`
GrpcAPI string `json:"grpcAPI"`
JSONAPI string `json:"jsonAPI"`
Explorer string `json:"explorer"`
ExplorerAPI string `json:"explorerAPI"`
ExplorerVersion string `json:"explorerVersion"`
ExplorerConf string `json:"explorerConf"`
Dash string `json:"dash"`
DashAPI string `json:"dashAPI"`
DashVersion string `json:"dashVersion"`
Repository string `json:"repository"`
MinNodeVersion string `json:"minNodeVersion"`
MaxNodeVersion string `json:"maxNodeVersion"`
MinSmappRelease string `json:"minSmappRelease"`
LatestSmappRelease string `json:"latestSmappRelease"`
SmappBaseDownloadURL string `json:"smappBaseDownloadUrl"`
NodeBaseDownloadURL string `json:"nodeBaseDownloadUrl"`
}
netDiscoveryResp := NetworkDiscoveryResp{}
resp, err := http.Get(NetworkDiscoveryUrl)
cobra.CheckErr(err)
err = json.NewDecoder(resp.Body).Decode(&netDiscoveryResp)
cobra.CheckErr(err)

confUrl := netDiscoveryResp[0].Conf
err = util.DownloadFile(filePath, confUrl)
cobra.CheckErr(err)
}
42 changes: 42 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,55 @@ require (
)

require (
code.cloudfoundry.org/bytefmt v0.0.0-20180906201452-2aa6f33b730c // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/beorn7/perks v1.0.0 // indirect
github.com/btcsuite/btcd v0.0.0-20190629003639-c26ffa870fd8 // indirect
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-kit/kit v0.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/huin/goupnp v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/nullstyle/go-xdr v0.0.0-20180726165426-f4c839f75077 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v0.9.3 // indirect
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
github.com/prometheus/common v0.4.0 // indirect
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 // indirect
github.com/pyroscope-io/pyroscope v0.0.30 // indirect
github.com/seehuhn/mt19937 v0.0.0-20180715112136-cc7708819361 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/spacemeshos/ed25519 v0.0.0-20190530014421-e235766d15a1 // indirect
github.com/spacemeshos/go-spacemesh v0.1.45 // indirect
github.com/spacemeshos/merkle-tree v0.0.0-20191028110812-1908c3126c82 // indirect
github.com/spacemeshos/poet v0.1.1-0.20201013202053-99eed195dc2d // indirect
github.com/spacemeshos/post v0.0.0-20200707150818-013318bab6f4 // indirect
github.com/spacemeshos/sha256-simd v0.0.0-20190111104731-8575aafc88c9 // indirect
github.com/spacemeshos/smutil v0.0.0-20190604133034-b5189449f5c5 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20190318030020-c3a204f8e965 // indirect
github.com/tzdybal/go-disk-usage v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd // indirect
google.golang.org/grpc v1.46.2 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
nanomsg.org/go-mangos v1.4.0 // indirect
)

require (
Expand Down
Loading