add --init option for explicit cluster reset

This commit is contained in:
Leo Antunes 2019-03-27 22:25:14 +01:00
parent 694fd4dd86
commit 0165b7a504
2 changed files with 13 additions and 7 deletions

View File

@ -83,6 +83,7 @@ All options can be passed either as command-line flags or environment variables:
|---|---|---|---| |---|---|---|---|
| `--cluster-key KEY` | WESHER_CLUSTER_KEY | shared key for cluster membership; must be 32 bytes base64 encoded; will be generated if not provided | autogenerated/loaded | | `--cluster-key KEY` | WESHER_CLUSTER_KEY | shared key for cluster membership; must be 32 bytes base64 encoded; will be generated if not provided | autogenerated/loaded |
| `--join HOST,...` | WESHER_JOIN | comma separated list of hostnames or IP addresses to existing cluster members; if not provided, will attempt resuming any known state or otherwise wait for further members | | | `--join HOST,...` | WESHER_JOIN | comma separated list of hostnames or IP addresses to existing cluster members; if not provided, will attempt resuming any known state or otherwise wait for further members | |
| `--init` | WESHER_INIT | whether to explicitly (re)initialize the cluster; any known state from previous runs will be forgotten | `false` |
| `--bind-addr ADDR` | WESHER_BIND_ADDR | IP address to bind to for cluster membership | autodetected | | `--bind-addr ADDR` | WESHER_BIND_ADDR | IP address to bind to for cluster membership | autodetected |
| `--cluster-port PORT` | WESHER_CLUSTER_PORT | port used for membership gossip traffic (both TCP and UDP); must be the same across cluster | `7946` | | `--cluster-port PORT` | WESHER_CLUSTER_PORT | port used for membership gossip traffic (both TCP and UDP); must be the same across cluster | `7946` |
| `--wireguard-port PORT` | WESHER_WIREGUARD_PORT | port used for wireguard traffic (UDP); must be the same across cluster | `51820` | | `--wireguard-port PORT` | WESHER_WIREGUARD_PORT | port used for wireguard traffic (UDP); must be the same across cluster | `51820` |

View File

@ -43,7 +43,10 @@ const statePath = "/var/lib/wesher/state.json"
func newCluster(config *config, wg *wgState) (*cluster, error) { func newCluster(config *config, wg *wgState) (*cluster, error) {
clusterKey := config.ClusterKey clusterKey := config.ClusterKey
state := loadState() state := &ClusterState{}
if !config.Init {
loadState(state)
}
if len(clusterKey) == 0 { if len(clusterKey) == 0 {
clusterKey = state.ClusterKey clusterKey = state.ClusterKey
} }
@ -223,19 +226,21 @@ func (c *cluster) saveState() error {
return ioutil.WriteFile(statePath, stateOut, 0700) return ioutil.WriteFile(statePath, stateOut, 0700)
} }
func loadState() *ClusterState { func loadState(cs *ClusterState) {
content, err := ioutil.ReadFile(statePath) content, err := ioutil.ReadFile(statePath)
if err != nil { if err != nil {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
logrus.Warnf("could not open state in %s: %s", statePath, err) logrus.Warnf("could not open state in %s: %s", statePath, err)
} }
return &ClusterState{} return
} }
s := &ClusterState{} // avoid partially unmarshalled content by using a temp var
if err := json.Unmarshal(content, s); err != nil { csTmp := &ClusterState{}
if err := json.Unmarshal(content, csTmp); err != nil {
logrus.Warnf("could not decode state: %s", err) logrus.Warnf("could not decode state: %s", err)
return &ClusterState{} // avoid partially unmarshalled content } else {
*cs = *csTmp
} }
return s return
} }