示例#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
}
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
	}
}