From ffb75b94fdd8052f8bdb5ca2d2dec7c89ca3bb2b Mon Sep 17 00:00:00 2001 From: Christopher Obbard Date: Thu, 26 Nov 2020 11:29:40 +0000 Subject: [PATCH] cmd/debos: Add backend argument to choose fakemachine backend Fakemachine now allows the virtualisation backend to be chosen when creating the machine, so allow the backend to be chosen by the user. Signed-off-by: Christopher Obbard --- cmd/debos/debos.go | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/cmd/debos/debos.go b/cmd/debos/debos.go index a31ef98..0f9d6a1 100644 --- a/cmd/debos/debos.go +++ b/cmd/debos/debos.go @@ -59,6 +59,7 @@ func warnLocalhost(variable string, value string) { func main() { context := debos.DebosContext { &debos.CommonContext{}, "", "" } var options struct { + Backend string `short:"b" long:"fakemachine-backend" description:"Fakemachine backend to use" default:"auto"` ArtifactDir string `long:"artifactdir" description:"Directory for packed archives and ostree repositories (default: current directory)"` InternalImage string `long:"internal-image" hidden:"true"` TemplateVars map[string]string `short:"t" long:"template-var" description:"Template variables (use -t VARIABLE:VALUE syntax)"` @@ -94,8 +95,10 @@ func main() { }() parser := flags.NewParser(&options, flags.Default) - args, err := parser.Parse() + fakemachineBackends := parser.FindOptionByLongName("fakemachine-backend") + fakemachineBackends.Choices = fakemachine.BackendNames() + args, err := parser.Parse() if err != nil { flagsErr, ok := err.(*flags.Error) if ok && flagsErr.Type == flags.ErrHelp { @@ -112,6 +115,12 @@ func main() { return } + if options.DisableFakeMachine && options.Backend != "auto" { + log.Println("--disable-fakemachine and --fakemachine-backend are mutually exclusive") + exitcode = 1 + return + } + // Set interactive shell binary only if '--debug-shell' options passed if options.DebugShell { context.DebugShell = options.Shell @@ -140,12 +149,34 @@ func main() { return } - /* If fakemachine is supported the outer fake machine will never use the + /* If fakemachine is used the outer fake machine will never use the * scratchdir, so just set it to /scratch as a dummy to prevent the - * outer debos creating a temporary direction */ - if !options.DisableFakeMachine && (fakemachine.InMachine() || fakemachine.Supported()) { - context.Scratchdir = "/scratch" + * outer debos creating a temporary directory */ + context.Scratchdir = "/scratch" + + var runInFakeMachine = true + var m *fakemachine.Machine + if options.DisableFakeMachine || fakemachine.InMachine() { + runInFakeMachine = false } else { + // attempt to create a fakemachine + m, err = fakemachine.NewMachineWithBackend(options.Backend) + if err != nil { + log.Printf("error creating fakemachine: %v", err) + + /* fallback to running on the host unless the user has chosen + * a specific backend */ + if options.Backend == "auto" { + runInFakeMachine = false + } else { + exitcode = 1 + return + } + } + } + + // if running on the host create a scratchdir + if !runInFakeMachine && !fakemachine.InMachine() { log.Printf("fakemachine not supported, running on the host!") cwd, _ := os.Getwd() context.Scratchdir, err = ioutil.TempDir(cwd, ".debos-") @@ -212,8 +243,7 @@ func main() { return } - if !options.DisableFakeMachine && !fakemachine.InMachine() && fakemachine.Supported() { - m := fakemachine.NewMachine() + if runInFakeMachine { var args []string if options.Memory == "" {