Esempio n. 1
0
func (cs *ContentStore) ingestPaths(ref string) (string, string, lockfile.Lockfile, error) {
	cref := filepath.Clean(ref)
	if cref != ref {
		return "", "", "", errors.Errorf("invalid path after clean")
	}

	fp := filepath.Join(cs.root, "ingest", ref)

	// ensure we don't escape root
	if !strings.HasPrefix(fp, cs.root) {
		return "", "", "", errors.Errorf("path %q escapes root", ref)
	}

	// ensure we are just a single path component
	if ref != filepath.Base(fp) {
		return "", "", "", errors.Errorf("ref must be a single path component")
	}

	lockfilePath := filepath.Join(fp, "lock")
	lock, err := lockfile.New(lockfilePath)
	if err != nil {
		return "", "", "", errors.Wrapf(err, "error creating lockfile %v", lockfilePath)
	}

	return fp, filepath.Join(fp, "data"), lock, nil
}
Esempio n. 2
0
// Aquires a lock on the folder
func acquireLock(path string, seconds int) (*lockfile.Lockfile, error) {
	lock, err := lockfile.New(path)
	if err != nil {
		return nil, fmt.Errorf("Failed to create lock \"%s\" (%s)", path, err)
	}

	// Aquire the lock. Keep trying the lock until we get it
	attempts := 0
	for true {
		err := lock.TryLock()
		if err != nil {
			if err == lockfile.ErrBusy {
				// Keey track of how many times we tried to get the
				// lock
				attempts += 1
				if attempts > seconds {
					return nil, fmt.Errorf("Gave up trying to aquire a lock on \"%s\" (%s)", path, err)
				}

				// Try again in a second
				commentf("Could not aquire lock on \"%s\" (%s)", path, err)
				commentf("Trying again in 1 second...")
				time.Sleep(1 * time.Second)
			} else {
				return nil, err
			}
		} else {
			break
		}
	}

	return &lock, nil
}
func NewHasHTemplateManager(basepath string) (*HashTemplateManager, error) {
	res := &HashTemplateManager{
		byPath:   make(map[string]Template),
		basepath: basepath,
	}
	var err error
	err = os.MkdirAll(basepath, 0755)
	if err != nil {
		return nil, err
	}
	res.lock, err = lockfile.New(path.Join(basepath, "global.lock"))
	if err != nil {
		return nil, err
	}

	if err := res.tryLock(); err != nil {
		return nil, err
	}
	defer res.unlock()

	err = res.load()
	if err != nil && err != io.EOF {
		return nil, err
	}
	return res, nil

}
Esempio n. 4
0
// Release will remove the lockfile.
func Release(lockPath string) error {
	lock, err := lockfile.New(lockPath)
	if err != nil {
		return err
	}

	return lock.Unlock()
}
Esempio n. 5
0
// TryAcquire tries to acquire the lock at `lockPath`.
// It will not retry if it fails.
func TryAcquire(lockPath string) error {
	lock, err := lockfile.New(lockPath)
	if err != nil {
		return err
	}

	return lock.TryLock()
}
Esempio n. 6
0
func (runner *Runner) lockPidFile(pidFilePath string) (err error) {
	log.Println("lockPidFile():", pidFilePath)
	runner.lockfile, err = lockfile.New(pidFilePath)
	if err == nil {
		err = runner.lockfile.TryLock()
	}
	return
}
Esempio n. 7
0
func UnlockCatlight() error {
	lock, err := lockfile.New(CatlightLockFile)
	if err != nil {
		return err
	}

	return lock.Unlock()
}
Esempio n. 8
0
func getAppLock() {
	var err error
	appLock, err = lockfile.New(filepath.Join(GetAppDir(), "run.lock"))
	if err != nil {
		LogErr.Fatalf("Cannot init app lock: %v\n", err)
	}

	err = appLock.TryLock()
	if err != nil {
		LogErr.Fatalf("Cannot obtain app lock: %v\n", err)
	}
}
Esempio n. 9
0
func (p *instanceManager) TryLockPID(instanceName string) (err error) {
	pidPath := getSpiritHome(p.spiritName) + "/" + instanceName

	if lckFile, e := lockfile.New(pidPath); e != nil {
		err = ERR_SPIRIT_WRITE_PID_FILE_ERROR.New(errors.Params{"err": e})
		return
	} else if e := lckFile.TryLock(); e != nil {
		err = ERR_SPIRIT_LOCK_PID_FILE_FAILED.New(errors.Params{"name": instanceName, "err": e})
		return
	}

	return
}
Esempio n. 10
0
func (p *instanceManager) ReadPID(instanceName string) (pid int, err error) {
	pidPath := getSpiritHome(p.spiritName) + "/" + instanceName

	if lckFile, e := lockfile.New(pidPath); e != nil {
		err = ERR_SPIRIT_WRITE_PID_FILE_ERROR.New(errors.Params{"err": e})
		return
	} else if ps, e := lckFile.GetOwner(); e != nil {
		err = ERR_SPIRIT_GET_INSTANCE_PID_FAILED.New(errors.Params{"name": instanceName, "err": e})
		return
	} else {
		pid = ps.Pid
	}

	return
}
Esempio n. 11
0
func lock(num int) (lockfile.Lockfile, error) {
	fileName := fmt.Sprintf(LOCK_FMT, num)
	lock, err := lockfile.New(fileName)
	if err != nil {
		return lock, err
	}

	err = lock.TryLock()

	// Error handling is essential, as we only try to get the lock.
	if err != nil {
		return lock, err
	}

	return lock, nil
}
Esempio n. 12
0
// Create a new lock file. Ensures that only one of these command runs
// concurrently on this machine.  Also cleans up stale locks of dead instances.
func createLock(killRunning bool) (lockfile.Lockfile, error) {
	var zero lockfile.Lockfile
	filename := filepath.Join(os.TempDir(), "periodicnoise-"+
		monitoringEvent, monitoringEvent+".lock")

	dirname := filepath.Dir(filename)
	if err := privateSubdir(dirname); err != nil {
		return zero, err
	}

	lock, err := lockfile.New(filename)
	if err != nil {
		return zero, err
	}

	if err := lock.TryLock(); err != nil {
		if err != lockfile.ErrBusy {
			return lock, err
		}

		if killRunning {
			process, err := lock.GetOwner()
			if err != nil {
				return zero, err
			}
			if err := process.Kill(); err != nil {
				return zero, err
			}
			// FIXME(mlafeldt) Remove old lock. Not really safe as
			// the process might not be killed yet. lockfile.Unlock()
			// should check if it actually holds the lock.
			if err := lock.Unlock(); err != nil {
				return zero, err
			}
			// Create new lock
			if err := lock.TryLock(); err != nil {
				return zero, err
			}
		} else {
			return zero, err
		}
	}

	// Lock successfully created
	return lock, err
}
Esempio n. 13
0
func checkLock() bool {
	flock, err := lockfile.New("/tmp/wmserve.now.lck")
	lock = flock
	if err != nil {
		log.Printf("Cannot init lock. reason: %v", err)
		panic(err)
	}
	err = lock.TryLock()
	// Error handling is essential, as we only try to get the lock.
	if err != nil {
		log.Printf("Cannot lock %v reason: %v", lock, err)
		panic(err)
	}

	//
	return true
}
Esempio n. 14
0
/* {{{ func (mux *Mux) Run()
 * Run ogo application.
 */
func (mux *Mux) Run() {
	defer func() {
		if err := recover(); err != nil {
			WriteMsg("App crashed with error:", err)
			for i := 1; ; i++ {
				_, file, line, ok := runtime.Caller(i)
				if !ok {
					break
				}
				WriteMsg(file, line)
			}
			//panic要输出到console
			fmt.Println("App crashed with error:", err)
		}
	}()
	if env.Daemonize {
		godaemon.MakeDaemon(&godaemon.DaemonAttr{})
	}
	//check&write pidfile, added by odin
	dir := filepath.Dir(env.PidFile)
	if _, err := os.Stat(dir); err != nil {
		if os.IsNotExist(err) {
			//mkdir
			if err := os.Mkdir(dir, 0755); err != nil {
				panic(err)
			}
		}
	}
	if l, err := lockfile.New(env.PidFile); err == nil {
		if le := l.TryLock(); le != nil {
			panic(le)
		}
	} else {
		panic(err)
	}

	Warn("Starting Ogo(http mode) on: %s", env.Port)

	// in goji appengine mode (tags --appengine)
	goji.Serve()

}
Esempio n. 15
0
func LockCatlight() error {
	lock, err := lockfile.New(CatlightLockFile)
	if err != nil {
		return err
	}

	for {
		err := lock.TryLock()
		if err == lockfile.ErrBusy {
			time.Sleep(500 * time.Millisecond)
			continue
		}

		if err != nil {
			return err
		}

		return nil
	}
}
Esempio n. 16
0
// Acquire tries to lock the lock file at `lockPath`.
// If it is already locked it will re-try after a short timeout.
func Acquire(lockPath string) error {
	lock, err := lockfile.New(lockPath)
	if err != nil {
		return err
	}

	for {
		if err := lock.TryLock(); err != nil {
			if err == lockfile.ErrBusy {
				time.Sleep(250 * time.Millisecond)
				continue
			} else {
				return err
			}
		}

		break
	}

	return nil
}
Esempio n. 17
0
func acquireLock() lockfile.Lockfile {
	lock, err := lockfile.New(conf.Lockfile)
	if err == lockfile.ErrNeedAbsPath {
		log.Panicf(
			`Error: you must specify an absolute path for the lockfile.
	("%s" is not an absolute path)`, conf.Lockfile)
	}
	if err != nil {
		log.Panicf("Something wrong happened (%v)", err)
	}

	err = lock.TryLock()
	if err == lockfile.ErrBusy {
		log.Panicf("Another instance of gorbs is already running. Aborting.")
	}
	if err != nil {
		log.Panicf("Unable to acquire the lockfile (%v). Aborting.", err)
	}

	return lock
}
Esempio n. 18
0
func Daemonize(logFilePath, pidFilePath string) {

	if os.Getenv("__DAEMON_CWD") == "" {
		cwd, err := os.Getwd()
		if err != nil {
			fmt.Fprintln(os.Stderr, "Cannot determine working directory: %v", err)
			os.Exit(1)
		}
		os.Setenv("__DAEMON_CWD", cwd)
	}

	logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		fmt.Fprintln(os.Stderr, "Could not open local log file: %v", err)
		os.Exit(1)
	}

	stdout, stderr, err := godaemon.MakeDaemon(&godaemon.DaemonAttr{CaptureOutput: true})
	if err != nil {
		fmt.Fprintln(os.Stderr, "Could not Daemonize: %v", err)
		os.Exit(1)
	}

	go func() {
		io.Copy(logFile, stdout)
	}()
	go func() {
		io.Copy(logFile, stderr)
	}()

	lock, err := lockfile.New(pidFilePath)
	err = lock.TryLock()
	if err != nil {
		fmt.Println("Cannot lock \"%v\": %v", lock, err)
		os.Exit(1)
	}

}
Esempio n. 19
0
/* {{{ func (mux *Mux) Run()
 * Run ogo application.
 */
func (mux *Mux) Run() {
	defer func() {
		if err := recover(); err != nil {
			WriteMsg("App crashed with error:", err)
			for i := 1; ; i++ {
				_, file, line, ok := runtime.Caller(i)
				if !ok {
					break
				}
				WriteMsg(file, line)
			}
			//panic要输出到console
			fmt.Println("App crashed with error:", err)
		}
	}()
	if env.Daemonize {
		godaemon.MakeDaemon(&godaemon.DaemonAttr{})
	}
	//check&write pidfile, added by odin
	dir := filepath.Dir(env.PidFile)
	if _, err := os.Stat(dir); err != nil {
		if os.IsNotExist(err) {
			//mkdir
			if err := os.Mkdir(dir, 0755); err != nil {
				panic(err)
			}
		}
	}
	if l, err := lockfile.New(env.PidFile); err == nil {
		if le := l.TryLock(); le != nil {
			panic(le)
		}
	} else {
		panic(err)
	}

	var mainErr error

	Debug("will run worker: %v", env.Worker)
	if worker, ok := DMux.Workers[env.Worker]; ok {
		vw := reflect.New(worker.WorkerType)
		execWorker, ok := vw.Interface().(WorkerInterface)
		if !ok {
			panic("worker is not WorkerInterface")
		}

		//Init
		execWorker.Init(DMux, env.Worker)

		//Main
		mainErr = execWorker.Main()
	} else {
		mainErr = fmt.Errorf("not found worker: %s", env.Worker)
	}

	if mainErr != nil {
		panic(mainErr)
	}
	//睡一段时间再结束
	time.Sleep(1000 * time.Microsecond)
}
Esempio n. 20
0
func pull() error {
	logTime := strconv.FormatInt(time.Now().Unix(), 10)
	gitDir := filepath.Join(*configfilesDir, ".git")
	fi, err := os.Stat(gitDir)
	if err != nil {
		return fmt.Errorf("cannot stat %q: %v\n", err)
	}
	if !*force && time.Since(fi.ModTime()) < 1*time.Hour {
		log.Printf("%q was modified within the last hour, skipping update\n", gitDir)
		return nil
	}

	// Skip update if the remote git points to the same commit as the local
	// repository.
	log.Printf("Resolving HEAD of %q\n", *readOnlyGitRemote)
	remoteHead, err := gitResolve(*readOnlyGitRemote)
	if err != nil {
		log.Printf("Skipping this update, as resolving git HEAD failed: %v\n", err)
		f, err := os.Create(filepath.Join(*configfilesDir, ".logs", logTime+".skipped"))
		if err != nil {
			return err
		}
		return f.Close()
	}

	cmd := exec.Command("git", "log", "--format=%H", "--max-count=1")
	cmd.Dir = *configfilesDir
	out, err := cmd.Output()
	if err != nil {
		return fmt.Errorf("Could not run git log: %v\n", err)
	}
	if !*force && strings.TrimSpace(string(out)) == remoteHead {
		log.Printf("Already up-to-date (both local and remote are at %s)\n", remoteHead)
		return nil
	}

	// Lock to guard against multiple updater processes running at the same
	// time, potentially stepping on eath other’s toes.
	lockPath := filepath.Join(*configfilesDir, "update_lock")
	lock, err := lockfile.New(lockPath)
	if err != nil {
		return err
	}
	if err := lock.TryLock(); err != nil {
		return fmt.Errorf("Could not lock %q: %v", lockPath, err)
	}
	defer lock.Unlock()

	// Copy configfiles to a temporary working directory (|twd|), so that we
	// can update it without overwriting configfiles.
	abs, err := filepath.Abs(*configfilesDir)
	if err != nil {
		return err
	}
	twd := filepath.Join(filepath.Dir(abs), "."+filepath.Base(abs)+".TMP")

	log.Printf("Cloning configfiles from %q to %q\n", abs, twd)

	if err := os.RemoveAll(twd); err != nil {
		return err
	}
	if err := os.MkdirAll(filepath.Dir(abs), 0755); err != nil {
		return err
	}

	if err := copyAll(abs, twd); err != nil {
		return err
	}

	if err := os.MkdirAll(filepath.Join(*configfilesDir, ".logs"), 0755); err != nil {
		return err
	}

	errLogPath := filepath.Join(*configfilesDir, ".logs", logTime+".failure")
	errLog, err := os.Create(errLogPath)
	if err != nil {
		return err
	}
	defer errLog.Close()

	log.Printf("Pulling new changes via git\n")

	if err := pullStashed(twd, errLog); err != nil {
		return err
	}

	log.Printf("Moving new files to %q\n", *configfilesDir)

	if err := moveGitFiles(twd, *configfilesDir, errLog); err != nil {
		return err
	}

	successLogPath := errLogPath[:len(errLogPath)-len(".failure")] + ".success"
	if err := os.Rename(errLogPath, successLogPath); err != nil {
		return err
	}

	log.Printf("Update done successfully\n")

	if err := copyAll(filepath.Join(twd, ".git"), filepath.Join(*configfilesDir, ".git")); err != nil {
		return err
	}

	return judgeErrors()
}
func NewSymlinkManager(a WebotsArchive) (*SymlinkWebotsManager, error) {
	var err error
	res := &SymlinkWebotsManager{
		archive: a,
	}

	res.basepath, res.workpath, res.installpath, err = symlinkManagerPath()
	if err != nil {
		return nil, err
	}

	res.templates, err = NewHasHTemplateManager(path.Join(res.workpath, "templates"))
	if err != nil {
		return nil, err
	}

	res.usedpath = path.Join(res.workpath, "used")
	res.lock, err = lockfile.New(path.Join(res.workpath, "global.lock"))
	if err != nil {
		return nil, err
	}

	err = res.listInstalled()
	if err != nil {
		return nil, err
	}
	err = res.listUsed()
	if err != nil {
		return nil, err
	}

	//checks that we have the right gid
	res.gid, err = getGid("webots-manager")
	if err != nil {
		return nil, err
	}

	found := false
	userGroups, err := os.Getgroups()
	if err != nil {
		return nil, err
	}

	for _, g := range userGroups {
		if g == res.gid {
			found = true
			break
		}
	}

	if found == false {
		return nil, fmt.Errorf("Current use is not in 'webots-manager' group")
	}

	webotsHome := os.Getenv("WEBOTS_HOME")
	if len(webotsHome) == 0 {
		fmt.Printf("WEBOTS_HOME is not set, please consider exporting WEBOTS_HOME=%s", res.installpath)
	} else if webotsHome != res.installpath {
		return nil, fmt.Errorf("Invalid WEBOTS_HOME=%s, please use WEBOTS_HOME=%s", webotsHome, res.installpath)
	}

	return res, nil
}