func SendCommand(cmd *Command, path *tree_graph.Path, cb func(*tree_event.Event, Command) bool) (err tree_lib.TreeError) { // If command ID not set just setting random string if len(cmd.ID) == 0 { cmd.ID = tree_lib.RandomString(10) } var ( cmd_data []byte ) cmd_data, err.Err = ffjson.Marshal(cmd) if !err.IsNull() { err.From = tree_lib.FROM_SEND_COMMAND return } e := &tree_event.Event{ Name: tree_event.ON_API_COMMAND, Data: cmd_data, FromApi: node_info.CurrentNodeInfo.Value, From: path.From, } tree_event.Emit(e, path) if cb != nil { subscribed_command_callbacks[cmd.ID] = cb_str{f: cb, c: make(chan bool)} <-subscribed_command_callbacks[cmd.ID].c } return }
// Init API node for connection to targets func API_INIT(targets ...string) bool { var err tree_lib.TreeError err.From = tree_lib.FROM_API_INIT if len(targets) == 0 { tree_log.Error(err.From, "For running API client you need to specify target node(s) to connect") return false } for _, n := range targets { node_info.ChildsNodeInfo[n], err = tree_db.GetNodeInfo(n) if !err.IsNull() { tree_log.Error(err.From, fmt.Sprintf("Unable Getting target (%s) node info from Node database, ", n), err.Error()) return false } } rand.Seed(time.Now().UnixNano()) node_info.CurrentNodeInfo = node_info.NodeInfo{ Name: fmt.Sprintf("%s|%s", API_NAME_PREFIX, tree_lib.RandomString(10)), Childs: targets, // Getting next prime number based on Unix Timestamp nanoseconds and // TODO: Think about making this in a different way Value: tree_lib.NextPrimeNumber((1 * rand.Int63n(100)) + int64(100)), } node_info.CurrentNodeValue = big.NewInt(node_info.CurrentNodeInfo.Value) // Setting node values based on child list node_info.CalculateChildParentNodeValues() // After we have child information lets connect to them node_info.ChildsConnectionUpdate() return true }
func SendDockerCommand(cmd tree_docker.DockerCmd, node string, target []string, tag []string, group []string) { var err tree_lib.TreeError if !tree_api.API_INIT(node) { fmt.Println("Unable to init api client") fmt.Println("Exiting ...") return } err.From = tree_lib.FROM_SEND_DOCKER_COMMAND var ( api_cmd = tree_api.Command{} wait = make(chan bool) ) api_cmd.ID = tree_lib.RandomString(20) api_cmd.Data, err.Err = ffjson.Marshal(cmd) if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } api_cmd.CommandType = tree_api.COMMAND_CONTAINER tree_event.ON(tree_event.ON_CHILD_CONNECTED, func(ev *tree_event.Event) { path := &tree_graph.Path{From: node, Nodes: target, Groups: group, Tags: tag} tree_api.SendCommand(&api_cmd, path, func(e *tree_event.Event, c tree_api.Command) bool { fmt.Println(string(c.Data)) fmt.Println(c.Ended) if c.Ended { return false } return true }) wait <- true }) <-wait }
func SendEventTriggerCommand(cmd *cobra.Command, args []string) { var ( event_name string err tree_lib.TreeError node string targets []string target_groups []string target_tags []string ) err.From = tree_lib.FROM_SEND_COMMAND node, err.Err = cmd.Flags().GetString("node") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } targets, err.Err = cmd.Flags().GetStringSlice("target") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } target_groups, err.Err = cmd.Flags().GetStringSlice("group") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } target_tags, err.Err = cmd.Flags().GetStringSlice("tag") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } event_name, err.Err = cmd.Flags().GetString("event") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } if !tree_api.API_INIT(node) { fmt.Println("Unable to init api client") fmt.Println("Exiting ...") return } var ( api_cmd = tree_api.Command{} wait_to_end = make(chan bool) ) api_cmd.Data = []byte(event_name) api_cmd.ID = tree_lib.RandomString(20) api_cmd.CommandType = tree_api.COMMAND_TRIGGER_EVENT tree_event.ON(tree_event.ON_CHILD_CONNECTED, func(ev *tree_event.Event) { path := &tree_graph.Path{From: node, Nodes: targets, Tags: target_tags, Groups: target_groups} tree_api.SendCommand(&api_cmd, path, func(e *tree_event.Event, c tree_api.Command) bool { fmt.Println(string(c.Data)) fmt.Println(c.Ended) // TODO: End coming faster than other messages FIX !!!! if c.Ended { return false } return true }) wait_to_end <- true }) <-wait_to_end }
func ListInfos(cmd *cobra.Command, args []string) { var ( err tree_lib.TreeError node string targets []string groups []string tags []string info tree_api.Info ) err.From = tree_lib.FROM_LIST_INFOS node, err.Err = cmd.Flags().GetString("node") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } targets, err.Err = cmd.Flags().GetStringSlice("target") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } groups, err.Err = cmd.Flags().GetStringSlice("group") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } tags, err.Err = cmd.Flags().GetStringSlice("tag") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } if !tree_api.API_INIT(node) { fmt.Println("Unable to init api client") fmt.Println("Exiting ...") return } var ( api_cmd = tree_api.Command{} wait = make(chan bool) ) info.Group = groups info.Target = targets info.Tag = tags api_cmd.Data, err.Err = ffjson.Marshal(info) if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } api_cmd.ID = tree_lib.RandomString(20) api_cmd.CommandType = tree_api.COMMAND_LIST tree_event.ON(tree_event.ON_CHILD_CONNECTED, func(ev *tree_event.Event) { path := &tree_graph.Path{From: node, Nodes: []string{node}} tree_api.SendCommand(&api_cmd, path, func(e *tree_event.Event, c tree_api.Command) bool { var ( err tree_lib.TreeError info = make(map[string]node_info.NodeInfo) ) err.Err = ffjson.Unmarshal(c.Data, &info) if !err.IsNull() { tree_log.Error(err.From, err.Error()) return false } for _, a := range info { fmt.Println("name: ", a.Name, ", Adress: ", a.TreeIp, ":", a.TreePort, ", Value: ", a.Value) fmt.Println("groups:", a.Groups) fmt.Println("tags:", a.Tags) fmt.Println("childs", a.Childs) } return false }) wait <- true }) <-wait }
func SendAddEventHandlerCommand(cmd *cobra.Command, args []string) { var ( event_name string err tree_lib.TreeError handler custom_event.Handler node string targets []string target_groups []string target_tags []string ) err.From = tree_lib.FROM_SEND_COMMAND node, err.Err = cmd.Flags().GetString("node") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } targets, err.Err = cmd.Flags().GetStringSlice("target") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } target_groups, err.Err = cmd.Flags().GetStringSlice("group") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } target_tags, err.Err = cmd.Flags().GetStringSlice("tag") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } event_name, err.Err = cmd.Flags().GetString("event") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } handler.Handler, err.Err = cmd.Flags().GetString("handler") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } handler.IsFile, err.Err = cmd.Flags().GetBool("file") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } handler.ExecUser, err.Err = cmd.Flags().GetString("user") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } handler.Password, err.Err = cmd.Flags().GetString("pass") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } var ( api_cmd = tree_api.Command{} wait_to_end = make(chan bool) ) api_cmd.Data, err.Err = ffjson.Marshal(map[string]interface{}{ "name": event_name, "handlers": []custom_event.Handler{handler}, }) if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } if !tree_api.API_INIT(node) { fmt.Println("Unable to init api client") fmt.Println("Exiting ...") return } api_cmd.ID = tree_lib.RandomString(20) api_cmd.CommandType = tree_api.COMMAND_ADD_CUSTOM_EVENT tree_event.ON(tree_event.ON_CHILD_CONNECTED, func(ev *tree_event.Event) { path := &tree_graph.Path{From: node, Nodes: targets, Tags: target_tags, Groups: target_groups} tree_api.SendCommand(&api_cmd, path, func(e *tree_event.Event, c tree_api.Command) bool { fmt.Println(string(c.Data)) fmt.Println(c.Ended) // TODO: End coming faster than other messages FIX !!!! if c.Ended { return false } return true }) wait_to_end <- true }) <-wait_to_end }
func UpdateInfo(cmd *cobra.Command, args []string) { var ( node string err tree_lib.TreeError target string ip string port int add_to_group string delete_from_group string add_to_tag string delete_from_tag string delete_child string add_child string info node_info.NodeInfo ) err.From = tree_lib.FROM_UPDATE_INFO node, err.Err = cmd.Flags().GetString("node") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } target, err.Err = cmd.Flags().GetString("target") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } ip, err.Err = cmd.Flags().GetString("ip") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } port, err.Err = cmd.Flags().GetInt("port") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } add_to_group, err.Err = cmd.Flags().GetString("add_group") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } delete_from_group, err.Err = cmd.Flags().GetString("delete_group") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } add_to_tag, err.Err = cmd.Flags().GetString("add_tag") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } delete_from_tag, err.Err = cmd.Flags().GetString("delete_tag") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } add_child, err.Err = cmd.Flags().GetString("add_child") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } delete_child, err.Err = cmd.Flags().GetString("delete_child") if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } if !tree_api.API_INIT(node) { fmt.Println("Unable to init api client") fmt.Println("Exiting ...") return } info.Name = target info.TreeIp = ip info.TreePort = port info.Childs = append(info.Childs, add_child, delete_child) info.Groups = append(info.Groups, add_to_group, delete_from_group) info.Tags = append(info.Tags, add_to_tag, delete_from_tag) var ( api_cmd = tree_api.Command{} wait = make(chan bool) ) api_cmd.ID = tree_lib.RandomString(20) api_cmd.Data, err.Err = ffjson.Marshal(info) if !err.IsNull() { tree_log.Error(err.From, err.Error()) return } api_cmd.CommandType = tree_api.COMMAND_UPDATE tree_event.ON(tree_event.ON_CHILD_CONNECTED, func(ev *tree_event.Event) { path := &tree_graph.Path{From: node, Nodes: []string{node}} tree_api.SendCommand(&api_cmd, path, func(e *tree_event.Event, c tree_api.Command) bool { return false }) wait <- true }) <-wait }