2021-03-31 07:23:48 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-01-12 11:13:25 +01:00
|
|
|
"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"
|
2022-01-31 15:14:37 +01:00
|
|
|
"io"
|
2022-01-12 11:13:25 +01:00
|
|
|
"net"
|
2021-03-31 07:23:48 +02:00
|
|
|
"net/http"
|
2022-02-09 15:03:12 +01:00
|
|
|
"net/url"
|
2021-03-31 07:23:48 +02:00
|
|
|
"os"
|
2022-01-12 11:13:25 +01:00
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2021-03-31 07:23:48 +02:00
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
|
|
|
"github.com/99designs/gqlgen/graphql/playground"
|
2022-01-27 09:40:59 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/api"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/auth"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/config"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/graph"
|
|
|
|
"github.com/ClusterCockpit/cc-backend/graph/generated"
|
2022-01-27 10:35:26 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/log"
|
2022-01-27 09:40:59 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/metricdata"
|
2022-02-07 07:09:47 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/repository"
|
2022-01-27 09:40:59 +01:00
|
|
|
"github.com/ClusterCockpit/cc-backend/templates"
|
2021-03-31 07:23:48 +02:00
|
|
|
"github.com/gorilla/handlers"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2022-01-20 10:00:55 +01:00
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2021-03-31 07:23:48 +02:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2021-11-26 10:34:29 +01:00
|
|
|
var db *sqlx.DB
|
2022-02-10 18:48:58 +01:00
|
|
|
var jobRepo *repository.JobRepository
|
2021-11-26 10:34:29 +01:00
|
|
|
|
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"`
|
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
// 'sqlite3' or 'mysql' (mysql will work for mariadb as well)
|
|
|
|
DBDriver string `json:"db-driver"`
|
|
|
|
|
|
|
|
// For sqlite3 a filename, for mysql a DSN in this format: https://github.com/go-sql-driver/mysql#dsn-data-source-name (Without query parameters!).
|
2021-12-16 09:35:03 +01:00
|
|
|
DB string `json:"db"`
|
|
|
|
|
|
|
|
// Path to the job-archive
|
|
|
|
JobArchive string `json:"job-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",
|
2022-01-20 10:00:55 +01:00
|
|
|
DBDriver: "sqlite3",
|
2021-12-08 10:15:25 +01:00
|
|
|
DB: "./var/job.db",
|
|
|
|
JobArchive: "./var/job-archive",
|
2021-12-16 09:35:03 +01:00
|
|
|
DisableArchive: false,
|
2022-02-15 10:03:09 +01:00
|
|
|
LdapConfig: nil,
|
|
|
|
HttpsCertFile: "",
|
|
|
|
HttpsKeyFile: "",
|
2021-12-08 10:15:25 +01:00
|
|
|
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"},
|
2022-01-17 13:31:40 +01:00
|
|
|
"plot_view_plotsPerRow": 2,
|
2021-12-08 10:15:25 +01:00
|
|
|
"plot_view_showPolarplot": true,
|
|
|
|
"plot_view_showRoofline": true,
|
|
|
|
"plot_view_showStatTable": true,
|
2022-02-02 13:59:08 +01:00
|
|
|
"system_view_selectedMetric": "cpu_load",
|
2021-12-08 10:15:25 +01:00
|
|
|
},
|
|
|
|
}
|
2021-10-26 10:24:43 +02:00
|
|
|
|
2022-02-03 09:39:04 +01:00
|
|
|
func setupJobRoute(i InfoType, r *http.Request) InfoType {
|
|
|
|
i["id"] = mux.Vars(r)["id"]
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupUserRoute(i InfoType, r *http.Request) InfoType {
|
|
|
|
i["id"] = mux.Vars(r)["id"]
|
2022-02-03 11:00:59 +01:00
|
|
|
i["username"] = mux.Vars(r)["id"]
|
2022-02-03 09:39:04 +01:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupClusterRoute(i InfoType, r *http.Request) InfoType {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
i["id"] = vars["cluster"]
|
|
|
|
i["cluster"] = vars["cluster"]
|
|
|
|
from, to := r.URL.Query().Get("from"), r.URL.Query().Get("to")
|
|
|
|
if from != "" || to != "" {
|
|
|
|
i["from"] = from
|
|
|
|
i["to"] = to
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupNodeRoute(i InfoType, r *http.Request) InfoType {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
i["cluster"] = vars["cluster"]
|
|
|
|
i["hostname"] = vars["hostname"]
|
|
|
|
from, to := r.URL.Query().Get("from"), r.URL.Query().Get("to")
|
|
|
|
if from != "" || to != "" {
|
|
|
|
i["from"] = from
|
|
|
|
i["to"] = to
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-02-03 10:42:15 +01:00
|
|
|
func setupAnalysisRoute(i InfoType, r *http.Request) InfoType {
|
|
|
|
i["cluster"] = mux.Vars(r)["cluster"]
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-02-10 18:48:58 +01:00
|
|
|
func setupTaglistRoute(i InfoType, r *http.Request) InfoType {
|
|
|
|
tags, counts, _ := jobRepo.GetTags()
|
|
|
|
tagMap := make(map[string][]map[string]interface{})
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
tagItem := map[string]interface{}{
|
|
|
|
"id": tag.ID,
|
|
|
|
"name": tag.Name,
|
|
|
|
"count": counts[tag.Name],
|
|
|
|
}
|
|
|
|
tagMap[tag.Type] = append(tagMap[tag.Type], tagItem)
|
|
|
|
}
|
|
|
|
log.Infof("TAGS %+v", tags)
|
|
|
|
i["tagmap"] = tagMap
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-02-03 09:39:04 +01:00
|
|
|
var routes []Route = []Route{
|
2022-02-15 13:18:27 +01:00
|
|
|
{"/", "home.tmpl", "ClusterCockpit", false, func(i InfoType, r *http.Request) InfoType { i["clusters"] = config.Clusters; return i }},
|
2022-02-03 09:39:04 +01:00
|
|
|
{"/monitoring/jobs/", "monitoring/jobs.tmpl", "Jobs - ClusterCockpit", true, func(i InfoType, r *http.Request) InfoType { return i }},
|
|
|
|
{"/monitoring/job/{id:[0-9]+}", "monitoring/job.tmpl", "Job <ID> - ClusterCockpit", false, setupJobRoute},
|
|
|
|
{"/monitoring/users/", "monitoring/list.tmpl", "Users - ClusterCockpit", true, func(i InfoType, r *http.Request) InfoType { i["listType"] = "USER"; return i }},
|
|
|
|
{"/monitoring/projects/", "monitoring/list.tmpl", "Projects - ClusterCockpit", true, func(i InfoType, r *http.Request) InfoType { i["listType"] = "PROJECT"; return i }},
|
2022-02-10 18:48:58 +01:00
|
|
|
{"/monitoring/tags/", "monitoring/taglist.tmpl", "Tags - ClusterCockpit", false, setupTaglistRoute},
|
2022-02-03 09:39:04 +01:00
|
|
|
{"/monitoring/user/{id}", "monitoring/user.tmpl", "User <ID> - ClusterCockpit", true, setupUserRoute},
|
|
|
|
{"/monitoring/systems/{cluster}", "monitoring/systems.tmpl", "Cluster <ID> - ClusterCockpit", false, setupClusterRoute},
|
|
|
|
{"/monitoring/node/{cluster}/{hostname}", "monitoring/node.tmpl", "Node <ID> - ClusterCockpit", false, setupNodeRoute},
|
2022-02-03 10:42:15 +01:00
|
|
|
{"/monitoring/analysis/{cluster}", "monitoring/analysis.tmpl", "Analaysis - ClusterCockpit", true, setupAnalysisRoute},
|
2022-02-03 09:39:04 +01: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-27 09:29:11 +01:00
|
|
|
flag.StringVar(&flagNewUser, "add-user", "", "Add a new user. Argument format: `<username>:[admin,api,user]:<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
|
|
|
|
2022-01-31 15:14:37 +01:00
|
|
|
if strings.HasPrefix(programConfig.DB, "env:") {
|
|
|
|
envvar := strings.TrimPrefix(programConfig.DB, "env:")
|
|
|
|
programConfig.DB = os.Getenv(envvar)
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
var err error
|
2022-01-20 10:00:55 +01:00
|
|
|
if programConfig.DBDriver == "sqlite3" {
|
|
|
|
db, err = sqlx.Open("sqlite3", fmt.Sprintf("%s?_foreign_keys=on", programConfig.DB))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.SetMaxOpenConns(1)
|
|
|
|
} else if programConfig.DBDriver == "mysql" {
|
|
|
|
db, err = sqlx.Open("mysql", fmt.Sprintf("%s?multiStatements=true", programConfig.DB))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-10-11 11:11:14 +02:00
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
db.SetConnMaxLifetime(time.Minute * 3)
|
|
|
|
db.SetMaxOpenConns(10)
|
|
|
|
db.SetMaxIdleConns(10)
|
|
|
|
} else {
|
|
|
|
log.Fatalf("unsupported database driver: %s", programConfig.DBDriver)
|
|
|
|
}
|
2021-12-16 09:35:03 +01:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
// Initialize sub-modules...
|
2021-11-26 10:34:29 +01:00
|
|
|
|
2022-02-14 14:22:44 +01:00
|
|
|
authentication := &auth.Authentication{}
|
2021-12-08 10:15:25 +01:00
|
|
|
if !programConfig.DisableAuthentication {
|
2022-02-14 14:22:44 +01:00
|
|
|
if err := authentication.Init(db, programConfig.LdapConfig); err != nil {
|
2021-12-08 10:15:25 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-03-31 07:23:48 +02:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagNewUser != "" {
|
2022-02-14 14:22:44 +01:00
|
|
|
if err := authentication.AddUser(flagNewUser); err != nil {
|
2021-12-08 10:15:25 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if flagDelUser != "" {
|
2022-02-14 14:22:44 +01:00
|
|
|
if err := authentication.DelUser(flagDelUser); err != nil {
|
2021-12-08 10:15:25 +01:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 10:34:29 +01:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
if flagSyncLDAP {
|
2022-02-14 14:22:44 +01:00
|
|
|
if err := authentication.SyncWithLDAP(true); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-12-08 10:15:25 +01:00
|
|
|
}
|
2022-01-10 16:14:54 +01:00
|
|
|
|
|
|
|
if flagGenJWT != "" {
|
2022-02-14 14:22:44 +01:00
|
|
|
user, err := authentication.FetchUser(flagGenJWT)
|
2022-01-10 16:14:54 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-01-27 09:29:11 +01:00
|
|
|
if !user.HasRole(auth.RoleApi) {
|
2022-01-27 10:35:26 +01:00
|
|
|
log.Warn("that user does not have the API role")
|
2022-01-10 16:14:54 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 14:22:44 +01:00
|
|
|
jwt, err := authentication.ProvideJWT(user)
|
2022-01-10 16:14:54 +01:00
|
|
|
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 != "" {
|
2022-01-27 10:35:26 +01:00
|
|
|
log.Fatal("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}
|
2022-01-31 15:14:37 +01:00
|
|
|
if err := resolver.Init(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-12-08 15:50:03 +01:00
|
|
|
graphQLEndpoint := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: resolver}))
|
2022-01-20 10:00:55 +01:00
|
|
|
if os.Getenv("DEBUG") != "1" {
|
|
|
|
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)
|
|
|
|
}
|
2022-01-10 16:14:54 +01:00
|
|
|
|
2022-01-20 10:00:55 +01:00
|
|
|
return errors.New("internal server error (panic)")
|
|
|
|
})
|
|
|
|
}
|
2022-01-10 16:14:54 +01:00
|
|
|
|
2022-02-10 18:48:58 +01:00
|
|
|
jobRepo = &repository.JobRepository{DB: db}
|
|
|
|
|
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-02-10 18:48:58 +01:00
|
|
|
JobRepository: jobRepo,
|
2022-01-07 09:39:00 +01:00
|
|
|
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-02-01 17:48:56 +01:00
|
|
|
templates.Render(rw, r, "login.tmpl", &templates.Page{
|
2021-12-08 10:15:25 +01:00
|
|
|
Title: "Login",
|
|
|
|
})
|
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-02-01 17:48:56 +01:00
|
|
|
templates.Render(rw, r, "404.tmpl", &templates.Page{
|
2021-12-08 10:15:25 +01:00
|
|
|
Title: "Not found",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.Handle("/playground", graphQLPlayground)
|
2022-02-03 09:39:04 +01:00
|
|
|
|
2021-12-08 10:15:25 +01:00
|
|
|
r.HandleFunc("/login", handleGetLogin).Methods(http.MethodGet)
|
2022-02-03 09:39:04 +01:00
|
|
|
r.HandleFunc("/imprint", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
templates.Render(rw, r, "imprint.tmpl", &templates.Page{
|
|
|
|
Title: "Imprint",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
r.HandleFunc("/privacy", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
templates.Render(rw, r, "privacy.tmpl", &templates.Page{
|
|
|
|
Title: "Privacy",
|
|
|
|
})
|
|
|
|
})
|
2021-12-08 10:15:25 +01:00
|
|
|
|
|
|
|
secured := r.PathPrefix("/").Subrouter()
|
|
|
|
if !programConfig.DisableAuthentication {
|
2022-02-14 14:22:44 +01:00
|
|
|
r.Handle("/login", authentication.Login(
|
|
|
|
// On success:
|
|
|
|
http.RedirectHandler("/", http.StatusTemporaryRedirect),
|
|
|
|
|
|
|
|
// On failure:
|
|
|
|
func(rw http.ResponseWriter, r *http.Request, loginErr error) {
|
|
|
|
rw.WriteHeader(http.StatusUnauthorized)
|
|
|
|
templates.Render(rw, r, "login.tmpl", &templates.Page{
|
|
|
|
Title: "Login failed - ClusterCockpit",
|
2022-02-15 12:39:16 +01:00
|
|
|
Error: loginErr.Error(),
|
2022-02-14 14:22:44 +01:00
|
|
|
})
|
|
|
|
})).Methods(http.MethodPost)
|
|
|
|
|
|
|
|
r.Handle("/logout", authentication.Logout(http.RedirectHandler("/login", http.StatusTemporaryRedirect))).Methods(http.MethodPost)
|
|
|
|
|
|
|
|
secured.Use(func(next http.Handler) http.Handler {
|
|
|
|
return authentication.Auth(
|
|
|
|
// On success;
|
|
|
|
next,
|
|
|
|
|
|
|
|
// On failure:
|
|
|
|
func(rw http.ResponseWriter, r *http.Request, authErr error) {
|
|
|
|
rw.WriteHeader(http.StatusUnauthorized)
|
|
|
|
templates.Render(rw, r, "login.tmpl", &templates.Page{
|
|
|
|
Title: "Authentication failed - ClusterCockpit",
|
|
|
|
Error: err.Error(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2021-12-08 10:15:25 +01:00
|
|
|
}
|
|
|
|
secured.Handle("/query", graphQLEndpoint)
|
2021-12-16 09:35:03 +01:00
|
|
|
|
2022-02-09 15:03:12 +01:00
|
|
|
secured.HandleFunc("/search", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if search := r.URL.Query().Get("searchId"); search != "" {
|
|
|
|
job, username, err := api.JobRepository.FindJobOrUser(r.Context(), search)
|
|
|
|
if err == repository.ErrNotFound {
|
|
|
|
http.Redirect(rw, r, "/monitoring/jobs/?jobId="+url.QueryEscape(search), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
http.Error(rw, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if username != "" {
|
|
|
|
http.Redirect(rw, r, "/monitoring/user/"+username, http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
http.Redirect(rw, r, fmt.Sprintf("/monitoring/job/%d", job), http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http.Error(rw, "'searchId' query parameter missing", http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-02-03 09:39:04 +01:00
|
|
|
setupRoutes(secured, routes)
|
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)))
|
2022-01-31 15:14:37 +01:00
|
|
|
r.Use(handlers.CompressHandler)
|
|
|
|
r.Use(handlers.CORS(
|
2021-12-08 10:15:25 +01:00
|
|
|
handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"}),
|
|
|
|
handlers.AllowedMethods([]string{"GET", "POST", "HEAD", "OPTIONS"}),
|
2022-01-31 15:14:37 +01:00
|
|
|
handlers.AllowedOrigins([]string{"*"})))
|
|
|
|
handler := handlers.CustomLoggingHandler(log.InfoWriter, r, func(w io.Writer, params handlers.LogFormatterParams) {
|
|
|
|
log.Finfof(w, "%s %s (Response: %d, Size: %d)", params.Request.Method, params.URL.RequestURI(), params.StatusCode, params.Size)
|
|
|
|
})
|
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
|
|
|
}
|