164 lines
3.7 KiB
Go
164 lines
3.7 KiB
Go
package node
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"mediarr/cluster"
|
|
"mediarr/database"
|
|
"mediarr/ident"
|
|
"mediarr/identity"
|
|
"time"
|
|
)
|
|
|
|
func (n *Node) Up() error {
|
|
|
|
ctx := context.Background()
|
|
now := time.Now().UnixMilli()
|
|
|
|
for _, principal := range requiredNodePrincipals(now) {
|
|
_, err := n.db.Queries.GetPrincipal(ctx, principal.ID)
|
|
if err == nil {
|
|
continue
|
|
}
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
return err
|
|
}
|
|
|
|
if _, err := n.db.WriteQueries.CreatePrincipal(ctx, principal); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if err := n.ensureStorageBackend(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
return n.ensureClusterIdentity(ctx)
|
|
|
|
}
|
|
|
|
func (n *Node) ensureStorageBackend(ctx context.Context) error {
|
|
_, err := n.db.Queries.GetStorageBackend(ctx, n.backendID)
|
|
switch {
|
|
case err == nil:
|
|
return nil
|
|
case !errors.Is(err, sql.ErrNoRows):
|
|
return fmt.Errorf("stockage : lecture du backend : %w", err)
|
|
}
|
|
|
|
config, err := json.Marshal(map[string]string{"root": n.storagePath})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := n.db.WriteQueries.CreateStorageBackend(ctx, database.CreateStorageBackendParams{
|
|
ID: n.backendID,
|
|
Kind: "hub_fs",
|
|
Name: "local",
|
|
Config: string(config),
|
|
Enabled: 1,
|
|
State: "ok",
|
|
CreatedAt: time.Now().UnixMilli(),
|
|
}); err != nil {
|
|
return fmt.Errorf("stockage : création du backend : %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (n *Node) ensureClusterIdentity(ctx context.Context) error {
|
|
|
|
principal, err := n.db.Queries.GetClusterPrincipal(ctx)
|
|
|
|
switch {
|
|
case err == nil:
|
|
clusterIdentity, loadErr := cluster.LoadOrCreate(n.clusterKeyPath, false)
|
|
if loadErr != nil {
|
|
return fmt.Errorf("cluster : principal présent en base mais clé indisponible : %w", loadErr)
|
|
}
|
|
|
|
if !bytes.Equal(principal.PublicKey, clusterIdentity.PublicKey) {
|
|
return errors.New("cluster : la clé publique de cluster.key ne correspond pas au principal en base")
|
|
}
|
|
|
|
n.cluster = clusterIdentity
|
|
return nil
|
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
clusterIdentity, createErr := cluster.LoadOrCreate(n.clusterKeyPath, true)
|
|
if createErr != nil {
|
|
return createErr
|
|
}
|
|
|
|
clusterID, idErr := ident.NewV7()
|
|
if idErr != nil {
|
|
return idErr
|
|
}
|
|
|
|
_, createErr = n.db.WriteQueries.CreatePrincipal(ctx, database.CreatePrincipalParams{
|
|
ID: clusterID,
|
|
Kind: "cluster",
|
|
Name: "cluster",
|
|
PublicKey: clusterIdentity.PublicKeyBytes(),
|
|
Status: identity.StatusActive,
|
|
CreatedAt: time.Now().UnixMilli(),
|
|
})
|
|
if createErr != nil {
|
|
existing, getErr := n.db.Queries.GetClusterPrincipal(ctx)
|
|
if getErr == nil && bytes.Equal(existing.PublicKey, clusterIdentity.PublicKey) {
|
|
n.cluster = clusterIdentity
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("cluster : création du principal : %w", createErr)
|
|
}
|
|
|
|
n.cluster = clusterIdentity
|
|
return nil
|
|
|
|
default:
|
|
return fmt.Errorf("cluster : lecture du principal : %w", err)
|
|
}
|
|
|
|
}
|
|
|
|
func requiredNodePrincipals(now int64) []database.CreatePrincipalParams {
|
|
|
|
return []database.CreatePrincipalParams{
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000001"),
|
|
Kind: "system",
|
|
Name: "system",
|
|
Status: identity.StatusActive,
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000002"),
|
|
Kind: "group",
|
|
Name: "admins",
|
|
Status: identity.StatusActive,
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000003"),
|
|
Kind: "group",
|
|
Name: "members",
|
|
Status: identity.StatusActive,
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000004"),
|
|
Kind: "group",
|
|
Name: "guests",
|
|
Status: identity.StatusActive,
|
|
CreatedAt: now,
|
|
},
|
|
}
|
|
|
|
}
|