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 }
func removeHostFromCluster(backend env.Backend, params ClusterHostParams) (result interface{}, err error) { conn := backend.Redis() defer conn.Close() hostID := config.Conf.HostID.String() conn.Send("MULTI") conn.Send("SREM", "Cluster:"+params.ClusterID, hostID) conn.Send("SREM", "Host:"+hostID+":clusters", params.ClusterID) _, err = conn.Do("EXEC") scheduler.StopListen(backend, params.ClusterID) return "Removed", err }
func addHostToCluster(backend env.Backend, params ClusterHostParams) (result interface{}, err error) { conn := backend.Redis() defer conn.Close() hostID := config.Conf.HostID.String() conn.Send("MULTI") conn.Send("SADD", "Cluster:"+params.ClusterID, hostID) conn.Send("SADD", "Host:"+hostID+":clusters", params.ClusterID) _, err = conn.Do("EXEC") go scheduler.Listen(backend, params.ClusterID) return "Added", err }
func Listen(backend env.Backend, queue string) { conn := backend.Redis() defer conn.Close() log.Println("Started listening on queue:", queue) for { values, err := redis.Values(conn.Do("BRPOP", queue, 0)) if err != nil { log.Fatal(err) } body := values[1].([]byte) var request Request err = json.Unmarshal(body, &request) if err != nil { fmt.Println("command.Listen: received malformed response. Skipping.") continue } id := request.ID if id == "" { fmt.Println("command.Listen: Received a command without 'id'. Skipping") continue } name := request.Name if name == "" { respondError(conn, id, "Received a command without 'name'") continue } cmd := commands[name] if cmd == nil { respondError(conn, id, "Unknown command") continue } result, err := invokeCommandHandler(backend, cmd.handler, request.Params) if err != nil { respondError(conn, id, err.Error()) } else { respondSuccess(conn, id, result) } } }
func removeVM(backend env.Backend, params *IDParams) (result interface{}, err error) { conn := backend.Redis() defer conn.Close() exists, err := conn.Exists("VM:" + params.ID.String()) if err != nil { return } if !exists { return nil, fmt.Errorf("VM with ID '%s' does not exist", params.ID) } tx := conn.Tx().Begin() tx.Delete(&model.VM{ID: params.ID}) err = tx.Commit() return "Removed", err }
func runVM(backend env.Backend, params *IDParams) (result interface{}, err error) { conn := backend.Redis() defer conn.Close() ok, err := conn.Exists("VM:" + params.ID.String()) if err != nil { return } if !ok { return nil, fmt.Errorf("VM with ID '%s' does not exist", params.ID) } vm := &model.VM{ID: params.ID} err = conn.Get(vm) if err != nil { return } err = backend.Virt().StartVM(vm) return "Started", err }
func Run(backend env.Backend, name string, params map[string]interface{}) (result map[string]interface{}, err error) { cmd := commands[name] if cmd == nil { return nil, fmt.Errorf(`command.Run: Unknown command: "%s"`, name) } request := Request{ Name: name, ID: uuid.NewV4().String(), Params: params, } queue, err := queueByClass(cmd.Class, params) if err != nil { return nil, err } conn := backend.Redis() defer conn.Close() requestBody, err := json.Marshal(request) if err != nil { return nil, err } _, err = conn.Do("LPUSH", queue, requestBody) if err != nil { return nil, err } reply := replyQueue(request.ID) values, err := redis.Values(conn.Do("BRPOP", reply, config.Conf.CommandTimeout)) if err != nil || values[0] == nil { return nil, TimedOut } response := values[1].([]byte) err = json.Unmarshal(response, &result) return }
func updateVM(backend env.Backend, params *UpdateVmParams) (result interface{}, err error) { conn := backend.Redis() defer conn.Close() ok, err := conn.Exists("VM:" + params.ID.String()) if err != nil { return } if !ok { return nil, fmt.Errorf("VM with ID '%s' does not exist", params.ID) } vm := &model.VM{ ID: params.ID, Name: params.Name, } tx := conn.Tx().Begin() tx.Put(vm) err = tx.Commit() return "Updated", err }