88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.nevets.tech/Steven/certman/app"
|
|
"git.nevets.tech/Steven/certman/client"
|
|
"git.nevets.tech/Steven/certman/common"
|
|
)
|
|
|
|
type Daemon struct{}
|
|
|
|
func (d *Daemon) Init() {
|
|
fmt.Println("Starting CertManager in client mode...")
|
|
if err := app.LoadDomainConfigs(); err != nil {
|
|
log.Fatalf("Error loading domain configs: %v", err)
|
|
}
|
|
d.Tick()
|
|
}
|
|
|
|
func (d *Daemon) Tick() {
|
|
fmt.Println("tick!")
|
|
|
|
config := app.Config()
|
|
localDomainConfigs := app.DomainStore().Snapshot()
|
|
|
|
for domainStr, domainConfig := range localDomainConfigs {
|
|
if !domainConfig.Domain.Enabled {
|
|
continue
|
|
}
|
|
|
|
certsDir := common.CertsDir(config, domainConfig, domainStr)
|
|
|
|
// Short-circuit when the local copy already matches the remote HEAD.
|
|
// Only useful once the server has provisioned the repo; otherwise
|
|
// the RemoteCommitHash call returns ErrRepoNotFound and we skip
|
|
// this tick entirely (nothing to pull yet).
|
|
if domainConfig.Internal.RepoExists {
|
|
localHash, err := client.LocalCommitHash(certsDir)
|
|
if err != nil {
|
|
fmt.Printf("Error reading local hash for %s: %v\n", domainStr, err)
|
|
}
|
|
remoteHash, err := client.RemoteCommitHash(config, domainConfig, domainStr)
|
|
if err != nil {
|
|
if errors.Is(err, common.ErrRepoNotFound) {
|
|
fmt.Printf("Remote repo not yet provisioned for %s; skipping\n", domainStr)
|
|
continue
|
|
}
|
|
fmt.Printf("Error getting remote hash for %s: %v\n", domainStr, err)
|
|
continue
|
|
}
|
|
if localHash != "" && localHash == remoteHash {
|
|
fmt.Printf("Domain %s is up to date. Skipping...\n", domainStr)
|
|
continue
|
|
}
|
|
}
|
|
|
|
url := common.RepoURL(config, domainConfig, domainStr)
|
|
ws := common.NewGitWorkspace(domainStr, url)
|
|
if err := common.CloneRepo(ws, config); err != nil {
|
|
fmt.Printf("Error cloning domain repo %s: %v\n", domainStr, err)
|
|
continue
|
|
}
|
|
|
|
if err := client.DecryptAndWriteCertificates(certsDir, domainConfig, ws); err != nil {
|
|
fmt.Printf("Error decrypting certificates for %s: %v\n", domainStr, err)
|
|
continue
|
|
}
|
|
if err := client.UpdateSymlinks(domainStr, domainConfig, certsDir); err != nil {
|
|
fmt.Printf("Error updating symlinks for %s: %v\n", domainStr, err)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
func (d *Daemon) Reload() {
|
|
fmt.Println("Reloading configs...")
|
|
if err := app.LoadDomainConfigs(); err != nil {
|
|
fmt.Printf("Error loading domain configs: %v\n", err)
|
|
}
|
|
}
|
|
|
|
func (d *Daemon) Stop() {
|
|
fmt.Println("Shutting down client")
|
|
}
|