75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package cluster
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.staur.ca/stobbsm/clustvirt/lib/host"
|
|
log "git.staur.ca/stobbsm/simplelog"
|
|
)
|
|
|
|
// ClusterBuilder is used to build a Cluster object, which can then be used
|
|
type ClusterBuilder struct {
|
|
cluster *Cluster
|
|
}
|
|
|
|
// New starts the builder pattern for the Cluster.
|
|
// Sets the default interval time to 30 seconds.
|
|
func New() *ClusterBuilder {
|
|
return &ClusterBuilder{
|
|
cluster: &Cluster{
|
|
interval: time.Duration(time.Second * 30),
|
|
hosts: map[string]*host.Host{},
|
|
defaultURI: host.URI_QEMU_LOCAL,
|
|
},
|
|
}
|
|
}
|
|
|
|
// SetInterval sets the check interval of the Cluster being built.
|
|
func (c *ClusterBuilder) SetInterval(i time.Duration) *ClusterBuilder {
|
|
c.cluster.interval = i
|
|
return c
|
|
}
|
|
|
|
// Build retuns the built cluster
|
|
func (c *ClusterBuilder) Build() *Cluster {
|
|
return c.cluster
|
|
}
|
|
|
|
func (c *ClusterBuilder) AddHost(h string) *ClusterBuilder {
|
|
if _, ok := c.cluster.hosts[h]; ok {
|
|
log.Warn("cluster.AddHost").
|
|
Str("hostname", h).
|
|
Msg("won't overwrite existing host")
|
|
return c
|
|
}
|
|
hc, err := host.ConnectHost(c.cluster.defaultURI, h)
|
|
if err != nil {
|
|
log.Error("cluster.AddHost").
|
|
Str("hostname", h).
|
|
Err(err).
|
|
Msg("failed to connect to host")
|
|
return c
|
|
}
|
|
c.cluster.hosts[h] = hc
|
|
return c
|
|
}
|
|
|
|
func (c *ClusterBuilder) AddHostWithURI(h string, uri *host.URI) *ClusterBuilder {
|
|
if _, ok := c.cluster.hosts[h]; ok {
|
|
log.Warn("cluster.AddHostWithURI").
|
|
Str("hostname", h).
|
|
Msg("won't overwrite existing host")
|
|
return c
|
|
}
|
|
hc, err := host.ConnectHost(uri, h)
|
|
if err != nil {
|
|
log.Error("cluster.AddHostWithURI").
|
|
Str("hostname", h).
|
|
Err(err).
|
|
Msg("failed to connect to host")
|
|
return c
|
|
}
|
|
c.cluster.hosts[h] = hc
|
|
return c
|
|
}
|