Skip to content

Commit 471fd75

Browse files
committed
cmd/rofl/deploy: Detect term based on the offer
1 parent ccc43db commit 471fd75

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

cmd/rofl/deploy.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ var (
106106

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

109-
// Parse machine payment term.
110-
term := roflCommon.ParseMachineTerm(deployTerm)
111-
if deployTermCount < 1 {
112-
cobra.CheckErr("Number of terms must be at least 1.")
113-
}
114-
115109
// Push ORC to OCI repository.
116110
if deployment.OCIRepository == "" {
117111
// TODO: Support default OCI repository.
@@ -173,6 +167,11 @@ var (
173167

174168
fmt.Printf("Taking offer: %s [%s]\n", machine.Offer, offer.ID)
175169

170+
term := detectTerm(offer)
171+
if deployTermCount < 1 {
172+
cobra.CheckErr("Number of terms must be at least 1.")
173+
}
174+
176175
// Prepare transaction.
177176
tx := roflmarket.NewInstanceCreateTx(nil, &roflmarket.InstanceCreate{
178177
Provider: *providerAddr,
@@ -249,6 +248,34 @@ var (
249248
}
250249
)
251250

251+
// detectTerm returns the preferred (longest) period of the given offer.
252+
func detectTerm(offer *roflmarket.Offer) (term roflmarket.Term) {
253+
if offer == nil {
254+
cobra.CheckErr(fmt.Errorf("no offers exist to determine payment term"))
255+
}
256+
if offer.Payment.Native == nil {
257+
cobra.CheckErr(fmt.Errorf("no payment terms available for offer '%s'", offer.ID))
258+
}
259+
260+
if deployTerm != "" {
261+
// Custom deploy term.
262+
term = roflCommon.ParseMachineTerm(deployTerm)
263+
if _, ok := offer.Payment.Native.Terms[term]; !ok {
264+
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", deployTerm, offer.ID))
265+
}
266+
return
267+
}
268+
269+
// Take the longest payment period.
270+
// TODO: Sort by actual periods (e.g. seconds) instead of internal roflmarket.Term index.
271+
for t := range offer.Payment.Native.Terms {
272+
if t > term {
273+
term = t
274+
}
275+
}
276+
return
277+
}
278+
252279
func showProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) {
253280
offers, err := conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, provider)
254281
if err != nil {
@@ -299,7 +326,7 @@ func init() {
299326
providerFlags.StringVar(&deployProvider, "provider", "oasis1qp2ens0hsp7gh23wajxa4hpetkdek3swyyulyrmz", "set the provider address")
300327
providerFlags.StringVar(&deployOffer, "offer", "", "set the provider's offer identifier")
301328
providerFlags.StringVar(&deployMachine, "machine", buildRofl.DefaultMachineName, "machine to deploy into")
302-
providerFlags.StringVar(&deployTerm, "term", roflCommon.TermMonth, "term to pay for in advance")
329+
providerFlags.StringVar(&deployTerm, "term", "", "term to pay for in advance")
303330
providerFlags.Uint64Var(&deployTermCount, "term-count", 1, "number of terms to pay for in advance")
304331
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
305332

0 commit comments

Comments
 (0)