Skip to content

Commit ba7031c

Browse files
committed
Change default factory schematic to a proper reference
1 parent ec703e8 commit ba7031c

File tree

9 files changed

+55
-13
lines changed

9 files changed

+55
-13
lines changed

app/controllers/talos_image_factory_settings_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ def update
44

55
talos_image_factory_setting_params = params.require(:talos_image_factory_setting).permit(
66
:version,
7-
:schematic_id,
7+
:talos_image_factory_schematic_id,
88
)
99

1010
if @talos_image_factory_setting.update(talos_image_factory_setting_params)
11-
# redirect_to settings_path, status: 303, notice: "Talos Image Factory settings updated successfully."
1211
flash.now[:talos_image_factory_update_notice] = "Talos Image Factory settings updated successfully."
1312
render :edit
1413
else

app/models/server.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,19 @@ def reset
125125
end
126126

127127
def bootstrap_image_url(platform: "metal", talos_version: TalosImageFactorySetting.singleton.version)
128-
schematic_id =
129-
talos_image_factory_schematic&.schematic_id || TalosImageFactory.create_schematic_with_talos_config.fetch("id")
128+
existing_schematic_id =
129+
talos_image_factory_schematic&.schematic_id ||
130+
TalosImageFactorySetting.singleton.talos_image_factory_schematic&.schematic_id
131+
schematic_id = existing_schematic_id || TalosImageFactory.create_schematic_with_talos_config.fetch("id")
130132

131133
"#{TalosImageFactory::BASE_URL}/image/#{schematic_id}/#{talos_version}/#{platform}-#{architecture}.raw.zst"
132134
end
133135

134136
def upgrade_image_url(platform: "metal", talos_version: TalosImageFactorySetting.singleton.version)
135-
schematic_id =
136-
talos_image_factory_schematic&.schematic_id || TalosImageFactory.create_schematic_with_talos_config.fetch("id")
137+
existing_schematic_id =
138+
talos_image_factory_schematic&.schematic_id ||
139+
TalosImageFactorySetting.singleton.talos_image_factory_schematic&.schematic_id
140+
schematic_id = existing_schematic_id || TalosImageFactory.create_schematic_with_talos_config.fetch("id")
137141

138142
"factory.talos.dev/#{platform}-installer/#{schematic_id}:#{talos_version}"
139143
end

app/models/talos_image_factory_schematic.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ class TalosImageFactorySchematic < ApplicationRecord
33
validates_uniqueness_of :name
44
validates_presence_of :body
55
validate :validate_and_normalize_and_post_body, if: :body_changed?
6+
validates_presence_of :schematic_id # must be validated after body is validated and posted
7+
8+
has_one :talos_image_factory_setting, dependent: :nullify
69

710
# private
811

app/models/talos_image_factory_setting.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ def self.singleton
77
first_or_create!
88
end
99

10-
validates :version, presence: true
11-
validates :version, uniqueness: true
12-
validates :version, format: { with: /\Av\d+\.\d+\.\d+\z/, message: "must be in the format vX.Y.Z" }
13-
validates :version, length: { maximum: 20 }
10+
belongs_to :talos_image_factory_schematic, optional: true
1411

1512
attribute :version, :string, default: "v1.10.4"
1613

1714
validates_presence_of :version
15+
validates :version, format: { with: /\Av\d+\.\d+\.\d+\z/, message: "must be in the format vX.Y.Z" }
1816
end

app/views/servers/prepare_bootstrap.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
f.select(
2424
:talos_image_factory_schematic_id,
2525
TalosImageFactorySchematic.order(:name).pluck(:name, :id),
26-
{ include_blank: "none", selected: TalosImageFactorySetting.singleton.schematic_id },
26+
{ include_blank: "none", selected: TalosImageFactorySetting.singleton.talos_image_factory_schematic_id },
2727
class: "w-full mb-4",
2828
label: "Talos Image Factory Schematic",
2929
required: false,

app/views/talos_image_factory_settings/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<%= f.select :version, sorted_talos_versions, { include_blank: false }, label: "Default Talos Version", required: false %>
44
<%=
55
f.select(
6-
:schematic_id,
6+
:talos_image_factory_schematic_id,
77
TalosImageFactorySchematic.order(:name).pluck(:name, :id),
88
{ include_blank: "none" },
99
label: "Default Talos Schematic",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class ChangeTalosImageFactorySettingsSchematicIdIntoReference < ActiveRecord::Migration[8.0]
2+
def up
3+
add_reference :talos_image_factory_settings, :talos_image_factory_schematic, foreign_key: true
4+
5+
setting = TalosImageFactorySetting.singleton
6+
schematic_id = setting.try(:schematic_id)
7+
talos_image_factory_schematic = schematic_id && TalosImageFactorySchematic.find_by(schematic_id:)
8+
9+
if talos_image_factory_schematic
10+
TalosImageFactorySetting.singleton.update!(talos_image_factory_schematic_id: talos_image_factory_schematic.id)
11+
end
12+
end
13+
14+
def down
15+
remove_reference :talos_image_factory_settings, :talos_image_factory_schematic, foreign_key: true
16+
end
17+
end

db/schema.rb

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/models/server_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@
2626
expect(server.errors[:name]).to include "has already been taken"
2727
end
2828

29+
describe "#bootstrap_image_url" do
30+
it "returns a URL to a Talos image with the correct schematic ID and Talos version" do
31+
talos_version = "v1.10.5"
32+
server = servers(:cloud_bootstrappable)
33+
server.update! talos_image_factory_schematic: talos_image_factory_schematics(:default)
34+
expected_url = "#{TalosImageFactory::BASE_URL}/image/#{talos_image_factory_schematics(:default).schematic_id}/#{talos_version}/metal-#{server.architecture}.raw.zst"
35+
expect(server.bootstrap_image_url(talos_version:)).to eq(expected_url)
36+
end
37+
38+
it "uses the default Talos version and schematic_id if none are provided" do
39+
server = Server.new
40+
default_setting = TalosImageFactorySetting.singleton
41+
default_setting.update! talos_image_factory_schematic: talos_image_factory_schematics(:default)
42+
expected_url = "#{TalosImageFactory::BASE_URL}/image/#{default_setting.talos_image_factory_schematic&.schematic_id}/#{default_setting.version}/metal-#{server.architecture}.raw.zst"
43+
expect(server.bootstrap_image_url).to eq(expected_url)
44+
end
45+
end
46+
2947
describe "#bootstrap!" do
3048
it "writes a Talos image to an eligble disk, saves metadata and reboots" do
3149
talos_version = "v1.10.5"

0 commit comments

Comments
 (0)