func replaceConfig(e *etcdm.EtcdManager, nc *cluster.NilConfig) error { lsi, _, err := e.GetLeaderSentinelInfo() if lsi == nil { return fmt.Errorf("leader sentinel info not available") } ncj, err := json.Marshal(nc) if err != nil { return fmt.Errorf("failed to marshall config: %v", err) } req, err := http.NewRequest("PUT", fmt.Sprintf("http://%s:%s/config/current", lsi.ListenAddress, lsi.Port), bytes.NewReader(ncj)) if err != nil { return fmt.Errorf("cannot create request: %v", err) } req.Header.Set("Content-Type", "application/json") client := &http.Client{} res, err := client.Do(req) if err != nil { return fmt.Errorf("error setting config: %v", err) } if res.StatusCode != http.StatusOK { return fmt.Errorf("error setting config: leader sentinel returned non ok code: %s", res.Status) } return nil }
func getConfig(e *etcdm.EtcdManager) (*cluster.NilConfig, error) { cv, _, err := e.GetClusterView() if err != nil { return nil, fmt.Errorf("cannot get clusterview: %v", err) } if cv == nil { return nil, fmt.Errorf("no clusterview available") } cfg := cv.Config if cfg == nil { return nil, nil } return cfg, nil }
func WaitClusterViewMaster(master string, e *etcdm.EtcdManager, timeout time.Duration) error { start := time.Now() for time.Now().Add(-timeout).Before(start) { cv, _, err := e.GetClusterView() if err != nil { goto end } if cv != nil { if cv.Master == master { return nil } } end: time.Sleep(2 * time.Second) } return fmt.Errorf("timeout") }