Initial cut of wwapi.

We need an API for Warewulf in order to automate around it. The initial version of the API will look a lot like wwctl. wwctl will call the API so that we do not need to maintain separate code paths. In this preview wwctl calls the API via direct function call, but we can change that to a gprc or REST call to a Warewulf server in the future.

This commit contains:
wwapid WareWulf API Daemon. A grpc server with mTLS auth.
wwapic WareWulf API Client. A grpc client with mTLS auth. It's just a sample that reads the version from the server.
wwapird WareWulf API Rest Daemon. A http REST reverse proxy to wwapid.
There are also sample insecure and secure curls to test with.

Implemented Functionality:
wwctl node add
wwctl node delete
wwctl node list
wwctl node set
wwctl node status
wwctl container build
wwctl container delete
wwctl container import
wwctl container list
wwctl container show

Some notes on the files:

    Logic that was in wwctl has moved to warewulf/internal/pkg/api. wwctl just calls the API. wwctl functionality is unchanged.
    Everything under the /google directory is copied google proto code to stand up wwapird. It's a bit strange to copy in the code, but that is currently how it's done.
    Everything in warewulf/internal/pkg/api/routes/wwapiv1 is generated code.

There are some loose ends here, such as no service installers. I just ran off the command line for development.
The Makefile could use improvement. I'm building by make clean setup proto all build wwapid wwapic wwapird ; echo $?
I copied the configs from warewulf/etc to /usr/local/etc/warewulf manually.
This commit is contained in:
MatthewHink
2022-05-13 15:12:32 -04:00
parent d976e7fc31
commit d8cd6049ac
57 changed files with 8842 additions and 1157 deletions

View File

@@ -1,15 +1,15 @@
package nodestatus
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/fatih/color"
"github.com/hpcng/warewulf/internal/pkg/api/node"
"github.com/hpcng/warewulf/internal/pkg/api/routes/wwapiv1"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/hpcng/warewulf/pkg/hostlist"
@@ -17,19 +17,7 @@ import (
"golang.org/x/term"
)
type allStatus struct {
Nodes map[string]*NodeStatus `json:"nodes"`
}
type NodeStatus struct {
NodeName string `json:"node name"`
Stage string `json:"stage"`
Sent string `json:"sent"`
Ipaddr string `json:"ipaddr"`
Lastseen int64 `json:"last seen"`
}
func CobraRunE(cmd *cobra.Command, args []string) error {
func CobraRunE(cmd *cobra.Command, args []string) (err error) {
controller, err := warewulfconf.New()
if err != nil {
@@ -42,30 +30,16 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
os.Exit(1)
}
statusURL := fmt.Sprintf("http://%s:%d/status", controller.Ipaddr, controller.Warewulf.Port)
for {
var elipsis bool
var height int
var count int
rightnow := time.Now().Unix()
wwlog.Printf(wwlog.VERBOSE, "Connecting to: %s\n", statusURL)
resp, err := http.Get(statusURL)
var nodeStatusResponse *wwapiv1.NodeStatusResponse
nodeStatusResponse, err = node.NodeStatus([]string{})
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not connect to Warewulf server: %s\n", err)
os.Exit(1)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var nodeStatus allStatus
err = decoder.Decode(&nodeStatus)
if err != nil {
wwlog.Printf(wwlog.ERROR, "Could not decode JSON: %s\n", err)
os.Exit(1)
return err
}
if SetWatch {
@@ -80,65 +54,50 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
fmt.Printf("%-20s %-20s %-25s %-10s\n", "NODENAME", "STAGE", "SENT", "LASTSEEN (s)")
fmt.Printf("%s\n", strings.Repeat("=", 80))
keys := make([]*NodeStatus, 0, len(nodeStatus.Nodes))
wwlog.Printf(wwlog.VERBOSE, "Building sort index\n")
var statuses []*wwapiv1.NodeStatus
if len(args) > 0 {
tmpMap := make(map[string]bool)
nodeList := hostlist.Expand(args)
for _, name := range nodeList {
tmpMap[name] = true
}
for name := range nodeStatus.Nodes {
if _, ok := tmpMap[name]; ok {
keys = append(keys, nodeStatus.Nodes[name])
for i := 0; i < len(nodeStatusResponse.NodeStatus); i++ {
for j := 0; j < len(nodeList); j++ {
if nodeStatusResponse.NodeStatus[i].NodeName == nodeList[j] {
statuses = append(statuses, nodeStatusResponse.NodeStatus[i])
break
}
}
}
} else {
for name := range nodeStatus.Nodes {
keys = append(keys, nodeStatus.Nodes[name])
for i := 0; i < len(nodeStatusResponse.NodeStatus); i++ {
statuses = append(statuses, nodeStatusResponse.NodeStatus[i])
}
}
wwlog.Printf(wwlog.VERBOSE, "Sorting index\n")
if SetSortLast {
sort.Slice(keys, func(i, j int) bool {
if keys[i].Lastseen > keys[j].Lastseen {
sort.Slice(statuses, func(i, j int) bool {
if statuses[i].Lastseen > statuses[j].Lastseen {
return true
} else if keys[i].Lastseen < keys[j].Lastseen {
} else if statuses[i].Lastseen < statuses[j].Lastseen {
return false
} else {
if keys[i].NodeName < keys[j].NodeName {
return true
} else {
return false
}
return statuses[i].NodeName < statuses[j].NodeName
}
//return keys[i].Lastseen > keys[j].Lastseen
})
} else {
sort.Slice(keys, func(i, j int) bool {
return keys[i].NodeName < keys[j].NodeName
})
}
if SetSortReverse {
var tmpsort []*NodeStatus
} else if SetSortReverse {
wwlog.Printf(wwlog.VERBOSE, "Reversing sort order\n")
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].NodeName > statuses[j].NodeName
})
for l := len(keys) - 1; l >= 0; l-- {
tmpsort = append(tmpsort, keys[l])
}
keys = tmpsort
} else {
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].NodeName < statuses[j].NodeName
})
}
wwlog.Printf(wwlog.VERBOSE, "Printing results\n")
for _, o := range keys {
for i := 0; i < len(statuses); i++ {
o := statuses[i]
if SetTime > 0 && o.Lastseen < SetTime {
continue
}
@@ -158,7 +117,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
color.HiBlack("%-20s %-20s %-25s %-10s\n", o.NodeName, "--", "--", "--")
}
if count+4 >= height && SetWatch {
if count+1 != len(keys) {
if count+1 != len(statuses) {
elipsis = true
}
break
@@ -175,6 +134,5 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
break
}
}
return nil
return
}