Skip to content
代码片段 群组 项目
提交 b3231a95 编辑于 作者: Gabriel Mazetto's avatar Gabriel Mazetto
浏览文件

Refactor SourceContext and move it into Context module

上级 315bea33
No related branches found
No related tags found
无相关合并请求
......@@ -13,13 +13,13 @@ module Backup
module Cli
autoload :BackupExecutor, 'gitlab/backup/cli/backup_executor'
autoload :Commands, 'gitlab/backup/cli/commands'
autoload :Context, 'gitlab/backup/cli/context'
autoload :Dependencies, 'gitlab/backup/cli/dependencies'
autoload :GitlabConfig, 'gitlab/backup/cli/gitlab_config'
autoload :Metadata, 'gitlab/backup/cli/metadata'
autoload :Output, 'gitlab/backup/cli/output'
autoload :RestoreExecutor, 'gitlab/backup/cli/restore_executor'
autoload :Runner, 'gitlab/backup/cli/runner'
autoload :SourceContext, 'gitlab/backup/cli/source_context'
autoload :Shell, 'gitlab/backup/cli/shell'
autoload :Targets, 'gitlab/backup/cli/targets'
autoload :Tasks, 'gitlab/backup/cli/tasks'
......
# frozen_string_literal: true
module Gitlab
module Backup
module Cli
module Context
autoload :SourceContext, 'gitlab/backup/cli/context/source_context'
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Backup
module Cli
module Context
# This context is equivalent to a Source Install or GDK instance
#
# Any specific information from the GitLab installation will be
# automatically discovered from the current machine
class SourceContext
# Defaults defined in `config/initializers/1_settings.rb`
DEFAULT_SHARED_PATH = 'shared'
DEFAULT_CI_BUILDS_PATH = 'builds'
DEFAULT_JOBS_ARTIFACTS_PATH = 'artifacts'
DEFAULT_SECURE_FILES_PATH = 'ci_secure_files'
DEFAULT_CI_LFS_PATH = 'lfs-objects'
DEFAULT_PACKAGES = 'packages'
DEFAULT_PAGES = 'pages'
DEFAULT_REGISTRY_PATH = 'registry'
DEFAULT_TERRAFORM_STATE_PATH = 'terraform_state'
DEFAULT_UPLOADS_PATH = 'public' # based on GitLab's root folder
def gitlab_version
File.read(gitlab_basepath.join("VERSION")).strip.freeze
end
def backup_basedir
path = gitlab_config[env]['backup']['path']
absolute_path(path)
end
# CI Builds basepath
def ci_builds_path
path = gitlab_config.dig(env, 'gitlab_ci', 'builds_path') || DEFAULT_CI_BUILDS_PATH
absolute_path(path)
end
# Job Artifacts basepath
def ci_job_artifacts_path
path = gitlab_config.dig(env, 'artifacts', 'path') ||
gitlab_config.dig(env, 'artifacts', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_JOBS_ARTIFACTS_PATH)
absolute_path(path)
end
# CI Secure Files basepath
def ci_secure_files_path
path = gitlab_config.dig(env, 'ci_secure_files', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_SECURE_FILES_PATH)
absolute_path(path)
end
# CI LFS basepath
def ci_lfs_path
path = gitlab_config.dig(env, 'lfs', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_CI_LFS_PATH)
absolute_path(path)
end
# Packages basepath
def packages_path
path = gitlab_config.dig(env, 'packages', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_PACKAGES)
absolute_path(path)
end
# GitLab Pages basepath
def pages_path
path = gitlab_config.dig(env, 'pages', 'path') ||
gitlab_shared_path.join(DEFAULT_PAGES)
absolute_path(path)
end
# Registry basepath
def registry_path
path = gitlab_config.dig(env, 'registry', 'path') ||
gitlab_shared_path.join(DEFAULT_REGISTRY_PATH)
absolute_path(path)
end
# Terraform State basepath
def terraform_state_path
path = gitlab_config.dig(env, 'terraform_state', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_TERRAFORM_STATE_PATH)
absolute_path(path)
end
# Upload basepath
def upload_path
path = gitlab_config.dig(env, 'uploads', 'storage_path') ||
gitlab_basepath.join(DEFAULT_UPLOADS_PATH)
absolute_path(path).join('uploads')
end
def config(object_type)
Gitlab.config[object_type]
end
def env
@env ||= ActiveSupport::EnvironmentInquirer.new(
ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
end
private
# Return the shared path used as a fallback base location to each blob type
# We use this to determine the storage location when everything else fails
# @return [Pathname]
def gitlab_shared_path
shared_path = gitlab_config.dig(env, 'shared', 'path') || DEFAULT_SHARED_PATH
Pathname(shared_path)
end
# Return a fullpath for a given path
#
# When the path is already a full one return itself as a Pathname
# otherwise uses gitlab_basepath as its base
# @param [String|Pathname] path
# @return [Pathname]
def absolute_path(path)
# Joins with gitlab_basepath when relative, otherwise return full path
Pathname(File.expand_path(path, gitlab_basepath))
end
# Return the GitLab base directory
# @return [Pathname]
def gitlab_basepath
return Pathname.new(GITLAB_PATH) if GITLAB_PATH
raise ::Gitlab::Backup::Cli::Error, 'GITLAB_PATH is missing'
end
def gitlab_config
@gitlab_config ||= Gitlab::Backup::Cli::GitlabConfig.new(gitlab_basepath.join('config/gitlab.yml'))
end
end
end
end
end
end
# frozen_string_literal: true
module Gitlab
module Backup
module Cli
# This context is equivalent to a Source Install or GDK instance
#
# Any specific information from the GitLab installation will be
# automatically discovered from the current machine
class SourceContext
# Defaults defined in `config/initializers/1_settings.rb`
DEFAULT_SHARED_PATH = 'shared'
DEFAULT_CI_BUILDS_PATH = 'builds'
DEFAULT_JOBS_ARTIFACTS_PATH = 'artifacts'
DEFAULT_SECURE_FILES_PATH = 'ci_secure_files'
DEFAULT_CI_LFS_PATH = 'lfs-objects'
DEFAULT_PACKAGES = 'packages'
DEFAULT_PAGES = 'pages'
DEFAULT_REGISTRY_PATH = 'registry'
DEFAULT_TERRAFORM_STATE_PATH = 'terraform_state'
DEFAULT_UPLOADS_PATH = 'public' # based on GitLab's root folder
def gitlab_version
File.read(gitlab_basepath.join("VERSION")).strip.freeze
end
def backup_basedir
path = gitlab_config[env]['backup']['path']
absolute_path(path)
end
# CI Builds basepath
def ci_builds_path
path = gitlab_config.dig(env, 'gitlab_ci', 'builds_path') || DEFAULT_CI_BUILDS_PATH
absolute_path(path)
end
# Job Artifacts basepath
def ci_job_artifacts_path
path = gitlab_config.dig(env, 'artifacts', 'path') ||
gitlab_config.dig(env, 'artifacts', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_JOBS_ARTIFACTS_PATH)
absolute_path(path)
end
# CI Secure Files basepath
def ci_secure_files_path
path = gitlab_config.dig(env, 'ci_secure_files', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_SECURE_FILES_PATH)
absolute_path(path)
end
# CI LFS basepath
def ci_lfs_path
path = gitlab_config.dig(env, 'lfs', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_CI_LFS_PATH)
absolute_path(path)
end
# Packages basepath
def packages_path
path = gitlab_config.dig(env, 'packages', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_PACKAGES)
absolute_path(path)
end
# GitLab Pages basepath
def pages_path
path = gitlab_config.dig(env, 'pages', 'path') ||
gitlab_shared_path.join(DEFAULT_PAGES)
absolute_path(path)
end
# Registry basepath
def registry_path
path = gitlab_config.dig(env, 'registry', 'path') ||
gitlab_shared_path.join(DEFAULT_REGISTRY_PATH)
absolute_path(path)
end
# Terraform State basepath
def terraform_state_path
path = gitlab_config.dig(env, 'terraform_state', 'storage_path') ||
gitlab_shared_path.join(DEFAULT_TERRAFORM_STATE_PATH)
absolute_path(path)
end
# Upload basepath
def upload_path
path = gitlab_config.dig(env, 'uploads', 'storage_path') ||
gitlab_basepath.join(DEFAULT_UPLOADS_PATH)
absolute_path(path).join('uploads')
end
def config(object_type)
Gitlab.config[object_type]
end
def env
@env ||= ActiveSupport::EnvironmentInquirer.new(
ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
end
private
# Return the shared path used as a fallback base location to each blob type
# We use this to determine the storage location when everything else fails
# @return [Pathname]
def gitlab_shared_path
shared_path = gitlab_config.dig(env, 'shared', 'path') || DEFAULT_SHARED_PATH
Pathname(shared_path)
end
# Return a fullpath for a given path
#
# When the path is already a full one return itself as a Pathname
# otherwise uses gitlab_basepath as its base
# @param [String|Pathname] path
# @return [Pathname]
def absolute_path(path)
# Joins with gitlab_basepath when relative, otherwise return full path
Pathname(File.expand_path(path, gitlab_basepath))
end
# Return the GitLab base directory
# @return [Pathname]
def gitlab_basepath
return Pathname.new(GITLAB_PATH) if GITLAB_PATH
raise ::Gitlab::Backup::Cli::Error, 'GITLAB_PATH is missing'
end
def gitlab_config
@gitlab_config ||= Gitlab::Backup::Cli::GitlabConfig.new(gitlab_basepath.join('config/gitlab.yml'))
end
end
end
end
end
......@@ -21,7 +21,7 @@ def object_storage?
def config
settings = {
object_store: {
connection: Gitlab::Backup::Cli::SourceContext.new.config('object_store').connection.to_hash,
connection: context.config('object_store').connection.to_hash,
remote_directory: options.container_registry_bucket
}
}
......
......@@ -69,7 +69,7 @@ def enabled?
end
def config
Gitlab::Backup::Cli::SourceContext.new.config(id)
context.config(id)
end
def object_storage?
......
# frozen_string_literal: true
RSpec.describe Gitlab::Backup::Cli::SourceContext do
RSpec.describe Gitlab::Backup::Cli::Context::SourceContext do
subject(:context) { described_class.new }
let(:fake_gitlab_basepath) { Pathname.new(Dir.mktmpdir('gitlab', temp_path)) }
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册