-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: client config file listen event file change
- Loading branch information
1 parent
b0e813e
commit d09f7be
Showing
3 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,64 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/fsnotify/fsnotify" | ||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
|
||
"github.com/fgouteroux/acme_manager/restclient" | ||
) | ||
|
||
func WatchCertificate(logger log.Logger, interval time.Duration, configPath string, acmeClient *restclient.Client) { | ||
func WatchLocalCertificate(logger log.Logger, interval time.Duration, acmeClient *restclient.Client) { | ||
// create a new Ticker | ||
tk := time.NewTicker(interval) | ||
|
||
// start the ticker | ||
for range tk.C { | ||
|
||
// Compare and create/update certificate from config file to remote server | ||
CheckCertificate(logger, configPath, acmeClient) | ||
|
||
// check local certificate are up-to-date | ||
CheckAndDeployLocalCertificate(logger, acmeClient) | ||
} | ||
} | ||
|
||
func WatchCertificateUpdate(logger log.Logger, configPath string, acmeClient *restclient.Client) { | ||
watcher, err := fsnotify.NewWatcher() | ||
if err != nil { | ||
_ = level.Error(logger).Log("err", err) | ||
os.Exit(1) | ||
} | ||
defer watcher.Close() | ||
|
||
fileName := filepath.Base(configPath) | ||
if !filepath.IsAbs(configPath) { | ||
fileName = "./" + fileName | ||
} | ||
|
||
// watch the parent dir of the file to catch changes | ||
err = watcher.Add(filepath.Dir(configPath)) | ||
if err != nil { | ||
_ = level.Error(logger).Log("err", err) | ||
os.Exit(1) | ||
} | ||
|
||
for { | ||
select { | ||
case event := <-watcher.Events: | ||
// only work on WRITE events of the original filename | ||
if event.Op&fsnotify.Write == fsnotify.Write && event.Name == fileName { | ||
_ = level.Info(logger).Log("msg", fmt.Sprintf("modified file: %s", configPath)) | ||
|
||
// Compare and create/update certificate from config file to remote server | ||
CheckCertificate(logger, configPath, acmeClient) | ||
|
||
// check local certificate are up-to-date | ||
CheckAndDeployLocalCertificate(logger, acmeClient) | ||
} | ||
case err := <-watcher.Errors: | ||
_ = level.Error(logger).Log("err", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters