2020-03-27 08:07:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Terraform
|
|
|
|
class State < ApplicationRecord
|
2020-05-20 23:08:00 -04:00
|
|
|
include UsageStatistics
|
2022-06-20 02:09:26 -04:00
|
|
|
include AfterCommitQueue
|
2020-05-20 23:08:00 -04:00
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
HEX_REGEXP = %r{\A\h+\z}.freeze
|
|
|
|
UUID_LENGTH = 32
|
|
|
|
|
2020-03-27 08:07:43 -04:00
|
|
|
belongs_to :project
|
2020-04-21 11:21:10 -04:00
|
|
|
belongs_to :locked_by_user, class_name: 'User'
|
2020-03-27 08:07:43 -04:00
|
|
|
|
2020-10-29 14:09:11 -04:00
|
|
|
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-09-16 08:10:15 -04:00
|
|
|
|
2020-09-30 08:09:53 -04:00
|
|
|
scope :ordered_by_name, -> { order(:name) }
|
2021-01-20 19:11:07 -05:00
|
|
|
scope :with_name, -> (name) { where(name: name) }
|
2020-09-16 08:10:15 -04:00
|
|
|
|
2022-06-02 23:09:20 -04:00
|
|
|
validates :project_id, :name, presence: true
|
2020-04-21 11:21:10 -04:00
|
|
|
validates :uuid, presence: true, uniqueness: true, length: { is: UUID_LENGTH },
|
2022-08-01 11:11:13 -04:00
|
|
|
format: { with: HEX_REGEXP, message: 'only allows hex characters' }
|
2020-04-21 11:21:10 -04:00
|
|
|
|
|
|
|
default_value_for(:uuid, allows_nil: false) { SecureRandom.hex(UUID_LENGTH / 2) }
|
2020-03-27 08:07:43 -04:00
|
|
|
|
2020-09-16 08:10:15 -04:00
|
|
|
def latest_file
|
2020-11-23 07:09:11 -05:00
|
|
|
latest_version&.file
|
2020-09-16 08:10:15 -04:00
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def locked?
|
|
|
|
self.lock_xid.present?
|
|
|
|
end
|
2020-09-16 08:10:15 -04:00
|
|
|
|
2020-10-25 20:09:02 -04:00
|
|
|
def update_file!(data, version:, build:)
|
2020-11-23 07:09:11 -05:00
|
|
|
# This check is required to maintain backwards compatibility with
|
|
|
|
# states that were created prior to versioning being supported.
|
|
|
|
# This can be removed in 14.0 when support for these states is dropped.
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/issues/258960
|
2020-09-16 08:10:15 -04:00
|
|
|
if versioning_enabled?
|
2020-10-25 20:09:02 -04:00
|
|
|
create_new_version!(data: data, version: version, build: build)
|
2020-09-16 08:10:15 -04:00
|
|
|
else
|
2020-11-23 07:09:11 -05:00
|
|
|
migrate_legacy_version!(data: data, version: version, build: build)
|
2020-09-16 08:10:15 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-13 05:08:27 -04:00
|
|
|
|
|
|
|
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
|
2020-10-25 20:09:02 -04:00
|
|
|
def migrate_legacy_version!(data:, version:, build:)
|
2020-10-13 05:08:27 -04:00
|
|
|
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))
|
2020-10-25 20:09:02 -04:00
|
|
|
create_new_version!(data: data, version: version, build: build)
|
2020-10-13 05:08:27 -04:00
|
|
|
end
|
|
|
|
|
2020-10-25 20:09:02 -04:00
|
|
|
def create_new_version!(data:, version:, build:)
|
|
|
|
new_version = versions.build(version: version, created_by_user: locked_by_user, build: build)
|
2020-10-13 05:08:27 -04:00
|
|
|
new_version.assign_attributes(file: data)
|
|
|
|
new_version.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_serial(file)
|
|
|
|
Gitlab::Json.parse(file)["serial"]
|
|
|
|
rescue JSON::ParserError
|
|
|
|
end
|
2020-03-27 08:07:43 -04:00
|
|
|
end
|
|
|
|
end
|
2021-05-05 20:10:32 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Terraform::State.prepend_mod
|