Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions cmd/rofl/machine/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,20 @@ import (
)

var logsCmd = &cobra.Command{
Use: "logs [<machine-name>]",
Use: "logs [<machine-name> | <provider-address>:<machine-id>]",
Short: "Show logs from the given machine",
Args: cobra.MaximumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
_, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
_, _, machineID, _, _, providerAddr, npa := resolveMachineManifestNpa(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})

machine, _, machineID := resolveMachine(args, deployment)

// Establish connection with the target network.
ctx := context.Background()
conn, err := connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)

// Resolve provider address.
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
if err != nil {
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
}

providerDsc, err := conn.Runtime(npa.ParaTime).ROFLMarket.Provider(ctx, client.RoundLatest, *providerAddr)
cobra.CheckErr(err)

Expand All @@ -54,7 +46,7 @@ var logsCmd = &cobra.Command{
}
var schedulerRAK ed25519.PublicKey
if err := schedulerRAK.UnmarshalText([]byte(schedulerRAKRaw)); err != nil {
cobra.CheckErr(fmt.Sprintf("Malformed scheduler RAK metadata: %s", err))
cobra.CheckErr(fmt.Errorf("malformed scheduler RAK metadata: %w", err))
}
pk := types.PublicKey{PublicKey: schedulerRAK}

Expand Down
139 changes: 84 additions & 55 deletions cmd/rofl/machine/mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"

Expand All @@ -20,13 +21,14 @@ import (
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>]",
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) {
Expand All @@ -42,7 +44,7 @@ var (
}

stopCmd = &cobra.Command{
Use: "stop [<machine-name>]",
Use: "stop [<machine-name> | <provider-address>:<machine-id>]",
Short: "Stop a machine",
Aliases: []string{"terminate"},
Args: cobra.MaximumNArgs(1),
Expand All @@ -59,30 +61,21 @@ var (
}

removeCmd = &cobra.Command{
Use: "remove [<machine-name>]",
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) {
txCfg := common.GetTransactionConfig()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same could be done in change-admin.


manifest, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
machine, machineName, machineID, manifest, _, providerAddr, npa := resolveMachineManifestNpa(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})

machine, machineName, machineID := resolveMachine(args, deployment)

// Resolve provider address.
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
if err != nil {
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
}

// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var err error
var conn connection.Connection
if !txCfg.Offline {
if !common.GetTransactionConfig().Offline {
conn, err = connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)
}
Expand Down Expand Up @@ -111,20 +104,21 @@ var (
// Update manifest to clear the machine ID as it has been cancelled.
machine.ID = ""

if err = manifest.Save(); err != nil {
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
if manifest != nil {
Copy link
Contributor

@peternose peternose Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This complicates everything. Since helper function can return nil manifest, I would expect it could also return nil for other values. So I would expect these kind of checks for all values. Therefore, I don't allow returning nil values, and if I do, the function returns also bool to indicate whether the value is nil.

if err = manifest.Save(); err != nil {
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))
}
}
},
}

changeAdminCmd = &cobra.Command{
Use: "change-admin [<machine-name>] <new-admin>",
Use: "change-admin [<machine-name> | <provider-address>:<machine-id>] <new-admin>",
Short: "Change the machine administrator",
Args: cobra.RangeArgs(1, 2),
Run: func(_ *cobra.Command, args []string) {
txCfg := common.GetTransactionConfig()

_, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
machine, machineName, machineID, _, _, providerAddr, npa := resolveMachineManifestNpa(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})
Expand All @@ -134,25 +128,15 @@ var (
case 1:
// Just admin address.
newAdminAddress = args[0]
args = nil
case 2:
// Machine and admin address.
newAdminAddress = args[1]
args = args[:1]
}

machine, machineName, machineID := resolveMachine(args, deployment)

// Resolve provider address.
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
if err != nil {
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
}

// Resolve new admin address.
newAdminAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, newAdminAddress)
if err != nil {
cobra.CheckErr(fmt.Sprintf("Invalid admin address: %s", err))
cobra.CheckErr(fmt.Errorf("invalid admin address: %w", err))
}

// When not in offline mode, connect to the given network endpoint.
Expand Down Expand Up @@ -196,13 +180,13 @@ var (
}

topUpCmd = &cobra.Command{
Use: "top-up [<machine-name>]",
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()

_, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
machine, machineName, machineID, _, _, providerAddr, npa := resolveMachineManifestNpa(args, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})
Expand All @@ -213,14 +197,6 @@ var (
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
}

machine, machineName, machineID := resolveMachine(args, deployment)

// Resolve provider address.
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
if err != nil {
cobra.CheckErr(fmt.Sprintf("invalid provider address: %s", err))
}

// Parse machine payment term.
if roflCommon.Term == "" {
cobra.CheckErr("no term period specified. Use --term to specify it")
Expand All @@ -231,6 +207,7 @@ var (
}

// When not in offline mode, connect to the given network endpoint.
var err error
var conn connection.Connection
var offer *roflmarket.Offer
if !txCfg.Offline {
Expand All @@ -240,7 +217,7 @@ var (
// Fetch chosen offer, so we can calculate price.
offers, err := conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, *providerAddr)
if err != nil {
cobra.CheckErr(fmt.Errorf("failed to query provider: %s", err))
cobra.CheckErr(fmt.Errorf("failed to query provider: %w", err))
}

for _, of := range offers {
Expand All @@ -249,6 +226,18 @@ var (
break
}
}
if offer == nil {
machineDsc, err := conn.Runtime(npa.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *providerAddr, 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", machine.Offer))
}
Expand Down Expand Up @@ -295,7 +284,56 @@ var (
}
)

func resolveMachine(args []string, deployment *buildRofl.Deployment) (*buildRofl.Machine, string, roflmarket.InstanceID) {
func resolveMachineManifestNpa(args []string, manifestOpts *roflCommon.ManifestOptions) (machineCfg *buildRofl.Machine, machineName string, machineID roflmarket.InstanceID, manifest *buildRofl.Manifest, extraCfg *roflCmdBuild.AppExtraConfig, providerAddr *types.Address, npa *common.NPASelection) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few remarks:

  • Returned data should be collected in a struct, as Go methods should return at most 2 values.
  • The returned values should also not be named, unless this is needed because of defer statement or to explain what the function returns.
  • The code is very complicated, and should be changed in such a way that you a) get npa b) resolve machine from args or manifest c) resolve provider address.

var err error
machineCfg, machineName, machineID = resolveMachineFromArgs(args)
if machineCfg != nil {
cfg := cliConfig.Global()
npa = common.GetNPASelection(cfg)
} else {
var deployment *buildRofl.Deployment
manifest, deployment, npa = roflCommon.LoadManifestAndSetNPA(manifestOpts)

machineCfg, machineName, machineID = resolveMachineFromManifest(args, deployment)

var appID rofl.AppID
if err = appID.UnmarshalText([]byte(deployment.AppID)); err != nil {
cobra.CheckErr(fmt.Errorf("malformed app id: %w", err))
}

if extraCfg, err = roflCmdBuild.ValidateApp(manifest, roflCmdBuild.ValidationOpts{
Offline: true,
}); err != nil {
cobra.CheckErr(fmt.Errorf("failed to validate app: %w", err))
}
}

if providerAddr, _, err = common.ResolveLocalAccountOrAddress(npa.Network, machineCfg.Provider); err != nil {
cobra.CheckErr(fmt.Errorf("invalid provider address: %w", err))
}

return machineCfg, machineName, machineID, manifest, extraCfg, providerAddr, npa
}

func resolveMachineFromArgs(args []string) (*buildRofl.Machine, string, roflmarket.InstanceID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If function optionally returns values, it should return two values, the second one being bool, e.g. func resolve() (*mystruct, bool).

Also, happy path should not be indented.

if len(args) == 0
...
if len(parts) != 2
...

if len(args) > 0 {
parts := strings.Split(args[0], ":")
if len(parts) == 2 {
m := &buildRofl.Machine{
Provider: parts[0],
ID: parts[1],
}
var machineID roflmarket.InstanceID
if err := machineID.UnmarshalText([]byte(m.ID)); err != nil {
cobra.CheckErr(fmt.Errorf("malformed machine ID: %w", err))
}
return m, args[0], machineID
}
}
return nil, "", roflmarket.InstanceID{}
}

func resolveMachineFromManifest(args []string, deployment *buildRofl.Deployment) (*buildRofl.Machine, string, roflmarket.InstanceID) {
machineName := buildRofl.DefaultMachineName
if len(args) > 0 {
machineName = args[0]
Expand Down Expand Up @@ -333,25 +371,16 @@ func resolveMachine(args []string, deployment *buildRofl.Deployment) (*buildRofl
}

func queueCommand(cliArgs []string, method string, args any, msgAfter string) {
txCfg := common.GetTransactionConfig()

_, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
machine, machineName, machineID, _, _, providerAddr, npa := resolveMachineManifestNpa(cliArgs, &roflCommon.ManifestOptions{
NeedAppID: true,
NeedAdmin: false,
})

machine, machineName, machineID := resolveMachine(cliArgs, deployment)

// Resolve provider address.
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
if err != nil {
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
}

// When not in offline mode, connect to the given network endpoint.
ctx := context.Background()
var err error
var conn connection.Connection
if !txCfg.Offline {
if !common.GetTransactionConfig().Offline {
conn, err = connection.Connect(ctx, npa.Network)
cobra.CheckErr(err)
}
Expand Down
Loading
Loading