Skip to content

Commit bfc4f22

Browse files
committed
cmd/rofl/deploy: Take the first offer automatically
1 parent 12929ad commit bfc4f22

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

cmd/rofl/deploy.go

+33-19
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
}
@@ -277,11 +279,22 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
277279
return
278280
}
279281

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

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

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

0 commit comments

Comments
 (0)