70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
package node
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"mediarr/database"
|
|
"mediarr/ident"
|
|
"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
|
|
}
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func requiredNodePrincipals(now int64) []database.CreatePrincipalParams {
|
|
|
|
return []database.CreatePrincipalParams{
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000001"),
|
|
Kind: "system",
|
|
Name: "system",
|
|
Status: "active",
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000002"),
|
|
Kind: "group",
|
|
Name: "admins",
|
|
Status: "active",
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000003"),
|
|
Kind: "group",
|
|
Name: "members",
|
|
Status: "active",
|
|
CreatedAt: now,
|
|
},
|
|
{
|
|
ID: ident.MustParse("00000000-0000-7000-8000-000000000004"),
|
|
Kind: "group",
|
|
Name: "guests",
|
|
Status: "active",
|
|
CreatedAt: now,
|
|
},
|
|
}
|
|
|
|
}
|