-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
118 lines (111 loc) · 2.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2020 Lennart Espe
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/digitalocean/go-libvirt"
"gopkg.in/yaml.v2"
)
type config struct {
Socket string `yaml:"socket"`
Network string `yaml:"network"`
Interval int64 `yaml:"interval"`
Hostfile string `yaml:"hostfile"`
Domain string `yaml:"domain"`
}
func run() error {
var (
cfg config
cfgpath = os.Args[1]
)
cfgdata, err := ioutil.ReadFile(cfgpath)
if err != nil {
return fmt.Errorf("read config: %v", err)
}
if err := yaml.Unmarshal(cfgdata, &cfg); err != nil {
return fmt.Errorf("parse config: %v", err)
}
c, err := net.DialTimeout("unix", cfg.Socket, 2*time.Second)
if err != nil {
return fmt.Errorf("open socket: %v", err)
}
l := libvirt.New(c)
if err := l.Connect(); err != nil {
return fmt.Errorf("failed to connect: %v", err)
}
defer func() {
log.Printf("shutting down daemon")
if err := l.Disconnect(); err != nil {
fmt.Printf("failed to disconnect: %v", err)
}
}()
v, err := l.Version()
if err != nil {
return fmt.Errorf("failed to retrieve libvirt version: %v", err)
}
log.Printf("connected to libvirt %s", v)
n, err := l.NetworkLookupByName(cfg.Network)
if err != nil {
return fmt.Errorf("failed to lookup network: %v", err)
}
log.Printf("found network %s", n.Name)
// Enter watch loop
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-sigs:
return nil
case <-time.After(time.Millisecond * time.Duration(cfg.Interval)):
}
leases, _, err := l.NetworkGetDhcpLeases(n, nil, -1, 0)
if err != nil {
log.Printf("failed to get leases: %v", err)
}
addrs := make(map[string]libvirt.NetworkDhcpLease)
for _, lease := range leases {
if len(lease.Hostname) == 0 {
continue
}
host := lease.Hostname[0]
if lease.Expirytime > addrs[host].Expirytime {
addrs[host] = lease
}
}
var buf bytes.Buffer
for host, lease := range addrs {
fqdn := host
if cfg.Domain != "" {
fqdn += "." + cfg.Domain
}
fmt.Fprintf(&buf, "%s\t%s\n", lease.Ipaddr, fqdn)
}
if err := ioutil.WriteFile(cfg.Hostfile, buf.Bytes(), 0644); err != nil {
log.Printf("failed to write hostfile: %v", err)
}
}
}
func main() {
if err := run(); err != nil {
log.Fatalf("error: %v", err)
}
}