Skip to content

Commit 248a55a

Browse files
committed
cmd/rofl/deploy: Take the first offer automatically
1 parent 471fd75 commit 248a55a

1 file changed

Lines changed: 33 additions & 19 deletions

File tree

cmd/rofl/deploy.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package rofl
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
78
"maps"
89
"os"
10+
"sort"
911

1012
"github.com/spf13/cobra"
1113
flag "github.com/spf13/pflag"
@@ -27,12 +29,13 @@ import (
2729
)
2830

2931
var (
30-
deployProvider string
31-
deployOffer string
32-
deployMachine string
33-
deployTerm string
34-
deployTermCount uint64
35-
deployForce bool
32+
deployProvider string
33+
deployOffer string
34+
deployMachine string
35+
deployTerm string
36+
deployTermCount uint64
37+
deployForce bool
38+
deployShowOffers bool
3639

3740
deployCmd = &cobra.Command{
3841
Use: "deploy",
@@ -106,6 +109,12 @@ var (
106109

107110
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
108111

112+
if deployShowOffers {
113+
// Display all offers supported by the provider.
114+
showProviderOffers(ctx, npa, conn, *providerAddr)
115+
return
116+
}
117+
109118
// Push ORC to OCI repository.
110119
if deployment.OCIRepository == "" {
111120
// TODO: Support default OCI repository.
@@ -137,24 +146,17 @@ var (
137146
// When machine is not set, we need to obtain one.
138147
fmt.Printf("No pre-existing machine configured, creating a new one...\n")
139148

140-
if machine.Offer == "" && deployOffer == "" {
141-
// Display all offers supported by the provider.
142-
showProviderOffers(ctx, npa, conn, *providerAddr)
143-
cobra.CheckErr(fmt.Sprintf("Offer not configured for deployment '%s' machine '%s'. Please specify --offer.", deploymentName, deployMachine))
144-
}
145149
if deployOffer != "" {
146150
machine.Offer = deployOffer
147151
}
148152

149153
// Resolve offer.
150-
var offers []*roflmarket.Offer
151-
offers, err = conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, *providerAddr)
152-
if err != nil {
153-
cobra.CheckErr(fmt.Sprintf("Failed to query provider: %s", err))
154-
}
154+
offers, err := fetchProviderOffers(ctx, npa, conn, *providerAddr)
155+
cobra.CheckErr(err)
155156
var offer *roflmarket.Offer
156157
for _, of := range offers {
157-
if of.Metadata[provider.SchedulerMetadataOfferKey] == machine.Offer {
158+
if of.Metadata[provider.SchedulerMetadataOfferKey] == machine.Offer || machine.Offer == "" {
159+
machine.Offer = of.Metadata[provider.SchedulerMetadataOfferKey]
158160
offer = of
159161
break
160162
}
@@ -276,11 +278,22 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
276278
return
277279
}
278280

279-
func showProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) {
280-
offers, err := conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, provider)
281+
func fetchProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) (offers []*roflmarket.Offer, err error) {
282+
offers, err = conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, provider)
281283
if err != nil {
284+
err = fmt.Errorf("failed to query provider: %s", err)
282285
return
283286
}
287+
// Order offers, newer first.
288+
sort.Slice(offers, func(i, j int) bool {
289+
return bytes.Compare(offers[i].ID[:], offers[j].ID[:]) > 0
290+
})
291+
return
292+
}
293+
294+
func showProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) {
295+
offers, err := fetchProviderOffers(ctx, npa, conn, provider)
296+
cobra.CheckErr(err)
284297

285298
fmt.Println()
286299
fmt.Printf("Offers available from the selected provider:\n")
@@ -329,6 +342,7 @@ func init() {
329342
providerFlags.StringVar(&deployTerm, "term", "", "term to pay for in advance")
330343
providerFlags.Uint64Var(&deployTermCount, "term-count", 1, "number of terms to pay for in advance")
331344
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
345+
providerFlags.BoolVar(&deployShowOffers, "show-offers", false, "show all provider offers and quit")
332346

333347
deployCmd.Flags().AddFlagSet(common.SelectorFlags)
334348
deployCmd.Flags().AddFlagSet(common.RuntimeTxFlags)

0 commit comments

Comments
 (0)