2020-01-29 04:01:06 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-29 04:01:06 +03:00
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
2021-07-14 17:43:13 +03:00
|
|
|
"context"
|
2020-01-29 04:01:06 +03:00
|
|
|
"fmt"
|
2022-03-31 20:01:43 +03:00
|
|
|
"io"
|
2020-01-29 04:01:06 +03:00
|
|
|
"net/http"
|
2020-07-06 03:07:07 +03:00
|
|
|
"net/url"
|
2022-06-24 13:49:47 +03:00
|
|
|
"strconv"
|
2020-01-29 04:01:06 +03:00
|
|
|
"time"
|
|
|
|
|
2021-07-24 19:03:58 +03:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2020-01-29 04:01:06 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Shutdown calls the internal shutdown function
|
2021-07-14 17:43:13 +03:00
|
|
|
func Shutdown(ctx context.Context) (int, string) {
|
2020-03-30 21:52:45 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/shutdown"
|
2020-01-29 04:01:06 +03:00
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-01-29 04:01:06 +03:00
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Shutting down"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restart calls the internal restart function
|
2021-07-14 17:43:13 +03:00
|
|
|
func Restart(ctx context.Context) (int, string) {
|
2020-03-30 21:52:45 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/restart"
|
2020-01-29 04:01:06 +03:00
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-01-29 04:01:06 +03:00
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Restarting"
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushOptions represents the options for the flush call
|
|
|
|
type FlushOptions struct {
|
|
|
|
Timeout time.Duration
|
|
|
|
NonBlocking bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushQueues calls the internal flush-queues function
|
2021-07-14 17:43:13 +03:00
|
|
|
func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) (int, string) {
|
2020-03-30 21:52:45 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
|
2020-01-29 04:01:06 +03:00
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-01-29 04:01:06 +03:00
|
|
|
if timeout > 0 {
|
|
|
|
req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second)
|
|
|
|
}
|
|
|
|
req = req.Header("Content-Type", "application/json")
|
|
|
|
jsonBytes, _ := json.Marshal(FlushOptions{
|
|
|
|
Timeout: timeout,
|
|
|
|
NonBlocking: nonBlocking,
|
|
|
|
})
|
|
|
|
req.Body(jsonBytes)
|
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Flushed"
|
|
|
|
}
|
2020-07-06 03:07:07 +03:00
|
|
|
|
|
|
|
// PauseLogging pauses logging
|
2021-07-14 17:43:13 +03:00
|
|
|
func PauseLogging(ctx context.Context) (int, string) {
|
2020-07-06 03:07:07 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
|
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-06 03:07:07 +03:00
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Logging Paused"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResumeLogging resumes logging
|
2021-07-14 17:43:13 +03:00
|
|
|
func ResumeLogging(ctx context.Context) (int, string) {
|
2020-07-06 03:07:07 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
|
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-06 03:07:07 +03:00
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Logging Restarted"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseReopenLogging releases and reopens logging files
|
2021-07-14 17:43:13 +03:00
|
|
|
func ReleaseReopenLogging(ctx context.Context) (int, string) {
|
2020-07-06 03:07:07 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
|
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-06 03:07:07 +03:00
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Logging Restarted"
|
|
|
|
}
|
|
|
|
|
2022-06-24 13:49:47 +03:00
|
|
|
// SetLogSQL sets database logging
|
|
|
|
func SetLogSQL(ctx context.Context, on bool) (int, string) {
|
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/set-log-sql?on=" + strconv.FormatBool(on)
|
|
|
|
|
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Log SQL setting set"
|
|
|
|
}
|
|
|
|
|
2020-07-06 03:07:07 +03:00
|
|
|
// LoggerOptions represents the options for the add logger call
|
|
|
|
type LoggerOptions struct {
|
|
|
|
Group string
|
|
|
|
Name string
|
|
|
|
Mode string
|
|
|
|
Config map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLogger adds a logger
|
2021-07-14 17:43:13 +03:00
|
|
|
func AddLogger(ctx context.Context, group, name, mode string, config map[string]interface{}) (int, string) {
|
2020-07-06 03:07:07 +03:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/add-logger"
|
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-06 03:07:07 +03:00
|
|
|
req = req.Header("Content-Type", "application/json")
|
|
|
|
jsonBytes, _ := json.Marshal(LoggerOptions{
|
|
|
|
Group: group,
|
|
|
|
Name: name,
|
|
|
|
Mode: mode,
|
|
|
|
Config: config,
|
|
|
|
})
|
|
|
|
req.Body(jsonBytes)
|
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Added"
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveLogger removes a logger
|
2021-07-14 17:43:13 +03:00
|
|
|
func RemoveLogger(ctx context.Context, group, name string) (int, string) {
|
2020-07-06 03:07:07 +03:00
|
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(group), url.PathEscape(name))
|
|
|
|
|
2021-07-14 17:43:13 +03:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-06 03:07:07 +03:00
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, "Removed"
|
|
|
|
}
|
2022-03-31 20:01:43 +03:00
|
|
|
|
|
|
|
// Processes return the current processes from this gitea instance
|
|
|
|
func Processes(ctx context.Context, out io.Writer, flat, noSystem, stacktraces, json bool, cancel string) (int, string) {
|
|
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/processes?flat=%t&no-system=%t&stacktraces=%t&json=%t&cancel-pid=%s", flat, noSystem, stacktraces, json, url.QueryEscape(cancel))
|
|
|
|
|
|
|
|
req := newInternalRequest(ctx, reqURL, "GET")
|
|
|
|
resp, err := req.Response()
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return resp.StatusCode, decodeJSONError(resp).Err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err.Error()
|
|
|
|
}
|
|
|
|
return http.StatusOK, ""
|
|
|
|
}
|