Skip to content

Commit

Permalink
Patch:Read config modtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rugwirobaker committed Aug 13, 2021
1 parent 62be7b0 commit e25d4da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions flyctl/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
ConfigInstaller = "installer"
BuildKitNodeID = "buildkit_node_id"

ConfigWireGuardState = "wire_guard_state"
ConfigWireGuardStateTimestamp = "wire_guard_state_timestamp"
ConfigWireGuardState = "wire_guard_state"
// ConfigWireGuardStateTimestamp = "wire_guard_state_timestamp"

ConfigRegistryHost = "registry_host"
)
Expand Down
2 changes: 1 addition & 1 deletion flyctl/flyctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func GetAPIToken() string {

}

var writeableConfigKeys = []string{ConfigAPIToken, ConfigInstaller, ConfigWireGuardState, BuildKitNodeID, ConfigWireGuardStateTimestamp}
var writeableConfigKeys = []string{ConfigAPIToken, ConfigInstaller, ConfigWireGuardState, BuildKitNodeID}

func SaveConfig() error {
BackgroundTaskWG.Add(1)
Expand Down
9 changes: 4 additions & 5 deletions internal/wireguard/wg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"regexp"
"strings"
"time"

badrand "math/rand"

Expand Down Expand Up @@ -132,7 +131,7 @@ func getWireGuardStateForOrg(orgSlug string) (*wg.WireGuardState, error) {

func setWireGuardState(s WireGuardStates) error {
viper.Set(flyctl.ConfigWireGuardState, s)
viper.Set(flyctl.ConfigWireGuardStateTimestamp, time.Now())
// viper.Set(flyctl.ConfigWireGuardStateTimestamp, time.Now())
if err := flyctl.SaveConfig(); err != nil {
return errors.Wrap(err, "error saving config file")
}
Expand Down Expand Up @@ -180,6 +179,6 @@ func PruneInvalidPeers(apiClient *api.Client) error {
return setWireGuardState(state)
}

func LastWireGuardStateChange() time.Time {
return viper.GetTime(flyctl.ConfigWireGuardStateTimestamp)
}
// func LastWireGuardStateChange() time.Time {
// return viper.GetTime(flyctl.ConfigWireGuardStateTimestamp)
// }
17 changes: 14 additions & 3 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/getsentry/sentry-go"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/flyctl"
"github.com/superfly/flyctl/internal/wireguard"
"github.com/superfly/flyctl/pkg/wg"
"github.com/superfly/flyctl/terminal"
Expand All @@ -42,14 +43,19 @@ type handlerFunc func(net.Conn, []string) error
func (s *Server) handle(c net.Conn) {
defer c.Close()

latestChange := wireguard.LastWireGuardStateChange()
info, err := os.Stat(flyctl.ConfigFilePath())
if err != nil {
s.errLog(c, "can't stat config file: %s", err)
return
}

latestChange := info.ModTime()

if latestChange.After(s.currentChange) {
s.currentChange = latestChange
err := s.validateTunnels()
if err != nil {
s.errLog(c, "can't validate peers: %s", err)
return
}
log.Printf("config change at: %s", s.currentChange.String())
}
Expand Down Expand Up @@ -114,7 +120,12 @@ func NewServer(path string, ctx *cmdctx.CmdContext) (*Server, error) {

l.SetUnlinkOnClose(true)

latestChange := wireguard.LastWireGuardStateChange()
info, err := os.Stat(flyctl.ConfigFilePath())
if err != nil {
return nil, fmt.Errorf("can't stat config file: %s", err)
}

latestChange := info.ModTime()

s := &Server{
listener: l,
Expand Down

0 comments on commit e25d4da

Please sign in to comment.