func (l *DeployLock) Lock() error { if l.locked { return nil } // check if we can lock by checking if any thing in the lock file is a prefix to us path := helper.GetBaseLockPath("deploy") mutex := zookeeper.NewMutex(Zk.Conn, path) if err := mutex.Lock(); err != nil { return err } defer mutex.Unlock() lockedPaths, err := getLockedPaths(path) if err != nil { return err } if allID, ok := lockedPaths[allPath]; ok && allID != "" { return LockConflictError(allID) } for p, conflictID := range lockedPaths { if strings.HasPrefix(l.path, p) { return LockConflictError(conflictID) } } // if no conflicts, register our lock lockedPaths[l.path] = l.id if err := setJson(path, lockedPaths); err != nil { return err } l.locked = true return nil }
func (l *RouterPortsLock) Lock() error { var path string if l.locked { return nil } if l.internal { path = helper.GetBaseLockPath("router_ports_internal") } else { path = helper.GetBaseLockPath("router_ports_external") } l.mutex = zookeeper.NewMutex(Zk.Conn, path) if err := l.mutex.Lock(); err != nil { return err } l.locked = true return nil }
func (l *DeployLock) Unlock() error { if !l.locked { return nil } // remove ourselves from the lock file path := helper.GetBaseLockPath("deploy") mutex := zookeeper.NewMutex(Zk.Conn, path) if err := mutex.Lock(); err != nil { return err } defer mutex.Unlock() lockedPaths, err := getLockedPaths(path) if err != nil { return err } delete(lockedPaths, l.path) if err := setJson(path, lockedPaths); err != nil { return err } l.locked = false return nil }