From 626fa6c733322808db47ddf81d6a5e052d610c7b Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Sun, 21 Apr 2024 12:42:59 -0600 Subject: [PATCH] fixes need to happen for refactor --- cluster/builder.go | 7 +---- cmd/clusterctl/cmd/daemon_restart.go | 3 ++ lib/host/lib.go | 2 +- lib/host/uri.go | 42 ++++++++++++++++++++++------ 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/cluster/builder.go b/cluster/builder.go index 412f1c8..b152de6 100644 --- a/cluster/builder.go +++ b/cluster/builder.go @@ -19,7 +19,7 @@ func New() *ClusterBuilder { cluster: &Cluster{ interval: time.Duration(time.Second * 30), hosts: map[string]*host.Host{}, - defaultURI: host.URI_QEMU_SSH_SYSTEM, + defaultURI: host.URI_QEMU_LOCAL, }, } } @@ -35,11 +35,6 @@ func (c *ClusterBuilder) Build() *Cluster { return c.cluster } -func (c *ClusterBuilder) DefaultHostURI(uri *host.URI) *ClusterBuilder { - c.cluster.defaultURI = uri - return c -} - func (c *ClusterBuilder) AddHost(h string) *ClusterBuilder { if _, ok := c.cluster.hosts[h]; ok { log.Warn("cluster.AddHost"). diff --git a/cmd/clusterctl/cmd/daemon_restart.go b/cmd/clusterctl/cmd/daemon_restart.go index 4c64915..c32ec81 100644 --- a/cmd/clusterctl/cmd/daemon_restart.go +++ b/cmd/clusterctl/cmd/daemon_restart.go @@ -68,6 +68,9 @@ var restartCmd = &cobra.Command{ Msg("unable to restart") os.Exit(1) } + log.Info("restartCommand"). + Any("ps.SysUsage", ps.SysUsage()). + Send() }, } diff --git a/lib/host/lib.go b/lib/host/lib.go index 54696a4..8f31d2c 100644 --- a/lib/host/lib.go +++ b/lib/host/lib.go @@ -94,7 +94,7 @@ func ConnectHost(uri *URI, host string) (*Host, error) { // connect creates a host connection func (h *Host) connect() error { var err error - h.conn, err = libvirt.NewConnect(h.uri.ConnectionString(h.HostName)) + h.conn, err = libvirt.NewConnect(h.uri.WithHost(h.HostName).ConnectionString()) return err } diff --git a/lib/host/uri.go b/lib/host/uri.go index 066773a..7b97570 100644 --- a/lib/host/uri.go +++ b/lib/host/uri.go @@ -1,6 +1,7 @@ package host import ( + "errors" "strings" log "git.staur.ca/stobbsm/simplelog" @@ -18,18 +19,22 @@ type URI struct { Options []string } -// CustomURI create and return a custom URI method, following RFC2396, +// Define 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, host string, options ...string) *URI { +func Define(driver, transport, path string, options ...string) *URI { return &URI{ Driver: driver, Transport: transport, Path: path, - Host: host, Options: options, } } +var ( + URI_QEMU_LOCAL = Define("qemu", "local", "system") + URI_QEMU_SSH = Define("qemu", "ssh", "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{ @@ -57,11 +62,18 @@ func (u *URI) IsRemote() bool { // 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 { +func (u *URI) validTransport() bool { if u.IsRemote() { - return u.Transport + return true + } + switch u.Transport { + case "unix": + fallthrough + case "local": + return true + default: + return false } - return "" } func (u *URI) validDriver() bool { @@ -69,6 +81,8 @@ func (u *URI) validDriver() bool { case "qemu": fallthrough case "xen": + fallthrough + case "lxc": return true default: return false @@ -79,13 +93,23 @@ func (u *URI) validDriver() bool { func (u *URI) ConnectionString() string { // Normalize the variables var driver, transport, path, host string - if u.IsRemote() { - transport = u.Transport + if !u.validTransport() { + log.Panic("host.URI.ConnectionString"). + Err(errors.New("invalid transport")). + Str("transport", u.Transport). + Send() } - if u.Host != "localhost" { + transport = u.Transport + if (u.Host == "localhost" && u.IsRemote()) || u.Host != "localhost" { host = u.Host } // TODO: validate driver, path + if !u.validDriver() { + log.Panic("host.URI.ConnectionString"). + Err(errors.New("invalid driver")). + Str("driver", u.Driver). + Send() + } driver = u.Driver path = u.Path