adding initial toml config

fixing URI host struct to work better with configuration
This commit is contained in:
Matthew Stobbs 2024-04-19 23:37:21 -06:00
parent 5be2744d10
commit 4b9782bb2a
2 changed files with 79 additions and 27 deletions

View File

@ -1,5 +1,18 @@
# Define the servers that part of the cluster # Define the servers that part of the cluster
[connections.localhost] ## daemon sets daemon configuration
uri = "qemu:///system" [daemon]
loglevel = "debug"
runpath = "/run/clustvirt.pid"
## connections are the VM hosts to connect to as part of the cluster
[connections]
# interval is the time in seconds to check host connections
interval = 60
[connections.goblin.uri]
driver = "qemu"
transport = "local"
host = "localhost"
path = "system"
user = "stobbsm" user = "stobbsm"

View File

@ -14,55 +14,94 @@ type URI struct {
Driver string Driver string
Transport string Transport string
Path string Path string
Host string
Options []string Options []string
} }
// CustomURI create and return a custom URI method, following RFC2396, // CustomURI create and return a custom URI method, following RFC2396,
// keeping in mind that the hostname will be inserted between the transport and path // keeping in mind that the hostname will be inserted between the transport and path
func CustomURI(driver, transport, path string, options ...string) *URI { func CustomURI(driver, transport, path, host string, options ...string) *URI {
return &URI{ return &URI{
Driver: driver, Driver: driver,
Transport: transport, Transport: transport,
Path: path, Path: path,
Host: host,
Options: options, Options: options,
} }
} }
// URIs available to build connections to a libvirt host // WithHost sets the hostname as part of a URI. It copies the original URI and returns a new one
var ( func (u *URI) WithHost(host string) *URI {
// URI for connecting to a remote QEMU system over SSH return &URI{
URI_QEMU_SSH_SYSTEM = &URI{Driver: "qemu", Transport: "ssh", Path: "system"} Driver: u.Driver,
// URI for connecting to a remote QEMU system over TLS Transport: u.Transport,
// Builds the URI qemu://<host:port>/system Path: u.Path,
URI_QEMU_TLS_SYSTEM = &URI{Driver: "qemu", Transport: "", Path: "system"} Host: host,
// URI for connecting to a remote QEMU session over SSH Options: u.Options,
URI_QEMU_SSH_SESSION = &URI{Driver: "qemu", Transport: "ssh", Path: "session"} }
// URI for connecting to a local QEMU system over a UNIX socket }
URI_QEMU_UNIX_SYSTEM = &URI{Driver: "qemu", Transport: "unix", Path: "system"}
// URI for connecting to a remote QEMU system over unsecured TCP
URI_QEMU_TCP_SYSTEM = &URI{Driver: "qemu", Transport: "tcp", Path: "system"}
// URI for connecting to a remote XEN system with SSH // IsRemote returns true if the URI references a remote trasnport (ssh, tcp or tls)
URI_XEN_SSH_SYSTEM = &URI{Driver: "xen", Transport: "ssh", Path: "system"} func (u *URI) IsRemote() bool {
// URI for connecting to a remote XEN system over TLS switch u.Transport {
URI_XEN_TLS_SYSTEM = &URI{Driver: "xen", Transport: "", Path: "system"} case "ssh":
) fallthrough
case "tls":
fallthrough
case "tcp":
return true
default:
return false
}
}
// validTransport makes sure the value of transport if valid or empty. If the transport
// isn't remote, it returns an empty string
func (u *URI) validTransport() string {
if u.IsRemote() {
return u.Transport
}
return ""
}
func (u *URI) validDriver() bool {
switch u.Driver {
case "qemu":
fallthrough
case "xen":
return true
default:
return false
}
}
// ConnectionString takes a host name to interpolate into a URI and returns the string // ConnectionString takes a host name to interpolate into a URI and returns the string
func (u *URI) ConnectionString(h string) string { func (u *URI) ConnectionString() string {
// Normalize the variables
var driver, transport, path, host string
if u.IsRemote() {
transport = u.Transport
}
if u.Host != "localhost" {
host = u.Host
}
// TODO: validate driver, path
driver = u.Driver
path = u.Path
var sb strings.Builder var sb strings.Builder
optlen := len(u.Options) optlen := len(u.Options)
sb.WriteString(u.Driver) sb.WriteString(driver)
if u.Transport != "" { if transport != "" {
sb.WriteRune('+') sb.WriteRune('+')
sb.WriteString(u.Transport) sb.WriteString(u.Transport)
} }
sb.WriteString("://") sb.WriteString("://")
if h != "" { if host != "" {
sb.WriteString(h) sb.WriteString(host)
} }
sb.WriteRune('/') sb.WriteRune('/')
sb.WriteString(u.Path) sb.WriteString(path)
if optlen > 0 { if optlen > 0 {
sb.WriteRune('?') sb.WriteRune('?')