func New(templateRoot string, cpu, mem int, kernel, initrd string) base.Factory { var vmName string for { vmName = fmt.Sprintf("template-vm-%s", pod.RandStr(10, "alpha")) if _, err := os.Stat(templateRoot + "/" + vmName); os.IsNotExist(err) { break } } s, err := template.CreateTemplateVM(templateRoot+"/"+vmName, vmName, cpu, mem, kernel, initrd) if err != nil { glog.Infof("failed to create template factory: %v", err) glog.Infof("use direct factory instead") return direct.New(cpu, mem, kernel, initrd) } return &templateFactory{s: s} }
} if context.GlobalBool("debug") { flag.CommandLine.Parse([]string{"-v", "3", "--log_dir", context.GlobalString("log_dir"), "--alsologtostderr"}) } else { flag.CommandLine.Parse([]string{"-v", "1", "--log_dir", context.GlobalString("log_dir")}) } var err error if hypervisor.HDriver, err = driverloader.Probe(context.GlobalString("driver")); err != nil { glog.V(1).Infof("%v\n", err) fmt.Printf("Failed to setup the driver: %v\n", err) os.Exit(-1) } if _, err := templatecore.CreateTemplateVM(template, "", context.Int("cpu"), context.Int("mem"), kernel, initrd); err != nil { fmt.Printf("Failed to create the template: %v\n", err) os.Exit(-1) } }, } var removeTemplateCommand = cli.Command{ Name: "remove-template", Usage: "remove the template VM on the directory specified by the global option --template", Action: func(context *cli.Context) { if err := syscall.Unmount(context.GlobalString("template"), 0); err != nil { fmt.Printf("Failed to remove the template: %v\n", err) os.Exit(-1) } },