From 88718323f0a575c33295f09c0b2e9a77a64e748d Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 9 Jan 2020 22:59:45 +0000 Subject: [PATCH] Add flag for build vm to `appvm generate` --- appvm.go | 39 ++++++++++++++++++++++++--------------- generate.go | 22 +++++++++++++++++++--- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/appvm.go b/appvm.go index fbe4e0e..de16807 100644 --- a/appvm.go +++ b/appvm.go @@ -254,7 +254,7 @@ func start(l *libvirt.Libvirt, name string, verbose, online, stateless bool, if !isAppvmConfigurationExists(appvmPath, name) { log.Println("No configuration exists for app, " + "trying to generate") - err := generate(l, name, "", "") + err := generate(name, "", "", false) if err != nil { log.Println("Can't auto generate") return @@ -435,19 +435,6 @@ func main() { log.Fatal(err) } - c, err := net.DialTimeout("unix", "/var/run/libvirt/libvirt-sock", time.Second) - if err != nil { - log.Fatal(err) - } - - l := libvirt.New(c) - if err := l.Connect(); err != nil { - log.Fatal(err) - } - defer l.Disconnect() - - cleanupStatelessVMs(l) - kingpin.Command("list", "List applications") autoballonCommand := kingpin.Command("autoballoon", "Automatically adjust/reduce app vm memory") minMemory := autoballonCommand.Flag("min-memory", "Set minimal memory (megabytes)").Default("1024").Uint64() @@ -468,19 +455,41 @@ func main() { generateName := generateCommand.Arg("name", "Nix package name").Required().String() generateBin := generateCommand.Arg("bin", "Binary").Default("").String() generateVMName := generateCommand.Flag("vm", "Use VM Name").Default("").String() + generateBuildVM := generateCommand.Flag("build", "Build VM").Bool() searchCommand := kingpin.Command("search", "Search for application") searchName := searchCommand.Arg("name", "Application name").Required().String() kingpin.Command("sync", "Synchronize remote repos for applications") + var l *libvirt.Libvirt + if kingpin.Parse() != "generate" { + c, err := net.DialTimeout( + "unix", + "/var/run/libvirt/libvirt-sock", + time.Second, + ) + if err != nil { + log.Fatal(err) + } + + l = libvirt.New(c) + if err := l.Connect(); err != nil { + log.Fatal(err) + } + defer l.Disconnect() + + cleanupStatelessVMs(l) + } + switch kingpin.Parse() { case "list": list(l) case "search": search(*searchName) case "generate": - generate(l, *generateName, *generateBin, *generateVMName) + generate(*generateName, *generateBin, *generateVMName, + *generateBuildVM) case "start": start(l, *startName, !*startQuiet, !*startOffline, *startStateless, diff --git a/generate.go b/generate.go index 3eae4b7..be8f019 100644 --- a/generate.go +++ b/generate.go @@ -8,8 +8,6 @@ import ( "os" "os/exec" "strings" - - "github.com/digitalocean/go-libvirt" ) var template = ` @@ -78,7 +76,7 @@ func filterDotfiles(files []os.FileInfo) (notHiddenFiles []os.FileInfo) { return } -func generate(l *libvirt.Libvirt, pkg, bin, vmname string) (err error) { +func generate(pkg, bin, vmname string, build bool) (err error) { // TODO refactor var name, channel string @@ -190,5 +188,23 @@ func generate(l *libvirt.Libvirt, pkg, bin, vmname string) (err error) { fmt.Print(appNixConfig + "\n") log.Println("Configuration file is saved to", appFilename) + + if build { + err = os.Chdir(configDir) + if err != nil { + return + } + + if vmname != "" { + _, _, _, err = generateVM(vmname, true) + } else { + _, _, _, err = generateVM(name, true) + } + + if err != nil { + return + } + } + return }