// dbConsolePodAcquireNodes will attempt to acquire the numbers of nodes requested by type.
// All acquired nodes will be added to the NodeConsoleInfo array.  Any error(s) will be returned.
dbConsolePodAcquireNodes will attempt to acquire the numbers of nodes requested by type.
All acquired nodes will be added to the NodeConsoleInfo array.  Any error(s) will be returned.
// dbConsolePodAcquireNodes will attempt to acquire the numbers of nodes requested by type . // All acquired nodes will be added to the NodeConsoleInfo array . Any error ( s ) will be returned .
func dbConsolePodAcquireNodes(pod_id string, numMtn, numRvr int) (rowsAffected int64, acquired []NodeConsoleInfo, err error) {
	if numMtn < 1 && numRvr < 1 {
		log.Printf("dbConsolePodAcquireNodes: the requested number of Mtn and Rvr was zero.  Returning.")
		return 0, []NodeConsoleInfo{}, nil
	}
	mu.Lock()
	defer mu.Unlock()
	var nodes string
	var errList []string
	acquired = []NodeConsoleInfo{}
	sqlStmt := `
		select node_name, node_bmc_name, node_bmc_fqdn, node_class, node_nid_number, node_role
		from ownership
		where node_class=$1 and console_pod_id is NULL
		limit $2
	`
	var i int = 0
	if numMtn > 0 {
		rows, err := DB.Query(sqlStmt, "Mountain", numMtn)
		defer rows.Close()
		if err != nil {
			errMsg := fmt.Sprintf("WARN: dbConsolePodAcquireNodes: There is a SELECT error: %s", err)
			log.Printf(errMsg)
			errList = append(errList, errMsg)
		}
		if rows != nil {
			for rows.Next() {
				var nci NodeConsoleInfo
				err := rows.Scan(&nci.NodeName,
					&nci.BmcName,
					&nci.BmcFqdn,
					&nci.Class,
					&nci.NID,
					&nci.Role)
				if err != nil {
					errList = append(errList, fmt.Sprintf("WARN: dbConsolePodAcquireNodes: Error scanning row: %s", err))
					continue 
				}
				acquired = append(acquired, nci)
				if i > 0 {
					nodes += fmt.Sprintf(",'%s' ", nci.NodeName)
				} else {
					nodes += fmt.Sprintf("'%s' ", nci.NodeName)
				}
				i++
			}
		}
	}
	if numRvr > 0 {
		rows, err := DB.Query(sqlStmt, "River", numRvr)
		defer rows.Close()
		if err != nil {
			errMsg := fmt.Sprintf("WARN: dbClearStaleNodes: There is a SELECT error: %s", err)
			log.Printf(errMsg)
			errList = append(errList, errMsg)
		}
		if rows != nil {
			for rows.Next() {
				var nci NodeConsoleInfo
				err := rows.Scan(&nci.NodeName,
					&nci.BmcName,
					&nci.BmcFqdn,
					&nci.Class,
					&nci.NID,
					&nci.Role)
				if err != nil {
					errList = append(errList, fmt.Sprintf("WARN: dbConsolePodAcquireNodes: Error scanning row: %s", err))
					continue 
				}
				acquired = append(acquired, nci)
				if i > 0 {
					nodes += fmt.Sprintf(",'%s' ", nci.NodeName)
				} else {
					nodes += fmt.Sprintf("'%s' ", nci.NodeName)
				}
				i++
			}
		}
	}
	if len(nodes) > 0 {
		sqlStmt = `
			update ownership set console_pod_id = '%s', heartbeat=now()
			where node_name in (%s)
		`
		debugLog.Println(fmt.Sprintf("pod_id=%s nodes=%s", pod_id, nodes))
		sqlStmt = fmt.Sprintf(sqlStmt, pod_id, nodes)
		debugLog.Println(fmt.Sprintf("dbConsolePodAcquireNodes running: %s", sqlStmt))
		result, err := DB.Exec(sqlStmt)
		rowsAffected = 0
		if err != nil {
			errMsg := fmt.Sprintf("WARN: dbConsolePodAcquireNodes: There is an UPDATE error: %s", err)
			log.Printf(errMsg)
			errList = append(errList, errMsg)
		}
		if result != nil {
			rowsAffected, _ = result.RowsAffected()
			debugLog.Println(fmt.Sprintf("result.RowsAffected %d", rowsAffected))
		}
	}
	if len(errList) > 0 {
		var errStr string
		for _, e := range errList {
			errStr += fmt.Sprintf("%s\n", e)
		}
		return rowsAffected, acquired, errors.New(errStr)
	} else {
		return rowsAffected, acquired, nil
	}
}
func dbConsolePodAcquireNodes ( pod_id string , numMtn , numRvr int ) ( rowsAffected int64 , acquired [ ] NodeConsoleInfo , err error ) { if numMtn < 1 && numRvr < 1 { log . Printf ( " " ) return 0 , [ ] NodeConsoleInfo { } , nil } mu . Lock ( ) defer mu . Unlock ( ) var nodes string var errList [ ] string acquired = [ ] NodeConsoleInfo { } sqlStmt := `
		select node_name, node_bmc_name, node_bmc_fqdn, node_class, node_nid_number, node_role
		from ownership
		where node_class=$1 and console_pod_id is NULL
		limit $2
	` var i int = 0 if numMtn > 0 { rows , err := DB . Query ( sqlStmt , " " , numMtn ) defer rows . Close ( ) if err != nil { errMsg := fmt . Sprintf ( " " , err ) log . Printf ( errMsg ) errList = append ( errList , errMsg ) } if rows != nil { for rows . Next ( ) { var nci NodeConsoleInfo err := rows . Scan ( & nci . NodeName , & nci . BmcName , & nci . BmcFqdn , & nci . Class , & nci . NID , & nci . Role ) if err != nil { errList = append ( errList , fmt . Sprintf ( " " , err ) ) continue } acquired = append ( acquired , nci ) if i > 0 { nodes += fmt . Sprintf ( " " , nci . NodeName ) } else { nodes += fmt . Sprintf ( " " , nci . NodeName ) } i ++ } } } if numRvr > 0 { rows , err := DB . Query ( sqlStmt , " " , numRvr ) defer rows . Close ( ) if err != nil { errMsg := fmt . Sprintf ( " " , err ) log . Printf ( errMsg ) errList = append ( errList , errMsg ) } if rows != nil { for rows . Next ( ) { var nci NodeConsoleInfo err := rows . Scan ( & nci . NodeName , & nci . BmcName , & nci . BmcFqdn , & nci . Class , & nci . NID , & nci . Role ) if err != nil { errList = append ( errList , fmt . Sprintf ( " " , err ) ) continue } acquired = append ( acquired , nci ) if i > 0 { nodes += fmt . Sprintf ( " " , nci . NodeName ) } else { nodes += fmt . Sprintf ( " " , nci . NodeName ) } i ++ } } } if len ( nodes ) > 0 { sqlStmt = `
			update ownership set console_pod_id = '%s', heartbeat=now()
			where node_name in (%s)
		` debugLog . Println ( fmt . Sprintf ( " " , pod_id , nodes ) ) sqlStmt = fmt . Sprintf ( sqlStmt , pod_id , nodes ) debugLog . Println ( fmt . Sprintf ( " " , sqlStmt ) ) result , err := DB . Exec ( sqlStmt ) rowsAffected = 0 if err != nil { errMsg := fmt . Sprintf ( " " , err ) log . Printf ( errMsg ) errList = append ( errList , errMsg ) } if result != nil { rowsAffected , _ = result . RowsAffected ( ) debugLog . Println ( fmt . Sprintf ( " " , rowsAffected ) ) } } if len ( errList ) > 0 { var errStr string for _ , e := range errList { errStr += fmt . Sprintf ( " \n " , e ) } return rowsAffected , acquired , errors . New ( errStr ) } else { return rowsAffected , acquired , nil } }
dbConsolePodAcquireNodes will attempt to acquire the numbers of nodes requested by type.
dbConsolePodAcquireNodes will attempt to acquire the numbers of nodes requested by type .
// Exit quickly when no nodes were requested. // Try next record. // Try next record. // On an update operation RowsAffected will be the count acually updated.