-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (107 loc) · 3.02 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"os"
"runtime/debug"
"time"
"github.com/carlmjohnson/versioninfo"
"github.com/coreos/go-systemd/v22/daemon"
"github.com/docker/docker/client"
"github.com/godbus/dbus/v5"
"github.com/holoplot/go-avahi"
"github.com/kelseyhightower/envconfig"
"ldddns.arnested.dk/internal/log"
)
var (
//go:embed LICENSE.md
license string
// Version string to be set at compile time via command line (-ldflags "-X main.version=1.2.3").
version string
)
// Config is the configuration used to create hostnams for containers.
//
//nolint:lll
type Config struct {
HostnameLookup []string `default:"env:VIRTUAL_HOST,containerName" json:"HostnameLookup" split_words:"true"`
IgnoreDockerComposeOneoff bool `default:"true" json:"IgnoreDockerComposeOneoff" split_words:"true"`
}
func main() {
version := getVersion()
if len(os.Args) <= 1 || os.Args[1] != "start" {
fmt.Fprintf(os.Stderr, "ldddns %s - https://ldddns.arnested.dk\n\n%s", version, license)
return
}
log.Logf(log.PriNotice, "Starting ldddns %s...", version)
defer log.Logf(log.PriNotice, "Stopped ldddns %s.", version)
// Setup stuff.
var config Config
err := envconfig.Process("ldddns", &config)
if err != nil {
panic(fmt.Errorf("could not read environment config: %w", err))
}
docker, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
panic(fmt.Errorf("cannot create docker client: %w", err))
}
defer docker.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
conn, err := dbus.SystemBus()
if err != nil {
panic(fmt.Errorf("cannot get dbus system bus: %w", err))
}
defer conn.Close()
avahiServer, err := avahi.ServerNew(conn)
if err != nil {
panic(fmt.Errorf("avahi new failed: %w", err))
}
defer avahiServer.Close()
egs := newEntryGroups(avahiServer)
started := time.Now()
err = sdNotify(daemon.SdNotifyReady, version, config)
if err != nil {
panic(fmt.Errorf("notifying systemd we're ready: %w", err))
}
// Do the magic work.
handleExistingContainers(ctx, config, docker, egs)
listen(ctx, config, docker, egs, started)
err = sdNotify(daemon.SdNotifyStopping, version, config)
if err != nil {
log.Logf(log.PriErr, "notifying systemd we're shutting down: %v", err)
}
}
func sdNotify(state string, version string, config Config) error {
cfg, err := json.Marshal(config)
if err != nil {
return fmt.Errorf("could not marshal config as JSON: %w", err)
}
_, err = daemon.SdNotify(true, fmt.Sprintf(
"%s\nSTATUS=version %s; %s",
state,
version,
cfg,
))
if err != nil {
return fmt.Errorf("failed to notify systemd: %w", err)
}
return nil
}
func getVersion() string {
if version == "" {
version = versioninfo.Revision
if versioninfo.DirtyBuild {
version += "-dirty"
}
}
buildinfo, ok := debug.ReadBuildInfo()
if ok && (buildinfo != nil) && (buildinfo.Main.Version != "(devel)") {
version = buildinfo.Main.Version
}
return version
}