示例#1
0
func (ws *webServer) DelFlag(w http.ResponseWriter, r *http.Request) {
	_, name := path.Split(r.URL.Path)

	macStr := r.FormValue("mac")
	var machine datasource.Machine
	var exist bool
	if macStr != "" {
		mac, err := net.ParseMAC(macStr)
		if err != nil {
			http.Error(w, `{"error": "Error while parsing the mac"}`, http.StatusInternalServerError)
			return
		}

		machine, exist = ws.ds.GetMachine(mac)
		if !exist {
			http.Error(w, `{"error": "Machine not found"}`, http.StatusInternalServerError)
			return
		}
	}

	var err error
	if machine != nil {
		err = machine.DeleteFlag(name)
	} else {
		// TODO deafult flags
		http.Error(w, `{"error": "Default flags not supported yet"}`, http.StatusInternalServerError)
	}

	if err != nil {
		http.Error(w, `{"error": "Error while delleting value"}`, http.StatusInternalServerError)
		return
	}

	io.WriteString(w, `"OK"`)
}
示例#2
0
func nodeToDetails(node datasource.Machine) (*nodeDetails, error) {
	name := node.Name()
	mac := node.Mac()
	ip, err := node.IP()
	if err != nil {
		return nil, errors.New("IP")
	}
	first, err := node.FirstSeen()
	if err != nil {
		return nil, errors.New("FIRST")
	}
	last, err := node.LastSeen()
	if err != nil {
		return nil, errors.New("LAST")
	}
	return &nodeDetails{name, mac.String(), ip, first, last}, nil
}
示例#3
0
func executeTemplate(rootTemplte *template.Template, templateName string, machine datasource.Machine, hostAddr string) (string, error) {
	template := rootTemplte.Lookup(templateName)

	if template == nil {
		return "", fmt.Errorf("template with name=%s wasn't found for root=%s",
			templateName, rootTemplte)
	}

	buf := new(bytes.Buffer)
	template.Funcs(map[string]interface{}{
		"V": func(key string) string {
			flag, err := machine.GetFlag(key)
			if err != nil { // TODO excepts Not-Found
				logging.Log(templatesDebugTag,
					"Error while getting flag key=%s for machine=%s: %s",
					key, machine.Name(), err)
				return ""
			}
			return flag
		},
		"b64": func(text string) string {
			return base64.StdEncoding.EncodeToString([]byte(text))
		},
		"b64template": func(templateName string) string {
			text, err := executeTemplate(rootTemplte, templateName, machine, hostAddr)
			if err != nil {
				logging.Log(templatesDebugTag,
					"Error while b64template for templateName=%s machine=%s: %s",
					templateName, machine.Name(), err)
				return ""
			}
			return base64.StdEncoding.EncodeToString([]byte(text))
		},
	})
	ip, _ := machine.IP()
	data := struct {
		Mac      string
		IP       string
		Hostname string
		Domain   string
		HostAddr string
	}{
		machine.Mac().String(),
		ip.String(),
		machine.Name(),
		machine.Domain(),
		hostAddr,
	}
	err := template.ExecuteTemplate(buf, templateName, &data)
	if err != nil {
		return "", err
	}
	str := buf.String()
	str = strings.Trim(str, "\n")
	return str, nil
}