-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmgmt.go
More file actions
473 lines (407 loc) · 15.1 KB
/
mgmt.go
File metadata and controls
473 lines (407 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package machine
import (
"context"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/quantity"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/roflmarket"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
buildRofl "github.com/oasisprotocol/cli/build/rofl"
buildRoflProvider "github.com/oasisprotocol/cli/build/rofl/provider"
"github.com/oasisprotocol/cli/build/rofl/scheduler"
"github.com/oasisprotocol/cli/cmd/common"
roflCmdBuild "github.com/oasisprotocol/cli/cmd/rofl/build"
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
cliConfig "github.com/oasisprotocol/cli/config"
)
var (
restartCmd = &cobra.Command{
Use: "restart [<machine-name> | <provider-address>:<machine-id>]",
Short: "Restart a running machine or start a stopped one",
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
queueCommand(
args,
scheduler.MethodRestart,
scheduler.RestartRequest{
WipeStorage: roflCommon.WipeStorage,
},
"Machine restart scheduled.",
)
},
}
stopCmd = &cobra.Command{
Use: "stop [<machine-name> | <provider-address>:<machine-id>]",
Short: "Stop a machine",
Aliases: []string{"terminate"},
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
queueCommand(
args,
scheduler.MethodTerminate,
scheduler.TerminateRequest{
WipeStorage: roflCommon.WipeStorage,
},
"Machine termination scheduled.",
)
},
}
removeCmd = &cobra.Command{
Use: "remove [<machine-name> | <provider-address>:<machine-id>]",
Short: "Cancel rental and remove the machine",
Aliases: []string{"cancel", "rm"},
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
mCfg, err := resolveMachineCfg(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})
cobra.CheckErr(err)
// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var conn connection.Connection
if !common.GetTransactionConfig().Offline {
conn, err = connection.Connect(ctx, mCfg.NPA.Network)
cobra.CheckErr(err)
}
fmt.Printf("Using provider: %s\n", common.PrettyAddress(mCfg.ProviderAddr.String()))
fmt.Printf("Canceling machine: %s [%s]\n", mCfg.MachineName, mCfg.Machine.ID)
common.Warn("WARNING: Canceling a machine will permanently destroy it including any persistent storage!")
roflCommon.PrintRentRefundWarning()
// Prepare transaction.
tx := roflmarket.NewInstanceCancelTx(nil, &roflmarket.InstanceCancel{
Provider: *mCfg.ProviderAddr,
ID: mCfg.MachineID,
})
acc := common.LoadAccount(cliConfig.Global(), mCfg.NPA.AccountName)
sigTx, meta, err := common.SignParaTimeTransaction(ctx, mCfg.NPA, acc, conn, tx, nil)
cobra.CheckErr(err)
if !common.BroadcastOrExportTransaction(ctx, mCfg.NPA, conn, sigTx, meta, nil) {
return
}
fmt.Printf("Machine removed.\n")
// Update manifest to clear the machine ID as it has been cancelled.
mCfg.Machine.ID = ""
if mCfg.Manifest != nil {
if err = mCfg.Manifest.Save(); err != nil {
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
}
}
},
}
changeAdminCmd = &cobra.Command{
Use: "set-admin [<machine-name> | <provider-address>:<machine-id>] <new-admin>",
Short: "Change the machine administrator",
Aliases: []string{"change-admin"},
Args: cobra.RangeArgs(1, 2),
Run: func(_ *cobra.Command, args []string) {
txCfg := common.GetTransactionConfig()
mCfg, err := resolveMachineCfg(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})
cobra.CheckErr(err)
var newAdminAddress string
switch len(args) {
case 1:
// Just admin address.
newAdminAddress = args[0]
case 2:
// Machine and admin address.
newAdminAddress = args[1]
}
// Resolve new admin address.
newAdminAddr, newAdminEthAddr, err := common.ResolveLocalAccountOrAddress(mCfg.NPA.Network, newAdminAddress)
if err != nil {
cobra.CheckErr(fmt.Errorf("invalid admin address: %w", err))
}
// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var conn connection.Connection
if !txCfg.Offline {
conn, err = connection.Connect(ctx, mCfg.NPA.Network)
cobra.CheckErr(err)
}
fmt.Printf("Provider: %s\n", common.PrettyAddress(mCfg.ProviderAddr.String()))
fmt.Printf("Machine: %s [%s]\n", mCfg.MachineName, mCfg.Machine.ID)
// Resolve old admin in online mode.
if !txCfg.Offline {
insDsc, err := conn.Runtime(mCfg.NPA.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *mCfg.ProviderAddr, mCfg.MachineID)
cobra.CheckErr(err)
fmt.Printf("Old admin: %s\n", common.PrettyAddress(insDsc.Admin.String()))
}
newAdminStr := newAdminAddr.String()
if newAdminEthAddr != nil {
newAdminStr = newAdminEthAddr.Hex()
}
fmt.Printf("New admin: %s\n", common.PrettyAddress(newAdminStr))
// Prepare transaction.
tx := roflmarket.NewInstanceChangeAdmin(nil, &roflmarket.InstanceChangeAdmin{
Provider: *mCfg.ProviderAddr,
ID: mCfg.MachineID,
Admin: *newAdminAddr,
})
acc := common.LoadAccount(cliConfig.Global(), mCfg.NPA.AccountName)
sigTx, meta, err := common.SignParaTimeTransaction(ctx, mCfg.NPA, acc, conn, tx, nil)
cobra.CheckErr(err)
if !common.BroadcastOrExportTransaction(ctx, mCfg.NPA, conn, sigTx, meta, nil) {
return
}
fmt.Printf("Machine admin address changed.\n")
},
}
topUpCmd = &cobra.Command{
Use: "top-up [<machine-name> | <provider-address>:<machine-id>]",
Short: "Top-up payment for a machine",
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
txCfg := common.GetTransactionConfig()
mCfg, err := resolveMachineCfg(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})
cobra.CheckErr(err)
ctx := context.Background()
// This is required for the price pretty printer to work...
if mCfg.NPA.ParaTime != nil {
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, mCfg.NPA.ParaTime)
}
// Parse machine payment term.
if roflCommon.Term == "" {
cobra.CheckErr("no term period specified. Use --term to specify it")
}
term := roflCommon.ParseMachineTerm(roflCommon.Term)
if roflCommon.TermCount < 1 {
cobra.CheckErr("number of terms must be at least 1.")
}
// When not in offline mode, connect to the given network endpoint.
var conn connection.Connection
var offer *roflmarket.Offer
if !txCfg.Offline {
conn, err = connection.Connect(ctx, mCfg.NPA.Network)
cobra.CheckErr(err)
// Fetch chosen offer, so we can calculate price.
offers, err := conn.Runtime(mCfg.NPA.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, *mCfg.ProviderAddr)
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to query provider: %w", err))
}
for _, of := range offers {
if of.Metadata[buildRoflProvider.SchedulerMetadataOfferKey] == mCfg.Machine.Offer {
offer = of
break
}
}
if offer == nil {
machineDsc, err := conn.Runtime(mCfg.NPA.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *mCfg.ProviderAddr, mCfg.MachineID)
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to query machine: %w", err))
}
for _, of := range offers {
if of.ID == machineDsc.Offer {
offer = of
break
}
}
}
if offer == nil {
cobra.CheckErr(fmt.Errorf("unable to find existing machine offer (%s) among market offers", mCfg.Machine.Offer))
}
}
fmt.Printf("Using provider: %s\n", common.PrettyAddress(mCfg.ProviderAddr.String()))
fmt.Printf("Top-up machine: %s [%s]\n", mCfg.MachineName, mCfg.Machine.ID)
if txCfg.Offline {
fmt.Printf("Top-up term: %d x %s\n", roflCommon.TermCount, roflCommon.Term)
} else {
// Calculate total price and print term info.
qTermCount := quantity.NewFromUint64(roflCommon.TermCount)
totalPrice, ok := offer.Payment.Native.Terms[term]
if !ok {
cobra.CheckErr("previously selected term not found in offer terms")
}
cobra.CheckErr(totalPrice.Mul(qTermCount))
tp := types.NewBaseUnits(totalPrice, offer.Payment.Native.Denomination)
fmt.Printf("Top-up term: %d x %s (", roflCommon.TermCount, roflCommon.Term)
tp.PrettyPrint(ctx, "", os.Stdout)
fmt.Println(" total)")
}
roflCommon.PrintRentRefundWarning()
// Prepare transaction.
tx := roflmarket.NewInstanceTopUpTx(nil, &roflmarket.InstanceTopUp{
Provider: *mCfg.ProviderAddr,
ID: mCfg.MachineID,
Term: term,
TermCount: roflCommon.TermCount,
})
acc := common.LoadAccount(cliConfig.Global(), mCfg.NPA.AccountName)
sigTx, meta, err := common.SignParaTimeTransaction(ctx, mCfg.NPA, acc, conn, tx, nil)
cobra.CheckErr(err)
if !common.BroadcastOrExportTransaction(ctx, mCfg.NPA, conn, sigTx, meta, nil) {
return
}
fmt.Printf("Machine topped up.\n")
},
}
)
// machineCfg contains all resolved machine configuration and related metadata.
type machineCfg struct {
Machine *buildRofl.Machine
MachineName string
MachineID roflmarket.InstanceID
Manifest *buildRofl.Manifest
ExtraCfg *roflCmdBuild.AppExtraConfig
ProviderAddr *types.Address
NPA *common.NPASelection
}
// resolveMachineCfg resolves machine configuration from command-line arguments or manifest file.
// If args contains a provider:machine-id pair, it constructs the configuration from that.
// Otherwise, it loads the manifest file and resolves the machine from the deployment section.
// The function also resolves the provider address and validates the configuration.
func resolveMachineCfg(args []string, manifestOpts *roflCommon.ManifestOptions) (*machineCfg, error) {
mCfg, err := resolveMachineCfgFromArgs(args)
if err != nil {
return nil, err
}
if mCfg != nil {
mCfg.NPA = common.GetNPASelection(cliConfig.Global())
} else {
var deployment *buildRofl.Deployment
mCfg = &machineCfg{}
mCfg.Manifest, deployment, mCfg.NPA = roflCommon.LoadManifestAndSetNPA(manifestOpts)
mCfg.Machine, mCfg.MachineName, mCfg.MachineID = resolveMachineFromManifest(args, deployment)
var appID rofl.AppID
if err = appID.UnmarshalText([]byte(deployment.AppID)); err != nil {
return nil, fmt.Errorf("malformed app id: %w", err)
}
if mCfg.ExtraCfg, err = roflCmdBuild.ValidateApp(mCfg.Manifest, roflCmdBuild.ValidationOpts{
Offline: true,
}); err != nil {
return nil, fmt.Errorf("failed to validate app: %w", err)
}
}
if mCfg.ProviderAddr, _, err = common.ResolveLocalAccountOrAddress(mCfg.NPA.Network, mCfg.Machine.Provider); err != nil {
return nil, fmt.Errorf("invalid provider address: %w", err)
}
return mCfg, nil
}
func resolveMachineCfgFromArgs(args []string) (*machineCfg, error) {
mCfg := &machineCfg{}
if len(args) == 0 {
return nil, nil
}
parts := strings.Split(args[0], ":")
if len(parts) != 2 {
return nil, nil
}
mCfg.MachineName = args[0]
mCfg.Machine = &buildRofl.Machine{
Provider: parts[0],
ID: parts[1],
}
if err := mCfg.MachineID.UnmarshalText([]byte(mCfg.Machine.ID)); err != nil {
return nil, fmt.Errorf("malformed machine ID: %w", err)
}
return mCfg, nil
}
func resolveMachineFromManifest(args []string, deployment *buildRofl.Deployment) (*buildRofl.Machine, string, roflmarket.InstanceID) {
machineName := buildRofl.DefaultMachineName
if len(args) > 0 {
machineName = args[0]
}
var appID rofl.AppID
if err := appID.UnmarshalText([]byte(deployment.AppID)); err != nil {
cobra.CheckErr(fmt.Errorf("malformed ROFL app ID: %w", err))
}
var machine *buildRofl.Machine
for name, mach := range deployment.Machines {
if name == machineName {
machine = mach
break
}
}
if machine == nil {
fmt.Println("The following machines are configured in the app manifest:")
for name := range deployment.Machines {
fmt.Printf(" - %s\n", name)
}
cobra.CheckErr(fmt.Errorf("machine '%s' not found in manifest", machineName))
return nil, "", roflmarket.InstanceID{}
}
if machine.ID == "" {
cobra.CheckErr(fmt.Errorf("machine '%s' not yet deployed, use `oasis rofl deploy`", machineName))
}
var machineID roflmarket.InstanceID
if err := machineID.UnmarshalText([]byte(machine.ID)); err != nil {
cobra.CheckErr(fmt.Errorf("malformed machine ID: %w", err))
}
return machine, machineName, machineID
}
func queueCommand(cliArgs []string, method string, args any, msgAfter string) {
mCfg, err := resolveMachineCfg(cliArgs, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})
cobra.CheckErr(err)
// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var conn connection.Connection
if !common.GetTransactionConfig().Offline {
conn, err = connection.Connect(ctx, mCfg.NPA.Network)
cobra.CheckErr(err)
}
fmt.Printf("Using provider: %s\n", common.PrettyAddress(mCfg.ProviderAddr.String()))
fmt.Printf("Machine: %s [%s]\n", mCfg.MachineName, mCfg.Machine.ID)
fmt.Printf("Command: %s\n", method)
fmt.Printf("Args:\n")
fmt.Println(common.PrettyPrint(mCfg.NPA, " ", args))
// Prepare transaction.
tx := roflmarket.NewInstanceExecuteCmdsTx(nil, &roflmarket.InstanceExecuteCmds{
Provider: *mCfg.ProviderAddr,
ID: mCfg.MachineID,
Cmds: [][]byte{cbor.Marshal(scheduler.Command{
Method: method,
Args: cbor.Marshal(args),
})},
})
acc := common.LoadAccount(cliConfig.Global(), mCfg.NPA.AccountName)
sigTx, meta, err := common.SignParaTimeTransaction(ctx, mCfg.NPA, acc, conn, tx, nil)
cobra.CheckErr(err)
if !common.BroadcastOrExportTransaction(ctx, mCfg.NPA, conn, sigTx, meta, nil) {
return
}
fmt.Println(msgAfter)
fmt.Printf("Use `oasis rofl machine show` to see status.\n")
}
func showCommandArgs[V any](npa *common.NPASelection, raw []byte, args V) {
if err := cbor.Unmarshal(raw, &args); err != nil {
fmt.Printf(" %X\n", raw)
} else {
fmt.Println(common.PrettyPrint(npa, " ", args))
}
}
func init() {
common.AddSelectorFlags(restartCmd)
restartCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
restartCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
restartCmd.Flags().AddFlagSet(roflCommon.WipeFlags)
common.AddSelectorFlags(stopCmd)
stopCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
stopCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
stopCmd.Flags().AddFlagSet(roflCommon.WipeFlags)
common.AddSelectorFlags(removeCmd)
removeCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
removeCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
common.AddAccountFlag(changeAdminCmd)
changeAdminCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
changeAdminCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
common.AddSelectorFlags(topUpCmd)
topUpCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
topUpCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
topUpCmd.Flags().AddFlagSet(roflCommon.TermFlags)
}