diff --git a/cli/factory.go b/cli/factory.go index c2c1adcff..7b462e64c 100644 --- a/cli/factory.go +++ b/cli/factory.go @@ -18,6 +18,7 @@ import ( var factorySubCmds = []cli.Command{ initFactoryCommand, destroyFactoryCommand, + statusFactoryCommand, } var factoryCLICommand = cli.Command{ @@ -54,10 +55,10 @@ var initFactoryCommand = cli.Command{ kataLog.WithError(err).Error("create vm factory failed") return err } - fmt.Println("vm factory initialized") + fmt.Fprintln(defaultOutputFile, "vm factory initialized") } else { kataLog.Error("vm factory is not enabled") - fmt.Println("vm factory is not enabled") + fmt.Fprintln(defaultOutputFile, "vm factory is not enabled") } return nil @@ -92,7 +93,41 @@ var destroyFactoryCommand = cli.Command{ f.CloseFactory() } } - fmt.Println("vm factory destroyed") + fmt.Fprintln(defaultOutputFile, "vm factory destroyed") + return nil + }, +} + +var statusFactoryCommand = cli.Command{ + Name: "status", + Usage: "query the status of VM factory", + Action: func(context *cli.Context) error { + runtimeConfig, ok := context.App.Metadata["runtimeConfig"].(oci.RuntimeConfig) + if !ok { + return errors.New("invalid runtime config") + } + + if runtimeConfig.FactoryConfig.Template { + factoryConfig := vf.Config{ + Template: true, + VMConfig: vc.VMConfig{ + HypervisorType: runtimeConfig.HypervisorType, + HypervisorConfig: runtimeConfig.HypervisorConfig, + AgentType: runtimeConfig.AgentType, + AgentConfig: runtimeConfig.AgentConfig, + }, + } + kataLog.WithField("factory", factoryConfig).Info("load vm factory") + f, err := vf.NewFactory(factoryConfig, true) + if err != nil { + fmt.Fprintln(defaultOutputFile, "vm factory is off") + } else { + f.CloseFactory() + fmt.Fprintln(defaultOutputFile, "vm factory is on") + } + } else { + fmt.Fprintln(defaultOutputFile, "vm factory not enabled") + } return nil }, } diff --git a/cli/factory_test.go b/cli/factory_test.go index ccf48856d..4681172cd 100644 --- a/cli/factory_test.go +++ b/cli/factory_test.go @@ -115,3 +115,39 @@ func TestFactoryCLIFunctionDestroy(t *testing.T) { err = fn(ctx) assert.Nil(err) } + +func TestFactoryCLIFunctionStatus(t *testing.T) { + assert := assert.New(t) + + tmpdir, err := ioutil.TempDir("", "") + assert.NoError(err) + defer os.RemoveAll(tmpdir) + + runtimeConfig, err := newTestRuntimeConfig(tmpdir, testConsole, true) + assert.NoError(err) + + set := flag.NewFlagSet("", 0) + + set.String("console-socket", "", "") + + app := cli.NewApp() + ctx := cli.NewContext(app, set, nil) + app.Name = "foo" + + // No template + ctx.App.Metadata = map[string]interface{}{ + "runtimeConfig": runtimeConfig, + } + fn, ok := statusFactoryCommand.Action.(func(context *cli.Context) error) + assert.True(ok) + err = fn(ctx) + assert.Nil(err) + + // With template + runtimeConfig.FactoryConfig.Template = true + runtimeConfig.HypervisorType = vc.MockHypervisor + runtimeConfig.AgentType = vc.NoopAgentType + ctx.App.Metadata["runtimeConfig"] = runtimeConfig + err = fn(ctx) + assert.Nil(err) +}