Example #1
0
func AddStorageNodeToDB(hostname string, node_state int, node_status string) error {
	// Add the node details to the DB
	var storage_node models.Node
	storage_node.Hostname = hostname
	storage_node.State = node_state
	storage_node.Status = node_status

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)

	var node models.Node
	err = coll.Find(bson.M{"hostname": storage_node.Hostname}).One(&node)
	if err != mgo.ErrNotFound {
		logger.Get().Critical(fmt.Sprintf("Node with name: %v already exists", storage_node.Hostname))
		return errors.New(fmt.Sprintf("Node with name: %v already exists", storage_node.Hostname))
	}

	// Persist the node details
	if err := coll.Insert(storage_node); err != nil {
		logger.Get().Critical("Error adding the node: %s. error: %v", storage_node.Hostname, err)
		return err
	}
	return nil
}
Example #2
0
func (a *App) PATCH_Disk(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	node_id_str := vars["node-id"]
	node_id, err := uuid.Parse(node_id_str)
	if err != nil {
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing node id: %s. Error: %v", node_id_str, err))
		return
	}

	disk_id_str := vars["disk-id"]
	disk_id, err := uuid.Parse(disk_id_str)
	if err != nil {
		util.HttpResponse(w, http.StatusBadRequest, fmt.Sprintf("Error parsing node id: %s. Error: %v", disk_id_str, err))
		return
	}

	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()

	collection := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)
	var node models.Node
	if err := collection.Find(bson.M{"nodeid": *node_id}).One(&node); err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting the node detail for node: %s. error: %v", node_id_str, err))
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logger.Get().Error("Error parsing http request body:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}
	var m map[string]interface{}

	if err = json.Unmarshal(body, &m); err != nil {
		logger.Get().Error("Unable to Unmarshall the data:%s", err)
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
		return
	}

	var disks []backend.Disk
	if val, ok := m["storageprofile"]; ok {
		//update the field
		for _, disk := range node.StorageDisks {
			if disk.DiskId == *disk_id {
				disk.StorageProfile = val.(string)
			}
			disks = append(disks, disk)
		}
		node.StorageDisks = disks
	}
	//Save
	err = collection.Update(bson.M{"nodeid": *node_id}, bson.M{"$set": node})
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error updating record in DB for node: %s. error: %v", node_id_str, err))
		util.HttpResponse(w, http.StatusInternalServerError, err.Error())
	}
}
Example #3
0
func updateStorageNodeToDB(storage_node models.Node) error {
	// Add the node details to the DB
	sessionCopy := db.GetDatastore().Copy()
	defer sessionCopy.Close()
	coll := sessionCopy.DB(conf.SystemConfig.DBConfig.Database).C(models.COLL_NAME_STORAGE_NODES)

	storage_node.State = models.NODE_STATE_ACTIVE
	if err := coll.Update(bson.M{"hostname": storage_node.Hostname}, storage_node); err != nil {
		logger.Get().Critical("Error Updating the node: %s. error: %v", storage_node.Hostname, err)
		return err
	}
	return nil
}
func GetStorageNodeInstance(hostname string, sProfiles []models.StorageProfile) (*models.Node, bool) {
	var storage_node models.Node
	storage_node.Hostname = hostname
	storage_node.Enabled = true
	storage_node.NodeId, _ = salt_backend.GetNodeID(hostname)
	networkInfo, err := salt_backend.GetNodeNetwork(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting network details for node: %s. error: %v", hostname, err))
		return nil, false
	}
	storage_node.NetworkInfo.Subnet = networkInfo.Subnet
	storage_node.NetworkInfo.Ipv4 = networkInfo.IPv4
	storage_node.NetworkInfo.Ipv6 = networkInfo.IPv6
	addrs, err := net.LookupHost(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error looking up node IP for: %s. error: %v", hostname, err))
		return nil, false
	}
	storage_node.ManagementIP4 = addrs[0]
	ok, err := salt_backend.NodeUp(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting status of node: %s. error: %v", hostname, err))
		return nil, false
	}
	if ok {
		storage_node.Status = models.STATUS_UP
	} else {
		storage_node.Status = models.STATUS_DOWN
	}
	disks, err := salt_backend.GetNodeDisk(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting disk details for node: %s. error: %v", hostname, err))
		return nil, false
	}
	for _, disk := range disks {
		dId, err := uuid.New()
		if err != nil {
			logger.Get().Error(fmt.Sprintf("Unable to generate uuid for disk : %s. error: %v", disk.DevName, err))
			return nil, false
		}
		disk.DiskId = *dId
		applyStorageProfile(&disk, sProfiles)
		storage_node.StorageDisks = append(storage_node.StorageDisks, disk)
	}

	cpus, err := salt_backend.GetNodeCpu(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting cpu details for node: %s. error: %v", hostname, err))
		return nil, false
	}
	for _, cpu := range cpus {
		storage_node.CPUs = append(storage_node.CPUs, cpu)
	}

	osInfo, err := salt_backend.GetNodeOs(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting os details for node: %s", hostname))
		return nil, false
	}
	storage_node.OS.Name = osInfo.Name
	storage_node.OS.OSVersion = osInfo.OSVersion
	storage_node.OS.KernelVersion = osInfo.KernelVersion
	storage_node.OS.SELinuxMode = osInfo.SELinuxMode

	memoryInfo, err := salt_backend.GetNodeMemory(hostname)
	if err != nil {
		logger.Get().Error(fmt.Sprintf("Error getting memory details for node: %s", hostname))
		return nil, false
	}
	storage_node.Memory.TotalSize = memoryInfo.TotalSize
	storage_node.Memory.SwapTotal = memoryInfo.SwapTotal
	storage_node.Memory.Active = memoryInfo.Active
	storage_node.Memory.Type = memoryInfo.Type

	if !storage_node.NodeId.IsZero() && len(storage_node.NetworkInfo.Subnet) != 0 && len(storage_node.StorageDisks) != 0 {
		return &storage_node, true
	} else {
		return nil, false
	}
}