Skip to content

Commit

Permalink
Support Foreman 3.9 and newer
Browse files Browse the repository at this point in the history
- migrate to settings DSL
- drop old Foreman version support
- switch to upstream CI
- switch to upstream rubocop
  • Loading branch information
laugmanuel committed May 6, 2024
1 parent 55dc0de commit 559209a
Show file tree
Hide file tree
Showing 24 changed files with 149 additions and 180 deletions.
113 changes: 27 additions & 86 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,93 +1,34 @@
name: CI
on: [push, pull_request]
env:
RAILS_ENV: test
DATABASE_URL: postgresql://postgres:@localhost/test
DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: true
name: Ruby test

on:
pull_request:
push:
branches:
- master

concurrency:
group: ${{ github.ref_name }}-${{ github.workflow }}
cancel-in-progress: true

jobs:
rubocop:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.4
- name: Setup
run: |
gem install bundler
bundle install --jobs=3 --retry=3
- name: Run rubocop
run: bundle exec rubocop
uses: theforeman/actions/.github/workflows/rubocop.yml@v0
with:
command: bundle exec rubocop --parallel --format github

test:
runs-on: ubuntu-latest
name: Ruby
needs: rubocop
services:
postgres:
image: postgres:12.1
ports: ['5432:5432']
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
strategy:
fail-fast: false
matrix:
foreman-core-branch: [1.21-stable, 1.22-stable, 1.23-stable, 1.24-stable, 2.0-stable, 2.1-stable, 2.2-stable, develop]
ruby-version: [2.4, 2.5, 2.6]
node-version: [10]
exclude:
- foreman-core-branch: 2.0-stable
ruby-version: 2.4
- foreman-core-branch: 2.1-stable
ruby-version: 2.4
- foreman-core-branch: 2.2-stable
ruby-version: 2.4
- foreman-core-branch: develop
ruby-version: 2.4
steps:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install build-essential libcurl4-openssl-dev libvirt-dev ruby-libvirt zlib1g-dev libpq-dev
- uses: actions/checkout@v2
with:
repository: theforeman/foreman
ref: ${{ matrix.foreman-core-branch }}
- uses: actions/checkout@v2
with:
path: foreman_rescue
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Setup Bundler
run: |
echo "gem 'foreman_rescue', path: './foreman_rescue'" > bundler.d/foreman_rescue.local.rb
gem install bundler
bundle config path vendor/bundle
bundle config set without journald development console mysql2 sqlite
bundle lock --update
- name: Cache gems
uses: actions/cache@v2
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
restore-keys: |
${{ runner.os }}-gems-
- name: Setup Plugin
run: |
bundle install --jobs=3 --retry=3
bundle exec rake db:create
bundle exec rake db:migrate
npm install
bundle exec rake webpack:compile
- name: Run plugin tests
run: |
bundle exec rake test:foreman_rescue
bundle exec rake test TEST="test/unit/foreman/access_permissions_test.rb"
- name: Precompile plugin assets
run: bundle exec rake 'plugin:assets:precompile[foreman_rescue]'
env:
RAILS_ENV: production
foreman:
- "develop"
- "3.10-stable"
- "3.9-stable"
uses: theforeman/actions/.github/workflows/foreman_plugin.yml@v0
with:
plugin: foreman_rescue
foreman_version: ${{ matrix.foreman }}
environment_variables: |
FOREMAN_VERSION=${{ matrix.foreman }}
26 changes: 11 additions & 15 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
AllCops:
TargetRubyVersion: 2.2
TargetRailsVersion: 5.1
Exclude:
- '*.spec'
- 'Rakefile'
inherit_gem:
theforeman-rubocop:
- default.yml

Rails:
Enabled: true
AllCops:
TargetRubyVersion: '2.7'
Exclude:
- 'vendor/bundle/**/*'

Rails/ActionFilter:
EnforcedStyle: action
Style/FrozenStringLiteralComment:
Exclude:
- Rakefile

# Don't enforce documentation
Style/Documentation:
Expand All @@ -22,10 +22,6 @@ Metrics/ClassLength:
Exclude:
- 'test/**/*'

Performance/FixedSize:
Exclude:
- 'test/**/*'

Metrics/BlockLength:
Exclude:
- 'test/**/*'
Expand All @@ -47,7 +43,7 @@ Style/HashSyntax:
- ruby19
- hash_rockets

Metrics/LineLength:
Layout/LineLength:
Max: 190

Style/SymbolArray:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)

Bundler::GemHelper.install_tasks

Expand All @@ -38,7 +38,7 @@ task default: :test
begin
require 'rubocop/rake_task'
RuboCop::RakeTask.new
rescue => _
rescue StandardError => _e
puts 'Rubocop not loaded.'
end

Expand Down
15 changes: 10 additions & 5 deletions app/controllers/foreman_rescue/hosts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# frozen_string_literal: true

module ForemanRescue
class HostsController < ::HostsController
before_action :find_resource, :only => [:rescue, :set_rescue, :cancel_rescue]
define_action_permission ['rescue', 'set_rescue', 'cancel_rescue'], :rescue

def rescue; end
def rescue
end

def set_rescue
forward_url_options
Expand All @@ -16,10 +19,10 @@ def set_rescue
_('Enabled %s for boot into rescue system on next boot, but failed to power cycle the host.')
end
process_success :success_msg => message % @host, :success_redirect => :back
rescue StandardError => error
rescue StandardError => e
message = _('Failed to reboot %s.') % @host
warning(message)
Foreman::Logging.exception(message, error)
Foreman::Logging.exception(message, e)
process_success :success_msg => _('Enabled %s for rescue system on next boot.') % @host, :success_redirect => :back
end
else
Expand All @@ -35,8 +38,10 @@ def cancel_rescue
process_success :success_msg => _('Canceled booting into rescue system for %s.') % @host.name, :success_redirect => :back
else
process_error :redirect => :back,
:error_msg => _('Failed to cancel booting into rescue system for %{hostname} with the following errors: %{errors}') %
{ :hostname => @host.name, :errors => @host.errors.full_messages.to_sentence }
:error_msg => _('Failed to cancel booting into rescue system for %{hostname} with the following errors: %{errors}') % {
:hostname => @host.name,
:errors => @host.errors.full_messages.to_sentence,
}
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions app/helpers/concerns/foreman_rescue/hosts_helper_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
# frozen_string_literal: true

module ForemanRescue
module HostsHelperExtensions
def host_title_actions(host)
title_actions(
button_group(
if host.rescue_mode?
link_to_if_authorized(_('Cancel rescue'), hash_for_cancel_rescue_host_path(:id => host).merge(:auth_object => host, :permission => 'rescue_hosts'),
:disabled => host.can_be_rescued?,
:title => _('Cancel rescue system for this host.'),
:class => 'btn btn-default',
:method => :put)
:disabled => host.can_be_rescued?,
:title => _('Cancel rescue system for this host.'),
:class => 'btn btn-default',
:method => :put)
else
link_to_if_authorized(_('Rescue'), hash_for_rescue_host_path(:id => host).merge(:auth_object => host, :permission => 'rescue_hosts'),
:disabled => !host.can_be_rescued?,
:title => _('Activate rescue mode for this host.'),
:class => 'btn btn-default')
:disabled => !host.can_be_rescued?,
:title => _('Activate rescue mode for this host.'),
:class => 'btn btn-default')
end
)
)
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/foreman_rescue/host_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ForemanRescue
module HostExtensions
def self.prepended(base)
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/foreman_rescue/orchestration/tftp.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ForemanRescue
module Orchestration
module TFTP
Expand Down
36 changes: 2 additions & 34 deletions app/models/setting/rescue.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
# frozen_string_literal: true

class Setting
class Rescue < ::Setting
BLANK_ATTRS.concat ['rescue_pxegrub_tftp_template', 'rescue_pxegrub2_tftp_template']

def self.default_settings
[
set('rescue_pxelinux_tftp_template',
N_('PXELinux template used when booting rescue system'),
'Kickstart rescue PXELinux', N_('PXELinux rescue template'), nil,
:collection => proc { Setting::Rescue.templates('PXELinux') }),
set('rescue_pxegrub_tftp_template',
N_('PXEGrub template used when booting rescue system'),
'', N_('PXEGrub rescue template'), nil,
:collection => proc { Setting::Rescue.templates('PXEGrub') }),
set('rescue_pxegrub2_tftp_template',
N_('PXEGrub2 template used when booting rescue system'),
'', N_('PXEGrub2 rescue template'), nil,
:collection => proc { Setting::Rescue.templates('PXEGrub2') })
]
end

def self.load_defaults
# Check the table exists
return unless super

transaction do
default_settings.each { |s| create! s.update(:category => 'Setting::Rescue') }
end

true
end

def self.templates(kind)
template_kind = TemplateKind.find_by(name: kind)
templates = ProvisioningTemplate.where(:template_kind => template_kind)
templates.each_with_object({}) do |template, hsh|
hsh[template.name] = template.name
end
end

def self.humanized_category
N_('Rescue System')
end
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Rails.application.routes.draw do
constraints(:id => /[^\/]+/) do
resources :hosts, controller: 'foreman_rescue/hosts', :only => [] do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddRescueModeToHost < ActiveRecord::Migration[4.2]
def change
add_column :hosts, :rescue_mode, :boolean, default: false, index: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class MigrateRescueSettingsCategoryToDsl < ActiveRecord::Migration[6.0]
class MigrationSettings < ApplicationRecord
self.table_name = :settings
end

def up
MigrationSettings.where(category: 'Setting::Rescue').update_all(category: 'Setting') if column_exists?(
:settings, :category
)
end
end
4 changes: 3 additions & 1 deletion db/seeds.d/103-provisioning_templates.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

User.as_anonymous_admin do
templates = [
{ :name => 'Kickstart rescue PXELinux', :source => 'PXELinux/kickstart_rescue_pxelinux.erb', :template_kind => TemplateKind.find_by(:name => 'PXELinux') }
{ :name => 'Kickstart rescue PXELinux', :source => 'PXELinux/kickstart_rescue_pxelinux.erb', :template_kind => TemplateKind.find_by(:name => 'PXELinux') },
]

templates.each do |template|
Expand Down
8 changes: 6 additions & 2 deletions foreman_rescue.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require File.expand_path('../lib/foreman_rescue/version', __FILE__)
# frozen_string_literal: true

require File.expand_path('lib/foreman_rescue/version', __dir__)

Gem::Specification.new do |s|
s.name = 'foreman_rescue'
Expand All @@ -11,9 +13,11 @@ Gem::Specification.new do |s|
# also update locale/gemspec.rb
s.description = 'Foreman Plugin to provide the ability to boot a host into a rescue system.'

s.required_ruby_version = '>= 2.7', '< 4'

s.files = Dir['{app,config,db,lib,locale}/**/*'] + ['LICENSE', 'Rakefile', 'README.md']
s.test_files = Dir['test/**/*']

s.add_development_dependency 'rdoc'
s.add_development_dependency 'rubocop', '0.52.0'
s.add_development_dependency 'theforeman-rubocop', '~> 0.1.2'
end
2 changes: 2 additions & 0 deletions lib/foreman_rescue.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'foreman_rescue/engine'

module ForemanRescue
Expand Down
Loading

0 comments on commit 559209a

Please sign in to comment.