Skip to content

Commit 12929ad

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

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

cmd/rofl/deploy.go

Lines changed: 35 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,35 @@ 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+
return // Linter complains otherwise.
256+
}
257+
if offer.Payment.Native == nil {
258+
cobra.CheckErr(fmt.Errorf("no payment terms available for offer '%s'", offer.ID))
259+
}
260+
261+
if deployTerm != "" {
262+
// Custom deploy term.
263+
term = roflCommon.ParseMachineTerm(deployTerm)
264+
if _, ok := offer.Payment.Native.Terms[term]; !ok {
265+
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", deployTerm, offer.ID))
266+
}
267+
return
268+
}
269+
270+
// Take the longest payment period.
271+
// TODO: Sort by actual periods (e.g. seconds) instead of internal roflmarket.Term index.
272+
for t := range offer.Payment.Native.Terms {
273+
if t > term {
274+
term = t
275+
}
276+
}
277+
return
278+
}
279+
252280
func showProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) {
253281
offers, err := conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, provider)
254282
if err != nil {
@@ -299,7 +327,7 @@ func init() {
299327
providerFlags.StringVar(&deployProvider, "provider", "oasis1qp2ens0hsp7gh23wajxa4hpetkdek3swyyulyrmz", "set the provider address")
300328
providerFlags.StringVar(&deployOffer, "offer", "", "set the provider's offer identifier")
301329
providerFlags.StringVar(&deployMachine, "machine", buildRofl.DefaultMachineName, "machine to deploy into")
302-
providerFlags.StringVar(&deployTerm, "term", roflCommon.TermMonth, "term to pay for in advance")
330+
providerFlags.StringVar(&deployTerm, "term", "", "term to pay for in advance")
303331
providerFlags.Uint64Var(&deployTermCount, "term-count", 1, "number of terms to pay for in advance")
304332
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
305333

0 commit comments

Comments
 (0)