Skip to content

Commit c53020f

Browse files
committed
feat(cmd/rofl): Add machine change-admin subcommand
1 parent fa0535f commit c53020f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

cmd/rofl/machine/machine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func init() {
1515
Cmd.AddCommand(restartCmd)
1616
Cmd.AddCommand(stopCmd)
1717
Cmd.AddCommand(removeCmd)
18+
Cmd.AddCommand(changeAdminCmd)
1819
Cmd.AddCommand(topUpCmd)
1920
Cmd.AddCommand(logsCmd)
2021
}

cmd/rofl/machine/mgmt.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/cobra"
88

99
"github.com/oasisprotocol/oasis-core/go/common/cbor"
10+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
1011
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
1112
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
1213
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/roflmarket"
@@ -110,6 +111,84 @@ var (
110111
},
111112
}
112113

114+
changeAdminCmd = &cobra.Command{
115+
Use: "change-admin [<machine-name>] <new-admin>",
116+
Short: "Change the machine administrator",
117+
Args: cobra.RangeArgs(1, 2),
118+
Run: func(_ *cobra.Command, args []string) {
119+
txCfg := common.GetTransactionConfig()
120+
121+
_, deployment, npa := roflCommon.LoadManifestAndSetNPA(&roflCommon.ManifestOptions{
122+
NeedAppID: true,
123+
NeedAdmin: false,
124+
})
125+
126+
var newAdminAddress string
127+
switch len(args) {
128+
case 1:
129+
// Just admin address.
130+
newAdminAddress = args[0]
131+
args = nil
132+
case 2:
133+
// Machine and admin address.
134+
newAdminAddress = args[1]
135+
args = args[:1]
136+
}
137+
138+
machine, machineName, machineID := resolveMachine(args, deployment)
139+
140+
// Resolve provider address.
141+
providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider)
142+
if err != nil {
143+
cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err))
144+
}
145+
146+
// Resolve new admin address.
147+
newAdminAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, newAdminAddress)
148+
if err != nil {
149+
cobra.CheckErr(fmt.Sprintf("Invalid admin address: %s", err))
150+
}
151+
152+
// When not in offline mode, connect to the given network endpoint.
153+
ctx := context.Background()
154+
var conn connection.Connection
155+
if !txCfg.Offline {
156+
conn, err = connection.Connect(ctx, npa.Network)
157+
cobra.CheckErr(err)
158+
}
159+
160+
fmt.Printf("Provider: %s (%s)\n", machine.Provider, providerAddr)
161+
fmt.Printf("Machine: %s [%s]\n", machineName, machine.ID)
162+
163+
// Resolve old admin in online mode.
164+
if !txCfg.Offline {
165+
insDsc, err := conn.Runtime(npa.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *providerAddr, machineID)
166+
cobra.CheckErr(err)
167+
168+
fmt.Printf("Old admin: %s\n", insDsc.Admin)
169+
}
170+
171+
fmt.Printf("New admin: %s\n", newAdminAddr)
172+
173+
// Prepare transaction.
174+
tx := roflmarket.NewInstanceChangeAdmin(nil, &roflmarket.InstanceChangeAdmin{
175+
Provider: *providerAddr,
176+
ID: machineID,
177+
Admin: *newAdminAddr,
178+
})
179+
180+
acc := common.LoadAccount(cliConfig.Global(), npa.AccountName)
181+
sigTx, meta, err := common.SignParaTimeTransaction(ctx, npa, acc, conn, tx, nil)
182+
cobra.CheckErr(err)
183+
184+
if !common.BroadcastOrExportTransaction(ctx, npa, conn, sigTx, meta, nil) {
185+
return
186+
}
187+
188+
fmt.Printf("Machine admin address changed.\n")
189+
},
190+
}
191+
113192
topUpCmd = &cobra.Command{
114193
Use: "top-up [<machine-name>]",
115194
Short: "Top-up payment for a machine",
@@ -284,6 +363,10 @@ func init() {
284363
removeCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
285364
removeCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
286365

366+
changeAdminCmd.Flags().AddFlagSet(common.AccountFlag)
367+
changeAdminCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
368+
changeAdminCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)
369+
287370
topUpCmd.Flags().AddFlagSet(common.SelectorFlags)
288371
topUpCmd.Flags().AddFlagSet(common.RuntimeTxFlags)
289372
topUpCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags)

0 commit comments

Comments
 (0)