Skip to content

Commit

Permalink
Implement GetBootParamsBy{Name,Mac,Nid}()
Browse files Browse the repository at this point in the history
Also, implement logic in boot_data.go to call these functions if
Postgres is being used instead of KV for retrieving data for boot
scripts.
  • Loading branch information
synackd committed Oct 5, 2023
1 parent b9251fa commit 2f7f7ac
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 3 deletions.
63 changes: 63 additions & 0 deletions cmd/boot-script-service/boot_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,27 @@ func LookupByName(name string) (BootData, SMComponent) {
comp_name = comp.ID
role = comp.Role
}
if useSQL {
var result BootData
bps, err := bssdb.GetBootParamsByName([]string{name})
if err != nil {
err = fmt.Errorf("Could not retrieve boot parameters with name %q: %v", name, err)
log.Printf("ERROR: %v", err)
return result, comp
}
if len(bps) == 0 {
// Not found.
log.Printf("WARNING: Name %q did not return any results.", name)
return result, comp
} else if len(bps) > 1 {
debugf("BootParams returned: %v", bps)
log.Printf("WARNING: More than 1 node found for name %q, taking first one: %v", name, bps[0])
}
result.Kernel = ImageData{bps[0].Kernel, ""}
result.Initrd = ImageData{bps[0].Initrd, ""}
result.Params = bps[0].Params
return result, comp
}
return lookup(comp_name, name, role, DefaultTag), comp
}

Expand All @@ -952,6 +973,27 @@ func LookupByMAC(mac string) (BootData, SMComponent) {
comp_name = comp.ID
role = comp.Role
}
if useSQL {
var result BootData
bps, err := bssdb.GetBootParamsByMac([]string{mac})
if err != nil {
err = fmt.Errorf("Could not retrieve boot parameters with mac %q: %v", mac, err)
log.Printf("ERROR: %v", err)
return result, comp
}
if len(bps) == 0 {
// Not found.
log.Printf("WARNING: MAC %q did not return any results.", mac)
return result, comp
} else if len(bps) > 1 {
debugf("BootParams returned: %v", bps)
log.Printf("WARNING: More than 1 node found for MAC %q, taking first one: %v", mac, bps[0])
}
result.Kernel = ImageData{bps[0].Kernel, ""}
result.Initrd = ImageData{bps[0].Initrd, ""}
result.Params = bps[0].Params
return result, comp
}
return lookup(comp_name, mac, role, DefaultTag), comp
}

Expand All @@ -964,6 +1006,27 @@ func LookupByNid(nid int) (BootData, SMComponent) {
comp_name = comp.ID
role = comp.Role
}
if useSQL {
var result BootData
bps, err := bssdb.GetBootParamsByNid([]int32{int32(nid)})
if err != nil {
err = fmt.Errorf("Could not retrieve boot parameters with NID %d: %v", nid, err)
log.Printf("ERROR: %v", err)
return result, comp
}
if len(bps) == 0 {
// Not found.
log.Printf("WARNING: NID %d did not return any results.", nid)
return result, comp
} else if len(bps) > 1 {
debugf("BootParams returned: %v", bps)
log.Printf("WARNING: More than 1 node found for NID %d, taking first one: %v", nid, bps[0])
}
result.Kernel = ImageData{bps[0].Kernel, ""}
result.Initrd = ImageData{bps[0].Initrd, ""}
result.Params = bps[0].Params
return result, comp
}
return lookup(comp_name, nid_str, role, DefaultTag), comp
}

Expand Down
143 changes: 140 additions & 3 deletions internal/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,143 @@ func (bddb BootDataDatabase) GetBootParamsAll() ([]bssTypes.BootParams, error) {
return results, err
}

//func (bddb BootDataDatabase) GetBootParamsByNode(macs, xnames []string, nids []int32) ([]bssTypes.BootParams, error) {
// var results []bssTypes.BootParams
//}
func (bddb BootDataDatabase) GetBootParamsByName(names []string) ([]bssTypes.BootParams, error) {
var results []bssTypes.BootParams

// If input is empty, so is the output.
if len(names) == 0 {
return results, nil
}

qstr := "SELECT n.xname, bc.kernel_uri, bc.initrd_uri, bc.cmdline FROM nodes AS n" +
" LEFT JOIN boot_group_assignments AS bga ON n.id=bga.node_id" +
" JOIN boot_groups AS bg on bga.boot_group_id=bg.id" +
" JOIN boot_configs AS bc ON bg.boot_config_id=bc.id" +
" WHERE n.xname IN " + stringSliceToSql(names) +
";"
rows, err := bddb.DB.Query(qstr)
if err != nil {
err = fmt.Errorf("postgres.GetBootParamsByName: Unable to query database: %v", err)
return results, err
}
defer rows.Close()

// rows.Next() returns false if either there is no next result (i.e. it
// doesn't exist) or an error occurred. We return rows.Err() to
// distinguish between the two cases.
for rows.Next() {
var (
name string
bp bssTypes.BootParams
)
err = rows.Scan(&name, &bp.Kernel, &bp.Initrd, &bp.Params)
if err != nil {
err = fmt.Errorf("postgres.GetBootParamsByName: Could not scan SQL result: %v", err)
return results, err
}
bp.Hosts = append(bp.Hosts, name)

results = append(results, bp)
}
// Did a rows.Next() return an error?
if err = rows.Err(); err != nil {
err = fmt.Errorf("postgres.GetBootParamsByName: Could not parse query results: %v", err)
return results, err
}

return results, err
}

func (bddb BootDataDatabase) GetBootParamsByMac(macs []string) ([]bssTypes.BootParams, error) {
var results []bssTypes.BootParams

// If inout is empty, so is the output.
if len(macs) == 0 {
return results, nil
}

qstr := "SELECT n.boot_mac, bc.kernel_uri, bc.initrd_uri, bc.cmdline FROM nodes AS n" +
" LEFT JOIN boot_group_assignments AS bga ON n.id=bga.node_id" +
" JOIN boot_groups AS bg on bga.boot_group_id=bg.id" +
" JOIN boot_configs AS bc ON bg.boot_config_id=bc.id" +
" WHERE n.boot_mac IN " + stringSliceToSql(macs) +
";"
rows, err := bddb.DB.Query(qstr)
if err != nil {
err = fmt.Errorf("postgres.GetBootParamsByMac: Unable to query database: %v", err)
return results, err
}
defer rows.Close()

// rows.Next() returns false if either there is no next result (i.e. it
// doesn't exist) or an error occurred. We return rows.Err() to
// distinguish between the two cases.
for rows.Next() {
var (
mac string
bp bssTypes.BootParams
)
err = rows.Scan(&mac, &bp.Kernel, &bp.Initrd, &bp.Params)
if err != nil {
err = fmt.Errorf("postgres.GetBootParamsByMac: Could not scan SQL result: %v", err)
return results, err
}
bp.Macs = append(bp.Macs, mac)

results = append(results, bp)
}
// Did a rows.Next() return an error?
if err = rows.Err(); err != nil {
err = fmt.Errorf("postgres.GetBootParamsByName: Could not parse query results: %v", err)
return results, err
}

return results, err
}

func (bddb BootDataDatabase) GetBootParamsByNid(nids []int32) ([]bssTypes.BootParams, error) {
var results []bssTypes.BootParams

// If input is empty, so is the output.
if len(nids) == 0 {
return results, nil
}

qstr := "SELECT n.nid, bc.kernel_uri, bc.initrd_uri, bc.cmdline FROM nodes AS n" +
" LEFT JOIN boot_group_assignments AS bga ON n.id=bga.node_id" +
" JOIN boot_groups AS bg on bga.boot_group_id=bg.id" +
" JOIN boot_configs AS bc ON bg.boot_config_id=bc.id" +
" WHERE n.nid IN " + int32SliceToSql(nids) +
";"
rows, err := bddb.DB.Query(qstr)
if err != nil {
err = fmt.Errorf("postgres.GetBootParamsByNid: Unable to query database: %v", err)
return results, err
}
defer rows.Close()

// rows.Next() returns false if either there is no next result (i.e. it
// doesn't exist) or an error occurred. We return rows.Err() to
// distinguish between the two cases.
for rows.Next() {
var (
nid int32
bp bssTypes.BootParams
)
err = rows.Scan(&nid, &bp.Kernel, &bp.Initrd, &bp.Params)
if err != nil {
err = fmt.Errorf("postgres.GetBootParamsByNid: Could not scan SQL result: %v", err)
return results, err
}
bp.Nids = append(bp.Nids, nid)

results = append(results, bp)
}
// Did a rows.Next() return an error?
if err = rows.Err(); err != nil {
err = fmt.Errorf("postgres.GetBootParamsByNid: Could not parse query results: %v", err)
return results, err
}

return results, err
}

0 comments on commit 2f7f7ac

Please sign in to comment.