This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
139 lines (107 loc) · 2.92 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
135
136
137
138
139
package main
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/azrod/updateip/pkg/config"
"github.com/azrod/updateip/pkg/metrics"
uip_aws "github.com/azrod/updateip/pkg/providers/aws"
uip_cloudflare "github.com/azrod/updateip/pkg/providers/cloudflare"
uip_ovh "github.com/azrod/updateip/pkg/providers/ovh"
)
var (
m *metrics.Metrics
Paws uip_aws.Paws
Povh uip_ovh.Povh
PCloudflare uip_cloudflare.PCloudflare
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// Load config
c, err := config.LoadConfig()
if err != nil {
log.Fatal().Err(err).Msg("Failed to load config")
}
if c.Log.Humanize {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339})
} else {
log.Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
}
// Parse loglevel
if c.Log.Level == "" {
c.Log.Level = "info"
}
if l, err := zerolog.ParseLevel(c.Log.Level); err != nil {
log.Fatal().Err(err).Msg("cannot parse log level")
} else {
zerolog.SetGlobalLevel(l)
log.Info().Msgf("Log level set to %s", l.String())
}
log.Info().Msg("Starting UpdateIP")
if c.Providers.AWSAccount.Enable {
Paws = uip_aws.Paws{
Record: c.Providers.AWSAccount.Record,
Secret: c.Providers.AWSAccount.Secret,
}
if err := Paws.NewClient(); err != nil {
log.Fatal().Err(err).Interface("config", Paws).Msg("Failed to setup AWS client")
}
go func() {
if err := Paws.Run(); err != nil {
log.Error().Err(err).Interface("config", Paws).Msg("Error on module AWS")
}
}()
}
if c.Providers.OVHAccount.Enable {
Povh = uip_ovh.Povh{
Record: c.Providers.OVHAccount.Record,
Secret: c.Providers.OVHAccount.Secret,
}
if err := Povh.NewClient(); err != nil {
log.Fatal().Err(err).Msg("Failed to setup OVH client")
}
go func() {
if err := Povh.Run(); err != nil {
log.Error().Err(err).Msg("Error on module OVH")
}
}()
}
if c.Providers.CLOUDFLAREAccount.Enable {
PCloudflare = uip_cloudflare.PCloudflare{
Record: c.Providers.CLOUDFLAREAccount.Record,
Secret: c.Providers.CLOUDFLAREAccount.Secret,
}
if err := PCloudflare.NewClient(); err != nil {
log.Fatal().Err(err).Msg("Failed to setup Cloudflare client")
}
go func() {
if err := PCloudflare.Run(); err != nil {
log.Error().Err(err).Msg("Error on module Cloudflare")
}
}()
}
if c.Metrics.Enable {
log.Info().Msg("Starting Metrics Server")
m = metrics.Init(c.Metrics)
if c.Providers.AWSAccount.Enable {
m.RegisterPkg(Paws.RegistryMetrics())
}
if c.Providers.CLOUDFLAREAccount.Enable {
m.RegisterPkg(PCloudflare.RegistryMetrics())
}
if c.Providers.OVHAccount.Enable {
m.RegisterPkg(PCloudflare.RegistryMetrics())
}
m.Run()
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
func() {
<-sigs
log.Info().Msg("Shutting down")
os.Exit(1)
}()
}