Exemple #1
0
func getApp(name string, s cp.Snapshotable) (*App, error) {
	sp := s.GetSnapshot()
	app := storeFromSnapshotable(s).NewApp(name, "", "")

	f, err := sp.GetSnapshot().GetFile(app.dir.Prefix("attrs"), new(cp.JsonCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, `app "%s" not found`, app.Name)
		}
		return nil, err
	}

	value := f.Value.(map[string]interface{})

	app.RepoURL = value["repo-url"].(string)
	app.Stack = value["stack"].(string)
	app.DeployType = value["deploy-type"].(string)

	f, err = app.dir.GetFile(registeredPath, new(cp.StringCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, "registered not found for %s", app.Name)
		}
		return nil, err
	}
	app.Registered, err = parseTime(f.Value.(string))
	if err != nil {
		return nil, err
	}

	return app, nil
}
Exemple #2
0
func getProc(app *App, name string, s cp.Snapshotable) (*Proc, error) {
	p := &Proc{
		dir:  cp.NewDir(app.dir.Prefix(procsPath, name), s.GetSnapshot()),
		Name: name,
		App:  app,
	}

	port, err := p.dir.GetFile(procsPortPath, new(cp.IntCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			exists, _, err := s.GetSnapshot().Exists(p.dir.Name)
			if err != nil {
				return nil, err
			}
			if !exists {
				return nil, errorf(ErrNotFound, `proc "%s" not found for app %s`, name, app.Name)
			}
			return nil, errorf(ErrNotFound, "port not found for %s:%s", app.Name, name)
		}
		return nil, err
	}
	p.Port = port.Value.(int)

	controlPort, err := p.dir.GetFile(procsControlPortPath, new(cp.IntCodec))
	if err != nil {
		if IsErrNotFound(err) {
			p.ControlPort = 0
		} else {
			return nil, err
		}
	} else {
		p.ControlPort = controlPort.Value.(int)
	}

	_, err = p.dir.GetFile(procsAttrsPath, &cp.JsonCodec{DecodedVal: &p.Attrs})
	if err != nil && !cp.IsErrNoEnt(err) {
		return nil, err
	}

	f, err := p.dir.GetFile(registeredPath, new(cp.StringCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, "registered not found for %s:%s", app.Name, name)
		}
		return nil, err
	}
	p.Registered, err = parseTime(f.Value.(string))
	if err != nil {
		return nil, err
	}

	return p, nil
}
Exemple #3
0
func getProcInstanceIds(p *Proc, s cp.Snapshotable) ([]int64, error) {
	sp := s.GetSnapshot()
	revs, err := sp.Getdir(p.dir.Prefix("instances"))
	if err != nil {
		return nil, err
	}
	ids := []int64{}
	for _, rev := range revs {
		iids, err := getInstanceIds(p.App.Name, rev, p.Name, sp)
		if err != nil {
			return nil, err
		}
		ids = append(ids, iids...)
	}
	return ids, nil
}
Exemple #4
0
func getTag(a *App, name string, s cp.Snapshotable) (*Tag, error) {
	t := &Tag{}
	c := &cp.JsonCodec{DecodedVal: t}

	f, err := s.GetSnapshot().GetFile(a.dir.Prefix(tagsPath, name), c)
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, `tag "%s" not found`, name)
		}
		return nil, err
	}

	t.file = f
	t.App = a

	return t, nil
}
Exemple #5
0
func getRunner(addr string, s cp.Snapshotable) (*Runner, error) {
	sp := s.GetSnapshot()
	f, err := sp.GetFile(runnerPath(addr), new(cp.ListCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, "runner '%s' not found", addr)
		}
		return nil, err
	}
	data := f.Value.([]string)
	insIDStr := data[0]
	insID, err := parseInstanceID(insIDStr)
	if err != nil {
		return nil, err
	}

	return storeFromSnapshotable(sp).NewRunner(addr, insID), nil
}
Exemple #6
0
func getHook(app *App, name string, s cp.Snapshotable) (*Hook, error) {
	c := new(cp.JsonCodec)
	c.DecodedVal = &Hook{}

	f, err := s.GetSnapshot().GetFile(app.dir.Prefix(hooksPath, name), c)
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, `hook not found for "%s"`, name)
		}
		return nil, err
	}

	h, ok := f.Value.(*Hook)
	if !ok {
		return nil, errors.New("retrieved file is not a hook")
	}
	h.file = f
	h.App = app

	return h, nil
}
Exemple #7
0
func getInstanceIds(app, rev, proc string, s cp.Snapshotable) (ids Int64Slice, err error) {
	sp := s.GetSnapshot()
	p := procInstancesPath(app, rev, proc)
	exists, _, err := sp.Exists(p)
	if err != nil || !exists {
		return
	}

	dir, err := sp.Getdir(p)
	if err != nil {
		return
	}
	ids = Int64Slice{}
	for _, f := range dir {
		id, e := parseInstanceID(f)
		if e != nil {
			return nil, e
		}
		ids = append(ids, id)
	}
	sort.Sort(ids)
	return
}
Exemple #8
0
func getRevision(app *App, ref string, s cp.Snapshotable) (*Revision, error) {
	r := &Revision{
		dir: cp.NewDir(app.dir.Prefix(revsPath, ref), s.GetSnapshot()),
		App: app,
		Ref: ref,
	}

	f, err := r.dir.GetFile(archiveURLPath, new(cp.StringCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			exists, _, err := s.GetSnapshot().Exists(r.dir.Name)
			if err != nil {
				return nil, err
			}
			if !exists {
				return nil, errorf(ErrNotFound, `revision "%s" not found for app %s`, ref, app.Name)
			}
			return nil, errorf(ErrNotFound, "archive-url not found for %s:%s", app.Name, ref)
		}
		return nil, err
	}
	r.ArchiveURL = f.Value.(string)

	f, err = r.dir.GetFile(registeredPath, new(cp.StringCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, "registered not found for %s:%s", app.Name, ref)
		}
		return nil, err
	}
	r.Registered, err = parseTime(f.Value.(string))
	if err != nil {
		return nil, err
	}

	return r, nil
}
Exemple #9
0
func getEnv(app *App, ref string, s cp.Snapshotable) (*Env, error) {
	e := &Env{
		dir: cp.NewDir(app.dir.Prefix(envsPath, ref), s.GetSnapshot()),
		App: app,
		Ref: ref,
	}

	_, err := e.dir.GetFile(varsPath, &cp.JsonCodec{DecodedVal: &e.Vars})
	if err != nil {
		if cp.IsErrNoEnt(err) {
			exists, _, err := s.GetSnapshot().Exists(e.dir.Name)
			if err != nil {
				return nil, err
			}
			if !exists {
				return nil, errorf(ErrNotFound, `env "%s" not found for app %s`, ref, app.Name)
			}
			return nil, errorf(ErrNotFound, "vars not found for %s#%s", app.Name, ref)
		}
		return nil, err
	}

	f, err := e.dir.GetFile(registeredPath, new(cp.StringCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			err = errorf(ErrNotFound, `registered not found for %s`, ref)
		}
		return nil, err
	}
	e.Registered, err = parseTime(f.Value.(string))
	if err != nil {
		return nil, err
	}

	return e, nil
}
Exemple #10
0
func storeFromSnapshotable(sp cp.Snapshotable) *Store {
	return &Store{sp.GetSnapshot()}
}
Exemple #11
0
func getInstance(id int64, s cp.Snapshotable) (*Instance, error) {
	i := &Instance{
		ID:     id,
		Status: InsStatusPending,
		dir:    cp.NewDir(instancePath(id), s.GetSnapshot()),
	}

	exists, _, err := s.GetSnapshot().Exists(i.dir.Name)
	if err != nil {
		return nil, err
	}
	if !exists {
		return nil, errorf(ErrNotFound, `instance '%d' not found`, id)
	}

	f, err := i.dir.GetFile(startPath, new(cp.ListCodec))
	if cp.IsErrNoEnt(err) {
		// Ignore
	} else if err != nil {
		return nil, err
	} else {
		fields := f.Value.([]string)

		if len(fields) > 0 { // IP
			i.Status = InsStatusClaimed
			i.IP = fields[0]
		}
		if len(fields) > 1 { // Port
			i.Status = InsStatusRunning
			i.Port, err = strconv.Atoi(fields[1])
			if err != nil {
				return nil, errorf(ErrInvalidPort, "invalid port: "+fields[1])
			}
		}
		if len(fields) > 2 { // Hostname
			i.Host = fields[2]
		}
		if len(fields) > 3 { // TelePort
			i.TelePort, err = strconv.Atoi(fields[3])
			if err != nil {
				return nil, errorf(ErrInvalidPort, "invalid teleport: "+fields[3])
			}
		}
	}

	statusStr, _, err := i.dir.Get(statusPath)
	if cp.IsErrNoEnt(err) {
		err = nil
	} else if err == nil {
		i.Status = InsStatus(statusStr)
	} else {
		return nil, err
	}

	if i.Status == InsStatusRunning {
		_, _, err := i.dir.Get(stopPath)
		if err == nil {
			i.Status = InsStatusStopping
		} else if !cp.IsErrNoEnt(err) {
			return nil, err
		}
	}

	f, err = i.dir.GetFile(objectPath, new(cp.ListCodec))
	if err != nil {
		return nil, errorf(ErrNotFound, "object file not found for instance %d", id)
	}

	fields := f.Value.([]string)
	if len(fields) < 3 {
		return nil, errorf(ErrInvalidFile, "object file for %d has %d instead %d fields", id, len(fields), 3)
	}

	i.AppName = fields[0]
	i.RevisionName = fields[1]
	i.ProcessName = fields[2]
	i.Env = fields[3]

	i.Restarts, _, err = i.getRestarts()
	if err != nil {
		return nil, err
	}

	f, err = i.dir.GetFile(registeredPath, new(cp.StringCodec))
	if err != nil {
		return nil, err
	}
	i.Registered, err = parseTime(f.Value.(string))
	if err != nil {
		return nil, err
	}

	f, err = i.claimDir().GetFile(i.IP, new(cp.StringCodec))
	if err != nil {
		if cp.IsErrNoEnt(err) {
			return i, nil
		}
		return nil, err
	}

	i.Claimed, err = parseTime(f.Value.(string))
	if err != nil {
		return nil, err
	}

	return i, nil
}
Exemple #12
0
func waitRunners(s cp.Snapshotable) (cp.Event, error) {
	sp := s.GetSnapshot()
	return sp.Wait(path.Join(runnersPath, "*", "*"))
}