Skip to content

Commit

Permalink
cmd/rofl: Refactor build subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Oct 16, 2024
1 parent cd458d7 commit 6a599d4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 52 deletions.
65 changes: 65 additions & 0 deletions cmd/rofl/build/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package build

import (
"context"
"fmt"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"

"github.com/oasisprotocol/cli/cmd/common"
)

// Build modes.
const (
buildModeProduction = "production"
buildModeUnsafe = "unsafe"
buildModeAuto = "auto"
)

var (
outputFn string
buildMode string
offline bool

Cmd = &cobra.Command{
Use: "build",
Short: "Build a ROFL application",
}
)

func detectBuildMode(npa *common.NPASelection) {
// Configure build mode. In case auto is selected and not offline, query the network. If
// autodetection fails, default to production mode.
switch {
case buildMode == buildModeAuto && !offline:
ctx := context.Background()
conn, err := connection.Connect(ctx, npa.Network)
if err != nil {
cobra.CheckErr(fmt.Errorf("unable to autodetect build mode, please provide --mode flag manually (failed to connect to GRPC endpoint: %w)", err))
}

params, err := conn.Consensus().Registry().ConsensusParameters(ctx, consensus.HeightLatest)
if err != nil {
cobra.CheckErr(fmt.Errorf("unable to autodetect build mode, please provide --mode flag manually (failed to get consensus parameters: %w)", err))
}

if params.DebugAllowTestRuntimes {
buildMode = buildModeUnsafe
}
default:
}
}

func init() {
globalFlags := flag.NewFlagSet("", flag.ContinueOnError)
globalFlags.StringVar(&buildMode, "mode", "auto", "build mode [production, unsafe, auto]")
globalFlags.BoolVar(&offline, "offline", false, "do not perform any operations requiring network access")
globalFlags.StringVar(&outputFn, "output", "", "output bundle filename")

Cmd.PersistentFlags().AddFlagSet(globalFlags)
Cmd.AddCommand(sgxCmd)
}
56 changes: 5 additions & 51 deletions cmd/rofl/build.go → cmd/rofl/build/sgx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rofl
package build

import (
"context"
"crypto/rand"
"crypto/rsa"
"fmt"
Expand All @@ -17,39 +16,21 @@ import (
"github.com/oasisprotocol/oasis-core/go/common/sgx"
"github.com/oasisprotocol/oasis-core/go/common/sgx/sigstruct"
"github.com/oasisprotocol/oasis-core/go/common/version"
consensus "github.com/oasisprotocol/oasis-core/go/consensus/api"
"github.com/oasisprotocol/oasis-core/go/runtime/bundle"
"github.com/oasisprotocol/oasis-core/go/runtime/bundle/component"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"

"github.com/oasisprotocol/cli/build/cargo"
"github.com/oasisprotocol/cli/build/sgxs"
"github.com/oasisprotocol/cli/cmd/common"
cliConfig "github.com/oasisprotocol/cli/config"
)

// Build modes.
const (
buildModeProduction = "production"
buildModeUnsafe = "unsafe"
buildModeAuto = "auto"
)

var (
sgxHeapSize uint64
sgxStackSize uint64
sgxThreads uint64

outputFn string
buildMode string
offline bool

buildCmd = &cobra.Command{
Use: "build",
Short: "Build a ROFL application",
}

buildSgxCmd = &cobra.Command{
sgxCmd = &cobra.Command{
Use: "sgx",
Short: "Build an SGX-based Rust ROFL application",
Args: cobra.NoArgs,
Expand All @@ -65,26 +46,7 @@ var (

fmt.Println("Building an SGX-based Rust ROFL application...")

// Configure build mode. In case auto is selected and not offline, query the network.
// If autodetection fails, default to production mode.
switch {
case buildMode == buildModeAuto && !offline:
ctx := context.Background()
conn, err := connection.Connect(ctx, npa.Network)
if err != nil {
cobra.CheckErr(fmt.Errorf("unable to autodetect build mode, please provide --mode flag manually: failed to connect to GRPC endpoint: %v", err))
}

params, err := conn.Consensus().Registry().ConsensusParameters(ctx, consensus.HeightLatest)
if err != nil {
cobra.CheckErr(fmt.Errorf("unable to autodetect build mode, please provide --mode flag manually: failed to get consensus parameters: %v", err))
}

if params.DebugAllowTestRuntimes {
buildMode = buildModeUnsafe
}
default:
}
detectBuildMode(npa)
features := sgxSetupBuildEnv()

// Obtain package metadata.
Expand Down Expand Up @@ -333,15 +295,7 @@ func init() {
sgxFlags.Uint64Var(&sgxHeapSize, "sgx-heap-size", 512*1024*1024, "SGX enclave heap size")
sgxFlags.Uint64Var(&sgxStackSize, "sgx-stack-size", 2*1024*1024, "SGX enclave stack size")
sgxFlags.Uint64Var(&sgxThreads, "sgx-threads", 32, "SGX enclave maximum number of threads")
sgxFlags.StringVar(&outputFn, "output", "", "output bundle filename")

globalFlags := flag.NewFlagSet("", flag.ContinueOnError)
globalFlags.StringVar(&buildMode, "mode", "auto", "build mode [production, unsafe, auto]")
globalFlags.BoolVar(&offline, "offline", false, "do not perform any operations requiring network access")

buildSgxCmd.Flags().AddFlagSet(common.SelectorNPFlags)
buildSgxCmd.Flags().AddFlagSet(sgxFlags)

buildCmd.PersistentFlags().AddFlagSet(globalFlags)
buildCmd.AddCommand(buildSgxCmd)
sgxCmd.Flags().AddFlagSet(common.SelectorNPFlags)
sgxCmd.Flags().AddFlagSet(sgxFlags)
}
4 changes: 3 additions & 1 deletion cmd/rofl/rofl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rofl

import (
"github.com/spf13/cobra"

"github.com/oasisprotocol/cli/cmd/rofl/build"
)

var Cmd = &cobra.Command{
Expand All @@ -16,6 +18,6 @@ func init() {
Cmd.AddCommand(removeCmd)
Cmd.AddCommand(showCmd)
Cmd.AddCommand(trustRootCmd)
Cmd.AddCommand(buildCmd)
Cmd.AddCommand(build.Cmd)
Cmd.AddCommand(identityCmd)
}

0 comments on commit 6a599d4

Please sign in to comment.