Beispiel #1
0
func machineFromMongodb(db *mongodb.MongoDB) (*machines, error) {
	from, err := time.Parse(time.RFC3339, "2015-03-07T13:15:00Z")
	if err != nil {
		return nil, err
	}

	to, err := time.Parse(time.RFC3339, "2015-04-10T07:30:00Z")
	if err != nil {
		return nil, err
	}

	machines := newMachines()
	err = db.Run("jMachines", func(c *mgo.Collection) error {
		var m koding.Machine
		iter := c.Find(
			bson.M{
				"createdAt": bson.M{
					"$gte": from.UTC(),
					"$lt":  to.UTC(),
				},
				// "status.state": machinestate.Stopped.String(),
			},
		).Iter()

		for iter.Next(&m) {
			machines.docs = append(machines.docs, m)
		}

		return iter.Close()
	})

	return machines, err
}
Beispiel #2
0
func deleteDocument(i *ec2.Instance, db *mongodb.MongoDB) error {
	if i == nil {
		return errors.New("nil instance")
	}

	var id bson.ObjectId

	for _, tag := range i.Tags {
		key := strings.ToLower(aws.StringValue(tag.Key))
		value := strings.ToLower(aws.StringValue(tag.Value))

		if key != "koding-machineid" || value == "" {
			continue
		}

		id = bson.ObjectIdHex(value)
		break
	}

	if !id.Valid() {
		return errors.New("unable to find valid jMachine.ObjectId")
	}

	return db.Run(modelhelper.MachinesColl, func(c *mgo.Collection) error { return c.RemoveId(id) })
}
Beispiel #3
0
// Iter accepts mongo and iterOptions and runs the query
func Iter(mongo *mongodb.MongoDB, iterOptions *iterOptions) error {
	if iterOptions.CollectionName == "" {
		return errors.New("Collection name is not set")
	}

	if iterOptions.F == nil {
		return errors.New("Iteration function is not given")
	}
	if iterOptions.Result == nil {
		return errors.New("Datatype is not given")
	}

	return mongo.Run(iterOptions.CollectionName, createQuery(iterOptions))
}
Beispiel #4
0
func machinesFromInstanceId(db *mongodb.MongoDB, instanceId string) (*machines, error) {
	machines := newMachines()
	err := db.Run("jMachines", func(c *mgo.Collection) error {
		var m MachineDocument
		iter := c.Find(bson.M{"meta.instanceId": instanceId}).Iter()

		for iter.Next(&m) {
			machines.docs = append(machines.docs, m)
		}

		return iter.Close()
	})

	return machines, err
}
Beispiel #5
0
func machinesFromUsername(db *mongodb.MongoDB, username string) (*machines, error) {
	machines := newMachines()
	err := db.Run("jMachines", func(c *mgo.Collection) error {
		var m MachineDocument
		iter := c.Find(bson.M{"credential": username}).Iter()

		for iter.Next(&m) {
			machines.docs = append(machines.docs, m)
		}

		return iter.Close()
	})

	return machines, err
}