Skip to content

Commit dcee297

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

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

cmd/rofl/deploy.go

Lines changed: 29 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,29 @@ 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 || offer.Payment.Native == nil {
254+
cobra.CheckErr(fmt.Errorf("unable do determine payment term for offer '%s'", offer.ID))
255+
}
256+
257+
if deployTerm != "" {
258+
// Custom deploy term.
259+
term = roflCommon.ParseMachineTerm(deployTerm)
260+
if _, ok := offer.Payment.Native.Terms[term]; !ok {
261+
cobra.CheckErr(fmt.Errorf("term '%s' is not available for offer '%s'", deployTerm, offer.ID))
262+
}
263+
return
264+
}
265+
266+
for t := range offer.Payment.Native.Terms {
267+
if t > term {
268+
term = t
269+
}
270+
}
271+
return
272+
}
273+
252274
func showProviderOffers(ctx context.Context, npa *common.NPASelection, conn connection.Connection, provider types.Address) {
253275
offers, err := conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, provider)
254276
if err != nil {
@@ -299,7 +321,7 @@ func init() {
299321
providerFlags.StringVar(&deployProvider, "provider", "oasis1qp2ens0hsp7gh23wajxa4hpetkdek3swyyulyrmz", "set the provider address")
300322
providerFlags.StringVar(&deployOffer, "offer", "", "set the provider's offer identifier")
301323
providerFlags.StringVar(&deployMachine, "machine", buildRofl.DefaultMachineName, "machine to deploy into")
302-
providerFlags.StringVar(&deployTerm, "term", roflCommon.TermMonth, "term to pay for in advance")
324+
providerFlags.StringVar(&deployTerm, "term", "", "term to pay for in advance")
303325
providerFlags.Uint64Var(&deployTermCount, "term-count", 1, "number of terms to pay for in advance")
304326
providerFlags.BoolVar(&deployForce, "force", false, "force deployment")
305327

0 commit comments

Comments
 (0)