From 0b6350d463ae19a2ef923971157e2088f2f99b00 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 21 Oct 2021 18:32:01 +0300 Subject: [PATCH] [#733] neofs-adm: fetch contracts release from Github Signed-off-by: Evgenii Stratonikov --- .../internal/modules/morph/download.go | 50 ++++++++++++++++++ .../internal/modules/morph/initialize.go | 8 +++ go.mod | 1 + go.sum | Bin 98463 -> 99026 bytes 4 files changed, 59 insertions(+) create mode 100644 cmd/neofs-adm/internal/modules/morph/download.go diff --git a/cmd/neofs-adm/internal/modules/morph/download.go b/cmd/neofs-adm/internal/modules/morph/download.go new file mode 100644 index 0000000000..a8e72ce2d6 --- /dev/null +++ b/cmd/neofs-adm/internal/modules/morph/download.go @@ -0,0 +1,50 @@ +package morph + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "os" + "strings" + + "github.com/google/go-github/v39/github" + "github.com/spf13/cobra" +) + +func downloadContractsFromGithub(cmd *cobra.Command) (string, error) { + gcl := github.NewClient(nil) + release, _, err := gcl.Repositories.GetLatestRelease(context.Background(), "nspcc-dev", "neofs-contract") + if err != nil { + return "", fmt.Errorf("can't fetch release info: %w", err) + } + + cmd.Printf("Found %s (%s), downloading...\n", release.GetTagName(), release.GetName()) + + var url string + for _, a := range release.Assets { + if strings.HasPrefix(a.GetName(), "neofs-contract") { + url = a.GetBrowserDownloadURL() + break + } + } + if url == "" { + return "", errors.New("can't find contracts archive in release assets") + } + + resp, err := http.Get(url) + if err != nil { + return "", fmt.Errorf("can't fetch contracts archive: %w", err) + } + defer resp.Body.Close() + + f, err := os.CreateTemp("", "neofs-contract-*.tar.gz") + if err != nil { + return "", fmt.Errorf("can't allocate temporary file: %w", err) + } + defer f.Close() + + _, err = io.Copy(f, resp.Body) + return f.Name(), err +} diff --git a/cmd/neofs-adm/internal/modules/morph/initialize.go b/cmd/neofs-adm/internal/modules/morph/initialize.go index 1ca762ff87..34d178e0b3 100644 --- a/cmd/neofs-adm/internal/modules/morph/initialize.go +++ b/cmd/neofs-adm/internal/modules/morph/initialize.go @@ -127,6 +127,14 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex if err != nil { return nil, fmt.Errorf("missing contracts path: %w", err) } + if ctrPath == "" { + cmd.Println("Contracts flag is missing, latest release will be fetched from Github.") + ctrPath, err = downloadContractsFromGithub(cmd) + if err != nil { + return nil, err + } + cmd.Printf("Saved to %s\n", ctrPath) + } } ns, err := c.GetNativeContracts() diff --git a/go.mod b/go.mod index 4bb5b853b5..ca87ae54da 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/nspcc-dev/neofs-node go 1.16 require ( + github.com/google/go-github/v39 v39.2.0 github.com/google/uuid v1.2.0 github.com/hashicorp/golang-lru v0.5.4 github.com/klauspost/compress v1.13.1 diff --git a/go.sum b/go.sum index b010e8b46308ffaba1812b1197a48ab27fec8317..e7f1556a5f03cec6164f72b9b48caab7cdc648b5 100644 GIT binary patch delta 496 zcmZ|Kv2L3{0LF23$xwBP)Jhq>wowcR92}hr=YRoYCvm`MZ(wS$Q*-t~u<24|h}5ma zJwRTeRkplDl|De~4t#fJN zrMB2`tl#%#07!W<_y}1iDO_T#B#JCmBf6EJ4aj*_QaKvt4dL7+0?ZWBK7UKZsAz7XKVj=6Uj^kdg;VFzX2Sc_g{8N7a&^q{^b$hq_ee(xwR-}#q delta 138 zcmccA$~M1|Z9{*!zCwnfm3Kykd2v~hb3txJMTv(~p=p*;Zh?M9SXObSMMj`+nx9XZ zmz!fwk$ZaRWWy+t$>&3PCijK=ZvGId(V+)2KtI$!w=}#eFs-5@-!il$C&IV9G%(b{ p**87iFf}ToFeIovJ2=oQ+}nRL<2{AR{~yLpmVd^(+5Yk5`2hD`HCzAy