Skip to content

Commit 34102fb

Browse files
author
tok-kkk
committed
change the update command
1 parent ec9d579 commit 34102fb

7 files changed

Lines changed: 47 additions & 22 deletions

File tree

cmd/provider/aws.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (p providerAws) Deploy(ctx *cli.Context) error {
6767
name := ctx.String("name")
6868
tags := ctx.String("tags")
6969

70-
latestVersion, err := util.LatestReleaseVersion()
70+
latestVersion, err := util.LatestStableRelease()
7171
if err != nil {
7272
return err
7373
}

cmd/provider/do.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (p providerDo) Deploy(ctx *cli.Context) error {
3333
name := ctx.String("name")
3434
tags := ctx.String("tags")
3535

36-
latestVersion, err := util.LatestReleaseVersion()
36+
latestVersion, err := util.LatestStableRelease()
3737
if err != nil {
3838
return err
3939
}

cmd/provider/gcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (p providerGcp) Deploy(ctx *cli.Context) error {
7171
name := ctx.String("name")
7272
tags := ctx.String("tags")
7373

74-
latestVersion, err := util.LatestReleaseVersion()
74+
latestVersion, err := util.LatestStableRelease()
7575
if err != nil {
7676
return err
7777
}

cmd/update.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"io/ioutil"
67
"net/http"
78
"strings"
89

910
"github.com/fatih/color"
11+
"github.com/google/go-github/v31/github"
1012
"github.com/hashicorp/go-version"
1113
"github.com/renproject/darknode-cli/util"
1214
"github.com/renproject/phi"
@@ -26,7 +28,7 @@ func updateNode(ctx *cli.Context) error {
2628

2729
// Use latest version if user doesn't provide a version number
2830
if version == "" {
29-
version, err = util.LatestReleaseVersion()
31+
version, err = util.LatestStableRelease()
3032
if err != nil {
3133
return err
3234
}
@@ -62,16 +64,13 @@ func updateSingleNode(name, ver string) error {
6264
url := fmt.Sprintf("https://github.com/renproject/darknode-release/releases/download/%v", ver)
6365
script := fmt.Sprintf(`mv ~/.darknode/bin/darknode ~/.darknode/bin/darknode-backup &&
6466
curl -sL %v/darknode > ~/.darknode/bin/darknode &&
65-
curl -sL %v/migration > ~/.darknode/bin/migration &&
6667
chmod +x ~/.darknode/bin/darknode &&
67-
chmod +x ~/.darknode/bin/migration &&
6868
systemctl --user stop darknode &&
6969
cp -a ~/.darknode/db/. ~/.darknode/db_bak/ &&
70-
~/.darknode/bin/migration &&
7170
rm -rf ~/.darknode/db &&
7271
mv ~/.darknode/db_bak ~/.darknode/db &&
7372
echo %v > ~/.darknode/version &&
74-
systemctl --user restart darknode`, url, url, ver)
73+
systemctl --user restart darknode`, url, ver)
7574
err = util.RemoteRun(name, script)
7675
if err != nil {
7776
color.Red("cannot update darknode %v, error = %v", name, err)
@@ -83,8 +82,8 @@ systemctl --user restart darknode`, url, url, ver)
8382
}
8483

8584
func validateVersion(version string) error {
86-
url := fmt.Sprintf("https://api.github.com/repos/renproject/darknode-release/releases/tags/%v", version)
87-
response, err := http.Get(url)
85+
client := github.NewClient(nil)
86+
_, response ,err := client.Repositories.GetReleaseByTag(context.Background(), "renproject", "darknode-release", version )
8887
if err != nil {
8988
return err
9089
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/ethereum/go-ethereum v1.9.6
99
github.com/fatih/color v1.7.0
1010
github.com/google/go-github v17.0.0+incompatible
11-
github.com/google/go-querystring v1.0.0 // indirect
11+
github.com/google/go-github/v31 v31.0.0
1212
github.com/hashicorp/go-version v1.2.0
1313
github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6
1414
github.com/multiformats/go-multiaddr v0.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
112112
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
113113
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
114114
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
115+
github.com/google/go-github/v31 v31.0.0 h1:JJUxlP9lFK+ziXKimTCprajMApV1ecWD4NB6CCb0plo=
116+
github.com/google/go-github/v31 v31.0.0/go.mod h1:NQPZol8/1sMoWYGN2yaALIBytu17gAWfhbweiEed3pM=
115117
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
116118
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
117119
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=

util/node.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package util
22

33
import (
4+
"context"
45
"encoding/hex"
5-
"encoding/json"
66
"errors"
77
"fmt"
88
"io/ioutil"
99
"net/http"
1010
"path/filepath"
11+
"regexp"
1112
"strings"
1213

14+
"github.com/google/go-github/v31/github"
15+
"github.com/hashicorp/go-version"
1316
"github.com/renproject/darknode-cli/darknode"
1417
"github.com/renproject/darknode-cli/darknode/addr"
1518
"golang.org/x/crypto/ssh"
@@ -160,27 +163,48 @@ func ValidateTags(have, required string) bool {
160163
return true
161164
}
162165

163-
// LatestReleaseVersion checks the darknode release repo and return the version
166+
// LatestStableRelease checks the darknode release repo and return the version
164167
// of the latest release.
165-
func LatestReleaseVersion() (string, error) {
166-
url := "https://api.github.com/repos/renproject/darknode-release/releases/latest"
167-
response, err := http.Get(url)
168+
func LatestStableRelease() (string, error) {
169+
client := github.NewClient(nil)
170+
releases, response ,err := client.Repositories.ListReleases(context.Background(), "renproject", "darknode-release", nil )
168171
if err != nil {
169172
return "", err
170173
}
171174
if response.StatusCode != http.StatusOK {
172175
return "", fmt.Errorf("cannot get latest darknode release from github, error code = %v", response.StatusCode)
173176
}
174177

175-
resp := struct {
176-
TagName string `json:"tag_name"`
177-
}{}
178-
err = json.NewDecoder(response.Body).Decode(&resp)
179-
return resp.TagName, err
178+
latest, err := version.NewVersion("0.0.0")
179+
if err != nil {
180+
return "", err
181+
}
182+
verReg := "^v?[0-9]+\\.[0-9]+\\.[0-9]+$"
183+
for _, release := range releases {
184+
match, err := regexp.MatchString(verReg, *release.TagName)
185+
if err != nil {
186+
return "", err
187+
}
188+
if match {
189+
ver, err:= version.NewVersion(*release.TagName)
190+
if err != nil {
191+
return "", err
192+
}
193+
if ver.GreaterThan(latest){
194+
latest = ver
195+
}
196+
}
197+
}
198+
if latest.String() == "0.0.0"{
199+
return "", errors.New("cannot find any stable release")
200+
}
201+
202+
return latest.String(), nil
180203
}
181204

205+
182206
func isDeployed(name string) bool {
183207
path := NodePath(name)
184208
script := fmt.Sprintf("cd %v && terraform output ip", path)
185209
return SilentRun("bash", "-c", script) == nil
186-
}
210+
}

0 commit comments

Comments
 (0)