Skip to content

Commit

Permalink
Merge pull request #17 from bbockelm/ui_apis
Browse files Browse the repository at this point in the history
Additional user interfaces and APIs
  • Loading branch information
bbockelm authored Aug 20, 2023
2 parents 5853034 + 33ba1ea commit 5fe1bf6
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 6 deletions.
28 changes: 26 additions & 2 deletions cmd/origin_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"text/template"
"time"

"github.com/pelicanplatform/pelican"
"github.com/pelicanplatform/pelican/metrics"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/origin_ui"
"github.com/pelicanplatform/pelican/web_ui"
Expand Down Expand Up @@ -66,6 +66,10 @@ type XrootdConfig struct {
func init() {
err := config.InitServer()
cobra.CheckErr(err)
err = metrics.SetComponentHealthStatus("xrootd", "critical", "xrootd has not been started")
cobra.CheckErr(err)
err = metrics.SetComponentHealthStatus("cmsd", "critical", "cmsd has not been started")
cobra.CheckErr(err)
}

func checkXrootdEnv() error {
Expand Down Expand Up @@ -430,6 +434,9 @@ func launchXrootd() error {
return err
}
log.Info("Successfully launched xrootd")
if err := metrics.SetComponentHealthStatus("xrootd", "ok", ""); err != nil {
return err
}

cmsdCmd := exec.Command("cmsd", "-f", "-c", configPath)
if cmsdCmd.Err != nil {
Expand All @@ -443,6 +450,9 @@ func launchXrootd() error {
return err
}
log.Info("Successfully launched cmsd")
if err := metrics.SetComponentHealthStatus("cmsd", "ok", ""); err != nil {
return err
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
Expand Down Expand Up @@ -470,6 +480,10 @@ func launchXrootd() error {
if !cmsdExpiry.IsZero() {
return nil
}
if err = metrics.SetComponentHealthStatus("xrootd", "critical",
"xrootd process failed unexpectedly"); err != nil {
return err
}
return errors.Wrap(waitResult, "xrootd process failed unexpectedly")
}
return nil
Expand All @@ -478,6 +492,10 @@ func launchXrootd() error {
if !xrootdExpiry.IsZero() {
return nil
}
if err = metrics.SetComponentHealthStatus("cmsd", "critical",
"cmsd process failed unexpectedly"); err != nil {
return nil
}
return errors.Wrap(waitResult, "cmsd process failed unexpectedly")
}
return nil
Expand All @@ -504,7 +522,7 @@ func serveOrigin(/*cmd*/ *cobra.Command, /*args*/ []string) error {
log.Warningln("Failed to do service auto-discovery:", err)
}

monitorPort, err := pelican.ConfigureMonitoring()
monitorPort, err := metrics.ConfigureMonitoring()
if err != nil {
return err
}
Expand All @@ -527,12 +545,18 @@ func serveOrigin(/*cmd*/ *cobra.Command, /*args*/ []string) error {
}

go web_ui.RunEngine(engine)
if err = metrics.SetComponentHealthStatus("web-ui", "warning", "Authentication not initialized"); err != nil {
return err
}

// Ensure we wait until the origin has been initialized
// before launching XRootD.
if err = origin_ui.WaitUntilLogin(); err != nil {
return err
}
if err = metrics.SetComponentHealthStatus("web-ui", "ok", ""); err != nil {
return err
}

err = launchXrootd()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func InitServer() error {
if err != nil {
return err
}
viper.SetDefault("Hostname", hostname)
viper.SetDefault("Sitename", hostname)

err = viper.MergeConfig(strings.NewReader(defaultsYaml))
Expand Down
92 changes: 92 additions & 0 deletions metrics/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

package metrics

import (
"fmt"
"sync"
)

type (

ComponentStatus struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
}

componentStatusInternal struct {
Status int
Message string
}

HealthStatus struct {
OverallStatus string `json:"status"`
ComponentStatus map[string]ComponentStatus `json:"components"`
}

)

var (
healthStatus = sync.Map{}
)

func statusToInt(status string) (int, error) {
switch status {
case "ok":
return 3, nil
case "warning":
return 2, nil
case "critical":
return 1, nil
}
return 0, fmt.Errorf("Unknown component status: %v", status)
}

func intToStatus(statusInt int) string {
switch statusInt {
case 3:
return "ok"
case 2:
return "warning"
case 1:
return "critical"
}
return "unknown"
}

func SetComponentHealthStatus(name, state, msg string) error {
statusInt, err := statusToInt(state)
if err != nil {
return err
}
healthStatus.Store(name, componentStatusInternal{statusInt, msg})
return nil
}

func GetHealthStatus() HealthStatus {
status := HealthStatus{}
status.OverallStatus = "unknown"
overallStatus := 4
healthStatus.Range(func(component, compstat any) bool {
componentStatus, ok := compstat.(componentStatusInternal)
if !ok {
return true
}
componentString, ok := component.(string)
if !ok {
return true
}
if status.ComponentStatus == nil {
status.ComponentStatus = make(map[string]ComponentStatus)
}
status.ComponentStatus[componentString] = ComponentStatus{
intToStatus(componentStatus.Status),
componentStatus.Message,
}
if componentStatus.Status < overallStatus {
overallStatus = componentStatus.Status
}
return true
})
status.OverallStatus = intToStatus(overallStatus)
return status
}
2 changes: 1 addition & 1 deletion metrics.go → metrics/xrootd_metrics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pelican
package metrics

import (
"bytes"
Expand Down
38 changes: 36 additions & 2 deletions origin_ui/origin.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/viper"
"github.com/tg123/go-htpasswd"
"golang.org/x/crypto/bcrypt"
"golang.org/x/term"
)

type (
Expand Down Expand Up @@ -82,12 +83,29 @@ func WaitUntilLogin() error {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)

hostname := viper.GetString("Hostname")
webPort := viper.GetInt("WebPort")
isTTY := false
if term.IsTerminal(int(os.Stdout.Fd())) {
isTTY = true
fmt.Printf("\n\n\n\n")
}

for {
previousCode.Store(currentCode.Load())
newCode := fmt.Sprintf("%06v", rand.Intn(1000000))
currentCode.Store(&newCode)
fmt.Println("Pelican admin interface is not initialized; to initialize, login at https://localhost:8080 with the following code:")
fmt.Println(*currentCode.Load())
if isTTY {
fmt.Printf("\033[A\033[A\033[A\033[A")
fmt.Printf("\033[2K\n")
fmt.Printf("\033[2K\rPelican admin interface is not initialized\n\033[2KTo initialize, "+
"login at \033[1;34mhttps://%v:%v\033[0m with the following code:\n",
hostname, webPort)
fmt.Printf("\033[2K\r\033[1;34m%v\033[0m\n", *currentCode.Load())
} else {
fmt.Printf("Pelican admin interface is not initialized\n To initialize, login at https://%v:%v with the following code:\n", hostname, webPort)
fmt.Println(*currentCode.Load())
}
start := time.Now()
for time.Since(start) < 30 * time.Second {
select {
Expand Down Expand Up @@ -334,6 +352,22 @@ func ConfigureOriginUI(router *gin.Engine) error {
group.POST("/login", loginHandler)
group.POST("/initLogin", initLoginHandler)
group.POST("/resetLogin", resetLoginHandler)
group.GET("/whoami", func(ctx *gin.Context) {
user := ctx.GetString("User")
if user == "" {
ctx.JSON(200, gin.H{"authenticated": false})
} else {
ctx.JSON(200, gin.H{"authenticated": true, "user": user})
}
})
group.GET("/loginInitialized", func(ctx *gin.Context) {
db := authDB.Load()
if db == nil {
ctx.JSON(200, gin.H{"initialized": false})
} else {
ctx.JSON(200, gin.H{"initialized": true})
}
})

router.StaticFS("/assets", http.FS(webAssets))
router.GET("favicon.ico", func(ctx *gin.Context) {
Expand Down
4 changes: 3 additions & 1 deletion web_ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/pelicanplatform/pelican/metrics"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/zsais/go-gin-prometheus"
Expand All @@ -16,7 +17,8 @@ func ConfigureMetrics(engine *gin.Engine) error {
prometheusMonitor.Use(engine)

engine.GET("/api/v1.0/health", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{"status": "healthy"})
healthStatus := metrics.GetHealthStatus()
ctx.JSON(http.StatusOK, healthStatus)
})
return nil
}
Expand Down

0 comments on commit 5fe1bf6

Please sign in to comment.