debian-mirror-gitlab/app/models/terraform/state.rb

113 lines
3.8 KiB
Ruby
Raw Normal View History

2020-04-22 19:07:51 +05:30
# frozen_string_literal: true
module Terraform
class State < ApplicationRecord
2020-06-23 00:09:42 +05:30
include UsageStatistics
2020-10-24 23:57:45 +05:30
include FileStoreMounter
2021-01-03 14:25:43 +05:30
include IgnorableColumns
# These columns are being removed since geo replication falls to the versioned state
# Tracking in https://gitlab.com/gitlab-org/gitlab/-/issues/258262
ignore_columns %i[verification_failure verification_retry_at verified_at verification_retry_count verification_checksum],
remove_with: '13.7',
remove_after: '2020-12-22'
2020-06-23 00:09:42 +05:30
2020-04-22 19:07:51 +05:30
HEX_REGEXP = %r{\A\h+\z}.freeze
UUID_LENGTH = 32
belongs_to :project
belongs_to :locked_by_user, class_name: 'User'
2021-01-29 00:20:46 +05:30
has_many :versions,
class_name: 'Terraform::StateVersion',
foreign_key: :terraform_state_id,
inverse_of: :terraform_state
has_one :latest_version, -> { ordered_by_version_desc },
class_name: 'Terraform::StateVersion',
foreign_key: :terraform_state_id,
inverse_of: :terraform_state
2020-11-24 15:15:51 +05:30
scope :versioning_not_enabled, -> { where(versioning_enabled: false) }
2021-01-03 14:25:43 +05:30
scope :ordered_by_name, -> { order(:name) }
2020-11-24 15:15:51 +05:30
2020-04-22 19:07:51 +05:30
validates :project_id, presence: true
validates :uuid, presence: true, uniqueness: true, length: { is: UUID_LENGTH },
format: { with: HEX_REGEXP, message: 'only allows hex characters' }
default_value_for(:uuid, allows_nil: false) { SecureRandom.hex(UUID_LENGTH / 2) }
2020-11-24 15:15:51 +05:30
default_value_for(:versioning_enabled, true)
2020-04-22 19:07:51 +05:30
2020-10-24 23:57:45 +05:30
mount_file_store_uploader StateUploader
2020-04-22 19:07:51 +05:30
def file_store
super || StateUploader.default_store
end
2020-11-24 15:15:51 +05:30
def latest_file
2021-01-03 14:25:43 +05:30
if versioning_enabled?
latest_version&.file
else
latest_version&.file || file
end
2020-10-24 23:57:45 +05:30
end
2020-04-22 19:07:51 +05:30
def locked?
self.lock_xid.present?
end
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
def update_file!(data, version:, build:)
2020-11-24 15:15:51 +05:30
if versioning_enabled?
2021-01-29 00:20:46 +05:30
create_new_version!(data: data, version: version, build: build)
2021-01-03 14:25:43 +05:30
elsif latest_version.present?
2021-01-29 00:20:46 +05:30
migrate_legacy_version!(data: data, version: version, build: build)
2020-11-24 15:15:51 +05:30
else
self.file = data
save!
end
end
2021-01-03 14:25:43 +05:30
private
##
# If a Terraform state was created before versioning support was
# introduced, it will have a single version record whose file
# uses a legacy naming scheme in object storage. To update
# these states and versions to use the new behaviour, we must do
# the following when creating the next version:
#
# * Read the current, non-versioned file from the old location.
# * Update the :versioning_enabled flag, which determines the
# naming scheme
# * Resave the existing file with the updated name and location,
# using a version number one prior to the new version
# * Create the new version as normal
#
# This migration only needs to happen once for each state, from
# then on the state will behave as if it was always versioned.
#
# The code can be removed in the next major version (14.0), after
# which any states that haven't been migrated will need to be
# recreated: https://gitlab.com/gitlab-org/gitlab/-/issues/258960
2021-01-29 00:20:46 +05:30
def migrate_legacy_version!(data:, version:, build:)
2021-01-03 14:25:43 +05:30
current_file = latest_version.file.read
current_version = parse_serial(current_file) || version - 1
update!(versioning_enabled: true)
reload_latest_version.update!(version: current_version, file: CarrierWaveStringFile.new(current_file))
2021-01-29 00:20:46 +05:30
create_new_version!(data: data, version: version, build: build)
2021-01-03 14:25:43 +05:30
end
2021-01-29 00:20:46 +05:30
def create_new_version!(data:, version:, build:)
new_version = versions.build(version: version, created_by_user: locked_by_user, build: build)
2021-01-03 14:25:43 +05:30
new_version.assign_attributes(file: data)
new_version.save!
end
def parse_serial(file)
Gitlab::Json.parse(file)["serial"]
rescue JSON::ParserError
end
2020-04-22 19:07:51 +05:30
end
end