Exemplo n.º 1
0
func main() {
	interval := flag.IntP("interval", "i", 60, "update interval in seconds")
	ttl := flag.IntP("ttl", "t", 0, "heartbeat ttl in seconds")
	eaddr := flag.StringP("etcd", "e", "http://localhost:4001", "address of etcd machine")
	id := flag.StringP("id", "d", "", "hypervisor id")
	logLevel := flag.StringP("log-level", "l", "info", "log level")
	flag.Parse()

	if err := logx.DefaultSetup(*logLevel); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "logx.DefaultSetup",
			"level": logLevel,
		}).Fatal("failed to set up logging")
	}

	if *ttl == 0 {
		*ttl = 2 * (*interval)
	}

	e := etcd.NewClient([]string{*eaddr})
	c := lochness.NewContext(e)

	hn, err := lochness.SetHypervisorID(*id)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "lochness.SetHypervisorID",
			"id":    id,
		}).Fatal("failed to set hypervisor id")
	}

	hv, err := c.Hypervisor(hn)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "context.Hypervisor",
			"id":    hn,
		}).Fatal("failed to instantiate hypervisor")
	}

	for {
		if err = hv.UpdateResources(); err != nil {
			log.WithFields(log.Fields{
				"error": err,
				"func":  "hv.UpdateResources",
			}).Fatal("failed to update hypervisor resources")
		}
		if err = hv.Heartbeat(time.Duration(*ttl)); err != nil {
			log.WithFields(log.Fields{
				"error": err,
				"func":  "hv.Heartbeat",
				"ttl":   *ttl,
			}).Fatal("failed to beat heart")
		}
		time.Sleep(time.Duration(*interval) * time.Second)
	}
}
Exemplo n.º 2
0
func TestGetHypervisorID(t *testing.T) {
	uuid := "d3cac004-4d89-4f26-9776-97df74a41417"
	id, err := lochness.SetHypervisorID(uuid)
	h.Ok(t, err)
	h.Equals(t, uuid, id)

	id = lochness.GetHypervisorID()
	h.Equals(t, uuid, id)

}
Exemplo n.º 3
0
func TestVerifyOnHV(t *testing.T) {
	defer contextCleanup(t)
	hv := newHypervisor(t)

	// failing test
	uuid := "d3cac004-4d89-4f26-9776-97df74a41417"
	id, err := lochness.SetHypervisorID(uuid)
	h.Ok(t, err)
	h.Equals(t, uuid, id)

	err = hv.VerifyOnHV()
	h.Assert(t, err != nil, "should have got an error")

	// passing
	id, err = lochness.SetHypervisorID(hv.ID)
	h.Ok(t, err)
	h.Equals(t, hv.ID, id)

	err = hv.VerifyOnHV()
	h.Ok(t, err)
}
Exemplo n.º 4
0
func TestSetHypervisorID(t *testing.T) {
	// passing test with uuid
	uuid := "d3cac004-4d89-4f26-9776-97df74a41417"
	id, err := lochness.SetHypervisorID(uuid)
	h.Ok(t, err)
	h.Equals(t, uuid, id)

	id, err = lochness.SetHypervisorID("foo")
	h.Assert(t, err != nil, "should have got an error")
	h.Equals(t, "", id)

	// set with ENV
	uuid = "3e0f2128-0342-49f6-8e5f-ecd401bae99e"
	if err := os.Setenv("HYPERVISOR_ID", uuid); err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"uuid":  uuid,
		}).Error("failed to set HYPERVISOR_ID env variable")
	}
	id, err = lochness.SetHypervisorID("")
	h.Ok(t, err)
	h.Equals(t, uuid, id)

}
Exemplo n.º 5
0
func getHV(hn string, e *etcd.Client, c *ln.Context) *ln.Hypervisor {
	var err error
	hn, err = ln.SetHypervisorID(hn)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "lochness.SetHypervisorID",
		}).Fatal("failed")
	}

	log.WithField("hypervisor_id", hn).Info("using id")

	hv, err := c.Hypervisor(hn)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err,
			"func":  "context.Hypervisor",
		}).Fatal("failed to fetch hypervisor info")
	}
	return hv
}
Exemplo n.º 6
0
func TestGuestCandidates(t *testing.T) {
	c := newContext(t)
	defer contextCleanup(t)

	s := newSubnet(t)
	hv := newHypervisor(t)

	n := c.NewNetwork()
	err := n.Save()
	h.Ok(t, err)
	err = n.AddSubnet(s)
	h.Ok(t, err)

	err = hv.AddSubnet(s, "br0")
	h.Ok(t, err)

	f := c.NewFlavor()
	f.Resources.Memory = 1024
	f.Resources.CPU = 2
	f.Resources.Disk = 8192
	err = f.Save()
	h.Ok(t, err)

	// horrible hack for testing
	hv, err = c.Hypervisor(hv.ID)
	h.Ok(t, err)

	hv.AvailableResources = lochness.Resources{
		Memory: 8192,
		CPU:    4,
		Disk:   65536,
	}

	hv.TotalResources = hv.AvailableResources
	err = hv.Save()
	h.Ok(t, err)

	// cheesy
	_, err = lochness.SetHypervisorID(hv.ID)
	h.Ok(t, err)

	err = hv.Heartbeat(9999 * time.Second)
	h.Ok(t, err)

	g := c.NewGuest()
	g.FlavorID = f.ID
	g.NetworkID = n.ID

	err = g.Save()
	h.Ok(t, err)

	candidates, err := g.Candidates(lochness.DefaultCandidateFunctions...)
	h.Ok(t, err)

	h.Equals(t, 1, len(candidates))

	h.Equals(t, hv.ID, candidates[0].ID)

	// umm what about IP?? we need to reserve an ip on this hv in proper subnet

	err = hv.AddGuest(g)
	h.Ok(t, err)
}