Implements verbose flag
This commit is contained in:
		| @@ -49,7 +49,7 @@ Autodetection is a bash-spaghetti, so you need to check results. BTW it's just a | |||||||
|  |  | ||||||
| ## Run application | ## Run application | ||||||
|  |  | ||||||
|     $ appvm start chromium |     $ appvm start chromium --verbose | ||||||
|     $ # ... long wait for first time, because we need to collect a lot of packages |     $ # ... long wait for first time, because we need to collect a lot of packages | ||||||
|  |  | ||||||
| You can customize local settings in `$GOPATH/github.com/jollheef/appvm/nix/local.nix`. | You can customize local settings in `$GOPATH/github.com/jollheef/appvm/nix/local.nix`. | ||||||
|   | |||||||
							
								
								
									
										46
									
								
								appvm.go
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								appvm.go
									
									
									
									
									
								
							| @@ -22,6 +22,7 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	"github.com/digitalocean/go-libvirt" | 	"github.com/digitalocean/go-libvirt" | ||||||
|  | 	"github.com/go-cmd/cmd" | ||||||
| 	"github.com/jollheef/go-system" | 	"github.com/jollheef/go-system" | ||||||
| 	"github.com/olekukonko/tablewriter" | 	"github.com/olekukonko/tablewriter" | ||||||
| 	kingpin "gopkg.in/alecthomas/kingpin.v2" | 	kingpin "gopkg.in/alecthomas/kingpin.v2" | ||||||
| @@ -167,11 +168,28 @@ func prepareTemplates(appvmPath string) (err error) { | |||||||
| 	return | 	return | ||||||
| } | } | ||||||
|  |  | ||||||
| func generateVM(name string) (realpath, reginfo, qcow2 string, err error) { | func streamStdOutErr(command *cmd.Cmd) { | ||||||
| 	stdout, stderr, ret, err := system.System("nix-build", "<nixpkgs/nixos>", "-A", "config.system.build.vm", | 	for { | ||||||
|  | 		select { | ||||||
|  | 		case line := <-command.Stdout: | ||||||
|  | 			fmt.Println(line) | ||||||
|  | 		case line := <-command.Stderr: | ||||||
|  | 			fmt.Fprintln(os.Stderr, line) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func generateVM(name string, verbose bool) (realpath, reginfo, qcow2 string, err error) { | ||||||
|  | 	command := cmd.NewCmdOptions(cmd.Options{Buffered: false, Streaming: true}, | ||||||
|  | 		"nix-build", "<nixpkgs/nixos>", "-A", "config.system.build.vm", | ||||||
| 		"-I", "nixos-config=nix/"+name+".nix", "-I", ".") | 		"-I", "nixos-config=nix/"+name+".nix", "-I", ".") | ||||||
| 	if err != nil { | 	if verbose { | ||||||
| 		log.Println(err, stdout, stderr, ret) | 		go streamStdOutErr(command) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	status := <-command.Start() | ||||||
|  | 	if status.Error != nil || status.Exit != 0 { | ||||||
|  | 		log.Println(status.Error, status.Stdout, status.Stderr) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -206,13 +224,13 @@ func isRunning(l *libvirt.Libvirt, name string) bool { | |||||||
| 	return err == nil | 	return err == nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func generateAppVM(l *libvirt.Libvirt, appvmPath, name string) (err error) { | func generateAppVM(l *libvirt.Libvirt, appvmPath, name string, verbose bool) (err error) { | ||||||
| 	err = os.Chdir(appvmPath) | 	err = os.Chdir(appvmPath) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	realpath, reginfo, qcow2, err := generateVM(name) | 	realpath, reginfo, qcow2, err := generateVM(name, verbose) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| @@ -237,7 +255,7 @@ func stupidProgressBar() { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| func start(l *libvirt.Libvirt, name string) { | func start(l *libvirt.Libvirt, name string, verbose bool) { | ||||||
| 	// Currently binary-only installation is not supported, because we need *.nix configurations | 	// Currently binary-only installation is not supported, because we need *.nix configurations | ||||||
| 	appvmPath := os.Getenv("GOPATH") + "/src/github.com/jollheef/appvm" | 	appvmPath := os.Getenv("GOPATH") + "/src/github.com/jollheef/appvm" | ||||||
|  |  | ||||||
| @@ -248,8 +266,10 @@ func start(l *libvirt.Libvirt, name string) { | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	if !isRunning(l, name) { | 	if !isRunning(l, name) { | ||||||
| 		go stupidProgressBar() | 		if !verbose { | ||||||
| 		err = generateAppVM(l, appvmPath, name) | 			go stupidProgressBar() | ||||||
|  | 		} | ||||||
|  | 		err = generateAppVM(l, appvmPath, name, verbose) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			log.Fatal(err) | 			log.Fatal(err) | ||||||
| 		} | 		} | ||||||
| @@ -348,7 +368,11 @@ func main() { | |||||||
| 	autoballonCommand := kingpin.Command("autoballoon", "Automatically adjust/reduce app vm memory") | 	autoballonCommand := kingpin.Command("autoballoon", "Automatically adjust/reduce app vm memory") | ||||||
| 	minMemory := autoballonCommand.Flag("min-memory", "Set minimal memory (megabytes)").Default("1024").Uint64() | 	minMemory := autoballonCommand.Flag("min-memory", "Set minimal memory (megabytes)").Default("1024").Uint64() | ||||||
| 	adjustPercent := autoballonCommand.Flag("adj-memory", "Adjust memory ammount (percents)").Default("20").Uint64() | 	adjustPercent := autoballonCommand.Flag("adj-memory", "Adjust memory ammount (percents)").Default("20").Uint64() | ||||||
| 	startName := kingpin.Command("start", "Start application").Arg("name", "Application name").Required().String() |  | ||||||
|  | 	startCommand := kingpin.Command("start", "Start application") | ||||||
|  | 	startName := startCommand.Arg("name", "Application name").Required().String() | ||||||
|  | 	startVerbose := startCommand.Flag("verbose", "Increase verbosity").Default("False").Bool() | ||||||
|  |  | ||||||
| 	stopName := kingpin.Command("stop", "Stop application").Arg("name", "Application name").Required().String() | 	stopName := kingpin.Command("stop", "Stop application").Arg("name", "Application name").Required().String() | ||||||
| 	dropName := kingpin.Command("drop", "Remove application data").Arg("name", "Application name").Required().String() | 	dropName := kingpin.Command("drop", "Remove application data").Arg("name", "Application name").Required().String() | ||||||
|  |  | ||||||
| @@ -356,7 +380,7 @@ func main() { | |||||||
| 	case "list": | 	case "list": | ||||||
| 		list(l) | 		list(l) | ||||||
| 	case "start": | 	case "start": | ||||||
| 		start(l, *startName) | 		start(l, *startName, *startVerbose) | ||||||
| 	case "stop": | 	case "stop": | ||||||
| 		stop(l, *stopName) | 		stop(l, *stopName) | ||||||
| 	case "drop": | 	case "drop": | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user