From 252610af57fb8a45276bb989261fd5db7c9f1271 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Thu, 12 Jul 2018 07:30:16 +0000 Subject: [PATCH] Ignore local ({local,monitor}.nix) settings --- .gitignore | 2 ++ appvm.go | 40 ++++++++++++++++++++++- nix/{local.nix => local.nix.template} | 0 nix/{monitor.nix => monitor.nix.template} | 0 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 .gitignore rename nix/{local.nix => local.nix.template} (100%) rename nix/{monitor.nix => monitor.nix.template} (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0fccf8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +nix/local.nix +nix/monitor.nix \ No newline at end of file diff --git a/appvm.go b/appvm.go index aa60293..741caef 100644 --- a/appvm.go +++ b/appvm.go @@ -9,6 +9,7 @@ package main import ( "fmt" + "io" "io/ioutil" "log" "net" @@ -119,14 +120,51 @@ func list(l *libvirt.Libvirt) { } } +func copyFile(to, from string) (err error) { + source, err := os.Open(from) + if err != nil { + return err + } + defer source.Close() + + destination, err := os.Create(to) + if err != nil { + return + } + + _, err = io.Copy(destination, source) + if err != nil { + destination.Close() + return + } + + return destination.Close() +} + func start(l *libvirt.Libvirt, name string) { // Currently binary-only installation is not supported, because we need *.nix configurations gopath := os.Getenv("GOPATH") - err := os.Chdir(gopath + "/src/github.com/jollheef/appvm") + appvmPath := gopath + "/src/github.com/jollheef/appvm" + err := os.Chdir(appvmPath) if err != nil { log.Fatal(err) } + // Copy templates + if _, err := os.Stat(appvmPath + "/nix/local.nix"); os.IsNotExist(err) { + err = copyFile(appvmPath+"/nix/local.nix.template", appvmPath+"/nix/local.nix") + if err != nil { + log.Fatal(err) + } + } + + if _, err := os.Stat(appvmPath + "/nix/monitor.nix"); os.IsNotExist(err) { + err = copyFile(appvmPath+"/nix/monitor.nix.template", appvmPath+"/nix/monitor.nix") + if err != nil { + log.Fatal(err) + } + } + stdout, stderr, ret, err := system.System("nix-build", "", "-A", "config.system.build.vm", "-I", "nixos-config=nix/"+name+".nix", "-I", ".") if err != nil { diff --git a/nix/local.nix b/nix/local.nix.template similarity index 100% rename from nix/local.nix rename to nix/local.nix.template diff --git a/nix/monitor.nix b/nix/monitor.nix.template similarity index 100% rename from nix/monitor.nix rename to nix/monitor.nix.template