2020-01-28 19:01:06 -06:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package private
|
|
|
|
|
|
|
|
import (
|
2021-07-14 09:43:13 -05:00
|
|
|
"context"
|
2020-01-28 19:01:06 -06:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-07-05 19:07:07 -05:00
|
|
|
"net/url"
|
2020-01-28 19:01:06 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-03-01 15:08:10 -06:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-01-28 19:01:06 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Shutdown calls the internal shutdown function
|
2021-07-14 09:43:13 -05:00
|
|
|
func Shutdown(ctx context.Context) (int, string) {
|
2020-03-30 13:52:45 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/shutdown"
|
2020-01-28 19:01:06 -06:00
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-01-28 19:01:06 -06: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 09:43:13 -05:00
|
|
|
func Restart(ctx context.Context) (int, string) {
|
2020-03-30 13:52:45 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/restart"
|
2020-01-28 19:01:06 -06:00
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-01-28 19:01:06 -06: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 09:43:13 -05:00
|
|
|
func FlushQueues(ctx context.Context, timeout time.Duration, nonBlocking bool) (int, string) {
|
2020-03-30 13:52:45 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/flush-queues"
|
2020-01-28 19:01:06 -06:00
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-01-28 19:01:06 -06:00
|
|
|
if timeout > 0 {
|
|
|
|
req.SetTimeout(timeout+10*time.Second, timeout+10*time.Second)
|
|
|
|
}
|
|
|
|
req = req.Header("Content-Type", "application/json")
|
2021-03-01 15:08:10 -06:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-01-28 19:01:06 -06:00
|
|
|
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-05 19:07:07 -05:00
|
|
|
|
|
|
|
// PauseLogging pauses logging
|
2021-07-14 09:43:13 -05:00
|
|
|
func PauseLogging(ctx context.Context) (int, string) {
|
2020-07-05 19:07:07 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/pause-logging"
|
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-05 19:07:07 -05: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 09:43:13 -05:00
|
|
|
func ResumeLogging(ctx context.Context) (int, string) {
|
2020-07-05 19:07:07 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/resume-logging"
|
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-05 19:07:07 -05: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 09:43:13 -05:00
|
|
|
func ReleaseReopenLogging(ctx context.Context) (int, string) {
|
2020-07-05 19:07:07 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/release-and-reopen-logging"
|
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-05 19:07:07 -05: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"
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 09:43:13 -05:00
|
|
|
func AddLogger(ctx context.Context, group, name, mode string, config map[string]interface{}) (int, string) {
|
2020-07-05 19:07:07 -05:00
|
|
|
reqURL := setting.LocalURL + "api/internal/manager/add-logger"
|
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-05 19:07:07 -05:00
|
|
|
req = req.Header("Content-Type", "application/json")
|
2021-03-01 15:08:10 -06:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-07-05 19:07:07 -05:00
|
|
|
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 09:43:13 -05:00
|
|
|
func RemoveLogger(ctx context.Context, group, name string) (int, string) {
|
2020-07-05 19:07:07 -05:00
|
|
|
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/manager/remove-logger/%s/%s", url.PathEscape(group), url.PathEscape(name))
|
|
|
|
|
2021-07-14 09:43:13 -05:00
|
|
|
req := newInternalRequest(ctx, reqURL, "POST")
|
2020-07-05 19:07:07 -05: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"
|
|
|
|
}
|