package web import ( "context" "net/http" "strings" "gitea.mynodes.xyz/ilker/llmgw/internal/config" "gitea.mynodes.xyz/ilker/llmgw/internal/slurm" "gitea.mynodes.xyz/ilker/llmgw/internal/store" ) // SlurmKeys keeps the SSH keys a person logs in to the Slurm cluster with. type SlurmKeys interface { SlurmUserByID(ctx context.Context, userID string) (store.SlurmUser, error) SetSlurmSSHKeys(ctx context.Context, userID string, keys []string) error } // SetSlurm has the profile page offer the Slurm cluster's login and SSH // keys while settings say llmgw makes its users. func (p *AccountPages) SetSlurm(keys SlurmKeys, settings func() config.SlurmSettings) { p.slurmKeys, p.slurmSettings = keys, settings } // slurmView is the profile's Slurm part. type slurmView struct { Login string UID int Home string // Host is where jobs are submitted: the controller. Host string Keys string Saved bool Error string } // slurmOn is the settings, and whether the page offers the part. func (p *AccountPages) slurmOn() (config.SlurmSettings, bool) { if p.slurmKeys == nil || p.slurmSettings == nil { return config.SlurmSettings{}, false } s := p.slurmSettings() return s, s.Enabled && s.Users == config.UsersLlmgw } // fillSlurm puts the person's Slurm login in d, keeping what d says of a // save. func (p *AccountPages) fillSlurm(ctx context.Context, user store.User, d *profileData) { set, on := p.slurmOn() if !on || !user.CanSignIn() { return } u, err := p.slurmKeys.SlurmUserByID(ctx, user.ID) if err != nil { p.log.Error("could not read a person's Slurm login", "user", user.ID, "err", err) return } v := &slurmView{Login: u.Login, UID: u.UID, Home: set.HomeDir(u.Login), Host: set.Controller, Keys: strings.Join(u.SSHKeys, "\n")} if d.Slurm != nil { v.Saved, v.Error = d.Slurm.Saved, d.Slurm.Error if v.Error != "" { v.Keys = d.Slurm.Keys } } d.Slurm = v } func (p *AccountPages) setSlurmKeys(w http.ResponseWriter, r *http.Request) { noStore(w) sess, user, ok := p.session(r) if !ok { http.Redirect(w, r, "/login", http.StatusSeeOther) return } if _, on := p.slurmOn(); !on { http.NotFound(w, r) return } if !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) { badForm(w, r) return } l := langOf(r) text := r.FormValue("keys") keys, err := slurm.ParseSSHKeys(text) if err != nil { p.render(w, r, sess, user, profileData{Slurm: &slurmView{Keys: text, Error: l.T("profile.slurm.err_keys", err.Error())}}) return } if err := p.slurmKeys.SetSlurmSSHKeys(r.Context(), user.ID, keys); err != nil { p.log.Error("could not keep a person's SSH keys", "user", user.ID, "err", err) p.render(w, r, sess, user, profileData{Slurm: &slurmView{Keys: text, Error: l.T("profile.err.try_again")}}) return } p.log.Info("a user changed their SSH keys for Slurm", "user", user.ID, "keys", len(keys)) p.render(w, r, sess, user, profileData{Slurm: &slurmView{Saved: true}}) }