{{< details >}}
- Tier: 基础版,专业版,旗舰版
- Offering: 私有化部署
{{< /details >}}
极狐GitLab 提供 Rake 任务来检查各个组件的完整性。另请参阅检查极狐GitLab 配置 Rake 任务。
仓库完整性
尽管 Git 非常稳定,并尝试防止数据完整性问题,但有时事情会出错。以下 Rake 任务旨在帮助极狐GitLab 管理员诊断有问题的仓库,以便进行修复。
这些 Rake 任务使用三种不同的方法来确定 Git 仓库的完整性。
- Git 仓库文件系统检查(
git fsck
)。此步骤验证仓库中对象的连接性和有效性。 - 检查仓库目录中的
config.lock
。 - 检查
refs/heads
中的任何分支/引用锁文件。
config.lock
或引用锁的存在本身并不一定表示问题。锁文件通常在 Git 和极狐GitLab 对仓库执行操作时被创建和移除。它们可以防止数据完整性问题。然而,如果 Git 操作被中断,这些锁可能无法正确清理。
以下症状可能表明仓库完整性存在问题。如果用户遇到这些症状,您可以使用下面描述的 Rake 任务来确定哪些仓库导致了问题。
- 尝试推送代码时收到错误 -
remote: error: cannot lock ref
- 查看极狐GitLab 仪表板或访问特定项目时出现 500 错误。
检查项目代码仓库
此任务循环遍历项目代码仓库并运行之前描述的完整性检查。如果项目使用池仓库,也会进行检查。其他类型的 Git 仓库不会被检查。
要检查项目代码仓库:
{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
sudo gitlab-rake gitlab:git:fsck
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
sudo -u git -H bundle exec rake gitlab:git:fsck RAILS_ENV=production
{{< /tab >}}
{{< /tabs >}}
仓库引用的校验和
可以通过对每个仓库的所有引用进行校验和来比较两个 Git 仓库。如果两个仓库具有相同的引用,并且都通过完整性检查,那么我们可以确信两个仓库是相同的。
例如,这可以用于将仓库的备份与源仓库进行比较。
检查所有极狐GitLab 仓库
此任务循环遍历极狐GitLab 服务器上的所有仓库,并以 <PROJECT ID>,<CHECKSUM>
格式输出校验和。
- 如果仓库不存在,项目 ID 是一个空白校验和。
- 如果仓库存在但为空,输出的校验和是
0000000000000000000000000000000000000000
。 - 不存在的项目被跳过。
要检查所有极狐GitLab 仓库:
{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
sudo gitlab-rake gitlab:git:checksum_projects
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
sudo -u git -H bundle exec rake gitlab:git:checksum_projects RAILS_ENV=production
{{< /tab >}}
{{< /tabs >}}
例如,如果:
- ID 为 2 的项目不存在,则跳过。
- ID 为 4 的项目没有仓库,其校验和为空。
- ID 为 5 的项目有一个空仓库,其校验和为
0000000000000000000000000000000000000000
。
输出将如下所示:
1,cfa3f06ba235c13df0bb28e079bcea62c5848af2
3,3f3fb58a8106230e3a6c6b48adc2712fb3b6ef87
4,
5,0000000000000000000000000000000000000000
6,6c6b48adc2712fb3b6ef87cfa3f06ba235c13df0
检查特定极狐GitLab 仓库
可以选择通过设置环境变量 CHECKSUM_PROJECT_IDS
来校验特定项目 ID,使用逗号分隔的整数列表,例如:
sudo CHECKSUM_PROJECT_IDS="1,3" gitlab-rake gitlab:git:checksum_projects
上传文件的完整性
用户可以上传各种类型的文件到极狐GitLab 安装中。这些完整性检查可以检测丢失的文件。此外,对于本地存储的文件,上传时会生成校验和并存储在数据库中,这些检查会将它们与当前文件进行验证。
完整性检查支持以下类型的文件:
- CI 产物
- LFS 对象
- 项目级安全文件(在极狐GitLab 16.1.0 中引入)
- 用户上传
要检查上传文件的完整性:
{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
sudo gitlab-rake gitlab:artifacts:check
sudo gitlab-rake gitlab:ci_secure_files:check
sudo gitlab-rake gitlab:lfs:check
sudo gitlab-rake gitlab:uploads:check
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
sudo -u git -H bundle exec rake gitlab:artifacts:check RAILS_ENV=production
sudo -u git -H bundle exec rake gitlab:ci_secure_files:check RAILS_ENV=production
sudo -u git -H bundle exec rake gitlab:lfs:check RAILS_ENV=production
sudo -u git -H bundle exec rake gitlab:uploads:check RAILS_ENV=production
{{< /tab >}}
{{< /tabs >}}
这些任务也接受一些环境变量,您可以用来覆盖某些值:
Variable | Type | Description |
---|---|---|
BATCH |
integer | 指定批次大小。默认为 200。 |
ID_FROM |
integer | 指定开始 ID,包含该值。 |
ID_TO |
integer | 指定结束 ID,包含该值。 |
VERBOSE |
boolean | 导致故障单独列出,而不是总结。 |
sudo gitlab-rake gitlab:artifacts:check BATCH=100 ID_FROM=50 ID_TO=250
sudo gitlab-rake gitlab:ci_secure_files:check BATCH=100 ID_FROM=50 ID_TO=250
sudo gitlab-rake gitlab:lfs:check BATCH=100 ID_FROM=50 ID_TO=250
sudo gitlab-rake gitlab:uploads:check BATCH=100 ID_FROM=50 ID_TO=250
示例输出:
$ sudo gitlab-rake gitlab:uploads:check
Checking integrity of Uploads
- 1..1350: Failures: 0
- 1351..2743: Failures: 0
- 2745..4349: Failures: 2
- 4357..5762: Failures: 1
- 5764..7140: Failures: 2
- 7142..8651: Failures: 0
- 8653..10134: Failures: 0
- 10135..11773: Failures: 0
- 11777..13315: Failures: 0
Done!
详细输出示例:
$ sudo gitlab-rake gitlab:uploads:check VERBOSE=1
Checking integrity of Uploads
- 1..1350: Failures: 0
- 1351..2743: Failures: 0
- 2745..4349: Failures: 2
- Upload: 3573: #<Errno::ENOENT: No such file or directory @ rb_sysopen - /opt/gitlab/embedded/service/gitlab-rails/public/uploads/user-foo/project-bar/7a77cc52947bfe188adeff42f890bb77/image.png>
- Upload: 3580: #<Errno::ENOENT: No such file or directory @ rb_sysopen - /opt/gitlab/embedded/service/gitlab-rails/public/uploads/user-foo/project-bar/2840ba1ba3b2ecfa3478a7b161375f8a/pug.png>
- 4357..5762: Failures: 1
- Upload: 4636: #<Google::Apis::ServerError: Server error>
- 5764..7140: Failures: 2
- Upload: 5812: #<NoMethodError: undefined method `hashed_storage?' for nil:NilClass>
- Upload: 5837: #<NoMethodError: undefined method `hashed_storage?' for nil:NilClass>
- 7142..8651: Failures: 0
- 8653..10134: Failures: 0
- 10135..11773: Failures: 0
- 11777..13315: Failures: 0
Done!
LDAP 检查
LDAP 检查 Rake 任务测试绑定 DN 和密码凭证(如果已配置),并列出 LDAP 用户的示例。此任务也作为 gitlab:check
任务的一部分执行,但可以单独运行。有关详细信息,请参阅 LDAP Rake Tasks - LDAP Check。
验证数据库值是否可以使用当前密钥解密
此任务遍历数据库中所有可能的加密值,验证它们是否可以使用当前密钥文件 (gitlab-secrets.json
) 解密。
自动解决方案尚未实现。如果您有无法解密的值,可以按照步骤重置它们,请参阅我们的文档 当密钥文件丢失时。
这可能需要很长时间,具体取决于您的数据库大小,因为它会检查所有表中的所有行。
要验证数据库值是否可以使用当前密钥解密:
{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
sudo gitlab-rake gitlab:doctor:secrets
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
bundle exec rake gitlab:doctor:secrets RAILS_ENV=production
{{< /tab >}}
{{< /tabs >}}
示例输出
I, [2020-06-11T17:17:54.951815 #27148] INFO -- : Checking encrypted values in the database
I, [2020-06-11T17:18:12.677708 #27148] INFO -- : - ApplicationSetting failures: 0
I, [2020-06-11T17:18:12.823692 #27148] INFO -- : - User failures: 0
[...] other models possibly containing encrypted data
I, [2020-06-11T17:18:14.938335 #27148] INFO -- : - Group failures: 1
I, [2020-06-11T17:18:15.559162 #27148] INFO -- : - Operations::FeatureFlagsClient failures: 0
I, [2020-06-11T17:18:15.575533 #27148] INFO -- : - ScimOauthAccessToken failures: 0
I, [2020-06-11T17:18:15.575678 #27148] INFO -- : Total: 1 row(s) affected
I, [2020-06-11T17:18:15.575711 #27148] INFO -- : Done!
详细模式
要获取有关哪些行和列无法解密的详细信息,您可以传递 VERBOSE
环境变量。
要使用详细信息验证数据库值是否可以使用当前密钥解密:
{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
sudo gitlab-rake gitlab:doctor:secrets VERBOSE=1
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
bundle exec rake gitlab:doctor:secrets RAILS_ENV=production VERBOSE=1
{{< /tab >}}
{{< /tabs >}}
详细输出示例
I, [2020-06-11T17:17:54.951815 #27148] INFO -- : Checking encrypted values in the database
I, [2020-06-11T17:18:12.677708 #27148] INFO -- : - ApplicationSetting failures: 0
I, [2020-06-11T17:18:12.823692 #27148] INFO -- : - User failures: 0
[...] other models possibly containing encrypted data
D, [2020-06-11T17:19:53.224344 #27351] DEBUG -- : > Something went wrong for Group[10].runners_token: Validation failed: Route can't be blank
I, [2020-06-11T17:19:53.225178 #27351] INFO -- : - Group failures: 1
D, [2020-06-11T17:19:53.225267 #27351] DEBUG -- : - Group[10]: runners_token
I, [2020-06-11T17:18:15.559162 #27148] INFO -- : - Operations::FeatureFlagsClient failures: 0
I, [2020-06-11T17:18:15.575533 #27148] INFO -- : - ScimOauthAccessToken failures: 0
I, [2020-06-11T17:18:15.575678 #27148] INFO -- : Total: 1 row(s) affected
I, [2020-06-11T17:18:15.575711 #27148] INFO -- : Done!
当无法恢复加密令牌时重置
{{< history >}}
- 引入于极狐GitLab 16.6。
{{< /history >}}
{{< alert type=”warning” >}}
此操作很危险,可能导致数据丢失。请务必极其谨慎地进行操作。您必须了解极狐GitLab 内部结构才能执行此操作。
{{< /alert >}}
在某些情况下,加密令牌无法再恢复并导致问题。通常情况下,群组和项目的 runner 注册令牌可能会在非常大的实例上出现故障。
要重置损坏的令牌:
- 确定具有损坏的加密令牌的数据库模型。例如,可以是
Group
和Project
。 - 确定损坏的令牌。例如
runners_token
。 -
要重置损坏的令牌,请使用
VERBOSE=true MODEL_NAMES=Model1,Model2 TOKEN_NAMES=broken_token1,broken_token2
运行gitlab:doctor:reset_encrypted_tokens
。例如:{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
VERBOSE=true MODEL_NAMES=Project,Group TOKEN_NAMES=runners_token gitlab-rake gitlab:doctor:reset_encrypted_tokens
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
bundle exec rake gitlab:doctor:reset_encrypted_tokens RAILS_ENV=production VERBOSE=true MODEL_NAMES=Project,Group TOKEN_NAMES=runners_token
{{< /tab >}}
{{< /tabs >}}
您将看到此任务将尝试执行的每个操作:
I, [2023-09-26T16:20:23.230942 #88920] INFO -- : Resetting runners_token on Project, Group if they can not be read I, [2023-09-26T16:20:23.230975 #88920] INFO -- : Executing in DRY RUN mode, no records will actually be updated D, [2023-09-26T16:20:30.151585 #88920] DEBUG -- : > Fix Project[1].runners_token I, [2023-09-26T16:20:30.151617 #88920] INFO -- : Checked 1/9 Projects D, [2023-09-26T16:20:30.151873 #88920] DEBUG -- : > Fix Project[3].runners_token D, [2023-09-26T16:20:30.152975 #88920] DEBUG -- : > Fix Project[10].runners_token I, [2023-09-26T16:20:30.152992 #88920] INFO -- : Checked 11/29 Projects I, [2023-09-26T16:20:30.153230 #88920] INFO -- : Checked 21/29 Projects I, [2023-09-26T16:20:30.153882 #88920] INFO -- : Checked 29 Projects D, [2023-09-26T16:20:30.195929 #88920] DEBUG -- : > Fix Group[22].runners_token I, [2023-09-26T16:20:30.196125 #88920] INFO -- : Checked 1/19 Groups D, [2023-09-26T16:20:30.196192 #88920] DEBUG -- : > Fix Group[25].runners_token D, [2023-09-26T16:20:30.197557 #88920] DEBUG -- : > Fix Group[82].runners_token I, [2023-09-26T16:20:30.197581 #88920] INFO -- : Checked 11/19 Groups I, [2023-09-26T16:20:30.198455 #88920] INFO -- : Checked 19 Groups I, [2023-09-26T16:20:30.198462 #88920] INFO -- : Done!
-
如果您确信此操作重置了正确的令牌,请禁用 dry-run 模式并再次运行操作:
{{< tabs >}}
{{< tab title=”Linux package (Omnibus)” >}}
DRY_RUN=false VERBOSE=true MODEL_NAMES=Project,Group TOKEN_NAMES=runners_token gitlab-rake gitlab:doctor:reset_encrypted_tokens
{{< /tab >}}
{{< tab title=”Self-compiled (source)” >}}
bundle exec rake gitlab:doctor:reset_encrypted_tokens RAILS_ENV=production DRY_RUN=false VERBOSE=true MODEL_NAMES=Project,Group TOKEN_NAMES=runners_token
{{< /tab >}}
{{< /tabs >}}
故障排除
以下是使用上述 Rake 任务可能发现的问题的解决方案。
悬挂对象
gitlab-rake gitlab:git:fsck
任务可以发现悬挂对象,例如:
dangling blob a12...
dangling commit b34...
dangling tag c56...
dangling tree d78...
要删除它们,请尝试运行维护。
如果问题仍然存在,请尝试通过 Rails 控制台触发垃圾收集:
p = Project.find_by_path("project-name")
Repositories::HousekeepingService.new(p, :gc).execute
如果悬挂对象比默认的 2 周宽限期年轻,并且您不想等到它们自动过期,请运行:
Repositories::HousekeepingService.new(p, :prune).execute
删除对缺失远程上传的引用
gitlab-rake gitlab:uploads:check VERBOSE=1
检测远程对象不存在,因为它们在外部被删除,但其引用仍然存在于极狐GitLab 数据库中。
带有错误消息的示例输出:
$ sudo gitlab-rake gitlab:uploads:check VERBOSE=1
Checking integrity of Uploads
- 100..434: Failures: 2
- Upload: 100: Remote object does not exist
- Upload: 101: Remote object does not exist
Done!
要删除这些对在外部删除的远程上传的引用,请打开 极狐GitLab Rails 控制台并运行:
uploads_deleted=0
Upload.find_each do |upload|
next if upload.retrieve_uploader.file.exists?
uploads_deleted=uploads_deleted + 1
p upload ### allow verification before destroy
# p upload.destroy! ### uncomment to actually destroy
end
p "#{uploads_deleted} remote objects were destroyed."
删除对缺失产物的引用
gitlab-rake gitlab:artifacts:check VERBOSE=1
检测产物(或 job.log
文件):
- 在极狐GitLab 外部被删除。
- 仍然在极狐GitLab 数据库中有引用。
当检测到这种情况时,Rake 任务显示错误消息。例如:
Checking integrity of Job artifacts
- 1..15: Failures: 2
- Job artifact: 9: #<Errno::ENOENT: No such file or directory @ rb_sysopen - /var/opt/gitlab/gitlab-rails/shared/artifacts/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a/2022_06_30/8/9/job.log>
- Job artifact: 15: Remote object does not exist
Done!
要删除对缺失的本地和/或远程产物(job.log
文件)的引用:
- 打开 极狐GitLab Rails 控制台。
-
运行以下 Ruby 代码:
artifacts_deleted = 0 ::Ci::JobArtifact.find_each do |artifact| ### Iterate artifacts # next if artifact.file.filename != "job.log" ### Uncomment if only `job.log` files' references are to be processed next if artifact.file.file.exists? ### Skip if the file reference is valid artifacts_deleted += 1 puts "#{artifact.id} #{artifact.file.path} is missing." ### Allow verification before destroy # artifact.destroy! ### Uncomment to actually destroy end puts "Count of identified/destroyed invalid references: #{artifacts_deleted}"
删除对缺失 LFS 对象的引用
如果 gitlab-rake gitlab:lfs:check VERBOSE=1
检测到存在于数据库但不在磁盘上的 LFS 对象,请按照 LFS 文档中的程序移除数据库条目。
更新悬挂对象存储引用
如果您已从对象存储迁移到本地存储,并且文件丢失,则悬挂数据库引用仍然存在。
这在迁移日志中可见,错误如下所示:
W, [2022-11-28T13:14:09.283833 #10025] WARN -- : Failed to transfer Ci::JobArtifact ID 11 with error: undefined method `body' for nil:NilClass
W, [2022-11-28T13:14:09.296911 #10025] WARN -- : Failed to transfer Ci::JobArtifact ID 12 with error: undefined method `body' for nil:NilClass
尝试删除对缺失产物的引用后禁用对象存储,导致以下错误:
RuntimeError (Object Storage is not enabled for JobArtifactUploader)
要更新这些引用以指向本地存储:
- 打开 极狐GitLab Rails 控制台。
-
运行以下 Ruby 代码:
artifacts_updated = 0 ::Ci::JobArtifact.find_each do |artifact| ### Iterate artifacts next if artifact.file_store != 2 ### Skip if file_store already points to local storage artifacts_updated += 1 # artifact.update(file_store: 1) ### Uncomment to actually update end puts "Updated file_store count: #{artifacts_updated}"
现在,脚本可以删除对缺失产物的引用,并正确清理数据库。
删除对缺失安全文件的引用
VERBOSE=1 gitlab-rake gitlab:ci_secure_files:check
检测安全文件:
- 在极狐GitLab 外部被删除。
- 仍然在极狐GitLab 数据库中有引用。
当检测到这种情况时,Rake 任务显示错误消息。例如:
Checking integrity of CI Secure Files
- 1..15: Failures: 2
- Job SecureFile: 9: #<Errno::ENOENT: No such file or directory @ rb_sysopen - /var/opt/gitlab/gitlab-rails/shared/ci_secure_files/4b/22/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a/2022_06_30/8/9/distribution.cer>
- Job SecureFile: 15: Remote object does not exist
Done!
要删除对缺失的本地或远程安全文件的引用:
- 打开 极狐GitLab Rails 控制台。
-
运行以下 Ruby 代码:
secure_files_deleted = 0 ::Ci::SecureFile.find_each do |secure_file| ### Iterate secure files next if secure_file.file.file.exists? ### Skip if the file reference is valid secure_files_deleted += 1 puts "#{secure_file.id} #{secure_file.file.path} is missing." ### Allow verification before destroy # secure_file.destroy! ### Uncomment to actually destroy end puts "Count of identified/destroyed invalid references: #{secure_files_deleted}"