mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-23 22:05:43 +03:00
061b68e995
Backport #25330 # The problem There were many "path tricks": * By default, Gitea uses its program directory as its work path * Gitea tries to use the "work path" to guess its "custom path" and "custom conf (app.ini)" * Users might want to use other directories as work path * The non-default work path should be passed to Gitea by GITEA_WORK_DIR or "--work-path" * But some Gitea processes are started without these values * The "serv" process started by OpenSSH server * The CLI sub-commands started by site admin * The paths are guessed by SetCustomPathAndConf again and again * The default values of "work path / custom path / custom conf" can be changed when compiling # The solution * Use `InitWorkPathAndCommonConfig` to handle these path tricks, and use test code to cover its behaviors. * When Gitea's web server runs, write the WORK_PATH to "app.ini", this value must be the most correct one, because if this value is not right, users would find that the web UI doesn't work and then they should be able to fix it. * Then all other sub-commands can use the WORK_PATH in app.ini to initialize their paths. * By the way, when Gitea starts for git protocol, it shouldn't output any log, otherwise the git protocol gets broken and client blocks forever. The "work path" priority is: WORK_PATH in app.ini > cmd arg --work-path > env var GITEA_WORK_DIR > builtin default The "app.ini" searching order is: cmd arg --config > cmd arg "work path / custom path" > env var "work path / custom path" > builtin default ## ⚠️ BREAKING If your instance's "work path / custom path / custom conf" doesn't meet the requirements (eg: work path must be absolute), Gitea will report a fatal error and exit. You need to set these values according to the error log.
110 lines
2.7 KiB
Go
110 lines
2.7 KiB
Go
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// Package cmd provides subcommands to the gitea binary - such as "web" or
|
|
// "admin".
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
// argsSet checks that all the required arguments are set. args is a list of
|
|
// arguments that must be set in the passed Context.
|
|
func argsSet(c *cli.Context, args ...string) error {
|
|
for _, a := range args {
|
|
if !c.IsSet(a) {
|
|
return errors.New(a + " is not set")
|
|
}
|
|
|
|
if util.IsEmptyString(c.String(a)) {
|
|
return errors.New(a + " is required")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// confirm waits for user input which confirms an action
|
|
func confirm() (bool, error) {
|
|
var response string
|
|
|
|
_, err := fmt.Scanln(&response)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
switch strings.ToLower(response) {
|
|
case "y", "yes":
|
|
return true, nil
|
|
case "n", "no":
|
|
return false, nil
|
|
default:
|
|
return false, errors.New(response + " isn't a correct confirmation string")
|
|
}
|
|
}
|
|
|
|
func initDB(ctx context.Context) error {
|
|
setting.MustInstalled()
|
|
setting.LoadDBSetting()
|
|
setting.InitSQLLoggersForCli(log.INFO)
|
|
|
|
if setting.Database.Type == "" {
|
|
log.Fatal(`Database settings are missing from the configuration file: %q.
|
|
Ensure you are running in the correct environment or set the correct configuration file with -c.
|
|
If this is the intended configuration file complete the [database] section.`, setting.CustomConf)
|
|
}
|
|
if err := db.InitEngine(ctx); err != nil {
|
|
return fmt.Errorf("unable to initialize the database using the configuration in %q. Error: %w", setting.CustomConf, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func installSignals() (context.Context, context.CancelFunc) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
go func() {
|
|
// install notify
|
|
signalChannel := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(
|
|
signalChannel,
|
|
syscall.SIGINT,
|
|
syscall.SIGTERM,
|
|
)
|
|
select {
|
|
case <-signalChannel:
|
|
case <-ctx.Done():
|
|
}
|
|
cancel()
|
|
signal.Reset()
|
|
}()
|
|
|
|
return ctx, cancel
|
|
}
|
|
|
|
func setupConsoleLogger(level log.Level, colorize bool, out io.Writer) {
|
|
if out != os.Stdout && out != os.Stderr {
|
|
panic("setupConsoleLogger can only be used with os.Stdout or os.Stderr")
|
|
}
|
|
|
|
writeMode := log.WriterMode{
|
|
Level: level,
|
|
Colorize: colorize,
|
|
WriterOption: log.WriterConsoleOption{Stderr: out == os.Stderr},
|
|
}
|
|
writer := log.NewEventWriterConsole("console-default", writeMode)
|
|
log.GetManager().GetLogger(log.DEFAULT).RemoveAllWriters().AddWriters(writer)
|
|
}
|