-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
99 lines (93 loc) · 2.65 KB
/
main.go
File metadata and controls
99 lines (93 loc) · 2.65 KB
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
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/go-acme/lego/v4/log"
"github.com/optionfactory/legopfa/certmanager"
"github.com/optionfactory/legopfa/dnsupdaters"
"github.com/optionfactory/legopfa/httpserverhandlers"
)
var version string
func main() {
thisExecutable, err := os.Executable()
if err != nil {
log.Fatalf("cannot get executable: %v", err)
}
thisExecutableName := filepath.Base(thisExecutable)
log.Infof("%s version %s", thisExecutableName, version)
if len(os.Args) != 2 {
log.Fatalf("usage: %s <configuration_path>", thisExecutableName)
}
configuration, err := loadJson[certmanager.Configuration](os.Args[1])
if err != nil {
log.Fatalf("error: %v", err)
}
cm, err := certmanager.MakeCertManager(configuration)
if err != nil {
log.Fatalf("error: %v", err)
}
httpServerHandler, err := httpserverhandlers.ByName(configuration.HttpServerHandler)
if err != nil {
log.Fatalf("error: %v", err)
}
isHttpServerRunning := httpServerHandler.IsRunning()
log.Infof("http server is running: %v", isHttpServerRunning)
if !isHttpServerRunning && len(configuration.DnsRecordsToUpdate) > 0 {
log.Infof("updating dns records: %v", configuration.DnsRecordsToUpdate)
err = dnsupdaters.FromConfiguration(configuration).Update()
if err != nil {
log.Fatalf("error: %v", err)
}
log.Infof("dns records updated")
}
needsCreationOrRenewal, daysUntilExpiration, err := cm.NeedsCreationOrRenewal()
if err != nil {
log.Fatalf("error: %v", err)
}
if !needsCreationOrRenewal {
log.Infof("certificate expires in %d days, threshold is 30 days: no renewal.", daysUntilExpiration)
log.Infof("done.")
return
}
account, err := cm.CreateAccount()
if err != nil {
log.Fatalf("error: %v", err)
}
client, err := cm.CreateClient(account, isHttpServerRunning)
if err != nil {
log.Fatalf("error: %v", err)
}
err = cm.CreateOrRenewCertificate(client)
if err != nil {
log.Fatalf("error: %v", err)
}
log.Infof("certificate renewed")
if httpServerHandler.IsRunning() {
log.Infof("reloading %s", httpServerHandler.ServerName())
err = httpServerHandler.ReloadConfiguration()
if err != nil {
log.Fatalf("error: %v", err)
}
}
log.Infof("done.")
}
func loadJson[K any](path string) (*K, error) {
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("Could not deserialize %s: %v", path, err)
}
defer file.Close()
var deserialized K
decoder := json.NewDecoder(file)
err = decoder.Decode(&deserialized)
if err != nil {
return nil, fmt.Errorf("Could not deserialize %s: %v", path, err)
}
return &deserialized, nil
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}