Add context

This commit adds and passes context around to (almost) every function.
This allows cancelling (almost) every function call globally.
This commit is contained in:
Tom Wiesing 2022-11-28 13:30:08 +01:00
parent 996ecb9f80
commit 3455f491ca
No known key found for this signature in database
104 changed files with 836 additions and 511 deletions

View file

@ -1,6 +1,7 @@
package barrel
import (
"context"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
@ -15,40 +16,40 @@ import (
// Build builds or rebuilds the barel connected to this instance.
//
// It also logs the current time into the metadata belonging to this instance.
func (barrel *Barrel) Build(stream stream.IOStream, start bool) error {
if !barrel.Locker.TryLock() {
func (barrel *Barrel) Build(ctx context.Context, stream stream.IOStream, start bool) error {
if !barrel.Locker.TryLock(ctx) {
err := locker.Locked
return err
}
defer barrel.Locker.Unlock()
defer barrel.Locker.Unlock(ctx)
stack := barrel.Stack()
var context component.InstallationContext
{
err := stack.Install(stream, context)
err := stack.Install(ctx, stream, context)
if err != nil {
return err
}
}
{
err := stack.Update(stream, start)
err := stack.Update(ctx, stream, start)
if err != nil {
return err
}
}
// store the current last rebuild
return barrel.setLastRebuild()
return barrel.setLastRebuild(ctx)
}
// TODO: Move this to time.Time
var lastRebuild = mstore.For[int64]("lastRebuild")
func (barrel Barrel) LastRebuild() (t time.Time, err error) {
epoch, err := lastRebuild.Get(barrel.MStore)
func (barrel Barrel) LastRebuild(ctx context.Context) (t time.Time, err error) {
epoch, err := lastRebuild.Get(ctx, barrel.MStore)
if err == meta.ErrMetadatumNotSet {
return t, nil
}
@ -60,8 +61,8 @@ func (barrel Barrel) LastRebuild() (t time.Time, err error) {
return time.Unix(epoch, 0), nil
}
func (barrel *Barrel) setLastRebuild() error {
return lastRebuild.Set(barrel.MStore, time.Now().Unix())
func (barrel *Barrel) setLastRebuild(ctx context.Context) error {
return lastRebuild.Set(ctx, barrel.MStore, time.Now().Unix())
}
type LastRebuildFetcher struct {
@ -70,7 +71,7 @@ type LastRebuildFetcher struct {
Barrel *Barrel
}
func (lbr *LastRebuildFetcher) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.LastRebuild, _ = lbr.Barrel.LastRebuild()
func (lbr *LastRebuildFetcher) Fetch(ctx context.Context, flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.LastRebuild, _ = lbr.Barrel.LastRebuild(ctx)
return
}

View file

@ -1,6 +1,7 @@
package drush
import (
"context"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
@ -15,8 +16,8 @@ var errCronFailed = exit.Error{
ExitCode: exit.ExitGeneric,
}
func (drush *Drush) Cron(io stream.IOStream) error {
code, err := drush.Barrel.Shell(io, "/runtime/cron.sh")
func (drush *Drush) Cron(ctx context.Context, io stream.IOStream) error {
code, err := drush.Barrel.Shell(ctx, io, "/runtime/cron.sh")
if err != nil {
io.EPrintln(err)
}
@ -29,9 +30,9 @@ func (drush *Drush) Cron(io stream.IOStream) error {
return nil
}
func (drush *Drush) LastCron(server *phpx.Server) (t time.Time, err error) {
func (drush *Drush) LastCron(ctx context.Context, server *phpx.Server) (t time.Time, err error) {
var timestamp int64
err = drush.PHP.EvalCode(server, &timestamp, `$val = \Drupal::state()->get('system.cron_last'); return $val; `)
err = drush.PHP.EvalCode(ctx, server, &timestamp, `$val = \Drupal::state()->get('system.cron_last'); return $val; `)
if err != nil {
return
}
@ -49,6 +50,6 @@ func (lbr *LastCronFetcher) Fetch(flags ingredient.FetcherFlags, info *status.Wi
return
}
info.LastRebuild, _ = lbr.Drush.LastCron(flags.Server)
info.LastRebuild, _ = lbr.Drush.LastCron(flags.Context, flags.Server)
return
}

View file

@ -1,6 +1,7 @@
package drush
import (
"context"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta"
@ -18,8 +19,8 @@ var errBlindUpdateFailed = exit.Error{
}
// Update performs a blind drush update
func (drush *Drush) Update(io stream.IOStream) error {
code, err := drush.Barrel.Shell(io, "/runtime/blind_update.sh")
func (drush *Drush) Update(ctx context.Context, io stream.IOStream) error {
code, err := drush.Barrel.Shell(ctx, io, "/runtime/blind_update.sh")
if err != nil {
return errBlindUpdateFailed.WithMessageF(drush.Slug, environment.ExecCommandError)
}
@ -27,13 +28,13 @@ func (drush *Drush) Update(io stream.IOStream) error {
return errBlindUpdateFailed.WithMessageF(drush.Slug, code)
}
return drush.setLastUpdate()
return drush.setLastUpdate(ctx)
}
const lastUpdate = mstore.For[int64]("lastUpdate")
func (drush *Drush) LastUpdate() (t time.Time, err error) {
epoch, err := lastUpdate.Get(drush.MStore)
func (drush *Drush) LastUpdate(ctx context.Context) (t time.Time, err error) {
epoch, err := lastUpdate.Get(ctx, drush.MStore)
if err == meta.ErrMetadatumNotSet {
return t, nil
}
@ -45,8 +46,8 @@ func (drush *Drush) LastUpdate() (t time.Time, err error) {
return time.Unix(epoch, 0), nil
}
func (drush *Drush) setLastUpdate() error {
return lastUpdate.Set(drush.MStore, time.Now().Unix())
func (drush *Drush) setLastUpdate(ctx context.Context) error {
return lastUpdate.Set(ctx, drush.MStore, time.Now().Unix())
}
type LastUpdateFetcher struct {
@ -56,6 +57,6 @@ type LastUpdateFetcher struct {
}
func (lbr *LastUpdateFetcher) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.LastUpdate, err = lbr.Drush.LastUpdate()
info.LastUpdate, err = lbr.Drush.LastUpdate(flags.Context)
return
}

View file

@ -1,6 +1,7 @@
package provisioner
import (
"context"
"errors"
"strings"
@ -19,10 +20,10 @@ type Provisioner struct {
}
// Provision provisions an instance, assuming that the required databases already exist.
func (provision *Provisioner) Provision(io stream.IOStream) error {
func (provision *Provisioner) Provision(ctx context.Context, io stream.IOStream) error {
// build the container
if err := provision.Barrel.Build(io, false); err != nil {
if err := provision.Barrel.Build(ctx, io, false); err != nil {
return err
}
@ -53,7 +54,7 @@ func (provision *Provisioner) Provision(io stream.IOStream) error {
// TODO: Move the provision script into the control plane!
provisionScript := "sudo PATH=$PATH -u www-data /bin/bash /provision_container.sh " + strings.Join(provisionParams, " ")
code, err := provision.Barrel.Stack().Run(io, true, "barrel", "/bin/bash", "-c", provisionScript)
code, err := provision.Barrel.Stack().Run(ctx, io, true, "barrel", "/bin/bash", "-c", provisionScript)
if err != nil {
return err
}

View file

@ -1,14 +1,16 @@
package barrel
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/tkw1536/goprogram/stream"
)
// Running checks if this WissKI is currently running.
func (barrel *Barrel) Running() (bool, error) {
ps, err := barrel.Stack().Ps(stream.FromNil())
func (barrel *Barrel) Running(ctx context.Context) (bool, error) {
ps, err := barrel.Stack().Ps(ctx, stream.FromNil())
if err != nil {
return false, err
}
@ -22,6 +24,6 @@ type RunningFetcher struct {
}
func (rf *RunningFetcher) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.Running, err = rf.Barrel.Running()
info.Running, err = rf.Barrel.Running(flags.Context)
return
}

View file

@ -1,8 +1,12 @@
package barrel
import "github.com/tkw1536/goprogram/stream"
import (
"context"
"github.com/tkw1536/goprogram/stream"
)
// Shell executes a shell command inside the instance.
func (barrel *Barrel) Shell(io stream.IOStream, argv ...string) (int, error) {
return barrel.Stack().Exec(io, "barrel", "/bin/sh", append([]string{"/user_shell.sh"}, argv...)...)
func (barrel *Barrel) Shell(ctx context.Context, io stream.IOStream, argv ...string) (int, error) {
return barrel.Stack().Exec(ctx, io, "barrel", "/bin/sh", append([]string{"/user_shell.sh"}, argv...)...)
}

View file

@ -1,6 +1,8 @@
package bookkeeping
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
)
@ -11,8 +13,8 @@ type Bookkeeping struct {
}
// Save saves this instance in the bookkeeping table
func (bk *Bookkeeping) Save() error {
sdb, err := bk.Malt.SQL.QueryTable(false, models.InstanceTable)
func (bk *Bookkeeping) Save(ctx context.Context) error {
sdb, err := bk.Malt.SQL.QueryTable(ctx, false, models.InstanceTable)
if err != nil {
return err
}
@ -27,8 +29,8 @@ func (bk *Bookkeeping) Save() error {
}
// Delete deletes this instance from the bookkeeping table
func (bk *Bookkeeping) Delete() error {
sdb, err := bk.Malt.SQL.QueryTable(false, models.InstanceTable)
func (bk *Bookkeeping) Delete(ctx context.Context) error {
sdb, err := bk.Malt.SQL.QueryTable(ctx, false, models.InstanceTable)
if err != nil {
return err
}

View file

@ -1,6 +1,8 @@
package ingredient
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
)
@ -15,6 +17,7 @@ type WissKIFetcher interface {
// FetcherFlags describes options for a WissKIFetcher
type FetcherFlags struct {
Quick bool
Server *phpx.Server
Context context.Context
Quick bool
Server *phpx.Server
}

View file

@ -1,6 +1,7 @@
package info
import (
"context"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/status"
@ -21,10 +22,11 @@ type Info struct {
// Information fetches information about this WissKI.
// TODO: Rework this to be able to determine what kind of information is available.
func (wisski *Info) Information(quick bool) (info status.WissKI, err error) {
func (wisski *Info) Information(ctx context.Context, quick bool) (info status.WissKI, err error) {
// setup flags
flags := ingredient.FetcherFlags{
Quick: quick,
Quick: quick,
Context: ctx,
}
// potentially setup a new server

View file

@ -16,6 +16,6 @@ func (lbr *SnapshotsFetcher) Fetch(flags ingredient.FetcherFlags, info *status.W
return
}
info.Snapshots, _ = lbr.Snapshots()
info.Snapshots, _ = lbr.Snapshots(flags.Context)
return
}

View file

@ -1,6 +1,8 @@
package locker
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
"github.com/tkw1536/goprogram/exit"
@ -17,8 +19,8 @@ var Locked = exit.Error{
}
// TryLock attemps to lock this WissKI and returns if it suceeded
func (lock *Locker) TryLock() bool {
table, err := lock.Malt.SQL.QueryTable(true, models.LockTable)
func (lock *Locker) TryLock(ctx context.Context) bool {
table, err := lock.Malt.SQL.QueryTable(ctx, true, models.LockTable)
if err != nil {
return false
}
@ -29,8 +31,8 @@ func (lock *Locker) TryLock() bool {
// TryUnlock attempts to unlock this WissKI and reports if it succeeded.
// An unlock can only
func (lock *Locker) TryUnlock() bool {
table, err := lock.Malt.SQL.QueryTable(true, models.LockTable)
func (lock *Locker) TryUnlock(ctx context.Context) bool {
table, err := lock.Malt.SQL.QueryTable(ctx, true, models.LockTable)
if err != nil {
return false
}
@ -39,6 +41,6 @@ func (lock *Locker) TryUnlock() bool {
}
// Unlock unlocks this WissKI, ignoring any error.
func (lock *Locker) Unlock() {
lock.TryUnlock()
func (lock *Locker) Unlock(ctx context.Context) {
lock.TryUnlock(ctx)
}

View file

@ -1,14 +1,17 @@
package locker
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/models"
"github.com/FAU-CDI/wisski-distillery/internal/status"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
)
// Locked checks if this WissKI is currently locked.
func (lock *Locker) Locked() (locked bool) {
table, err := lock.SQL.QueryTable(true, models.LockTable)
// If an error occurs, the instance is considered not locked.
func (lock *Locker) Locked(ctx context.Context) (locked bool) {
table, err := lock.SQL.QueryTable(ctx, true, models.LockTable)
if err != nil {
return false
}
@ -19,6 +22,6 @@ func (lock *Locker) Locked() (locked bool) {
}
func (locker *Locker) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.Locked = locker.Locked()
info.Locked = locker.Locked(flags.Context)
return
}

View file

@ -1,6 +1,8 @@
package mstore
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component/meta"
"github.com/FAU-CDI/wisski-distillery/internal/wisski/ingredient"
)
@ -14,26 +16,26 @@ type MStore struct {
// For is a Store for the provided value
type For[Value any] meta.TypedKey[Value]
func (f For[Value]) Get(m *MStore) (value Value, err error) {
return meta.TypedKey[Value](f).Get(m.Storage)
func (f For[Value]) Get(ctx context.Context, m *MStore) (value Value, err error) {
return meta.TypedKey[Value](f).Get(ctx, m.Storage)
}
func (f For[Value]) GetAll(m *MStore) (values []Value, err error) {
return meta.TypedKey[Value](f).GetAll(m.Storage)
func (f For[Value]) GetAll(ctx context.Context, m *MStore) (values []Value, err error) {
return meta.TypedKey[Value](f).GetAll(ctx, m.Storage)
}
func (f For[Value]) GetOrSet(m *MStore, dflt Value) (value Value, err error) {
return meta.TypedKey[Value](f).GetOrSet(m.Storage, dflt)
func (f For[Value]) GetOrSet(ctx context.Context, m *MStore, dflt Value) (value Value, err error) {
return meta.TypedKey[Value](f).GetOrSet(ctx, m.Storage, dflt)
}
func (f For[Value]) Set(m *MStore, value Value) error {
return meta.TypedKey[Value](f).Set(m.Storage, value)
func (f For[Value]) Set(ctx context.Context, m *MStore, value Value) error {
return meta.TypedKey[Value](f).Set(ctx, m.Storage, value)
}
func (f For[Value]) SetAll(m *MStore, values ...Value) error {
return meta.TypedKey[Value](f).SetAll(m.Storage, values...)
func (f For[Value]) SetAll(ctx context.Context, m *MStore, values ...Value) error {
return meta.TypedKey[Value](f).SetAll(ctx, m.Storage, values...)
}
func (f For[Value]) Delete(m *MStore) error {
return m.Storage.Delete(meta.Key(f))
func (f For[Value]) Delete(ctx context.Context, m *MStore) error {
return m.Storage.Delete(ctx, meta.Key(f))
}

View file

@ -1,6 +1,7 @@
package extras
import (
"context"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
@ -22,8 +23,8 @@ var pathbuilderPHP string
// All returns the ids of all pathbuilders in consistent order.
//
// server is the server to fetch the pathbuilders from, any may be nil.
func (pathbuilder *Pathbuilder) All(server *phpx.Server) (ids []string, err error) {
err = pathbuilder.PHP.ExecScript(server, &ids, pathbuilderPHP, "all_list")
func (pathbuilder *Pathbuilder) All(ctx context.Context, server *phpx.Server) (ids []string, err error) {
err = pathbuilder.PHP.ExecScript(ctx, server, &ids, pathbuilderPHP, "all_list")
slices.Sort(ids)
return
}
@ -32,16 +33,16 @@ func (pathbuilder *Pathbuilder) All(server *phpx.Server) (ids []string, err erro
// If it does not exist, it returns the empty string and nil error.
//
// server is the server to fetch the pathbuilders from, any may be nil.
func (pathbuilder *Pathbuilder) Get(server *phpx.Server, id string) (xml string, err error) {
err = pathbuilder.PHP.ExecScript(server, &xml, pathbuilderPHP, "one_xml", id)
func (pathbuilder *Pathbuilder) Get(ctx context.Context, server *phpx.Server, id string) (xml string, err error) {
err = pathbuilder.PHP.ExecScript(ctx, server, &xml, pathbuilderPHP, "one_xml", id)
return
}
// GetAll returns all pathbuilders serialized as xml
//
// server is the server to fetch the pathbuilders from, any may be nil.
func (pathbuilder *Pathbuilder) GetAll(server *phpx.Server) (pathbuilders map[string]string, err error) {
err = pathbuilder.PHP.ExecScript(server, &pathbuilders, pathbuilderPHP, "all_xml")
func (pathbuilder *Pathbuilder) GetAll(ctx context.Context, server *phpx.Server) (pathbuilders map[string]string, err error) {
err = pathbuilder.PHP.ExecScript(ctx, server, &pathbuilders, pathbuilderPHP, "all_xml")
return
}
@ -50,6 +51,6 @@ func (pathbuilder *Pathbuilder) Fetch(flags ingredient.FetcherFlags, info *statu
return
}
info.Pathbuilders, _ = pathbuilder.GetAll(flags.Server)
info.Pathbuilders, _ = pathbuilder.GetAll(flags.Context, flags.Server)
return
}

View file

@ -2,6 +2,7 @@ package extras
import (
"bufio"
"context"
"path/filepath"
"strings"
@ -37,8 +38,8 @@ var listURIPrefixesPHP string
//
// server is an optional server to fetch prefixes from.
// server may be nil.
func (prefixes *Prefixes) All(server *phpx.Server) ([]string, error) {
uris, err := prefixes.database(server)
func (prefixes *Prefixes) All(ctx context.Context, server *phpx.Server) ([]string, error) {
uris, err := prefixes.database(ctx, server)
if err != nil {
return nil, err
}
@ -51,9 +52,9 @@ func (prefixes *Prefixes) All(server *phpx.Server) ([]string, error) {
return append(uris, uris2...), nil
}
func (wisski *Prefixes) database(server *phpx.Server) (prefixes []string, err error) {
func (wisski *Prefixes) database(ctx context.Context, server *phpx.Server) (prefixes []string, err error) {
// get all the ugly prefixes
err = wisski.PHP.ExecScript(server, &prefixes, listURIPrefixesPHP, "list_prefixes")
err = wisski.PHP.ExecScript(ctx, server, &prefixes, listURIPrefixesPHP, "list_prefixes")
if err != nil {
return nil, err
}
@ -143,28 +144,28 @@ func (wisski *Prefixes) filePrefixes() (prefixes []string, err error) {
var prefix = mstore.For[string]("prefix")
// Prefixes returns the cached prefixes from the given instance
func (wisski *Prefixes) AllCached() (results []string, err error) {
return prefix.GetAll(wisski.MStore)
func (wisski *Prefixes) AllCached(ctx context.Context) (results []string, err error) {
return prefix.GetAll(ctx, wisski.MStore)
}
// Update updates the cached prefixes of this instance
func (wisski *Prefixes) Update() error {
prefixes, err := wisski.All(nil)
func (wisski *Prefixes) Update(ctx context.Context) error {
prefixes, err := wisski.All(ctx, nil)
if err != nil {
return err
}
return prefix.SetAll(wisski.MStore, prefixes...)
return prefix.SetAll(ctx, wisski.MStore, prefixes...)
}
func (prefixes *Prefixes) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err error) {
info.NoPrefixes = prefixes.NoPrefix()
if flags.Quick {
// quick mode: grab only the cached prefixes
info.Prefixes, _ = prefixes.AllCached()
info.Prefixes, _ = prefixes.AllCached(flags.Context)
} else {
// slow mode: grab the fresh prefixes from the server
// TODO: Do we want to update them while we are at it?
info.Prefixes, _ = prefixes.All(flags.Server)
info.Prefixes, _ = prefixes.All(flags.Context, flags.Server)
}
return
}

View file

@ -1,6 +1,7 @@
package extras
import (
"context"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
@ -17,11 +18,11 @@ type Settings struct {
//go:embed settings.php
var settingsPHP string
func (settings *Settings) Get(server *phpx.Server, key string) (value any, err error) {
err = settings.PHP.ExecScript(server, &value, settingsPHP, "get_setting", key)
func (settings *Settings) Get(ctx context.Context, server *phpx.Server, key string) (value any, err error) {
err = settings.PHP.ExecScript(ctx, server, &value, settingsPHP, "get_setting", key)
return
}
func (settings *Settings) Set(server *phpx.Server, key string, value any) error {
return settings.PHP.ExecScript(server, nil, settingsPHP, "set_setting", key, value)
func (settings *Settings) Set(ctx context.Context, server *phpx.Server, key string, value any) error {
return settings.PHP.ExecScript(ctx, server, nil, settingsPHP, "set_setting", key, value)
}

View file

@ -1,8 +1,8 @@
package extras
import (
"context"
_ "embed"
"log"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
"github.com/FAU-CDI/wisski-distillery/internal/status"
@ -20,11 +20,8 @@ type Stats struct {
var statsPHP string
// Get fetches all statistics from the server
func (stats *Stats) Get(server *phpx.Server) (data status.Statistics, err error) {
err = stats.PHP.ExecScript(server, &data, statsPHP, "export_statistics")
if err != nil {
log.Println(err)
}
func (stats *Stats) Get(ctx context.Context, server *phpx.Server) (data status.Statistics, err error) {
err = stats.PHP.ExecScript(ctx, server, &data, statsPHP, "export_statistics")
return
}
@ -33,6 +30,6 @@ func (stats *Stats) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (e
return
}
info.Statistics, _ = stats.Get(flags.Server)
info.Statistics, _ = stats.Get(flags.Context, flags.Server)
return
}

View file

@ -1,6 +1,7 @@
package php
import (
"context"
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
@ -28,7 +29,7 @@ type PHP struct {
// It's arguments are encoded as json using [json.Marshal] and decoded within php.
//
// The return value of the function is again marshaled with json and returned to the caller.
func (php *PHP) ExecScript(server *phpx.Server, value any, code string, entrypoint string, args ...any) (err error) {
func (php *PHP) ExecScript(ctx context.Context, server *phpx.Server, value any, code string, entrypoint string, args ...any) (err error) {
if server == nil {
server = php.NewServer()
if err != nil {
@ -38,15 +39,15 @@ func (php *PHP) ExecScript(server *phpx.Server, value any, code string, entrypoi
}
if code != "" {
if err := server.MarshalEval(nil, strings.TrimPrefix(code, "<?php")); err != nil {
if err := server.MarshalEval(ctx, nil, strings.TrimPrefix(code, "<?php")); err != nil {
return err
}
}
return server.MarshalCall(value, entrypoint, args...)
return server.MarshalCall(ctx, value, entrypoint, args...)
}
func (php *PHP) EvalCode(server *phpx.Server, value any, code string) (err error) {
func (php *PHP) EvalCode(ctx context.Context, server *phpx.Server, value any, code string) (err error) {
if server == nil {
server = php.NewServer()
if err != nil {
@ -55,5 +56,5 @@ func (php *PHP) EvalCode(server *phpx.Server, value any, code string) (err error
defer server.Close()
}
return server.MarshalEval(value, code)
return server.MarshalEval(ctx, value, code)
}

View file

@ -1,6 +1,7 @@
package php
import (
"context"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/phpx"
@ -14,11 +15,12 @@ import (
// See [PHPServer].
func (php *PHP) NewServer() *phpx.Server {
return &phpx.Server{
Context: context.Background(),
Executor: phpx.SpawnFunc(php.spawn),
}
}
func (php *PHP) spawn(str stream.IOStream, code string) error {
_, err := php.Barrel.Shell(str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", code}))
func (php *PHP) spawn(ctx context.Context, str stream.IOStream, code string) error {
_, err := php.Barrel.Shell(ctx, str, "-c", shellescape.QuoteCommand([]string{"drush", "php:eval", code}))
return err
}

View file

@ -15,11 +15,11 @@ import (
var errGetValidator = errors.New("GetPasswordValidator: Unknown Error")
func (u *Users) GetPasswordValidator(username string) (pv PasswordValidator, err error) {
func (u *Users) GetPasswordValidator(ctx context.Context, username string) (pv PasswordValidator, err error) {
server := u.PHP.NewServer()
var hash string
err = u.PHP.ExecScript(server, &hash, usersPHP, "get_password_hash", username)
err = u.PHP.ExecScript(ctx, server, &hash, usersPHP, "get_password_hash", username)
if err != nil {
server.Close()
return pv, err
@ -46,9 +46,9 @@ func (pv PasswordValidator) Close() error {
return pv.server.Close()
}
func (pv PasswordValidator) Check(password string) bool {
func (pv PasswordValidator) Check(ctx context.Context, password string) bool {
var result phpx.Boolean
err := pv.server.MarshalCall(&result, "check_password_hash", password, string(pv.hash))
err := pv.server.MarshalCall(ctx, &result, "check_password_hash", password, string(pv.hash))
if err != nil {
return false
}
@ -65,10 +65,10 @@ func (cpe CommonPasswordError) Error() string {
return fmt.Sprintf("%q from %q", cpe.Password.Password, cpe.Password.Source)
}
func (pv PasswordValidator) CheckDictionary(context context.Context, writer io.Writer) error {
func (pv PasswordValidator) CheckDictionary(ctx context.Context, writer io.Writer) error {
var counter int
if pv.Check(pv.username) {
if pv.Check(ctx, pv.username) {
if writer != nil {
counter++
fmt.Fprintln(writer, counter)
@ -76,10 +76,10 @@ func (pv PasswordValidator) CheckDictionary(context context.Context, writer io.W
return errPasswordUsername
}
for candidate := range CommonPasswords() {
if context.Err() != nil {
if ctx.Err() != nil {
continue
}
result := pv.Check(candidate.Password)
result := pv.Check(ctx, candidate.Password)
if writer != nil {
counter++
fmt.Fprintln(writer, counter)
@ -90,7 +90,7 @@ func (pv PasswordValidator) CheckDictionary(context context.Context, writer io.W
}
}
return context.Err()
return ctx.Err()
}
//go:embed passwords

View file

@ -1,6 +1,7 @@
package users
import (
"context"
_ "embed"
"errors"
"net/url"
@ -21,19 +22,19 @@ type Users struct {
var usersPHP string
// All returns all known usernames
func (u *Users) All(server *phpx.Server) (users []status.User, err error) {
err = u.PHP.ExecScript(server, &users, usersPHP, "list_users")
func (u *Users) All(ctx context.Context, server *phpx.Server) (users []status.User, err error) {
err = u.PHP.ExecScript(ctx, server, &users, usersPHP, "list_users")
return
}
var errLoginUnknownError = errors.New("Login: Unknown Error")
// Login generates a login link for the user with the given username
func (u *Users) Login(server *phpx.Server, username string) (dest *url.URL, err error) {
func (u *Users) Login(ctx context.Context, server *phpx.Server, username string) (dest *url.URL, err error) {
// generate a (relative) link
var path string
err = u.PHP.ExecScript(server, &path, usersPHP, "get_login_link", username)
err = u.PHP.ExecScript(ctx, server, &path, usersPHP, "get_login_link", username)
// if something went wrong, return
if err != nil {
@ -57,9 +58,9 @@ func (u *Users) Login(server *phpx.Server, username string) (dest *url.URL, err
var errSetPassword = errors.New("SetPassword: Unknown Error")
// SetPassword sets the password for a given user
func (u *Users) SetPassword(server *phpx.Server, username, password string) error {
func (u *Users) SetPassword(ctx context.Context, server *phpx.Server, username, password string) error {
var ok bool
err := u.PHP.ExecScript(server, &ok, usersPHP, "set_user_password", username, password)
err := u.PHP.ExecScript(ctx, server, &ok, usersPHP, "set_user_password", username, password)
if err != nil {
return err
}
@ -74,6 +75,6 @@ func (u *Users) Fetch(flags ingredient.FetcherFlags, info *status.WissKI) (err e
return
}
info.Users, _ = u.All(flags.Server)
info.Users, _ = u.All(flags.Context, flags.Server)
return
}

View file

@ -1,10 +1,14 @@
package liquid
import "github.com/FAU-CDI/wisski-distillery/internal/models"
import (
"context"
"github.com/FAU-CDI/wisski-distillery/internal/models"
)
// Snapshots returns the list of snapshots of this WissKI
// NOTE(twiesing): Not entirely sure where this should go.
// It's not that this is
func (liquid *Liquid) Snapshots() (snapshots []models.Export, err error) {
return liquid.Malt.ExporterLog.For(liquid.Slug)
func (liquid *Liquid) Snapshots(ctx context.Context) (snapshots []models.Export, err error) {
return liquid.Malt.ExporterLog.For(ctx, liquid.Slug)
}