mirror of
https://github.com/R0m1k3/Loki.git
synced 2026-10-22 22:48:11 +02:00
Installateurs Unix fusionnes, staticcheck sans avertissement
- sys_install_unix.go porte le parcours commun (arborescence, lien du binaire, /etc/default, chown) ; Linux et macOS ne gardent que leur declaration de services derriere installServices/uninstallServices (395 -> 339 lignes, et surtout une seule copie a corriger) - l'installateur macOS pose desormais les DEUX services (il n'en posait qu'un) - API bbolt depreciee remplacee, message d'erreur duplique factorise - staticcheck.conf : ST1005 ecartee, avec la raison (messages francais montres a l'utilisateur). Le projet est a zero avertissement.
This commit is contained in:
1 parent
7ecd869da0
commit
1aabc0f9c7
6 files changed
+242
-283
No files matched your search
@@ -6,18 +6,17 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// sys_install_darwin.go — installation macOS via launchd (LaunchDaemon), équivalent
|
||||
// de sys_install_linux.go (systemd). ⚠️ NON TESTÉ sur un vrai Mac : le support macOS
|
||||
// était totalement absent (le code systemd/Linux tournait par erreur et échouait
|
||||
// sur « systemctl: command not found » / « /etc/default/ajean », cf. issue #4).
|
||||
// Implémentation prudente ; à valider sur une machine Apple.
|
||||
// sys_install_darwin.go — la part launchd de l'installation. Le parcours commun
|
||||
// (dossiers, lien du binaire, /etc/default, chown) vit dans sys_install_unix.go.
|
||||
//
|
||||
// ⚠️ NON TESTÉ sur un vrai Mac. Le support macOS a été ajouté parce que le code
|
||||
// systemd tournait par erreur et échouait sur « systemctl: command not found ».
|
||||
// Implémentation prudente, à valider sur une machine Apple.
|
||||
|
||||
// launchdPlistTemplate — champs formatés : Label, chemin du binaire, UserName,
|
||||
// launchdPlistTemplate — champs : Label, binaire, argument, UserName,
|
||||
// WorkingDirectory, AJEAN_HOME, StandardOutPath, StandardErrorPath.
|
||||
// KeepAlive/SuccessfulExit=false ≈ Restart=on-failure ; RunAtLoad relance au
|
||||
// boot une fois chargé avec `-w`.
|
||||
@@ -29,7 +28,7 @@ const launchdPlistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<key>ProgramArguments</key>
|
||||
<array>
|
||||
<string>%s</string>
|
||||
<string>serve</string>
|
||||
<string>%s</string>
|
||||
</array>
|
||||
<key>UserName</key><string>%s</string>
|
||||
<key>WorkingDirectory</key><string>%s</string>
|
||||
@@ -44,127 +43,34 @@ const launchdPlistTemplate = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
</plist>
|
||||
`
|
||||
|
||||
func cmdInstall(args []string) error {
|
||||
if os.Geteuid() != 0 {
|
||||
return fmt.Errorf("ajean install doit être exécuté en root (sudo ajean install)")
|
||||
}
|
||||
targetUser := os.Getenv("SUDO_USER")
|
||||
if targetUser == "" {
|
||||
targetUser = "root"
|
||||
}
|
||||
for _, a := range args {
|
||||
if strings.HasPrefix(a, "--user=") {
|
||||
targetUser = strings.TrimPrefix(a, "--user=")
|
||||
// unitArgs associe chaque service à la sous-commande qu'il exécute.
|
||||
func unitArgs() map[string]string {
|
||||
return map[string]string{serviceName(): "serve", uiUnitName: "web"}
|
||||
}
|
||||
|
||||
func installServices(targetUser, ajeanHome string) error {
|
||||
exe := installedExePath()
|
||||
for name, arg := range unitArgs() {
|
||||
logPath := filepath.Join(ajeanHome, name+".log")
|
||||
plist := fmt.Sprintf(launchdPlistTemplate,
|
||||
launchdLabel(name), exe, arg, targetUser, ajeanHome, ajeanHome, logPath, logPath)
|
||||
path := launchdPlistPath(name)
|
||||
if err := os.WriteFile(path, []byte(plist), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf(" %s %s\n", green("✓"), path)
|
||||
}
|
||||
u, err := user.Lookup(targetUser)
|
||||
if err != nil {
|
||||
return fmt.Errorf("utilisateur '%s' introuvable: %w", targetUser, err)
|
||||
}
|
||||
ajeanHome := defaultAjeanHome()
|
||||
if v := os.Getenv("AJEAN_HOME"); v != "" {
|
||||
ajeanHome = v
|
||||
}
|
||||
svc := serviceName()
|
||||
|
||||
fmt.Printf("Installation pour utilisateur %s\n", cyan(targetUser))
|
||||
fmt.Printf(" AJEAN_HOME = %s\n", ajeanHome)
|
||||
fmt.Printf(" service = %s (launchd)\n", svc)
|
||||
|
||||
// 1. Arborescence + configuration de départ
|
||||
if err := provisionDataDir(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. Symlink idempotent vers /usr/local/bin/ajean — même garde-fou que Linux
|
||||
// (issue #5 : ne pas se lier sur soi-même si on tourne déjà depuis la cible).
|
||||
self, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rp, e := filepath.EvalSymlinks(self); e == nil {
|
||||
self = rp
|
||||
}
|
||||
target := installedExePath()
|
||||
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
alreadyInPlace := false
|
||||
if tp, e := filepath.EvalSymlinks(target); e == nil && tp == self {
|
||||
alreadyInPlace = true
|
||||
}
|
||||
if alreadyInPlace {
|
||||
fmt.Printf(" %s %s est déjà le binaire installé (aucun lien à créer)\n", green("✓"), target)
|
||||
} else {
|
||||
_ = os.Remove(target)
|
||||
if err := os.Symlink(self, target); err != nil {
|
||||
if data, err := os.ReadFile(self); err == nil {
|
||||
_ = os.WriteFile(target, data, 0o755)
|
||||
}
|
||||
}
|
||||
fmt.Printf(" %s %s -> %s\n", green("✓"), target, self)
|
||||
}
|
||||
|
||||
// 3. /etc/default/ajean pour que les invocations CLI résolvent AJEAN_HOME.
|
||||
_ = os.MkdirAll("/etc/default", 0o755)
|
||||
defaults := fmt.Sprintf("# Généré par ajean install — racine des données\nAJEAN_HOME=%s\n", ajeanHome)
|
||||
if err := os.WriteFile("/etc/default/ajean", []byte(defaults), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf(" %s /etc/default/ajean\n", green("✓"))
|
||||
|
||||
// 4. LaunchDaemon plist (le log va sous AJEAN_HOME, accessible au user cible)
|
||||
logPath := filepath.Join(ajeanHome, svc+".log")
|
||||
plist := fmt.Sprintf(launchdPlistTemplate, launchdLabel(svc), target, targetUser, ajeanHome, ajeanHome, logPath, logPath)
|
||||
plistPath := launchdPlistPath(svc)
|
||||
if err := os.WriteFile(plistPath, []byte(plist), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf(" %s %s\n", green("✓"), plistPath)
|
||||
|
||||
// 5. chown AJEAN_HOME au user cible
|
||||
chown(ajeanHome, u)
|
||||
|
||||
fmt.Println()
|
||||
fmt.Printf("%s installation terminée.\n", green("[ok]"))
|
||||
fmt.Printf("\nProchaines étapes :\n")
|
||||
fmt.Printf(" 1. édite la config : %s\n", bold("sudo -u "+targetUser+" ajean edit"))
|
||||
fmt.Printf(" (renseigne BIN, MODEL, etc.)\n")
|
||||
fmt.Printf(" 2. démarre le service: %s\n", bold("sudo ajean start"))
|
||||
fmt.Printf(" 3. UI web : %s\n", bold("sudo -u "+targetUser+" ajean web"))
|
||||
return nil
|
||||
}
|
||||
|
||||
func cmdUninstall(args []string) error {
|
||||
if os.Geteuid() != 0 {
|
||||
return fmt.Errorf("ajean uninstall doit être exécuté en root")
|
||||
}
|
||||
svc := serviceName()
|
||||
_ = exec.Command("launchctl", "unload", "-w", launchdPlistPath(svc)).Run()
|
||||
for _, p := range []string{
|
||||
launchdPlistPath(svc),
|
||||
"/etc/default/ajean",
|
||||
installedExePath(),
|
||||
} {
|
||||
if err := os.Remove(p); err == nil {
|
||||
fmt.Printf(" %s %s\n", green("✓"), p)
|
||||
// uninstallServices décharge et efface les deux daemons. L'interface passe
|
||||
// AVANT le moteur : elle sait le redémarrer, l'ordre inverse pourrait le
|
||||
// relancer sous nos pieds.
|
||||
func uninstallServices() {
|
||||
for _, name := range []string{uiUnitName, serviceName()} {
|
||||
_ = exec.Command("launchctl", "unload", "-w", launchdPlistPath(name)).Run()
|
||||
if err := os.Remove(launchdPlistPath(name)); err == nil {
|
||||
fmt.Printf(" %s %s\n", green("✓"), launchdPlistPath(name))
|
||||
}
|
||||
}
|
||||
// On ne supprime jamais AJEAN_HOME automatiquement (modèles, presets).
|
||||
fmt.Println(dim("(données utilisateur conservées — supprime $AJEAN_HOME manuellement si tu veux purger)"))
|
||||
fmt.Println(green("[ok]") + " désinstallé")
|
||||
return nil
|
||||
}
|
||||
|
||||
// chown recursively changes ownership of path to the given user/group.
|
||||
func chown(path string, u *user.User) {
|
||||
var uid, gid int
|
||||
fmt.Sscanf(u.Uid, "%d", &uid)
|
||||
fmt.Sscanf(u.Gid, "%d", &gid)
|
||||
filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
||||
if err == nil {
|
||||
_ = os.Chown(p, uid, gid)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user