Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions app/controllers/docker_manager/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_dependency "docker_manager/base_image"
require_dependency "docker_manager/git_repo"
require_dependency "docker_manager/upgrader"

Expand Down Expand Up @@ -48,22 +49,8 @@ def repos
response = { repos: repos }

if !Rails.env.development? && !Rails.env.test?
version =
begin
File.read("/VERSION")
rescue StandardError
"1.0.0"
end

version = Gem::Version.new(version)
expected_version = Gem::Version.new("2.0.20260915-0028")
ruby_version = Gem::Version.new(RUBY_VERSION)
expected_ruby_version = Gem::Version.new("3.4.10")
upgrade_image = version < expected_version
upgrade_ruby = ruby_version < expected_ruby_version
missing_core_branch = !core_repo.detached_head? && !core_repo.upstream_branch_exist?

response[:upgrade_required] = true if upgrade_image || upgrade_ruby || missing_core_branch
response[:upgrade_required] = true if missing_core_branch || image_outdated?
end

render json: response
Expand Down Expand Up @@ -162,6 +149,13 @@ def self.find_repos(path, upgrading: false)

private

def image_outdated?
DockerManager::BaseImage.outdated?
rescue => e
Discourse.warn_exception(e, message: "Unable to check the base image version")
false
end

def respond_progress(logs: "", percentage: 0, status: nil, repos: nil)
progress = { logs: logs, percentage: percentage, status: status, repos: repos }

Expand Down
27 changes: 27 additions & 0 deletions lib/docker_manager/base_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class DockerManager::BaseImage
REPO_URL = "https://github.com/discourse/discourse_docker.git"
TEMPLATE_PATH = "templates/web.template.yml"
VERSION_FILE = "/VERSION"
GIT_TIMEOUT = 10

def self.outdated?
return true if !File.exist?(VERSION_FILE)
Gem::Version.new(File.read(VERSION_FILE)) < expected_version
end

def self.expected_version
Dir.mktmpdir do |dir|
git(dir, "init", "--quiet", "--bare")
git(dir, "fetch", "--quiet", "--depth=1", REPO_URL, "main")
template = YAML.safe_load(git(dir, "show", "FETCH_HEAD:#{TEMPLATE_PATH}"))
Gem::Version.new(template.fetch("base_image").split(":").last)
end
end

def self.git(dir, *args)
Discourse::Utils.execute_command("git", *args, chdir: dir, timeout: GIT_TIMEOUT)
end
private_class_method :git
end
7 changes: 7 additions & 0 deletions lib/docker_manager/upgrader.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require_relative "base_image"
require_relative "web_server_adapter"
require_relative "unicorn_adapter"
require_relative "pitchfork_adapter"
Expand Down Expand Up @@ -46,6 +47,12 @@ def upgrade
raise "No #{server_name} master or launcher"
end

if DockerManager::BaseImage.outdated?
log("ABORTING, you are running an old version of the Discourse image")
log("Rebuild it with `./launcher rebuild app` before you update")
raise "Base image is out of date"
end

percent(5)

log("Cycling #{server_name}, to free up memory")
Expand Down
90 changes: 90 additions & 0 deletions spec/lib/docker_manager/base_image_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

require "docker_manager/base_image"
require_relative "../../support/git_helpers"

RSpec.describe DockerManager::BaseImage do
describe ".outdated?" do
let(:version_file) { Tempfile.new("VERSION") }

around do |example|
stub_const(described_class, "VERSION_FILE", version_file.path) { example.run }
end

before do
allow(described_class).to receive(:expected_version).and_return(
Gem::Version.new("2.0.20261027-1647"),
)
end

after { version_file.close! }

it "returns true when the image is older" do
File.write(version_file.path, "2.0.20261026\n")
expect(described_class.outdated?).to eq(true)
end

it "returns false when the image is from the same day" do
File.write(version_file.path, "2.0.20261027\n")
expect(described_class.outdated?).to eq(false)
end

it "returns false when the image is newer" do
File.write(version_file.path, "2.0.20261028\n")
expect(described_class.outdated?).to eq(false)
end

it "returns true without fetching when there is no version file" do
File.delete(version_file.path)
expect(described_class.outdated?).to eq(true)
expect(described_class).not_to have_received(:expected_version)
end
end

describe ".expected_version" do
let(:templates) do
[
"base_image: discourse/base:2.0.20261026-0138\nparams:\n version: tests-passed\n",
"base_image: discourse/base:2.0.20261027-1647\nparams:\n version: tests-passed\n",
]
end

let(:remote) do
GitHelpers::RemoteGitRepo.new(
cache_key: "base_image_#{Digest::SHA1.hexdigest(templates.join)}",
) do |repo|
repo.in_working_directory { FileUtils.mkdir_p("templates") }
repo.commit(
filename: "templates/web.template.yml",
commits: templates.map { |content| { content: } },
)
end
end

let(:repo_url) { remote.url }

around { |example| stub_const(described_class, "REPO_URL", repo_url) { example.run } }

after { remote.destroy }

it "reads the version from the latest template on main" do
expect(described_class.expected_version).to eq(Gem::Version.new("2.0.20261027-1647"))
end

context "when the template has no base_image" do
let(:templates) { ["params:\n version: tests-passed\n"] }

it "raises an error" do
expect { described_class.expected_version }.to raise_error(KeyError)
end
end

context "when the repository cannot be fetched" do
let(:repo_url) { "file:///nonexistent.git" }

it "raises an error" do
expect { described_class.expected_version }.to raise_error(Discourse::Utils::CommandError)
end
end
end
end
30 changes: 30 additions & 0 deletions spec/lib/docker_manager/upgrader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
before do
allow_any_instance_of(Kernel).to receive(:`).with(/pgrep/).and_return("")
allow(Open3).to receive(:popen2e).and_yield([], [], OpenStruct.new(value: 0))
allow(DockerManager::BaseImage).to receive(:outdated?).and_return(false)
allow(upgrader.web_server).to receive_messages(
launcher_pid:,
master_pid:,
Expand Down Expand Up @@ -106,6 +107,35 @@
end
end

context "when the base image is out of date" do
before { allow(DockerManager::BaseImage).to receive(:outdated?).and_return(true) }

it "aborts before it changes anything" do
expect {
expect { upgrader.upgrade }.to output(/FAILED TO UPGRADE/).to_stderr
}.to raise_error("Base image is out of date")
expect(upgrader.find_logs).to include("ABORTING")
expect(upgrader.web_server).not_to have_received(:reload)
expect(Open3).not_to have_received(:popen2e)
end
end

context "when the base image version cannot be read" do
before do
allow(DockerManager::BaseImage).to receive(:outdated?).and_raise(
Discourse::Utils::CommandError.new("git fetch failed"),
)
end

it "aborts before it changes anything" do
expect {
expect { upgrader.upgrade }.to output(/FAILED TO UPGRADE/).to_stderr
}.to raise_error(Discourse::Utils::CommandError)
expect(upgrader.web_server).not_to have_received(:reload)
expect(Open3).not_to have_received(:popen2e)
end
end

context "with valid configuration" do
it "scales down workers" do
upgrader.upgrade
Expand Down
44 changes: 39 additions & 5 deletions spec/requests/admin_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

RSpec.describe DockerManager::AdminController do
describe "anonymous user" do
it "should be a 404" do
it "is a 404" do
get "/admin/update"

expect(response.status).to eq(404)
end
end

describe "when user is not an admin" do
it "should 404" do
it "404S" do
sign_in(Fabricate(:user))

get "/admin/update"
Expand All @@ -19,14 +19,14 @@
end

describe "when user is an admin" do
it "should return the right response" do
it "returns the right response" do
sign_in(Fabricate(:admin))

get "/admin/update"
expect(response.status).to eq(200)
end

it "should redirect to the new route" do
it "redirects to the new route" do
sign_in(Fabricate(:admin))

get "/admin/upgrade"
Expand All @@ -44,12 +44,46 @@
DockerManager::GitRepo.stubs(:find_all).returns(repos[0...3])
end

it "should return the right response" do
it "returns the right response" do
sign_in(Fabricate(:admin))

get "/admin/docker/repos.json"
expect(response.status).to eq(200)
expect(response.parsed_body["repos"].first["official"]).to eq(false)
end

context "when the image check runs" do
before do
sign_in(Fabricate(:admin))
Rails.env.stubs(:test?).returns(false)
DockerManager::GitRepo.any_instance.stubs(:detached_head?).returns(true)
end

it "requires an upgrade when the base image is out of date" do
DockerManager::BaseImage.stubs(:outdated?).returns(true)

get "/admin/docker/repos.json"
expect(response.status).to eq(200)
expect(response.parsed_body["upgrade_required"]).to eq(true)
end

it "does not require an upgrade when the base image is current" do
DockerManager::BaseImage.stubs(:outdated?).returns(false)

get "/admin/docker/repos.json"
expect(response.status).to eq(200)
expect(response.parsed_body).not_to have_key("upgrade_required")
end

it "does not require an upgrade when the base image version cannot be read" do
DockerManager::BaseImage.stubs(:outdated?).raises(
Discourse::Utils::CommandError.new("git fetch failed"),
)

get "/admin/docker/repos.json"
expect(response.status).to eq(200)
expect(response.parsed_body).not_to have_key("upgrade_required")
end
end
end
end