From ec82dd578a18fd08294425d7d1b0271dc9c6a6b1 Mon Sep 17 00:00:00 2001 From: Artur Troian Date: Thu, 22 Aug 2024 09:47:11 -0500 Subject: [PATCH] feat: add get-manifest cli cmd Signed-off-by: Artur Troian --- _run/common-commands.mk | 8 +++ cmd/provider-services/cmd/helpers.go | 35 ++++++++++++ cmd/provider-services/cmd/manifest.go | 78 +++++++++++++++++++++++++++ cmd/provider-services/cmd/root.go | 2 +- gateway/rest/client.go | 41 ++++++++++++++ gateway/rest/path.go | 4 ++ gateway/rest/router.go | 2 +- 7 files changed, 168 insertions(+), 2 deletions(-) diff --git a/_run/common-commands.mk b/_run/common-commands.mk index 7bc16654..1afeecaa 100644 --- a/_run/common-commands.mk +++ b/_run/common-commands.mk @@ -82,6 +82,14 @@ send-manifest: --from "$(KEY_NAME)" \ --provider "$(PROVIDER_ADDRESS)" +.PHONY: get-manifest +get-manifest: + $(PROVIDER_SERVICES) get-manifest \ + --dseq "$(DSEQ)" \ + --from "$(KEY_NAME)" \ + --provider "$(PROVIDER_ADDRESS)" + + .PHONY: deployment-create deployment-create: $(AKASH) tx deployment create "$(SDL_PATH)" \ diff --git a/cmd/provider-services/cmd/helpers.go b/cmd/provider-services/cmd/helpers.go index 173ec0ba..397b79b2 100644 --- a/cmd/provider-services/cmd/helpers.go +++ b/cmd/provider-services/cmd/helpers.go @@ -80,6 +80,41 @@ func dseqFromFlags(flags *pflag.FlagSet) (uint64, error) { return flags.GetUint64(FlagDSeq) } +func leaseIDFromFlags(cflags *pflag.FlagSet, owner string) (mtypes.LeaseID, error) { + str, err := cflags.GetString(FlagProvider) + if err != nil { + return mtypes.LeaseID{}, err + } + + provider, err := sdk.AccAddressFromBech32(str) + if err != nil { + return mtypes.LeaseID{}, err + } + + dseq, err := cflags.GetUint64(FlagDSeq) + if err != nil { + return mtypes.LeaseID{}, err + } + + gseq, err := cflags.GetUint32(FlagGSeq) + if err != nil { + return mtypes.LeaseID{}, err + } + + oseq, err := cflags.GetUint32(FlagOSeq) + if err != nil { + return mtypes.LeaseID{}, err + } + + return mtypes.LeaseID{ + Owner: owner, + DSeq: dseq, + GSeq: gseq, + OSeq: oseq, + Provider: provider.String(), + }, nil +} + func providerFromFlags(flags *pflag.FlagSet) (sdk.Address, error) { provider, err := flags.GetString(FlagProvider) if err != nil { diff --git a/cmd/provider-services/cmd/manifest.go b/cmd/provider-services/cmd/manifest.go index 79147623..91de6c4e 100644 --- a/cmd/provider-services/cmd/manifest.go +++ b/cmd/provider-services/cmd/manifest.go @@ -25,6 +25,13 @@ var ( errSubmitManifestFailed = errors.New("submit manifest to some providers has been failed") ) +func ManifestCmds() []*cobra.Command { + return []*cobra.Command{ + SendManifestCmd(), + GetManifestCmd(), + } +} + // SendManifestCmd looks up the Providers blockchain information, // and POSTs the SDL file to the Gateway address. func SendManifestCmd() *cobra.Command { @@ -45,6 +52,77 @@ func SendManifestCmd() *cobra.Command { return cmd } +// GetManifestCmd reads current manifest from the provider +func GetManifestCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "get-manifest", + Args: cobra.ExactArgs(0), + Short: "Read manifest from provider", + SilenceUsage: true, + RunE: func(cmd *cobra.Command, args []string) error { + cctx, err := sdkclient.GetClientTxContext(cmd) + if err != nil { + return err + } + + ctx := cmd.Context() + + cl, err := aclient.DiscoverQueryClient(ctx, cctx) + if err != nil { + return err + } + + cert, err := cutils.LoadAndQueryCertificateForAccount(cmd.Context(), cctx, nil) + if err != nil { + return markRPCServerError(err) + } + + lid, err := leaseIDFromFlags(cmd.Flags(), cctx.GetFromAddress().String()) + if err != nil { + return err + } + + prov, _ := sdk.AccAddressFromBech32(lid.Provider) + gclient, err := gwrest.NewClient(ctx, cl, prov, []tls.Certificate{cert}) + if err != nil { + return err + } + + mani, err := gclient.GetManifest(ctx, lid) + if err != nil { + return err + } + + buf := &bytes.Buffer{} + + switch cmd.Flag(flagOutput).Value.String() { + case outputJSON: + err = json.NewEncoder(buf).Encode(mani) + case outputYAML: + err = yaml.NewEncoder(buf).Encode(mani) + } + + if err != nil { + return err + } + + _, err = fmt.Fprint(cmd.OutOrStdout(), buf.String()) + + if err != nil { + return err + } + + return nil + }, + } + + addLeaseFlags(cmd) + + cmd.Flags().StringP(flagOutput, "o", outputYAML, "output format json|yaml. default yaml") + + return cmd +} + func doSendManifest(cmd *cobra.Command, sdlpath string) error { cctx, err := sdkclient.GetClientTxContext(cmd) if err != nil { diff --git a/cmd/provider-services/cmd/root.go b/cmd/provider-services/cmd/root.go index 1d11f96f..e7611458 100644 --- a/cmd/provider-services/cmd/root.go +++ b/cmd/provider-services/cmd/root.go @@ -39,7 +39,7 @@ func NewRootCmd() *cobra.Command { return nil } - cmd.AddCommand(SendManifestCmd()) + cmd.AddCommand(ManifestCmds()...) cmd.AddCommand(statusCmd()) cmd.AddCommand(leaseStatusCmd()) cmd.AddCommand(leaseEventsCmd()) diff --git a/gateway/rest/client.go b/gateway/rest/client.go index f3439167..1862b2cf 100644 --- a/gateway/rest/client.go +++ b/gateway/rest/client.go @@ -48,6 +48,7 @@ type Client interface { Status(ctx context.Context) (*provider.Status, error) Validate(ctx context.Context, gspec dtypes.GroupSpec) (provider.ValidateGroupSpecResult, error) SubmitManifest(ctx context.Context, dseq uint64, mani manifest.Manifest) error + GetManifest(ctx context.Context, id mtypes.LeaseID) (manifest.Manifest, error) LeaseStatus(ctx context.Context, id mtypes.LeaseID) (LeaseStatus, error) LeaseEvents(ctx context.Context, id mtypes.LeaseID, services string, follow bool) (*LeaseKubeEvents, error) LeaseLogs(ctx context.Context, id mtypes.LeaseID, services string, follow bool, tailLines int64) (*ServiceLogs, error) @@ -425,6 +426,46 @@ func (c *client) SubmitManifest(ctx context.Context, dseq uint64, mani manifest. return createClientResponseErrorIfNotOK(resp, responseBuf) } +func (c *client) GetManifest(ctx context.Context, lid mtypes.LeaseID) (manifest.Manifest, error) { + uri, err := makeURI(c.host, getManifestPath(lid)) + if err != nil { + return nil, err + } + + req, err := http.NewRequestWithContext(ctx, "GET", uri, nil) + if err != nil { + return nil, err + } + + rCl := c.newReqClient(ctx) + resp, err := rCl.hclient.Do(req) + + if err != nil { + return nil, err + } + + defer func() { + _ = resp.Body.Close() + }() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + err = createClientResponseErrorIfNotOK(resp, bytes.NewBuffer(body)) + if err != nil { + return nil, err + } + + var mani manifest.Manifest + if err = json.Unmarshal(body, &mani); err != nil { + return nil, err + } + + return mani, nil +} + func (c *client) MigrateEndpoints(ctx context.Context, endpoints []string, dseq uint64, gseq uint32) error { uri, err := makeURI(c.host, "endpoint/migrate") if err != nil { diff --git a/gateway/rest/path.go b/gateway/rest/path.go index 175b6769..9126d3d6 100644 --- a/gateway/rest/path.go +++ b/gateway/rest/path.go @@ -34,6 +34,10 @@ func submitManifestPath(dseq uint64) string { return fmt.Sprintf("deployment/%d/manifest", dseq) } +func getManifestPath(id mtypes.LeaseID) string { + return fmt.Sprintf("%s/manifest", leasePath(id)) +} + func leaseStatusPath(id mtypes.LeaseID) string { return fmt.Sprintf("%s/status", leasePath(id)) } diff --git a/gateway/rest/router.go b/gateway/rest/router.go index 9ffb606a..4e91c291 100644 --- a/gateway/rest/router.go +++ b/gateway/rest/router.go @@ -155,7 +155,7 @@ func newRouter(log log.Logger, addr sdk.Address, pclient provider.Client, ctxCon ) // GET /lease//manifest - drouter.HandleFunc("/manifest", + lrouter.HandleFunc("/manifest", getManifestHandler(log, pclient.Cluster())). Methods(http.MethodGet)