|
7 | 7 | "github.com/spf13/cobra" |
8 | 8 |
|
9 | 9 | "github.com/oasisprotocol/oasis-core/go/common/cbor" |
| 10 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/client" |
10 | 11 | "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
11 | 12 | "github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl" |
12 | 13 | "github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/roflmarket" |
@@ -110,6 +111,84 @@ var ( |
110 | 111 | }, |
111 | 112 | } |
112 | 113 |
|
| 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 | + |
113 | 192 | topUpCmd = &cobra.Command{ |
114 | 193 | Use: "top-up [<machine-name>]", |
115 | 194 | Short: "Top-up payment for a machine", |
@@ -284,6 +363,10 @@ func init() { |
284 | 363 | removeCmd.Flags().AddFlagSet(common.RuntimeTxFlags) |
285 | 364 | removeCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags) |
286 | 365 |
|
| 366 | + changeAdminCmd.Flags().AddFlagSet(common.AccountFlag) |
| 367 | + changeAdminCmd.Flags().AddFlagSet(common.RuntimeTxFlags) |
| 368 | + changeAdminCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags) |
| 369 | + |
287 | 370 | topUpCmd.Flags().AddFlagSet(common.SelectorFlags) |
288 | 371 | topUpCmd.Flags().AddFlagSet(common.RuntimeTxFlags) |
289 | 372 | topUpCmd.Flags().AddFlagSet(roflCommon.DeploymentFlags) |
|
0 commit comments