Exemplo n.º 1
0
func (dt *DaoTest) TestDao_VhostRegistryCreate(t *C) {

	_, err := registry.VHostRegistry(dt.zkConn)
	t.Assert(err, IsNil)

	//test idempotence
	_, err = registry.VHostRegistry(dt.zkConn)
	t.Assert(err, IsNil)
}
Exemplo n.º 2
0
func (z *zkf) CheckRunningVHost(vhostName, serviceID string) error {
	rootBasedConnection, err := zzk.GetLocalConnection("/")
	if err != nil {
		return err
	}

	vr, err := zkregistry.VHostRegistry(rootBasedConnection)
	if err != nil {
		glog.Errorf("Error getting vhost registry: %v", err)
		return err
	}

	vhostEphemeralNodes, err := vr.GetVHostKeyChildren(rootBasedConnection, vhostName)
	if err != nil {
		glog.Errorf("GetVHostKeyChildren failed %v: %v", vhostName, err)
		return err
	}

	if len(vhostEphemeralNodes) > 0 {
		if vhost := vhostEphemeralNodes[0]; vhost.ServiceID != serviceID {
			err := fmt.Errorf("virtual host %s is already running under service %s", vhostName, vhost.ServiceID)
			return err
		}
	}

	return nil
}
Exemplo n.º 3
0
func (dt *DaoTest) TestDao_VhostRegistrySet(t *C) {

	vr, err := registry.VHostRegistry(dt.zkConn)
	t.Assert(err, IsNil)

	// TODO: add tests for ephemeral nodes and remove vr.SetEphemeral(false)
	vr.SetEphemeral(false)

	vep := registry.VhostEndpoint{}
	vep.EndpointName = "epn_test"
	vep.ServiceID = "svc_id"
	vep.HostIP = "testip"
	path, err := vr.SetItem(dt.zkConn, "testKey", vep)
	t.Assert(err, IsNil)
	t.Assert(path, Not(Equals), 0)

	var newVep *registry.VhostEndpoint
	newVep, err = vr.GetItem(dt.zkConn, path)
	t.Assert(err, IsNil)
	t.Assert(vep, NotNil)
	//remove version for equals
	newVep.SetVersion(nil)
	t.Assert(vep, Equals, *newVep)

	//test double add
	glog.Infof("%+v", vep)
	path, err = vr.SetItem(dt.zkConn, "testKey", vep)
	t.Assert(err, IsNil)
}
Exemplo n.º 4
0
// registerExportedEndpoints registers exported ApplicationEndpoints with zookeeper
func (c *Controller) registerExportedEndpoints() error {
	// TODO: accumulate the errors so that some endpoints get registered
	conn, err := zzk.GetLocalConnection("/")
	if err != nil {
		return err
	}

	endpointRegistry, err := registry.CreateEndpointRegistry(conn)
	if err != nil {
		glog.Errorf("Could not get EndpointRegistry. Endpoints not registered: %v", err)
		return err
	}

	var vhostRegistry *registry.VhostRegistry
	vhostRegistry, err = registry.VHostRegistry(conn)
	if err != nil {
		glog.Errorf("Could not get vhost registy. Endpoints not registered: %v", err)
		return err
	}

	c.vhostZKPaths = []string{}
	c.exportedEndpointZKPaths = []string{}

	// register exported endpoints
	for key, exportList := range c.exportedEndpoints {
		for _, export := range exportList {
			endpoint := export.endpoint
			for _, vhost := range export.vhosts {
				epName := fmt.Sprintf("%s_%v", export.endpointName, export.endpoint.InstanceID)
				//delete any existing vhost that hasn't been cleaned up
				vhostEndpoint := registry.NewVhostEndpoint(epName, endpoint)
				if paths, err := vhostRegistry.GetChildren(conn, vhost); err != nil {
					glog.V(1).Infof("error trying to clean out previous vhosts", err)
				} else {
					glog.V(1).Infof("cleaning vhost paths %v", paths)
					//clean paths
					for _, path := range paths {
						if vep, err := vhostRegistry.GetItem(conn, path); err != nil {
							glog.V(1).Infof("Could not read %s", path)
						} else {
							glog.V(4).Infof("checking instance id of %#v equal %v", vep, c.options.Service.InstanceID)
							if strconv.Itoa(vep.InstanceID) == c.options.Service.InstanceID {
								glog.V(1).Infof("Deleting stale vhost registration for %v at %v ", vhost, path)
								conn.Delete(path)
							}
						}
					}
				}

				// TODO: avoid set if item already exist with data we want
				var path string
				if path, err = vhostRegistry.SetItem(conn, vhost, vhostEndpoint); err != nil {
					glog.Errorf("could not register vhost %s for %s: %v", vhost, epName, err)
					return err
				} else {
					glog.Infof("Registered vhost %s for %s at %s", vhost, epName, path)
					c.vhostZKPaths = append(c.vhostZKPaths, path)
				}

			}
			//delete any exisiting endpoint that hasn't been cleaned up
			if paths, err := endpointRegistry.GetChildren(conn, c.tenantID, export.endpoint.Application); err != nil {
				glog.V(1).Infof("error trying to clean previous endpoints: %s", err)
			} else {
				glog.V(1).Infof("cleaning endpoint paths %v", paths)
				//clean paths
				for _, path := range paths {
					if epn, err := endpointRegistry.GetItem(conn, path); err != nil {
						glog.V(1).Infof("Could not read %s", path)
					} else {
						glog.V(4).Infof("checking instance id of %#v equal %v", epn, c.options.Service.InstanceID)
						if strconv.Itoa(epn.InstanceID) == c.options.Service.InstanceID {
							glog.V(1).Infof("Deleting stale endpoint registration for %v at %v ", export.endpointName, path)
							conn.Delete(path)
						}
					}
				}
			}
			glog.Infof("Registering exported endpoint[%s]: %+v", key, endpoint)
			path, err := endpointRegistry.SetItem(conn, registry.NewEndpointNode(c.tenantID, export.endpoint.Application, c.hostID, c.dockerID, endpoint))
			if err != nil {
				glog.Errorf("  unable to add endpoint: %+v %v", endpoint, err)
				return err
			}
			c.exportedEndpointZKPaths = append(c.exportedEndpointZKPaths, path)
			glog.V(1).Infof("  endpoint successfully added to path: %s", path)
		}
	}
	return nil
}