Example #1
0
func addVM(backend env.Backend, params *AddVmParams) (result interface{}, err error) {
	redis := backend.Redis()
	defer redis.Close()

	nameLock := vmNameLock(params.Name)
	nameLocked, err := redis.Lock(nameLock)
	if err != nil || !nameLocked {
		return nil, fmt.Errorf("VM with name '%s' is already being created", params.Name)
	}
	defer redis.Unlock(nameLock)

	existing, err := redis.GetString("q:VM:name:" + params.Name)
	if existing != "" {
		return nil, fmt.Errorf("VM with name '%s' already exists", params.Name)
	}

	vm := &model.VM{
		ID:        model.NewGUID(),
		Name:      params.Name,
		MemSizeMB: params.MemSizeMB,
	}

	tx := redis.Tx().Begin()
	tx.Put(vm)
	err = tx.Commit()

	return "Created", err
}
Example #2
0
	"log"

	"github.com/matobet/verdi/model"
)

type Config struct {
	HostID         model.GUID `json:"host_id"`
	RedisServer    string     `json:"redis_server"`
	CommandTimeout int        `json:"command_timeout"`

	HTTPPort string `json:"http_port"`
}

var Conf = Config{
	RedisServer:    ":6379",
	HostID:         model.NewGUID(),
	CommandTimeout: 5,
	HTTPPort:       ":4000",
}

func Load() error {
	configFile, err := ioutil.ReadFile("./config.json")
	if err != nil {
		log.Println("File 'config.json' not found. Creating one with default configuration ...")
		configFile, err = json.MarshalIndent(Conf, "", "   ")
		if err != nil {
			log.Fatal("Failed to write config file!")
		}
		return ioutil.WriteFile("./config.json", configFile, 0660)
	}