Generate appvm at start automatically if not exists

This commit is contained in:
Mikhail Klementev 2020-01-02 15:29:20 +00:00
parent 7e4aa33a0a
commit 04b2cf63ce
No known key found for this signature in database
GPG Key ID: BE44DA8C062D87DC
2 changed files with 29 additions and 3 deletions

View File

@ -196,9 +196,31 @@ func stupidProgressBar() {
}
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func isAppvmConfigurationExists(appvmPath, name string) bool {
return fileExists(appvmPath + "/nix/" + name + ".nix")
}
func start(l *libvirt.Libvirt, name string, verbose bool) {
appvmPath := configDir
if !isAppvmConfigurationExists(appvmPath, name) {
log.Println("No configuration exists for app, " +
"trying to generate")
err := generate(l, name, "", "")
if err != nil {
log.Println("Can't auto generate")
return
}
}
// Copy templates
err := prepareTemplates(appvmPath)
if err != nil {

View File

@ -71,7 +71,7 @@ func filterDotfiles(files []os.FileInfo) (notHiddenFiles []os.FileInfo) {
return
}
func generate(l *libvirt.Libvirt, pkg, bin, vmname string) {
func generate(l *libvirt.Libvirt, pkg, bin, vmname string) (err error) {
var name string
if strings.Contains(pkg, ".") {
@ -79,7 +79,8 @@ func generate(l *libvirt.Libvirt, pkg, bin, vmname string) {
} else {
log.Println("Package name does not contains channel")
log.Println("Trying to guess")
channel, err := guessChannel()
var channel string
channel, err = guessChannel()
if err != nil {
log.Println("Cannot guess channel")
log.Println("Check nix-channel --list")
@ -91,7 +92,9 @@ func generate(l *libvirt.Libvirt, pkg, bin, vmname string) {
}
if !isPackageExists(name) {
log.Println("Package", name, "does not exists")
s := "Package " + name + " does not exists"
err = errors.New(s)
log.Println(s)
return
}
@ -177,4 +180,5 @@ func generate(l *libvirt.Libvirt, pkg, bin, vmname string) {
fmt.Print(appNixConfig + "\n")
log.Println("Configuration file is saved to", appFilename)
return
}