1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-02 20:15:22 +02:00

Add zero-config self hosting on Render (#612)

* v1 of backend implementation for self hosting

* Add docs

* Add upgrades controller

* Add global helpers for self hosting mode

* Add self host settings controller

* Conditionally show self hosting settings

* Environment and config updates

* Complete upgrade prompting flow

* Update config for forked repo

* Move configuration of github provider within class

* Add upgrades cron

* Update deploy button

* Update guides

* Fix render deployer

* Typo

* Enable auto upgrades

* Fix cron

* Make upgrade modes more clear and consistent

* Trigger new available version

* Fix logic for displaying upgrade prompts

* Finish implementation

* Fix regression

* Trigger new version

* Add i18n translations

* trigger new version

* reduce caching time for testing

* Decrease cache for testing

* trigger upgrade

* trigger upgrade

* Only trigger deploy once

* trigger upgrade

* If target is commit, always upgrade if any upgrade is available

* trigger upgrade

* trigger upgrade

* Test release

* Change back to maybe repo for defaults

* Fix lint errors

* Clearer naming

* Fix relative link

* Add abs path

* Relative link

* Update docs
This commit is contained in:
Zach Gollwitzer 2024-04-13 09:28:45 -04:00 committed by GitHub
parent 2bbf120e2f
commit 5aca2ff9b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
53 changed files with 1356 additions and 111 deletions

View file

@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
include Authentication
include Authentication, Invitable, SelfHostable
include Pagy::Backend
before_action :sync_accounts
@ -11,11 +11,6 @@ class ApplicationController < ActionController::Base
private
def hosted_app?
ENV["HOSTED"] == "true"
end
helper_method :hosted_app?
def sync_accounts
return if Current.user.blank?

View file

@ -0,0 +1,12 @@
module Invitable
extend ActiveSupport::Concern
included do
helper_method :invite_code_required?
end
private
def invite_code_required?
ENV["REQUIRE_INVITE_CODE"] == "true"
end
end

View file

@ -0,0 +1,12 @@
module SelfHostable
extend ActiveSupport::Concern
included do
helper_method :self_hosted?
end
private
def self_hosted?
ENV["SELF_HOSTING_ENABLED"] == "true"
end
end

View file

@ -4,7 +4,7 @@ class RegistrationsController < ApplicationController
layout "auth"
before_action :set_user, only: :create
before_action :claim_invite_code, only: :create, if: :hosted_app?
before_action :claim_invite_code, only: :create, if: :invite_code_required?
def new
@user = User.new

View file

@ -0,0 +1,46 @@
class Settings::SelfHostingController < ApplicationController
before_action :verify_self_hosting_enabled
def edit
end
def update
if all_updates_valid?
self_hosting_params.keys.each do |key|
Setting.send("#{key}=", self_hosting_params[key].strip)
end
redirect_to edit_settings_self_hosting_path, notice: t(".success")
else
flash.now[:error] = @errors.first.message
render :edit, status: :unprocessable_entity
end
end
private
def all_updates_valid?
@errors = ActiveModel::Errors.new(Setting)
self_hosting_params.keys.each do |key|
setting = Setting.new(var: key)
setting.value = self_hosting_params[key].strip
unless setting.valid?
@errors.merge!(setting.errors)
end
end
if self_hosting_params[:upgrades_mode] == "auto" && self_hosting_params[:render_deploy_hook].blank?
@errors.add(:render_deploy_hook, t("settings.self_hosting.update.render_deploy_hook_error"))
end
@errors.empty?
end
def self_hosting_params
params.require(:setting).permit(:render_deploy_hook, :upgrades_mode, :upgrades_target)
end
def verify_self_hosting_enabled
head :not_found unless self_hosted?
end
end

View file

@ -0,0 +1,56 @@
class UpgradesController < ApplicationController
before_action :verify_upgrades_enabled
def acknowledge
commit_sha = params[:id]
upgrade = Upgrader.find_upgrade(commit_sha)
if upgrade
if upgrade.available?
Current.user.acknowledge_upgrade_prompt(upgrade.commit_sha)
flash[:notice] = t(".upgrade_dismissed")
elsif upgrade.complete?
Current.user.acknowledge_upgrade_alert(upgrade.commit_sha)
flash[:notice] = t(".upgrade_complete_dismiss")
else
flash[:alert] = t(".upgrade_not_available")
end
else
flash[:alert] = t(".upgrade_not_found")
end
redirect_back(fallback_location: root_path)
end
def deploy
commit_sha = params[:id]
upgrade = Upgrader.find_upgrade(commit_sha)
unless upgrade
flash[:alert] = t(".upgrade_not_found")
return redirect_back(fallback_location: root_path)
end
prior_acknowledged_upgrade_commit_sha = Current.user.last_prompted_upgrade_commit_sha
# Optimistically acknowledge the upgrade prompt
Current.user.acknowledge_upgrade_prompt(upgrade.commit_sha)
upgrade_result = Upgrader.upgrade_to(upgrade)
if upgrade_result[:success]
flash[:notice] = upgrade_result[:message]
else
# If the upgrade fails, revert to the prior acknowledged upgrade
Current.user.acknowledge_upgrade_prompt(prior_acknowledged_upgrade_commit_sha)
flash[:alert] = upgrade_result[:message]
end
redirect_back(fallback_location: root_path)
end
private
def verify_upgrades_enabled
head :not_found unless ENV["UPGRADES_ENABLED"] == "true"
end
end