1
1
package rofl
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"errors"
6
7
"fmt"
7
8
"maps"
8
9
"os"
10
+ "sort"
9
11
10
12
"github.com/spf13/cobra"
11
13
flag "github.com/spf13/pflag"
@@ -27,12 +29,13 @@ import (
27
29
)
28
30
29
31
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
36
39
37
40
deployCmd = & cobra.Command {
38
41
Use : "deploy" ,
@@ -106,6 +109,12 @@ var (
106
109
107
110
fmt .Printf ("Using provider: %s (%s)\n " , machine .Provider , providerAddr )
108
111
112
+ if deployShowOffers {
113
+ // Display all offers supported by the provider.
114
+ showProviderOffers (ctx , npa , conn , * providerAddr )
115
+ return
116
+ }
117
+
109
118
// Push ORC to OCI repository.
110
119
if deployment .OCIRepository == "" {
111
120
// TODO: Support default OCI repository.
@@ -137,24 +146,17 @@ var (
137
146
// When machine is not set, we need to obtain one.
138
147
fmt .Printf ("No pre-existing machine configured, creating a new one...\n " )
139
148
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
- }
145
149
if deployOffer != "" {
146
150
machine .Offer = deployOffer
147
151
}
148
152
149
153
// 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 )
155
156
var offer * roflmarket.Offer
156
157
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 ]
158
160
offer = of
159
161
break
160
162
}
@@ -277,11 +279,22 @@ func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
277
279
return
278
280
}
279
281
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 )
282
284
if err != nil {
285
+ err = fmt .Errorf ("failed to query provider: %s" , err )
283
286
return
284
287
}
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 )
285
298
286
299
fmt .Println ()
287
300
fmt .Printf ("Offers available from the selected provider:\n " )
@@ -330,6 +343,7 @@ func init() {
330
343
providerFlags .StringVar (& deployTerm , "term" , "" , "term to pay for in advance" )
331
344
providerFlags .Uint64Var (& deployTermCount , "term-count" , 1 , "number of terms to pay for in advance" )
332
345
providerFlags .BoolVar (& deployForce , "force" , false , "force deployment" )
346
+ providerFlags .BoolVar (& deployShowOffers , "show-offers" , false , "show all provider offers and quit" )
333
347
334
348
deployCmd .Flags ().AddFlagSet (common .SelectorFlags )
335
349
deployCmd .Flags ().AddFlagSet (common .RuntimeTxFlags )
0 commit comments