From 4b9782bb2a025d20f8b6768298fdfc29e981833a Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Fri, 19 Apr 2024 23:37:21 -0600 Subject: [PATCH] adding initial toml config fixing URI host struct to work better with configuration --- etc/clustvirt.toml | 17 +++++++-- lib/host/uri.go | 89 +++++++++++++++++++++++++++++++++------------- 2 files changed, 79 insertions(+), 27 deletions(-) diff --git a/etc/clustvirt.toml b/etc/clustvirt.toml index 389d7a6..91317ba 100644 --- a/etc/clustvirt.toml +++ b/etc/clustvirt.toml @@ -1,5 +1,18 @@ # Define the servers that part of the cluster -[connections.localhost] -uri = "qemu:///system" +## daemon sets daemon configuration +[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" diff --git a/lib/host/uri.go b/lib/host/uri.go index a492cab..066773a 100644 --- a/lib/host/uri.go +++ b/lib/host/uri.go @@ -14,55 +14,94 @@ type URI struct { Driver string Transport string Path string + Host string Options []string } // CustomURI create and return a custom URI method, following RFC2396, // 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{ Driver: driver, Transport: transport, Path: path, + Host: host, Options: options, } } -// URIs available to build connections to a libvirt host -var ( - // URI for connecting to a remote QEMU system over SSH - URI_QEMU_SSH_SYSTEM = &URI{Driver: "qemu", Transport: "ssh", Path: "system"} - // URI for connecting to a remote QEMU system over TLS - // Builds the URI qemu:///system - URI_QEMU_TLS_SYSTEM = &URI{Driver: "qemu", Transport: "", Path: "system"} - // URI for connecting to a remote QEMU session over SSH - 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"} +// WithHost sets the hostname as part of a URI. It copies the original URI and returns a new one +func (u *URI) WithHost(host string) *URI { + return &URI{ + Driver: u.Driver, + Transport: u.Transport, + Path: u.Path, + Host: host, + Options: u.Options, + } +} - // URI for connecting to a remote XEN system with SSH - URI_XEN_SSH_SYSTEM = &URI{Driver: "xen", Transport: "ssh", Path: "system"} - // URI for connecting to a remote XEN system over TLS - URI_XEN_TLS_SYSTEM = &URI{Driver: "xen", Transport: "", Path: "system"} -) +// IsRemote returns true if the URI references a remote trasnport (ssh, tcp or tls) +func (u *URI) IsRemote() bool { + switch u.Transport { + 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 -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 optlen := len(u.Options) - sb.WriteString(u.Driver) - if u.Transport != "" { + sb.WriteString(driver) + if transport != "" { sb.WriteRune('+') sb.WriteString(u.Transport) } sb.WriteString("://") - if h != "" { - sb.WriteString(h) + if host != "" { + sb.WriteString(host) } sb.WriteRune('/') - sb.WriteString(u.Path) + sb.WriteString(path) if optlen > 0 { sb.WriteRune('?')