| 33 | } |
| 34 | |
| 35 | func NewEtcd(machines []string, log kite.Logger) *Etcd { |
| 36 | if machines == nil || len(machines) == 0 { |
| 37 | machines = []string{"http://127.0.0.1:2379"} |
| 38 | } |
| 39 | |
| 40 | cfg := etcd.Config{ |
| 41 | Endpoints: machines, |
| 42 | Transport: etcd.DefaultTransport, |
| 43 | HeaderTimeoutPerRequest: time.Second, |
| 44 | } |
| 45 | |
| 46 | client, err := etcd.New(cfg) |
| 47 | if err != nil { |
| 48 | panic("cannot connect to etcd cluster: " + strings.Join(machines, ",")) |
| 49 | } |
| 50 | |
| 51 | e := &Etcd{ |
| 52 | client: NewKeysAPILogger(etcd.NewKeysAPI(client), log), |
| 53 | log: log, |
| 54 | } |
| 55 | |
| 56 | // Create our prefix as a directory if it does not exist |
| 57 | _, err = e.client.Get(context.Background(), KitesPrefix, nil) |
| 58 | if err == nil { |
| 59 | return e |
| 60 | } |
| 61 | |
| 62 | _, err = e.client.Set( |
| 63 | context.Background(), |
| 64 | KitesPrefix, |
| 65 | "", |
| 66 | &etcd.SetOptions{ |
| 67 | Dir: true, |
| 68 | PrevExist: etcd.PrevIgnore, |
| 69 | }, |
| 70 | ) |
| 71 | if err != nil { |
| 72 | log.Fatal("Could not create KitesPrefix %q: %v", KitesPrefix, err) |
| 73 | } |
| 74 | |
| 75 | return e |
| 76 | } |
| 77 | |
| 78 | func (e *Etcd) Delete(k *protocol.Kite) error { |
| 79 | etcdKey := KitesPrefix + k.String() |