2021-03-31 07:23:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-12 11:13:25 +01:00
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-10-26 10:24:43 +02:00
|
|
|
"encoding/json"
|
2022-01-12 11:13:25 +01:00
|
|
|
"errors"
|
2021-10-11 11:11:14 +02:00
|
|
|
"flag"
|
2021-12-08 15:50:03 +01:00
|
|
|
"fmt"
|
2021-03-31 07:23:48 +02:00
|
|
|
"log"
|
2022-01-12 11:13:25 +01:00
|
|
|
"net"
|
2021-03-31 07:23:48 +02:00
|
|
|
"net/http"
|
2022-01-07 09:39:00 +01:00
|
|
|
"net/url"
|
2021-03-31 07:23:48 +02:00
|
|
|
"os"
|
2022-01-12 11:13:25 +01:00
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"os/user"
|
2022-01-07 09:39:00 +01:00
|
|
|
"strconv"
|
2022-01-12 11:13:25 +01:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2021-03-31 07:23:48 +02:00
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2021-12-16 09:35:03 +01:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/api"
|
2021-12-08 10:15:25 +01:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/auth"
|
2021-10-26 10:24:43 +02:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/config"
|
2021-03-31 08:50:53 +02:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/graph"
|
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/graph/generated"
|
2021-10-26 10:24:43 +02:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/metricdata"
|
2021-12-20 10:49:46 +01:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/schema"
|
2021-12-08 10:15:25 +01:00
|
|
|
"github.com/ClusterCockpit/cc-jobarchive/templates"
|
2021-03-31 07:23:48 +02:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2021-11-26 10:34:29 +01:00
|
|
|
var db *sqlx.DB
|
|
|
|
|
2021-12-16 09:35:03 +01:00
|
|
|
// Format of the configurartion (file). See below for the defaults.
|
2021-12-08 10:15:25 +01:00
|
|
|
type ProgramConfig struct {
|
2021-12-16 09:35:03 +01:00
|
|
|
// Address where the http (or https) server will listen on (for example: 'localhost:80').
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
|
2022-01-12 11:13:25 +01:00
|
|
|
// Drop root permissions once .env was read and the port was taken.
|
|
|
|
User string `json:"user"`
|
|
|
|
Group string `json:"group"`
|
|
|
|
|
2021-12-16 09:35:03 +01:00
|
|
|
// Disable authentication (for everything: API, Web-UI, ...)
|
|
|
|
DisableAuthentication bool `json:"disable-authentication"`
|
|
|
|
|
|
|
|
// Folder where static assets can be found, will be served directly
|
|
|
|
StaticFiles string `json:"static-files"`
|
|
|
|
|
|
|
|
// Currently only SQLite3 ist supported, so this should be a filename
|
|
|
|
DB string `json:"db"`
|
|
|
|
|
|
|
|
// Path to the job-archive
|
|
|
|
JobArchive string `json:"job-archive"`
|
|
|
|
|
|
|
|
// Make the /api/jobs/stop_job endpoint do the heavy work in the background.
|
|
|
|
AsyncArchiving bool `json:"async-archive"`
|
|
|
|
|
|
|
|
// Keep all metric data in the metric data repositories,
|
|
|
|
// do not write to the job-archive.
|
|
|
|
DisableArchive bool `json:"disable-archive"`
|
|
|
|
|
|
|
|
// For LDAP Authentication and user syncronisation.
|
|
|
|
LdapConfig *auth.LdapConfig `json:"ldap"`
|
|
|
|
|
|
|
|
// If both those options are not empty, use HTTPS using those certificates.
|
|
|
|
HttpsCertFile string `json:"https-cert-file"`
|
|
|
|
HttpsKeyFile string `json:"https-key-file"`
|
|
|
|
|
|
|
|
// If overwriten, at least all the options in the defaults below must
|
|
|
|
// be provided! Most options here can be overwritten by the user.
|
|
|
|
UiDefaults map[string]interface{} `json:"ui-defaults"`
|
2022-01-07 09:39:00 +01:00
|
|
|
|
|
|
|
// Where to store MachineState files
|
|
|
|
MachineStateDir string `json:"machine-state-dir"`
|
2021-12-08 10:15:25 +01:00
|
|
|
}
|
2021-03-31 07:23:48 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
var programConfig ProgramConfig = ProgramConfig{
|
2022-01-12 11:13:25 +01:00
|
|
|
Addr: ":8080",
|
2021-12-08 10:15:25 +01:00
|
|
|
DisableAuthentication: false,
|
|
|
|
StaticFiles: "./frontend/public",
|
|
|
|
DB: "./var/job.db",
|
|
|
|
JobArchive: "./var/job-archive",
|
2021-12-16 09:35:03 +01:00
|
|
|
AsyncArchiving: true,
|
|
|
|
DisableArchive: false,
|
2021-12-08 10:15:25 +01:00
|
|
|
LdapConfig: &auth.LdapConfig{
|
|
|
|
Url: "ldap://localhost",
|
|
|
|
UserBase: "ou=hpc,dc=rrze,dc=uni-erlangen,dc=de",
|
|
|
|
SearchDN: "cn=admin,dc=rrze,dc=uni-erlangen,dc=de",
|
|
|
|
UserBind: "uid={username},ou=hpc,dc=rrze,dc=uni-erlangen,dc=de",
|
|
|
|
UserFilter: "(&(objectclass=posixAccount)(uid=*))",
|
|
|
|
},
|
|
|
|
HttpsCertFile: "",
|
|
|
|
HttpsKeyFile: "",
|
|
|
|
UiDefaults: map[string]interface{}{
|
|
|
|
"analysis_view_histogramMetrics": []string{"flops_any", "mem_bw", "mem_used"},
|
|
|
|
"analysis_view_scatterPlotMetrics": [][]string{{"flops_any", "mem_bw"}, {"flops_any", "cpu_load"}, {"cpu_load", "mem_bw"}},
|
|
|
|
"job_view_nodestats_selectedMetrics": []string{"flops_any", "mem_bw", "mem_used"},
|
|
|
|
"job_view_polarPlotMetrics": []string{"flops_any", "mem_bw", "mem_used", "net_bw", "file_bw"},
|
|
|
|
"job_view_selectedMetrics": []string{"flops_any", "mem_bw", "mem_used"},
|
|
|
|
"plot_general_colorBackground": true,
|
|
|
|
"plot_general_colorscheme": []string{"#00bfff", "#0000ff", "#ff00ff", "#ff0000", "#ff8000", "#ffff00", "#80ff00"},
|
|
|
|
"plot_general_lineWidth": 1,
|
|
|
|
"plot_list_jobsPerPage": 10,
|
|
|
|
"plot_list_selectedMetrics": []string{"cpu_load", "mem_used", "flops_any", "mem_bw", "clock"},
|
|
|
|
"plot_view_plotsPerRow": 4,
|
|
|
|
"plot_view_showPolarplot": true,
|
|
|
|
"plot_view_showRoofline": true,
|
|
|
|
"plot_view_showStatTable": true,
|
|
|
|
},
|
2022-01-07 09:39:00 +01:00
|
|
|
MachineStateDir: "./var/machine-state",
|
2021-12-08 10:15:25 +01:00
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
func main() {
|
|
|
|
var flagReinitDB, flagStopImmediately, flagSyncLDAP bool
|
|
|
|
var flagConfigFile string
|
2022-01-10 16:14:54 +01:00
|
|
|
var flagNewUser, flagDelUser, flagGenJWT string
|
2021-12-08 10:15:25 +01:00
|
|
|
flag.BoolVar(&flagReinitDB, "init-db", false, "Go through job-archive and re-initialize `job`, `tag`, and `jobtag` tables")
|
|
|
|
flag.BoolVar(&flagSyncLDAP, "sync-ldap", false, "Sync the `user` table with ldap")
|
|
|
|
flag.BoolVar(&flagStopImmediately, "no-server", false, "Do not start a server, stop right after initialization and argument handling")
|
|
|
|
flag.StringVar(&flagConfigFile, "config", "", "Location of the config file for this server (overwrites the defaults)")
|
2022-01-10 16:14:54 +01:00
|
|
|
flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `<username>:[admin|api]:<password>`")
|
2021-12-08 10:15:25 +01:00
|
|
|
flag.StringVar(&flagDelUser, "del-user", "", "Remove user by username")
|
2022-01-10 16:14:54 +01:00
|
|
|
flag.StringVar(&flagGenJWT, "jwt", "", "Generate and print a JWT for the user specified by the username")
|
2021-12-08 10:15:25 +01:00
|
|
|
flag.Parse()
|
2021-03-31 07:23:48 +02:00
|
|
|
|
2022-01-12 11:13:25 +01:00
|
|
|
if err := loadEnv("./.env"); err != nil && !os.IsNotExist(err) {
|
|
|
|
log.Fatalf("parsing './.env' file failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagConfigFile != "" {
|
|
|
|
data, err := os.ReadFile(flagConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &programConfig); err != nil {
|
2021-10-11 11:11:14 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
2021-10-11 11:11:14 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
var err error
|
2021-12-16 09:35:03 +01:00
|
|
|
// This might need to change for other databases:
|
|
|
|
db, err = sqlx.Open("sqlite3", fmt.Sprintf("%s?_foreign_keys=on", programConfig.DB))
|
2021-10-26 10:24:43 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2021-10-11 11:11:14 +02:00
|
|
|
}
|
|
|
|
|
2021-12-16 09:35:03 +01:00
|
|
|
// Only for sqlite, not needed for any other database:
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
// Initialize sub-modules...
|
2021-11-26 10:34:29 +01:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if !programConfig.DisableAuthentication {
|
|
|
|
if err := auth.Init(db, programConfig.LdapConfig); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-03-31 07:23:48 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagNewUser != "" {
|
|
|
|
if err := auth.AddUserToDB(db, flagNewUser); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flagDelUser != "" {
|
|
|
|
if err := auth.DelUserFromDB(db, flagDelUser); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 10:34:29 +01:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagSyncLDAP {
|
|
|
|
auth.SyncWithLDAP(db)
|
|
|
|
}
|
2022-01-10 16:14:54 +01:00
|
|
|
|
|
|
|
if flagGenJWT != "" {
|
|
|
|
user, err := auth.FetchUserFromDB(db, flagGenJWT)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.IsAPIUser {
|
|
|
|
log.Println("warning: that user does not have the API role")
|
|
|
|
}
|
|
|
|
|
|
|
|
jwt, err := auth.ProvideJWT(user)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("JWT for '%s': %s\n", user.Username, jwt)
|
|
|
|
}
|
2021-12-08 10:15:25 +01:00
|
|
|
} else if flagNewUser != "" || flagDelUser != "" {
|
|
|
|
log.Fatalln("arguments --add-user and --del-user can only be used if authentication is enabled")
|
2021-09-21 16:06:41 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if err := config.Init(db, !programConfig.DisableAuthentication, programConfig.UiDefaults, programConfig.JobArchive); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2021-12-16 09:35:03 +01:00
|
|
|
if err := metricdata.Init(programConfig.JobArchive, programConfig.DisableArchive); err != nil {
|
2021-12-08 10:15:25 +01:00
|
|
|
log.Fatal(err)
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagReinitDB {
|
|
|
|
if err := initDB(db, programConfig.JobArchive); err != nil {
|
|
|
|
log.Fatal(err)
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
2021-12-08 10:15:25 +01:00
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagStopImmediately {
|
|
|
|
return
|
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
// Build routes...
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2021-12-08 15:50:03 +01:00
|
|
|
resolver := &graph.Resolver{DB: db}
|
|
|
|
graphQLEndpoint := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: resolver}))
|
2022-01-10 16:14:54 +01:00
|
|
|
|
|
|
|
// graphQLEndpoint.SetRecoverFunc(func(ctx context.Context, err interface{}) error {
|
|
|
|
// switch e := err.(type) {
|
|
|
|
// case string:
|
|
|
|
// return fmt.Errorf("panic: %s", e)
|
|
|
|
// case error:
|
|
|
|
// return fmt.Errorf("panic caused by: %w", e)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return errors.New("internal server error (panic)")
|
|
|
|
// })
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
graphQLPlayground := playground.Handler("GraphQL playground", "/query")
|
2021-12-17 15:49:22 +01:00
|
|
|
api := &api.RestApi{
|
2022-01-07 09:39:00 +01:00
|
|
|
DB: db,
|
|
|
|
AsyncArchiving: programConfig.AsyncArchiving,
|
|
|
|
Resolver: resolver,
|
|
|
|
MachineStateDir: programConfig.MachineStateDir,
|
2021-12-16 09:35:03 +01:00
|
|
|
}
|
2021-12-08 10:15:25 +01:00
|
|
|
|
|
|
|
handleGetLogin := func(rw http.ResponseWriter, r *http.Request) {
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "login.html", &templates.Page{
|
2021-12-08 10:15:25 +01:00
|
|
|
Title: "Login",
|
|
|
|
Login: &templates.LoginPage{},
|
|
|
|
})
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
r := mux.NewRouter()
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "404.html", &templates.Page{
|
2021-12-08 10:15:25 +01:00
|
|
|
Title: "Not found",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Handle("/playground", graphQLPlayground)
|
|
|
|
r.Handle("/login", auth.Login(db)).Methods(http.MethodPost)
|
|
|
|
r.HandleFunc("/login", handleGetLogin).Methods(http.MethodGet)
|
|
|
|
r.HandleFunc("/logout", auth.Logout).Methods(http.MethodPost)
|
|
|
|
|
|
|
|
secured := r.PathPrefix("/").Subrouter()
|
|
|
|
if !programConfig.DisableAuthentication {
|
|
|
|
secured.Use(auth.Auth)
|
|
|
|
}
|
|
|
|
secured.Handle("/query", graphQLEndpoint)
|
2021-12-16 09:35:03 +01:00
|
|
|
|
2021-12-08 15:50:03 +01:00
|
|
|
secured.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
|
2021-12-09 16:27:48 +01:00
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
infos := map[string]interface{}{
|
|
|
|
"clusters": config.Clusters,
|
|
|
|
"username": "",
|
|
|
|
"admin": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if user := auth.GetUser(r.Context()); user != nil {
|
|
|
|
infos["username"] = user.Username
|
|
|
|
infos["admin"] = user.IsAdmin
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "home.html", &templates.Page{
|
2021-12-09 16:27:48 +01:00
|
|
|
Title: "ClusterCockpit",
|
|
|
|
Config: conf,
|
|
|
|
Infos: infos,
|
2021-12-08 15:50:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
monitoringRoutes(secured, resolver)
|
2021-12-17 15:49:22 +01:00
|
|
|
api.MountRoutes(secured)
|
2021-12-08 15:50:03 +01:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
r.PathPrefix("/").Handler(http.FileServer(http.Dir(programConfig.StaticFiles)))
|
|
|
|
handler := handlers.CORS(
|
|
|
|
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
|
|
|
|
handlers.AllowedMethods([]string{"GET", "POST", "HEAD", "OPTIONS"}),
|
2021-12-16 09:35:03 +01:00
|
|
|
handlers.AllowedOrigins([]string{"*"}))(handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(r)))
|
2021-12-08 10:15:25 +01:00
|
|
|
|
2022-01-12 11:13:25 +01:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
server := http.Server{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
Handler: handler,
|
|
|
|
Addr: programConfig.Addr,
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
// Start http or https server
|
2022-01-12 11:13:25 +01:00
|
|
|
|
|
|
|
listener, err := net.Listen("tcp", programConfig.Addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if programConfig.HttpsCertFile != "" && programConfig.HttpsKeyFile != "" {
|
2022-01-12 11:13:25 +01:00
|
|
|
cert, err := tls.LoadX509KeyPair(programConfig.HttpsCertFile, programConfig.HttpsKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
listener = tls.NewListener(listener, &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
})
|
|
|
|
log.Printf("HTTPS server listening at %s...", programConfig.Addr)
|
2021-12-08 10:15:25 +01:00
|
|
|
} else {
|
2022-01-12 11:13:25 +01:00
|
|
|
log.Printf("HTTP server listening at %s...", programConfig.Addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Because this program will want to bind to a privileged port (like 80), the listener must
|
|
|
|
// be established first, then the user can be changed, and after that,
|
|
|
|
// the actuall http server can be started.
|
|
|
|
if err := dropPrivileges(); err != nil {
|
|
|
|
log.Fatalf("error while changing user: %s", err.Error())
|
2021-12-08 10:15:25 +01:00
|
|
|
}
|
2022-01-12 11:13:25 +01:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
<-sigs
|
|
|
|
systemdNotifiy(false, "shutting down")
|
|
|
|
|
|
|
|
// First shut down the server gracefully (waiting for all ongoing requests)
|
|
|
|
server.Shutdown(context.Background())
|
|
|
|
|
|
|
|
// Then, wait for any async archivings still pending...
|
|
|
|
api.OngoingArchivings.Wait()
|
|
|
|
}()
|
|
|
|
|
|
|
|
systemdNotifiy(true, "running")
|
|
|
|
wg.Wait()
|
|
|
|
log.Print("Gracefull shutdown completed!")
|
2021-10-26 10:24:43 +02:00
|
|
|
}
|
2021-12-08 15:50:03 +01:00
|
|
|
|
|
|
|
func monitoringRoutes(router *mux.Router, resolver *graph.Resolver) {
|
2022-01-07 09:39:00 +01:00
|
|
|
buildFilterPresets := func(query url.Values) map[string]interface{} {
|
2021-12-09 16:27:48 +01:00
|
|
|
filterPresets := map[string]interface{}{}
|
2022-01-07 09:39:00 +01:00
|
|
|
|
2021-12-09 16:27:48 +01:00
|
|
|
if query.Get("cluster") != "" {
|
2021-12-20 10:49:46 +01:00
|
|
|
filterPresets["cluster"] = query.Get("cluster")
|
2021-12-09 16:27:48 +01:00
|
|
|
}
|
|
|
|
if query.Get("project") != "" {
|
2021-12-20 10:49:46 +01:00
|
|
|
filterPresets["project"] = query.Get("project")
|
2021-12-09 16:27:48 +01:00
|
|
|
}
|
2021-12-20 10:49:46 +01:00
|
|
|
if query.Get("state") != "" && schema.JobState(query.Get("state")).Valid() {
|
|
|
|
filterPresets["state"] = query.Get("state")
|
2021-12-09 16:27:48 +01:00
|
|
|
}
|
2022-01-07 09:39:00 +01:00
|
|
|
if rawtags, ok := query["tag"]; ok {
|
|
|
|
tags := make([]int, len(rawtags))
|
|
|
|
for i, tid := range rawtags {
|
|
|
|
var err error
|
|
|
|
tags[i], err = strconv.Atoi(tid)
|
|
|
|
if err != nil {
|
|
|
|
tags[i] = -1
|
|
|
|
}
|
2021-12-09 16:27:48 +01:00
|
|
|
}
|
2022-01-07 09:39:00 +01:00
|
|
|
filterPresets["tags"] = tags
|
|
|
|
}
|
|
|
|
|
|
|
|
return filterPresets
|
|
|
|
}
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/jobs/", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2021-12-09 16:27:48 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/jobs.html", &templates.Page{
|
2021-12-09 16:27:48 +01:00
|
|
|
Title: "Jobs - ClusterCockpit",
|
|
|
|
Config: conf,
|
2022-01-07 09:39:00 +01:00
|
|
|
FilterPresets: buildFilterPresets(r.URL.Query()),
|
2021-12-08 15:50:03 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/job/{id:[0-9]+}", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
job, err := resolver.Query().Job(r.Context(), id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/job.html", &templates.Page{
|
2021-12-16 13:17:48 +01:00
|
|
|
Title: fmt.Sprintf("Job %d - ClusterCockpit", job.JobID),
|
2021-12-08 15:50:03 +01:00
|
|
|
Config: conf,
|
|
|
|
Infos: map[string]interface{}{
|
|
|
|
"id": id,
|
|
|
|
"jobId": job.JobID,
|
2021-12-16 13:17:48 +01:00
|
|
|
"clusterId": job.Cluster,
|
2021-12-08 15:50:03 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/users/", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/users.html", &templates.Page{
|
2021-12-08 15:50:03 +01:00
|
|
|
Title: "Users - ClusterCockpit",
|
|
|
|
Config: conf,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/user/{id}", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id := mux.Vars(r)["id"]
|
|
|
|
// TODO: One could check if the user exists, but that would be unhelpfull if authentication
|
|
|
|
// is disabled or the user does not exist but has started jobs.
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/user.html", &templates.Page{
|
2022-01-07 09:39:00 +01:00
|
|
|
Title: fmt.Sprintf("User %s - ClusterCockpit", id),
|
|
|
|
Config: conf,
|
|
|
|
Infos: map[string]interface{}{"username": id},
|
|
|
|
FilterPresets: buildFilterPresets(r.URL.Query()),
|
2021-12-09 16:27:48 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/analysis/", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filterPresets := map[string]interface{}{}
|
|
|
|
query := r.URL.Query()
|
|
|
|
if query.Get("cluster") != "" {
|
|
|
|
filterPresets["clusterId"] = query.Get("cluster")
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/analysis.html", &templates.Page{
|
2021-12-09 16:27:48 +01:00
|
|
|
Title: "Analysis View - ClusterCockpit",
|
|
|
|
Config: conf,
|
|
|
|
FilterPresets: filterPresets,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/systems/", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filterPresets := map[string]interface{}{}
|
|
|
|
query := r.URL.Query()
|
|
|
|
if query.Get("cluster") != "" {
|
|
|
|
filterPresets["clusterId"] = query.Get("cluster")
|
|
|
|
}
|
|
|
|
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/systems.html", &templates.Page{
|
2021-12-09 16:27:48 +01:00
|
|
|
Title: "System View - ClusterCockpit",
|
|
|
|
Config: conf,
|
|
|
|
FilterPresets: filterPresets,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
router.HandleFunc("/monitoring/node/{clusterId}/{nodeId}", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
conf, err := config.GetUIConfig(r)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2022-01-10 16:14:54 +01:00
|
|
|
templates.Render(rw, r, "monitoring/node.html", &templates.Page{
|
2021-12-09 16:27:48 +01:00
|
|
|
Title: fmt.Sprintf("Node %s - ClusterCockpit", vars["nodeId"]),
|
|
|
|
Config: conf,
|
2021-12-08 15:50:03 +01:00
|
|
|
Infos: map[string]interface{}{
|
2021-12-09 16:27:48 +01:00
|
|
|
"nodeId": vars["nodeId"],
|
|
|
|
"clusterId": vars["clusterId"],
|
2021-12-08 15:50:03 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2022-01-12 11:13:25 +01:00
|
|
|
|
|
|
|
func loadEnv(file string) error {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
s := bufio.NewScanner(bufio.NewReader(f))
|
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
|
|
|
if strings.HasPrefix(line, "#") || len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(line, "#") {
|
|
|
|
return errors.New("'#' are only supported at the start of a line")
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimPrefix(line, "export ")
|
|
|
|
parts := strings.SplitN(line, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return fmt.Errorf("unsupported line: %#v", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
key := strings.TrimSpace(parts[0])
|
|
|
|
val := strings.TrimSpace(parts[1])
|
|
|
|
if strings.HasPrefix(val, "\"") {
|
|
|
|
if !strings.HasSuffix(val, "\"") {
|
|
|
|
return fmt.Errorf("unsupported line: %#v", line)
|
|
|
|
}
|
|
|
|
|
|
|
|
runes := []rune(val[1 : len(val)-1])
|
|
|
|
sb := strings.Builder{}
|
|
|
|
for i := 0; i < len(runes); i++ {
|
|
|
|
if runes[i] == '\\' {
|
|
|
|
i++
|
|
|
|
switch runes[i] {
|
|
|
|
case 'n':
|
|
|
|
sb.WriteRune('\n')
|
|
|
|
case 'r':
|
|
|
|
sb.WriteRune('\r')
|
|
|
|
case 't':
|
|
|
|
sb.WriteRune('\t')
|
|
|
|
case '"':
|
|
|
|
sb.WriteRune('"')
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupprorted escape sequence in quoted string: backslash %#v", runes[i])
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sb.WriteRune(runes[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
val = sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Setenv(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dropPrivileges() error {
|
|
|
|
if programConfig.Group != "" {
|
|
|
|
g, err := user.LookupGroup(programConfig.Group)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gid, _ := strconv.Atoi(g.Gid)
|
|
|
|
if err := syscall.Setgid(gid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if programConfig.User != "" {
|
|
|
|
u, err := user.Lookup(programConfig.User)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
uid, _ := strconv.Atoi(u.Uid)
|
|
|
|
if err := syscall.Setuid(uid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If started via systemd, inform systemd that we are running:
|
|
|
|
// https://www.freedesktop.org/software/systemd/man/sd_notify.html
|
|
|
|
func systemdNotifiy(ready bool, status string) {
|
|
|
|
if os.Getenv("NOTIFY_SOCKET") == "" {
|
|
|
|
// Not started using systemd
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{fmt.Sprintf("--pid=%d", os.Getpid())}
|
|
|
|
if ready {
|
|
|
|
args = append(args, "--ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != "" {
|
|
|
|
args = append(args, fmt.Sprintf("--status=%s", status))
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("systemd-notify", args...)
|
|
|
|
cmd.Run() // errors ignored on purpose, there is not much to do anyways.
|
|
|
|
}
|