base code for libvirt connections
This commit is contained in:
parent
7b9c7c57ef
commit
c2d111a928
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
lord.json
|
||||||
|
overlord
|
||||||
|
data/
|
@ -1,3 +1,3 @@
|
|||||||
# overlord
|
# Overlord
|
||||||
|
|
||||||
Full management suite for libvirt and kubernetes clusters!
|
Full management suite for libvirt and kubernetes clusters!
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module bitsnthings.dev/overlord
|
||||||
|
|
||||||
|
go 1.17
|
||||||
|
|
||||||
|
require libvirt.org/go/libvirt v1.7007.0 // indirect
|
4
go.sum
Normal file
4
go.sum
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
libvirt.org/go/libvirt v1.7007.0 h1:pTe6uRhxDpWzGS06SXvgeQXqWcG1aTKRfL+gS5xI9Kw=
|
||||||
|
libvirt.org/go/libvirt v1.7007.0/go.mod h1:1WiFE8EjZfq+FCVog+rvr1yatKbKZ9FaFMZgEqxEJqQ=
|
||||||
|
libvirt.org/libvirt-go v7.4.0+incompatible h1:1gFJevSrEoxx/5nZPB6GaTDdoc902NP2KyVPwQ07Ji4=
|
||||||
|
libvirt.org/libvirt-go v7.4.0+incompatible/go.mod h1:CPoljLoiC2aEw+62g1rZXl2oXAJaNsrq4YCSmJOELek=
|
15
overlord.go
Normal file
15
overlord.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
overlord "bitsnthings.dev/overlord/src"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
log.Println("Starting Overlord.")
|
||||||
|
state := overlord.NewState()
|
||||||
|
state.Setup()
|
||||||
|
log.Println("Stopping Overlord.")
|
||||||
|
state.Stop()
|
||||||
|
}
|
28
src/conf.go
Normal file
28
src/conf.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package overlord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
LibvirtHosts []string
|
||||||
|
PrivateKey string
|
||||||
|
ConfFilePath string
|
||||||
|
LogFilePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (conf *Config) ReadConfig() {
|
||||||
|
flag.StringVar(&conf.ConfFilePath, "c", "lord.json", "Config file path.")
|
||||||
|
flag.StringVar(&conf.LogFilePath, "l", "", "Log file path.")
|
||||||
|
flag.Parse()
|
||||||
|
file, err := os.Open(conf.ConfFilePath)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error opening config file! %s", err)
|
||||||
|
} else {
|
||||||
|
defer file.Close()
|
||||||
|
json.NewDecoder(file).Decode(&conf)
|
||||||
|
}
|
||||||
|
}
|
27
src/libvirt.go
Normal file
27
src/libvirt.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package overlord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
libvirt "libvirt.org/go/libvirt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Libvirt struct {
|
||||||
|
Hosts []*libvirt.Connect
|
||||||
|
Auth libvirt.ConnectAuth
|
||||||
|
Flags libvirt.ConnectFlags
|
||||||
|
}
|
||||||
|
|
||||||
|
func (virt *Libvirt) Connect(cstr string) (*libvirt.Connect, error) {
|
||||||
|
conn, err := libvirt.NewConnect(cstr) //, &virt.Auth, virt.Flags)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error connecting to libvirt host! %s", err)
|
||||||
|
}
|
||||||
|
return conn, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (virt *Libvirt) ConnectMany(cstrs []string) {
|
||||||
|
for _, cstr := range cstrs {
|
||||||
|
virt.Connect(cstr)
|
||||||
|
}
|
||||||
|
}
|
37
src/state.go
Normal file
37
src/state.go
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package overlord
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type State struct {
|
||||||
|
Libvirt Libvirt
|
||||||
|
Config Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewState() State {
|
||||||
|
return State{
|
||||||
|
Libvirt{},
|
||||||
|
Config{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (state *State) Setup() {
|
||||||
|
state.Config.ReadConfig()
|
||||||
|
if state.Config.LogFilePath != "" {
|
||||||
|
file, err := os.OpenFile(state.Config.LogFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Could not open log file! %s", state.Config.LogFilePath)
|
||||||
|
} else {
|
||||||
|
log.SetOutput(file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state.Libvirt.ConnectMany(state.Config.LibvirtHosts)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (state *State) Stop() {
|
||||||
|
for _, conn := range state.Libvirt.Hosts {
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user