예제 #1
0
파일: machine.go 프로젝트: koding/koding
func ChangeMachineState(machineId bson.ObjectId, reason string, state machinestate.State) error {
	query := func(c *mgo.Collection) error {
		return c.Update(
			bson.M{"_id": machineId},
			bson.M{
				"$set": bson.M{
					"status.state":      state.String(),
					"status.modifiedAt": time.Now().UTC(),
					"status.reason":     reason,
				},
			},
		)
	}

	return Mongo.Run(MachinesColl, query)
}
예제 #2
0
파일: machine.go 프로젝트: koding/koding
// CheckAndUpdate state updates only if the given machine id is not used by
// anyone else
func CheckAndUpdateState(machineId bson.ObjectId, state machinestate.State) error {
	query := func(c *mgo.Collection) error {
		return c.Update(
			bson.M{
				"_id": machineId,
				"assignee.inProgress": false, // only update if it's not locked by someone else
			},
			bson.M{
				"$set": bson.M{
					"status.state":      state.String(),
					"status.modifiedAt": time.Now().UTC(),
				},
			},
		)
	}

	return Mongo.Run(MachinesColl, query)
}
예제 #3
0
파일: machine.go 프로젝트: koding/koding
func (bm *BaseMachine) updateMachine(state *DialState, meta interface{}, dbState machinestate.State) error {
	obj := object.MetaBuilder.Build(meta)

	if state != nil && state.KiteURL != "" {
		if bm.RegisterURL != state.KiteURL {
			obj["registerUrl"] = state.KiteURL
		}

		if u, err := url.Parse(state.KiteURL); err == nil && u.Host != "" {
			if host, _, err := net.SplitHostPort(u.Host); err == nil {
				u.Host = host
			}

			if bm.IpAddress != u.Host {
				// TODO(rjeczalik): when path routing is added (#9021) either we
				// change the ipAddress field to more generic endpoint field,
				// or we use here state.KiteURL directly.
				obj["ipAddress"] = u.Host
			}
		}
	}

	if dbState != 0 {
		obj["status.modifiedAt"] = time.Now().UTC()
		obj["status.state"] = dbState.String()
		obj["status.reason"] = "Machine is " + dbState.String()
	}

	if len(obj) == 0 {
		return nil
	}

	bm.Log.Debug("update object for %q: %+v (%# v)", bm.Label, obj, state)

	return modelhelper.UpdateMachine(bm.ObjectId, bson.M{"$set": obj})
}