enclave.go 1.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
package main

import (
	"fmt"
	"os"
	"runtime"

	"github.com/opencontainers/runc/libcontainer/logs"
	"github.com/opencontainers/runc/libenclave"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"
)

func init() {
	if len(os.Args) > 1 && os.Args[1] == "enclave" {
		runtime.GOMAXPROCS(1)
		runtime.LockOSThread()

		level := os.Getenv("_LIBENCLAVE_LOGLEVEL")
		logLevel, err := logs.ParseLogLevel(level)
		if err != nil {
			panic(fmt.Sprintf("runelet: failed to parse log level: %q: %v", level,
				err))
		}

		err = logs.ConfigureLogging(logs.Config{
			LogPipeFd: os.Getenv("_LIBENCLAVE_LOGPIPE"),
			LogFormat: "json",
			LogLevel:  logLevel,
		})
		if err != nil {
			panic(fmt.Sprintf("runelet: failed to configure logging: %v", err))
		}
		logrus.Debug("runelet process started")
	}
}

var enclaveCommand = cli.Command{
	Name:  "enclave",
	Usage: `initialize the enclave runtime (do not call it outside of rune)`,
	Action: func(context *cli.Context) error {
		exitCode, err := libenclave.StartInitialization()
		if err != nil {
			logrus.Fatal(err)
		}
		os.Exit(int(exitCode))
		panic("runelet process failed to exit")
	},
}