Skip to content

Commit 63d7cbc

Browse files
6543zeripathdelvh
authored
Make mirror feature more configurable (#16957)
Rename`[repository]` `DISABLE_MIRRORS` to `[mirror]` `DISABLE_NEW_PULL` and add `ENABLED` and `DISABLE_NEW_PUSH` with the below meanings: - `ENABLED`: **true**: Enables the mirror functionality. Set to **false** to disable all mirrors. - `DISABLE_NEW_PULL`: **false**: Disable the creation of **new** mirrors. Pre-existing mirrors remain valid. - `DISABLE_NEW_PUSH`: **false**: Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid. Co-authored-by: zeripath <[email protected]> Co-authored-by: delvh <[email protected]>
1 parent ded438f commit 63d7cbc

File tree

16 files changed

+161
-74
lines changed

16 files changed

+161
-74
lines changed

custom/conf/app.example.ini

+6-3
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,6 @@ PATH =
800800
;; Prefix archive files by placing them in a directory named after the repository
801801
;PREFIX_ARCHIVE_FILES = true
802802
;;
803-
;; Disable the creation of new mirrors. Pre-existing mirrors remain valid.
804-
;DISABLE_MIRRORS = false
805-
;;
806803
;; Disable migrating feature.
807804
;DISABLE_MIGRATIONS = false
808805
;;
@@ -1945,6 +1942,12 @@ PATH =
19451942
;[mirror]
19461943
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
19471944
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1945+
;; Enables the mirror functionality. Set to **false** to disable all mirrors.
1946+
;ENABLED = true
1947+
;; Disable the creation of **new** pull mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
1948+
;DISABLE_NEW_PULL = false
1949+
;; Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
1950+
;DISABLE_NEW_PUSH = false
19481951
;; Default interval as a duration between each check
19491952
;DEFAULT_INTERVAL = 8h
19501953
;; Min interval as a duration must be > 1m

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
7373
- `DISABLED_REPO_UNITS`: **_empty_**: Comma separated list of globally disabled repo units. Allowed values: \[repo.issues, repo.ext_issues, repo.pulls, repo.wiki, repo.ext_wiki, repo.projects\]
7474
- `DEFAULT_REPO_UNITS`: **repo.code,repo.releases,repo.issues,repo.pulls,repo.wiki,repo.projects**: Comma separated list of default repo units. Allowed values: \[repo.code, repo.releases, repo.issues, repo.pulls, repo.wiki, repo.projects\]. Note: Code and Releases can currently not be deactivated. If you specify default repo units you should still list them for future compatibility. External wiki and issue tracker can't be enabled by default as it requires additional settings. Disabled repo units will not be added to new repositories regardless if it is in the default list.
7575
- `PREFIX_ARCHIVE_FILES`: **true**: Prefix archive files by placing them in a directory named after the repository.
76-
- `DISABLE_MIRRORS`: **false**: Disable the creation of **new** mirrors. Pre-existing mirrors remain valid.
7776
- `DISABLE_MIGRATIONS`: **false**: Disable migrating feature.
7877
- `DISABLE_STARS`: **false**: Disable stars feature.
7978
- `DEFAULT_BRANCH`: **master**: Default branch name of all repositories.
@@ -955,6 +954,9 @@ Task queue configuration has been moved to `queue.task`. However, the below conf
955954

956955
## Mirror (`mirror`)
957956

957+
- `ENABLED`: **true**: Enables the mirror functionality. Set to **false** to disable all mirrors.
958+
- `DISABLE_NEW_PULL`: **false**: Disable the creation of **new** pull mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
959+
- `DISABLE_NEW_PUSH`: **false**: Disable the creation of **new** push mirrors. Pre-existing mirrors remain valid. Will be ignored if `mirror.ENABLED` is `false`.
958960
- `DEFAULT_INTERVAL`: **8h**: Default interval between each check
959961
- `MIN_INTERVAL`: **10m**: Minimum interval for checking. (Must be >1m).
960962

integrations/api_settings_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestAPIExposedSettings(t *testing.T) {
4343

4444
DecodeJSON(t, resp, &repo)
4545
assert.EqualValues(t, &api.GeneralRepoSettings{
46-
MirrorsDisabled: setting.Repository.DisableMirrors,
46+
MirrorsDisabled: !setting.Mirror.Enabled,
4747
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
4848
MigrationsDisabled: setting.Repository.DisableMigrations,
4949
TimeTrackingDisabled: false,

modules/setting/mirror.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package setting
6+
7+
import (
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/log"
11+
)
12+
13+
var (
14+
// Mirror settings
15+
Mirror = struct {
16+
Enabled bool
17+
DisableNewPull bool
18+
DisableNewPush bool
19+
DefaultInterval time.Duration
20+
MinInterval time.Duration
21+
}{
22+
Enabled: true,
23+
DisableNewPull: false,
24+
DisableNewPush: false,
25+
MinInterval: 10 * time.Minute,
26+
DefaultInterval: 8 * time.Hour,
27+
}
28+
)
29+
30+
func newMirror() {
31+
// Handle old configuration through `[repository]` `DISABLE_MIRRORS`
32+
// - please note this was badly named and only disabled the creation of new pull mirrors
33+
if Cfg.Section("repository").Key("DISABLE_MIRRORS").MustBool(false) {
34+
log.Warn("Deprecated DISABLE_MIRRORS config is used, please change your config and use the options within the [mirror] section")
35+
// TODO: enable on v1.17.0: log.Error("Deprecated fallback used, will be removed in v1.18.0")
36+
Mirror.DisableNewPull = true
37+
}
38+
if err := Cfg.Section("mirror").MapTo(&Mirror); err != nil {
39+
log.Fatal("Failed to map Mirror settings: %v", err)
40+
}
41+
42+
if !Mirror.Enabled {
43+
Mirror.DisableNewPull = true
44+
Mirror.DisableNewPush = true
45+
}
46+
47+
if Mirror.MinInterval.Minutes() < 1 {
48+
log.Warn("Mirror.MinInterval is too low, set to 1 minute")
49+
Mirror.MinInterval = 1 * time.Minute
50+
}
51+
if Mirror.DefaultInterval < Mirror.MinInterval {
52+
if time.Hour*8 < Mirror.MinInterval {
53+
Mirror.DefaultInterval = Mirror.MinInterval
54+
} else {
55+
Mirror.DefaultInterval = time.Hour * 8
56+
}
57+
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval, set to %s", Mirror.DefaultInterval.String())
58+
}
59+
}

modules/setting/repository.go

-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ var (
4141
DisabledRepoUnits []string
4242
DefaultRepoUnits []string
4343
PrefixArchiveFiles bool
44-
DisableMirrors bool
4544
DisableMigrations bool
4645
DisableStars bool `ini:"DISABLE_STARS"`
4746
DefaultBranch string
@@ -155,7 +154,6 @@ var (
155154
DisabledRepoUnits: []string{},
156155
DefaultRepoUnits: []string{},
157156
PrefixArchiveFiles: true,
158-
DisableMirrors: false,
159157
DisableMigrations: false,
160158
DisableStars: false,
161159
DefaultBranch: "master",

modules/setting/setting.go

+1-17
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,6 @@ var (
348348

349349
ManifestData string
350350

351-
// Mirror settings
352-
Mirror struct {
353-
DefaultInterval time.Duration
354-
MinInterval time.Duration
355-
}
356-
357351
// API settings
358352
API = struct {
359353
EnableSwagger bool
@@ -938,17 +932,7 @@ func NewContext() {
938932

939933
newGit()
940934

941-
sec = Cfg.Section("mirror")
942-
Mirror.MinInterval = sec.Key("MIN_INTERVAL").MustDuration(10 * time.Minute)
943-
Mirror.DefaultInterval = sec.Key("DEFAULT_INTERVAL").MustDuration(8 * time.Hour)
944-
if Mirror.MinInterval.Minutes() < 1 {
945-
log.Warn("Mirror.MinInterval is too low")
946-
Mirror.MinInterval = 1 * time.Minute
947-
}
948-
if Mirror.DefaultInterval < Mirror.MinInterval {
949-
log.Warn("Mirror.DefaultInterval is less than Mirror.MinInterval")
950-
Mirror.DefaultInterval = time.Hour * 8
951-
}
935+
newMirror()
952936

953937
Langs = Cfg.Section("i18n").Key("LANGS").Strings(",")
954938
if len(Langs) == 0 {

routers/api/v1/repo/migrate.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func Migrate(ctx *context.APIContext) {
108108

109109
gitServiceType := convert.ToGitServiceType(form.Service)
110110

111-
if form.Mirror && setting.Repository.DisableMirrors {
112-
ctx.Error(http.StatusForbidden, "MirrorsGlobalDisabled", fmt.Errorf("the site administrator has disabled mirrors"))
111+
if form.Mirror && setting.Mirror.DisableNewPull {
112+
ctx.Error(http.StatusForbidden, "MirrorsGlobalDisabled", fmt.Errorf("the site administrator has disabled the creation of new pull mirrors"))
113113
return
114114
}
115115

routers/api/v1/repo/mirror.go

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/context"
12+
"code.gitea.io/gitea/modules/setting"
1213
mirror_service "code.gitea.io/gitea/services/mirror"
1314
)
1415

@@ -42,6 +43,11 @@ func MirrorSync(ctx *context.APIContext) {
4243
ctx.Error(http.StatusForbidden, "MirrorSync", "Must have write access")
4344
}
4445

46+
if !setting.Mirror.Enabled {
47+
ctx.Error(http.StatusBadRequest, "MirrorSync", "Mirror feature is disabled")
48+
return
49+
}
50+
4551
mirror_service.StartToMirror(repo.ID)
4652

4753
ctx.Status(http.StatusOK)

routers/api/v1/settings/settings.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetGeneralRepoSettings(ctx *context.APIContext) {
5858
// "200":
5959
// "$ref": "#/responses/GeneralRepoSettings"
6060
ctx.JSON(http.StatusOK, api.GeneralRepoSettings{
61-
MirrorsDisabled: setting.Repository.DisableMirrors,
61+
MirrorsDisabled: !setting.Mirror.Enabled,
6262
HTTPGitDisabled: setting.Repository.DisableHTTPGit,
6363
MigrationsDisabled: setting.Repository.DisableMigrations,
6464
StarsDisabled: setting.Repository.DisableStars,

routers/web/org/home.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func Home(ctx *context.Context) {
141141
ctx.Data["Members"] = members
142142
ctx.Data["Teams"] = org.Teams
143143

144-
ctx.Data["DisabledMirrors"] = setting.Repository.DisableMirrors
144+
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
145145

146146
pager := context.NewPagination(int(count), setting.UI.User.RepoPagingNum, page, 5)
147147
pager.SetDefaultParams(ctx)

routers/web/repo/migrate.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,20 @@ func MigratePost(ctx *context.Context) {
152152
return
153153
}
154154

155-
serviceType := structs.GitServiceType(form.Service)
155+
if form.Mirror && setting.Mirror.DisableNewPull {
156+
ctx.Error(http.StatusBadRequest, "MigratePost: the site administrator has disabled creation of new mirrors")
157+
return
158+
}
156159

157-
setMigrationContextData(ctx, serviceType)
160+
setMigrationContextData(ctx, form.Service)
158161

159162
ctxUser := checkContextUser(ctx, form.UID)
160163
if ctx.Written() {
161164
return
162165
}
163166
ctx.Data["ContextUser"] = ctxUser
164167

165-
tpl := base.TplName("repo/migrate/" + serviceType.Name())
168+
tpl := base.TplName("repo/migrate/" + form.Service.Name())
166169

167170
if ctx.HasError() {
168171
ctx.HTML(http.StatusOK, tpl)
@@ -198,12 +201,12 @@ func MigratePost(ctx *context.Context) {
198201

199202
var opts = migrations.MigrateOptions{
200203
OriginalURL: form.CloneAddr,
201-
GitServiceType: serviceType,
204+
GitServiceType: form.Service,
202205
CloneAddr: remoteAddr,
203206
RepoName: form.RepoName,
204207
Description: form.Description,
205208
Private: form.Private || setting.Repository.ForcePrivate,
206-
Mirror: form.Mirror && !setting.Repository.DisableMirrors,
209+
Mirror: form.Mirror,
207210
LFS: form.LFS,
208211
LFSEndpoint: form.LFSEndpoint,
209212
AuthUsername: form.AuthUsername,
@@ -246,7 +249,7 @@ func setMigrationContextData(ctx *context.Context, serviceType structs.GitServic
246249

247250
ctx.Data["LFSActive"] = setting.LFS.StartServer
248251
ctx.Data["IsForcedPrivate"] = setting.Repository.ForcePrivate
249-
ctx.Data["DisableMirrors"] = setting.Repository.DisableMirrors
252+
ctx.Data["DisableNewPullMirrors"] = setting.Mirror.DisableNewPull
250253

251254
// Plain git should be first
252255
ctx.Data["Services"] = append([]structs.GitServiceType{structs.PlainGitService}, structs.SupportedFullGitService...)

routers/web/repo/setting.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func Settings(ctx *context.Context) {
5252
ctx.Data["Title"] = ctx.Tr("repo.settings")
5353
ctx.Data["PageIsSettingsOptions"] = true
5454
ctx.Data["ForcePrivate"] = setting.Repository.ForcePrivate
55-
ctx.Data["DisabledMirrors"] = setting.Repository.DisableMirrors
55+
ctx.Data["MirrorsEnabled"] = setting.Mirror.Enabled
56+
ctx.Data["DisableNewPushMirrors"] = setting.Mirror.DisableNewPush
5657
ctx.Data["DefaultMirrorInterval"] = setting.Mirror.DefaultInterval
5758

5859
signing, _ := models.SigningKey(ctx.Repo.Repository.RepoPath())
@@ -144,7 +145,7 @@ func SettingsPost(ctx *context.Context) {
144145
ctx.Redirect(repo.Link() + "/settings")
145146

146147
case "mirror":
147-
if !repo.IsMirror {
148+
if !setting.Mirror.Enabled || !repo.IsMirror {
148149
ctx.NotFound("", nil)
149150
return
150151
}
@@ -220,7 +221,7 @@ func SettingsPost(ctx *context.Context) {
220221
ctx.Redirect(repo.Link() + "/settings")
221222

222223
case "mirror-sync":
223-
if !repo.IsMirror {
224+
if !setting.Mirror.Enabled || !repo.IsMirror {
224225
ctx.NotFound("", nil)
225226
return
226227
}
@@ -231,6 +232,11 @@ func SettingsPost(ctx *context.Context) {
231232
ctx.Redirect(repo.Link() + "/settings")
232233

233234
case "push-mirror-sync":
235+
if !setting.Mirror.Enabled {
236+
ctx.NotFound("", nil)
237+
return
238+
}
239+
234240
m, err := selectPushMirrorByForm(form, repo)
235241
if err != nil {
236242
ctx.NotFound("", nil)
@@ -243,6 +249,11 @@ func SettingsPost(ctx *context.Context) {
243249
ctx.Redirect(repo.Link() + "/settings")
244250

245251
case "push-mirror-remove":
252+
if !setting.Mirror.Enabled {
253+
ctx.NotFound("", nil)
254+
return
255+
}
256+
246257
// This section doesn't require repo_name/RepoName to be set in the form, don't show it
247258
// as an error on the UI for this action
248259
ctx.Data["Err_RepoName"] = nil
@@ -267,6 +278,11 @@ func SettingsPost(ctx *context.Context) {
267278
ctx.Redirect(repo.Link() + "/settings")
268279

269280
case "push-mirror-add":
281+
if setting.Mirror.DisableNewPush {
282+
ctx.NotFound("", nil)
283+
return
284+
}
285+
270286
// This section doesn't require repo_name/RepoName to be set in the form, don't show it
271287
// as an error on the UI for this action
272288
ctx.Data["Err_RepoName"] = nil
@@ -698,6 +714,7 @@ func SettingsPost(ctx *context.Context) {
698714

699715
log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
700716
ctx.Redirect(ctx.Repo.RepoLink + "/settings")
717+
701718
case "unarchive":
702719
if !ctx.Repo.IsOwner() {
703720
ctx.Error(http.StatusForbidden)

services/mirror/mirror.go

+13
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ var mirrorQueue = sync.NewUniqueQueue(setting.Repository.MirrorQueueLength)
2222

2323
// Update checks and updates mirror repositories.
2424
func Update(ctx context.Context) error {
25+
if !setting.Mirror.Enabled {
26+
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
27+
return nil
28+
}
2529
log.Trace("Doing: Update")
2630

2731
handler := func(idx int, bean interface{}) error {
@@ -89,15 +93,24 @@ func syncMirrors(ctx context.Context) {
8993

9094
// InitSyncMirrors initializes a go routine to sync the mirrors
9195
func InitSyncMirrors() {
96+
if !setting.Mirror.Enabled {
97+
return
98+
}
9299
go graceful.GetManager().RunWithShutdownContext(syncMirrors)
93100
}
94101

95102
// StartToMirror adds repoID to mirror queue
96103
func StartToMirror(repoID int64) {
104+
if !setting.Mirror.Enabled {
105+
return
106+
}
97107
go mirrorQueue.Add(fmt.Sprintf("pull %d", repoID))
98108
}
99109

100110
// AddPushMirrorToQueue adds the push mirror to the queue
101111
func AddPushMirrorToQueue(mirrorID int64) {
112+
if !setting.Mirror.Enabled {
113+
return
114+
}
102115
go mirrorQueue.Add(fmt.Sprintf("push %d", mirrorID))
103116
}

templates/org/home.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="ui eleven wide column">
2727
{{if .CanCreateOrgRepo}}
2828
<div class="text right">
29-
{{if not .DisabledMirrors}}
29+
{{if not .DisableNewPullMirrors}}
3030
<a class="ui green button" href="{{AppSubUrl}}/repo/migrate?org={{.Org.ID}}&mirror=1">{{.i18n.Tr "new_migrate"}}</a>
3131
{{end}}
3232
<a class="ui green button" href="{{AppSubUrl}}/repo/create?org={{.Org.ID}}">{{.i18n.Tr "new_repo"}}</a>

templates/repo/migrate/options.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div class="inline field">
22
<label>{{.i18n.Tr "repo.migrate_options"}}</label>
33
<div class="ui checkbox">
4-
{{if .DisableMirrors}}
4+
{{if .DisableNewPullMirrors}}
55
<input id="mirror" name="mirror" type="checkbox" readonly>
66
<label>{{.i18n.Tr "repo.migrate_options_mirror_disabled"}}</label>
77
{{else}}

0 commit comments

Comments
 (0)