virt: Allow the use of ~ to expand to home directory
This makes examples slightly nicer to commit, since you don't have to have a hardcoded ~/james/ in their source value. It's also probably a useful feature for the resource.
This commit is contained in:
15
examples/virt4.yaml
Normal file
15
examples/virt4.yaml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
virt:
|
||||||
|
- name: mgmt4
|
||||||
|
uri: 'qemu:///session'
|
||||||
|
boot:
|
||||||
|
- hd
|
||||||
|
disk:
|
||||||
|
- type: qcow2
|
||||||
|
source: "~/.local/share/libvirt/images/fedora-23-scratch.qcow2"
|
||||||
|
state: running
|
||||||
|
transient: false
|
||||||
|
edges: []
|
||||||
|
comment: "qemu-img create -b fedora-23.qcow2 -f qcow2 fedora-23-scratch.qcow2"
|
||||||
@@ -23,6 +23,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os/user"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/event"
|
"github.com/purpleidea/mgmt/event"
|
||||||
@@ -622,20 +625,22 @@ type filesystemDevice struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *diskDevice) GetXML(idx int) string {
|
func (d *diskDevice) GetXML(idx int) string {
|
||||||
|
source, _ := expandHome(d.Source) // TODO: should we handle errors?
|
||||||
var b string
|
var b string
|
||||||
b += "<disk type='file' device='disk'>"
|
b += "<disk type='file' device='disk'>"
|
||||||
b += fmt.Sprintf("<driver name='qemu' type='%s'/>", d.Type)
|
b += fmt.Sprintf("<driver name='qemu' type='%s'/>", d.Type)
|
||||||
b += fmt.Sprintf("<source file='%s'/>", d.Source)
|
b += fmt.Sprintf("<source file='%s'/>", source)
|
||||||
b += fmt.Sprintf("<target dev='vd%s' bus='virtio'/>", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
|
b += fmt.Sprintf("<target dev='vd%s' bus='virtio'/>", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
|
||||||
b += "</disk>"
|
b += "</disk>"
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *cdRomDevice) GetXML(idx int) string {
|
func (d *cdRomDevice) GetXML(idx int) string {
|
||||||
|
source, _ := expandHome(d.Source) // TODO: should we handle errors?
|
||||||
var b string
|
var b string
|
||||||
b += "<disk type='file' device='cdrom'>"
|
b += "<disk type='file' device='cdrom'>"
|
||||||
b += fmt.Sprintf("<driver name='qemu' type='%s'/>", d.Type)
|
b += fmt.Sprintf("<driver name='qemu' type='%s'/>", d.Type)
|
||||||
b += fmt.Sprintf("<source file='%s'/>", d.Source)
|
b += fmt.Sprintf("<source file='%s'/>", source)
|
||||||
b += fmt.Sprintf("<target dev='hd%s' bus='ide'/>", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
|
b += fmt.Sprintf("<target dev='hd%s' bus='ide'/>", (string)(idx+97)) // TODO: 26, 27... should be 'aa', 'ab'...
|
||||||
b += "<readonly/>"
|
b += "<readonly/>"
|
||||||
b += "</disk>"
|
b += "</disk>"
|
||||||
@@ -655,13 +660,14 @@ func (d *networkDevice) GetXML(idx int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *filesystemDevice) GetXML(idx int) string {
|
func (d *filesystemDevice) GetXML(idx int) string {
|
||||||
|
source, _ := expandHome(d.Source) // TODO: should we handle errors?
|
||||||
var b string
|
var b string
|
||||||
b += "<filesystem" // open
|
b += "<filesystem" // open
|
||||||
if d.Access != "" {
|
if d.Access != "" {
|
||||||
b += fmt.Sprintf(" accessmode='%s'", d.Access)
|
b += fmt.Sprintf(" accessmode='%s'", d.Access)
|
||||||
}
|
}
|
||||||
b += ">" // close
|
b += ">" // close
|
||||||
b += fmt.Sprintf("<source dir='%s'/>", d.Source)
|
b += fmt.Sprintf("<source dir='%s'/>", source)
|
||||||
b += fmt.Sprintf("<target dir='%s'/>", d.Target)
|
b += fmt.Sprintf("<target dir='%s'/>", d.Target)
|
||||||
if d.ReadOnly {
|
if d.ReadOnly {
|
||||||
b += "<readonly/>"
|
b += "<readonly/>"
|
||||||
@@ -785,3 +791,16 @@ func randMAC() string {
|
|||||||
fmt.Sprintf(":%x", rand.Intn(255)) +
|
fmt.Sprintf(":%x", rand.Intn(255)) +
|
||||||
fmt.Sprintf(":%x", rand.Intn(255))
|
fmt.Sprintf(":%x", rand.Intn(255))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// expandHome does a simple expansion of the tilde into your $HOME value.
|
||||||
|
func expandHome(p string) (string, error) {
|
||||||
|
// TODO: this doesn't match strings of the form: ~james/...
|
||||||
|
if !strings.HasPrefix(p, "~/") {
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
usr, err := user.Current()
|
||||||
|
if err != nil {
|
||||||
|
return p, fmt.Errorf("can't expand ~ into home directory")
|
||||||
|
}
|
||||||
|
return path.Join(usr.HomeDir, p[len("~/"):]), nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user