fixes need to happen for refactor

This commit is contained in:
Matthew Stobbs 2024-04-21 12:42:59 -06:00
parent 325d87ba27
commit 626fa6c733
4 changed files with 38 additions and 16 deletions

View File

@ -19,7 +19,7 @@ func New() *ClusterBuilder {
cluster: &Cluster{ cluster: &Cluster{
interval: time.Duration(time.Second * 30), interval: time.Duration(time.Second * 30),
hosts: map[string]*host.Host{}, 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 return c.cluster
} }
func (c *ClusterBuilder) DefaultHostURI(uri *host.URI) *ClusterBuilder {
c.cluster.defaultURI = uri
return c
}
func (c *ClusterBuilder) AddHost(h string) *ClusterBuilder { func (c *ClusterBuilder) AddHost(h string) *ClusterBuilder {
if _, ok := c.cluster.hosts[h]; ok { if _, ok := c.cluster.hosts[h]; ok {
log.Warn("cluster.AddHost"). log.Warn("cluster.AddHost").

View File

@ -68,6 +68,9 @@ var restartCmd = &cobra.Command{
Msg("unable to restart") Msg("unable to restart")
os.Exit(1) os.Exit(1)
} }
log.Info("restartCommand").
Any("ps.SysUsage", ps.SysUsage()).
Send()
}, },
} }

View File

@ -94,7 +94,7 @@ func ConnectHost(uri *URI, host string) (*Host, error) {
// connect creates a host connection // connect creates a host connection
func (h *Host) connect() error { func (h *Host) connect() error {
var err 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 return err
} }

View File

@ -1,6 +1,7 @@
package host package host
import ( import (
"errors"
"strings" "strings"
log "git.staur.ca/stobbsm/simplelog" log "git.staur.ca/stobbsm/simplelog"
@ -18,18 +19,22 @@ type URI struct {
Options []string 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 // 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{ return &URI{
Driver: driver, Driver: driver,
Transport: transport, Transport: transport,
Path: path, Path: path,
Host: host,
Options: options, 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 // 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 { func (u *URI) WithHost(host string) *URI {
return &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 // validTransport makes sure the value of transport if valid or empty. If the transport
// isn't remote, it returns an empty string // isn't remote, it returns an empty string
func (u *URI) validTransport() string { func (u *URI) validTransport() bool {
if u.IsRemote() { 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 { func (u *URI) validDriver() bool {
@ -69,6 +81,8 @@ func (u *URI) validDriver() bool {
case "qemu": case "qemu":
fallthrough fallthrough
case "xen": case "xen":
fallthrough
case "lxc":
return true return true
default: default:
return false return false
@ -79,13 +93,23 @@ func (u *URI) validDriver() bool {
func (u *URI) ConnectionString() string { func (u *URI) ConnectionString() string {
// Normalize the variables // Normalize the variables
var driver, transport, path, host string var driver, transport, path, host string
if u.IsRemote() { if !u.validTransport() {
transport = u.Transport 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 host = u.Host
} }
// TODO: validate driver, path // 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 driver = u.Driver
path = u.Path path = u.Path