Ejemplo n.º 1
0
func ModApp(appName string, config []byte, version int32) error {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/app/" + appName
	exists, _, err := zconn.Exists(zkPath)
	if err != nil {
		return fmt.Errorf("zk: call exist failed %v", err)
	}
	if !exists {
		return fmt.Errorf("zk: %s node not exists", appName)
	} else {
		//update node
		_, err := zconn.Set(zkPath, config, version)
		if err != nil {
			return fmt.Errorf("zk: set failed %v", err)
		}
		return nil
	}
}
Ejemplo n.º 2
0
func DelApp(appName string, version int32) error {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/app/" + appName

	children, err := childrenRecursive(zconn, zkPath, "")
	if err != nil {
		return fmt.Errorf("zk: %v", err)
	}
	for i := len(children) - 1; i >= 0; i-- {
		znode := zkPath + "/" + children[i]
		if err = zconn.Delete(znode, -1); err != nil {
			return fmt.Errorf("zk: %v", err)
		}
	}

	err = zconn.Delete(zkPath, version)
	if err != nil {
		return fmt.Errorf("zk: path delete %v", err)
	}
	return nil
}
Ejemplo n.º 3
0
func ListFailoverRecord() ([]string, error) {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return nil, fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/failover/history"
	exists, _, err := zconn.Exists(zkPath)
	if err != nil {
		return nil, fmt.Errorf("zk: call exist failed %v", err)
	}
	if exists {
		apps, stat, err := zconn.Children(zkPath)
		fmt.Println(*stat)
		if err != nil {
			return nil, fmt.Errorf("zk: call children failed %v", err)
		}
		return apps, nil
	}
	return nil, err
}
Ejemplo n.º 4
0
func AddApp(appName string, config []byte) error {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/app/" + appName
	exists, _, err := zconn.Exists(zkPath)
	if err != nil {
		return fmt.Errorf("zk: call exist failed %v", err)
	}
	if exists {
		return fmt.Errorf("zk: %s node already exists", appName)
	} else {
		//add node
		_, err := zconn.Create(zkPath, config, 0, zookeeper.WorldACL(zookeeper.PermAll))
		if err != nil {
			return fmt.Errorf("zk: create failed %v", err)
		}
		return nil
	}
}
Ejemplo n.º 5
0
func SetApp(appName string, zkAddr string) error {
	appContextName = appName
	zconn, _, err := meta.DialZk(zkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return fmt.Errorf("zk: can't connect: %v", err)
	}

	// get 1st controller
	children, _, err := zconn.Children("/r3/app/" + appName + "/controller")
	if len(children) == 0 {
		return fmt.Errorf("no controller found")
	}
	data, _, err := zconn.Get("/r3/app/" + appName + "/controller/" + children[0])
	var cc meta.ControllerConfig
	err = json.Unmarshal([]byte(data), &cc)
	if err != nil {
		return err
	}
	// fetch app info
	url := fmt.Sprintf("http://%s:%d"+api.AppInfoPath, cc.Ip, cc.HttpPort)
	resp, err := utils.HttpGet(url, nil, 5*time.Second)
	if err != nil {
		return err
	}
	// map to structure
	var res command.AppInfoResult
	err = utils.InterfaceToStruct(resp.Body, &res)
	if err != nil {
		return err
	}
	appConfig = *res.AppConfig
	controllerConfig = *res.Leader

	//fmt.Fprintf(os.Stderr, "[ leader : %s:%d ]\n", controllerConfig.Ip, controllerConfig.HttpPort)
	//fmt.Fprintf(os.Stderr, "[ web    : http://%s:%d/ui/cluster.html ]\n",
	//	controllerConfig.Ip, controllerConfig.HttpPort)
	err = CacheNodes()

	//send RenewToken(nil) request to server
	RenewToken()

	return err
}
Ejemplo n.º 6
0
func GetApp(appName string) ([]byte, int32, error) {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return nil, 0, fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/app/" + appName
	config, stat, err := zconn.Get(zkPath)
	if err != nil {
		return nil, 0, fmt.Errorf("zk: get: %v", err)
	}
	return config, stat.Version, nil
}
Ejemplo n.º 7
0
func GetFailoverRecord(record string) (string, int32, error) {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return "", 0, fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/failover/history/" + record
	config, stat, err := zconn.Get(zkPath)
	if err != nil {
		return "", 0, fmt.Errorf("zk: get: %v", err)
	}
	return string(config), stat.Version, nil
}
Ejemplo n.º 8
0
func ListApp() ([]string, error) {
	zconn, _, err := meta.DialZk(ZkAddr)
	defer func() {
		if zconn != nil {
			zconn.Close()
		}
	}()
	if err != nil {
		return nil, fmt.Errorf("zk: can't connect: %v", err)
	}
	zkPath := "/r3/app"
	exists, _, err := zconn.Exists(zkPath)
	if err != nil {
		return nil, fmt.Errorf("zk: call exist failed %v", err)
	}
	if exists {
		apps, _, err := zconn.Children(zkPath)
		if err != nil {
			return nil, fmt.Errorf("zk: call children failed %v", err)
		}
		return apps, nil
	}
	return nil, err
}