|
| 1 | +package machine |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + flag "github.com/spf13/pflag" |
| 10 | + |
| 11 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/client" |
| 12 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
| 13 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature" |
| 14 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/crypto/signature/ed25519" |
| 15 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/types" |
| 16 | + |
| 17 | + buildRofl "github.com/oasisprotocol/cli/build/rofl" |
| 18 | + "github.com/oasisprotocol/cli/build/rofl/scheduler" |
| 19 | + "github.com/oasisprotocol/cli/cmd/common" |
| 20 | + roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common" |
| 21 | + cliConfig "github.com/oasisprotocol/cli/config" |
| 22 | +) |
| 23 | + |
| 24 | +var logsCmd = &cobra.Command{ |
| 25 | + Use: "logs [<machine-name>]", |
| 26 | + Short: "Show logs from the given machine", |
| 27 | + Args: cobra.MaximumNArgs(1), |
| 28 | + Run: func(_ *cobra.Command, args []string) { |
| 29 | + cfg := cliConfig.Global() |
| 30 | + npa := common.GetNPASelection(cfg) |
| 31 | + |
| 32 | + _, deployment := roflCommon.LoadManifestAndSetNPA(cfg, npa, deploymentName, &roflCommon.ManifestOptions{ |
| 33 | + NeedAppID: true, |
| 34 | + NeedAdmin: false, |
| 35 | + }) |
| 36 | + |
| 37 | + machine, _, machineID := resolveMachine(args, deployment) |
| 38 | + |
| 39 | + // Establish connection with the target network. |
| 40 | + ctx := context.Background() |
| 41 | + conn, err := connection.Connect(ctx, npa.Network) |
| 42 | + cobra.CheckErr(err) |
| 43 | + |
| 44 | + // Resolve provider address. |
| 45 | + providerAddr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, machine.Provider) |
| 46 | + if err != nil { |
| 47 | + cobra.CheckErr(fmt.Sprintf("Invalid provider address: %s", err)) |
| 48 | + } |
| 49 | + |
| 50 | + providerDsc, err := conn.Runtime(npa.ParaTime).ROFLMarket.Provider(ctx, client.RoundLatest, *providerAddr) |
| 51 | + cobra.CheckErr(err) |
| 52 | + |
| 53 | + insDsc, err := conn.Runtime(npa.ParaTime).ROFLMarket.Instance(ctx, client.RoundLatest, *providerAddr, machineID) |
| 54 | + cobra.CheckErr(err) |
| 55 | + |
| 56 | + schedulerRAKRaw, ok := insDsc.Metadata[scheduler.MetadataKeySchedulerRAK] |
| 57 | + if !ok { |
| 58 | + cobra.CheckErr(fmt.Sprintf("Machine is missing scheduler RAK metadata.")) |
| 59 | + } |
| 60 | + var schedulerRAK ed25519.PublicKey |
| 61 | + if err := schedulerRAK.UnmarshalText([]byte(schedulerRAKRaw)); err != nil { |
| 62 | + cobra.CheckErr(fmt.Sprintf("Malformed scheduler RAK metadata: %s", err)) |
| 63 | + } |
| 64 | + pk := types.PublicKey{PublicKey: schedulerRAK} |
| 65 | + |
| 66 | + schedulerDsc, err := conn.Runtime(npa.ParaTime).ROFL.AppInstance(ctx, client.RoundLatest, providerDsc.SchedulerApp, pk) |
| 67 | + cobra.CheckErr(err) |
| 68 | + |
| 69 | + client, err := scheduler.NewClient(schedulerDsc) |
| 70 | + cobra.CheckErr(err) |
| 71 | + |
| 72 | + // TODO: Cache authentication token so we don't need to re-authenticate. |
| 73 | + acc := common.LoadAccount(cfg, npa.AccountName) |
| 74 | + |
| 75 | + sigCtx := &signature.RichContext{ |
| 76 | + RuntimeID: npa.ParaTime.Namespace(), |
| 77 | + ChainContext: npa.Network.ChainContext, |
| 78 | + Base: []byte(scheduler.StdAuthContextBase), |
| 79 | + } |
| 80 | + authRequest, err := scheduler.SignLogin(sigCtx, acc.Signer(), client.Host(), *providerAddr) |
| 81 | + cobra.CheckErr(err) |
| 82 | + |
| 83 | + err = client.Login(authRequest) |
| 84 | + cobra.CheckErr(err) |
| 85 | + |
| 86 | + logs, err := client.LogsGet(machineID, time.Time{}) |
| 87 | + cobra.CheckErr(err) |
| 88 | + for _, line := range logs { |
| 89 | + fmt.Println(line) |
| 90 | + } |
| 91 | + }, |
| 92 | +} |
| 93 | + |
| 94 | +func init() { |
| 95 | + deploymentFlags := flag.NewFlagSet("", flag.ContinueOnError) |
| 96 | + deploymentFlags.StringVar(&deploymentName, "deployment", buildRofl.DefaultDeploymentName, "deployment name") |
| 97 | + |
| 98 | + logsCmd.Flags().AddFlagSet(deploymentFlags) |
| 99 | +} |
0 commit comments