func NewInstance(name string, cfg *model.Config) *Instance {
	datadir := storage.GetInstancesDataPath(name, cfg)
	storage.CreateDir(datadir)
	instanceLogger, err := NewInstanceLogger(name, datadir)
	if err != nil {
		logrus.Errorf("Failed to initialize instance %s logger at %s", name, datadir)
		panic(err)
	}
	i := Instance{
		instance:   model.NewInstance(name),
		cfg:        cfg,
		store:      storage.GetStore(instanceCollection, cfg),
		logger:     instanceLogger,
		logContext: NewInstanceContext(),
	}
	i.instance.Port = NodeRedPort
	return &i
}
NewInstance new instance api
package api
import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"github.com/muka/redzilla/docker"
	"github.com/muka/redzilla/model"
	"github.com/muka/redzilla/storage"
	"github.com/sirupsen/logrus"
)
const instanceCollection = "instances"
var instancesCache = make(map[string]*Instance)
func ListInstances(cfg *model.Config) (*[]model.Instance, error) {
	store := storage.GetStore(instanceCollection, cfg)
	jsonlist, err := store.List()
	if err != nil {
		return nil, fmt.Errorf("store.List: %s", err)
	}
	list := make([]model.Instance, 0)
	for _, jsonstr := range jsonlist {
		item := new(model.Instance)
		err = json.Unmarshal([]byte(jsonstr), item)
		if err != nil {
			return nil, err
		}
		list = append(list, *item)
	}
	logrus.Debugf("Found %d instances", len(list))
	return &list, err
}
func GetInstance(name string, cfg *model.Config) *Instance {
	if _, ok := instancesCache[name]; !ok {
		instancesCache[name] = NewInstance(name, cfg)
	}
	return instancesCache[name]
}