Add --network switch to select a networking model (#22)

This commit is contained in:
msm-code 2020-04-06 09:12:46 +00:00 committed by GitHub
parent e588479701
commit ba80fba648
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 22 deletions

View File

@ -31,6 +31,15 @@ import (
kingpin "gopkg.in/alecthomas/kingpin.v2" kingpin "gopkg.in/alecthomas/kingpin.v2"
) )
type networkModel int
const (
networkOffline networkModel = iota
networkQemu networkModel = iota
networkLibvirt networkModel = iota
)
func list(l *libvirt.Libvirt) { func list(l *libvirt.Libvirt) {
domains, err := l.Domains() domains, err := l.Domains()
if err != nil { if err != nil {
@ -173,14 +182,14 @@ func isRunning(l *libvirt.Libvirt, name string) bool {
func generateAppVM(l *libvirt.Libvirt, func generateAppVM(l *libvirt.Libvirt,
nixName, vmName, appvmPath, sharedDir string, nixName, vmName, appvmPath, sharedDir string,
verbose, online, gui bool) (err error) { verbose bool, network networkModel, gui bool) (err error) {
realpath, reginfo, qcow2, err := generateVM(appvmPath, nixName, verbose) realpath, reginfo, qcow2, err := generateVM(appvmPath, nixName, verbose)
if err != nil { if err != nil {
return return
} }
xml := generateXML(vmName, online, gui, realpath, reginfo, qcow2, sharedDir) xml := generateXML(vmName, network, gui, realpath, reginfo, qcow2, sharedDir)
_, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate) _, err = l.DomainCreateXML(xml, libvirt.DomainStartValidate)
return return
} }
@ -209,8 +218,8 @@ func isAppvmConfigurationExists(appvmPath, name string) bool {
return fileExists(appvmPath + "/nix/" + name + ".nix") return fileExists(appvmPath + "/nix/" + name + ".nix")
} }
func start(l *libvirt.Libvirt, name string, verbose, online, gui, stateless bool, func start(l *libvirt.Libvirt, name string, verbose bool, network networkModel,
args, open string) { gui, stateless bool, args, open string) {
appvmPath := configDir appvmPath := configDir
@ -267,7 +276,7 @@ func start(l *libvirt.Libvirt, name string, verbose, online, gui, stateless bool
} }
err := generateAppVM(l, name, vmName, appvmPath, sharedDir, err := generateAppVM(l, name, vmName, appvmPath, sharedDir,
verbose, online, gui) verbose, network, gui)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -415,6 +424,22 @@ func cleanupStatelessVMs(l *libvirt.Libvirt) {
} }
} }
func parseNetworkModel(flagOffline bool, flagNetworking string) networkModel {
if flagNetworking != "" && flagOffline {
log.Fatal("Can't use both --network and --offline switches")
}
if flagOffline || flagNetworking == "offline" {
return networkOffline
}
if flagNetworking == "libvirt" {
return networkLibvirt
}
if flagNetworking == "qemu" {
return networkQemu
}
return networkQemu // qemu is the default network model
}
var configDir = os.Getenv("HOME") + "/.config/appvm/" var configDir = os.Getenv("HOME") + "/.config/appvm/"
var appvmHomesDir = os.Getenv("HOME") + "/appvm/" var appvmHomesDir = os.Getenv("HOME") + "/appvm/"
@ -454,6 +479,7 @@ func main() {
startOffline := startCommand.Flag("offline", "Disconnect").Bool() startOffline := startCommand.Flag("offline", "Disconnect").Bool()
startCli := startCommand.Flag("cli", "Disable graphics mode, enable serial").Bool() startCli := startCommand.Flag("cli", "Disable graphics mode, enable serial").Bool()
startStateless := startCommand.Flag("stateless", "Do not use default state directory").Bool() startStateless := startCommand.Flag("stateless", "Do not use default state directory").Bool()
startNetwork := startCommand.Flag("network", "Used networking model").Enum("offline", "qemu", "libvirt")
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()
@ -498,8 +524,9 @@ func main() {
generate(*generateName, *generateBin, *generateVMName, generate(*generateName, *generateBin, *generateVMName,
*generateBuildVM) *generateBuildVM)
case "start": case "start":
networkModel := parseNetworkModel(*startOffline, *startNetwork)
start(l, *startName, start(l, *startName,
!*startQuiet, !*startOffline, !*startCli, *startStateless, !*startQuiet, networkModel, !*startCli, *startStateless,
*startArgs, *startOpen) *startArgs, *startOpen)
case "stop": case "stop":
stop(l, *stopName) stop(l, *stopName)

40
xml.go
View File

@ -5,7 +5,7 @@ import "fmt"
// You may think that you want to rewrite to proper golang structures. // You may think that you want to rewrite to proper golang structures.
// Believe me, you shouldn't. // Believe me, you shouldn't.
func generateXML(vmName string, online, gui bool, func generateXML(vmName string, network networkModel, gui bool,
vmNixPath, reginfo, img, sharedDir string) string { vmNixPath, reginfo, img, sharedDir string) string {
devices := "" devices := ""
@ -14,7 +14,25 @@ func generateXML(vmName string, online, gui bool,
devices = guiDevices devices = guiDevices
} }
qemuParams := ` qemuParams := qemuParamsDefault
if network == networkQemu {
qemuParams = qemuParamsWithNetwork
} else if network == networkLibvirt {
devices += netDevices
}
return fmt.Sprintf(xmlTmpl, vmName, vmNixPath, vmNixPath, vmNixPath,
reginfo, img, sharedDir, sharedDir, sharedDir, devices, qemuParams)
}
var qemuParamsDefault = `
<qemu:commandline>
<qemu:arg value='-snapshot'/>
</qemu:commandline>
`
var qemuParamsWithNetwork = `
<qemu:commandline> <qemu:commandline>
<qemu:arg value='-device'/> <qemu:arg value='-device'/>
<qemu:arg value='e1000,netdev=net0'/> <qemu:arg value='e1000,netdev=net0'/>
@ -22,19 +40,13 @@ func generateXML(vmName string, online, gui bool,
<qemu:arg value='user,id=net0'/> <qemu:arg value='user,id=net0'/>
<qemu:arg value='-snapshot'/> <qemu:arg value='-snapshot'/>
</qemu:commandline> </qemu:commandline>
` `
if !online { var netDevices = `
qemuParams = ` <interface type='network'>
<qemu:commandline> <source network='default'/>
<qemu:arg value='-snapshot'/> </interface>
</qemu:commandline> `
`
}
return fmt.Sprintf(xmlTmpl, vmName, vmNixPath, vmNixPath, vmNixPath,
reginfo, img, sharedDir, sharedDir, sharedDir, devices, qemuParams)
}
var guiDevices = ` var guiDevices = `
<!-- Graphical console --> <!-- Graphical console -->