diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2afb61a..bc50187 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 }} diff --git a/.rubocop.yml b/.rubocop.yml index f5ebb93..f4b2fce 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -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: @@ -22,10 +22,6 @@ Metrics/ClassLength: Exclude: - 'test/**/*' -Performance/FixedSize: - Exclude: - - 'test/**/*' - Metrics/BlockLength: Exclude: - 'test/**/*' @@ -47,7 +43,7 @@ Style/HashSyntax: - ruby19 - hash_rockets -Metrics/LineLength: +Layout/LineLength: Max: 190 Style/SymbolArray: diff --git a/Gemfile b/Gemfile index fa75df1..7f4f5e9 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec diff --git a/Rakefile b/Rakefile index aaf755b..b0a91cf 100755 --- a/Rakefile +++ b/Rakefile @@ -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 @@ -38,7 +38,7 @@ task default: :test begin require 'rubocop/rake_task' RuboCop::RakeTask.new -rescue => _ +rescue StandardError puts 'Rubocop not loaded.' end diff --git a/app/controllers/foreman_rescue/hosts_controller.rb b/app/controllers/foreman_rescue/hosts_controller.rb index 72edf33..f61408d 100644 --- a/app/controllers/foreman_rescue/hosts_controller.rb +++ b/app/controllers/foreman_rescue/hosts_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/helpers/concerns/foreman_rescue/hosts_helper_extensions.rb b/app/helpers/concerns/foreman_rescue/hosts_helper_extensions.rb index 2eb2292..22383e1 100644 --- a/app/helpers/concerns/foreman_rescue/hosts_helper_extensions.rb +++ b/app/helpers/concerns/foreman_rescue/hosts_helper_extensions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ForemanRescue module HostsHelperExtensions def host_title_actions(host) @@ -5,15 +7,15 @@ def host_title_actions(host) 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 ) ) diff --git a/app/models/concerns/foreman_rescue/host_extensions.rb b/app/models/concerns/foreman_rescue/host_extensions.rb index 0351789..d52dc46 100644 --- a/app/models/concerns/foreman_rescue/host_extensions.rb +++ b/app/models/concerns/foreman_rescue/host_extensions.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ForemanRescue module HostExtensions def self.prepended(base) diff --git a/app/models/concerns/foreman_rescue/orchestration/tftp.rb b/app/models/concerns/foreman_rescue/orchestration/tftp.rb index e55f55f..b58484b 100644 --- a/app/models/concerns/foreman_rescue/orchestration/tftp.rb +++ b/app/models/concerns/foreman_rescue/orchestration/tftp.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ForemanRescue module Orchestration module TFTP diff --git a/app/models/concerns/foreman_rescue/provisioning_template_extensions.rb b/app/models/concerns/foreman_rescue/provisioning_template_extensions.rb new file mode 100644 index 0000000..70862de --- /dev/null +++ b/app/models/concerns/foreman_rescue/provisioning_template_extensions.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module ForemanRescue + module ProvisioningTemplateExtensions + def self.templates_by_kind(kind) + template_kind = TemplateKind.find_by(name: kind) + ProvisioningTemplate.where(:template_kind => template_kind).pluck(:name, :name).to_h + end + end +end diff --git a/app/models/setting/rescue.rb b/app/models/setting/rescue.rb deleted file mode 100644 index 602c956..0000000 --- a/app/models/setting/rescue.rb +++ /dev/null @@ -1,45 +0,0 @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 87cf83a..6d0ea40 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.routes.draw do constraints(:id => /[^\/]+/) do resources :hosts, controller: 'foreman_rescue/hosts', :only => [] do diff --git a/db/migrate/20170901131321_add_rescue_mode_to_host.foreman_rescue.rb b/db/migrate/20170901131321_add_rescue_mode_to_host.foreman_rescue.rb index fc9e2ce..931e079 100644 --- a/db/migrate/20170901131321_add_rescue_mode_to_host.foreman_rescue.rb +++ b/db/migrate/20170901131321_add_rescue_mode_to_host.foreman_rescue.rb @@ -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 diff --git a/db/migrate/20240506132712_migrate_rescue_settings_category_to_dsl.rb b/db/migrate/20240506132712_migrate_rescue_settings_category_to_dsl.rb new file mode 100644 index 0000000..595e6c7 --- /dev/null +++ b/db/migrate/20240506132712_migrate_rescue_settings_category_to_dsl.rb @@ -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 diff --git a/db/seeds.d/103-provisioning_templates.rb b/db/seeds.d/103-provisioning_templates.rb index ecf81ff..90446a8 100644 --- a/db/seeds.d/103-provisioning_templates.rb +++ b/db/seeds.d/103-provisioning_templates.rb @@ -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| diff --git a/foreman_rescue.gemspec b/foreman_rescue.gemspec index c6d15b6..b36f96e 100644 --- a/foreman_rescue.gemspec +++ b/foreman_rescue.gemspec @@ -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' @@ -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 diff --git a/lib/foreman_rescue.rb b/lib/foreman_rescue.rb index f4f47b8..ca6aa23 100644 --- a/lib/foreman_rescue.rb +++ b/lib/foreman_rescue.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'foreman_rescue/engine' module ForemanRescue diff --git a/lib/foreman_rescue/engine.rb b/lib/foreman_rescue/engine.rb index 096714b..8a5a887 100644 --- a/lib/foreman_rescue/engine.rb +++ b/lib/foreman_rescue/engine.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ForemanRescue class Engine < ::Rails::Engine engine_name 'foreman_rescue' @@ -12,20 +14,34 @@ class Engine < ::Rails::Engine end end - initializer 'foreman_monitoring.load_default_settings', - :before => :load_config_initializers do |_app| - if begin - Setting.table_exists? - rescue StandardError - false - end - require_dependency File.expand_path('../../../app/models/setting/rescue.rb', __FILE__) - end - end + initializer 'foreman_rescue.register_plugin', :before => :finisher_hook do |_app| # rubocop:disable Metrics/BlockLength + Foreman::Plugin.register :foreman_rescue do # rubocop:disable Metrics/BlockLength + requires_foreman '>= 3.9' - initializer 'foreman_rescue.register_plugin', :before => :finisher_hook do |_app| - Foreman::Plugin.register :foreman_rescue do - requires_foreman '>= 1.21' + settings do + category :rescue, N_('Rescue') do + setting('rescue_pxelinux_tftp_template', + type: :string, + default: 'Kickstart rescue PXELinux', + full_name: N_('PXELinux rescue template'), + description: N_('PXELinux template used when booting rescue system'), + collection: proc { ProvisioningTemplate.templates_by_kind('PXELinux') }) + + setting('rescue_pxegrub_tftp_template', + type: :string, + default: '', + full_name: N_('PXEGrub rescue template'), + description: N_('PXEGrub template used when booting rescue system'), + collection: proc { ProvisioningTemplate.templates_by_kind('PXEGrub') }) + + setting('rescue_pxegrub2_tftp_template', + type: :string, + default: '', + full_name: N_('PXEGrub2 rescue template'), + description: N_('PXEGrub2 template used when booting rescue system'), + collection: proc { ProvisioningTemplate.templates_by_kind('PXEGrub2') }) + end + end # Add permissions security_block :foreman_rescue do @@ -35,13 +51,12 @@ class Engine < ::Rails::Engine end config.to_prepare do - begin - Host::Managed.send(:prepend, ForemanRescue::HostExtensions) - HostsHelper.send(:prepend, ForemanRescue::HostsHelperExtensions) - Nic::Managed.send(:prepend, ForemanRescue::Orchestration::TFTP) - rescue StandardError => e - Rails.logger.warn "ForemanRescue: skipping engine hook (#{e})" - end + Host::Managed.prepend ForemanRescue::HostExtensions + HostsHelper.prepend ForemanRescue::HostsHelperExtensions + Nic::Managed.prepend ForemanRescue::Orchestration::TFTP + ProvisioningTemplate.prepend ForemanRescue::ProvisioningTemplateExtensions + rescue StandardError => e + Rails.logger.warn "ForemanRescue: skipping engine hook (#{e})" end rake_tasks do @@ -51,7 +66,7 @@ class Engine < ::Rails::Engine end initializer 'foreman_rescue.register_gettext', after: :load_config_initializers do |_app| - locale_dir = File.join(File.expand_path('../../..', __FILE__), 'locale') + locale_dir = File.join(File.expand_path('../..', __dir__), 'locale') locale_domain = 'foreman_rescue' Foreman::Gettext::Support.add_text_domain locale_domain, locale_dir end diff --git a/lib/foreman_rescue/version.rb b/lib/foreman_rescue/version.rb index f657fe7..779885b 100644 --- a/lib/foreman_rescue/version.rb +++ b/lib/foreman_rescue/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ForemanRescue - VERSION = '3.0.0'.freeze + VERSION = '3.0.0' end diff --git a/lib/tasks/foreman_rescue_tasks.rake b/lib/tasks/foreman_rescue_tasks.rake index 4635bdd..71f29ad 100644 --- a/lib/tasks/foreman_rescue_tasks.rake +++ b/lib/tasks/foreman_rescue_tasks.rake @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rake/testtask' # Tests @@ -12,24 +14,4 @@ namespace :test do end end -namespace :foreman_rescue do - task :rubocop do - begin - require 'rubocop/rake_task' - RuboCop::RakeTask.new(:rubocop_foreman_rescue) do |task| - task.patterns = ["#{ForemanRescue::Engine.root}/app/**/*.rb", - "#{ForemanRescue::Engine.root}/lib/**/*.rb", - "#{ForemanRescue::Engine.root}/test/**/*.rb"] - end - rescue StandardError - puts 'Rubocop not loaded.' - end - - Rake::Task['rubocop_foreman_rescue'].invoke - end -end - Rake::Task[:test].enhance ['test:foreman_rescue'] - -load 'tasks/jenkins.rake' -Rake::Task['jenkins:unit'].enhance ['test:foreman_rescue', 'foreman_rescue:rubocop'] if Rake::Task.task_defined?(:'jenkins:unit') diff --git a/locale/gemspec.rb b/locale/gemspec.rb index 14cbc6a..4491b1e 100644 --- a/locale/gemspec.rb +++ b/locale/gemspec.rb @@ -1,2 +1,4 @@ +# frozen_string_literal: true + # Matches foreman_rescue.gemspec -_('TODO: Description of ForemanRescue.') +_('Foreman Plugin to provide the ability to boot a host into a rescue system.') diff --git a/test/controllers/hosts_controller_test.rb b/test/controllers/hosts_controller_test.rb index 73b956a..f20fa8e 100644 --- a/test/controllers/hosts_controller_test.rb +++ b/test/controllers/hosts_controller_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_plugin_helper' module ForemanRescue diff --git a/test/factories/host.rb b/test/factories/host.rb index 992830f..36af20e 100644 --- a/test/factories/host.rb +++ b/test/factories/host.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + FactoryBot.modify do factory :host do trait :rescue_mode do diff --git a/test/lib/tasks/seeds_test.rb b/test/lib/tasks/seeds_test.rb index 3eac976..9ac3c77 100644 --- a/test/lib/tasks/seeds_test.rb +++ b/test/lib/tasks/seeds_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_plugin_helper' module ForemanRescue @@ -27,7 +29,7 @@ class SeedsTest < ActiveSupport::TestCase def seed User.current = FactoryBot.build(:user, :admin => true, :organizations => [], :locations => []) - load Rails.root.join('db', 'seeds.rb') + load Rails.root.join('db/seeds.rb') end end end diff --git a/test/models/host_test.rb b/test/models/host_test.rb index a457364..7f28642 100644 --- a/test/models/host_test.rb +++ b/test/models/host_test.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: true + require 'test_plugin_helper' class HostTest < ActiveSupport::TestCase setup do User.current = FactoryBot.build(:user, :admin) - setup_settings disable_orchestration end diff --git a/test/test_plugin_helper.rb b/test/test_plugin_helper.rb index ddf429b..0cd0f64 100644 --- a/test/test_plugin_helper.rb +++ b/test/test_plugin_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # This calls the main test_helper in Foreman-core require 'test_helper' require 'database_cleaner' @@ -9,10 +11,6 @@ # Foreman's setup doesn't handle cleaning up for Minitest::Spec DatabaseCleaner.strategy = :transaction -def setup_settings - Setting::Rescue.load_defaults -end - module Minitest class Spec before :each do diff --git a/test/unit/foreman_rescue/access_permissions_test.rb b/test/unit/foreman_rescue/access_permissions_test.rb new file mode 100644 index 0000000..6f28dec --- /dev/null +++ b/test/unit/foreman_rescue/access_permissions_test.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +require 'test_plugin_helper' +require 'unit/shared/access_permissions_test_base' + +# Permissions are added in AccessPermissions with lists of controllers and +# actions that they enable access to. For non-admin users, we need to test +# that there are permissions available that cover every controller action, else +# it can't be delegated and this will lead to parts of the application that +# aren't functional for non-admin users. +# +# In particular, it's important that actions for AJAX requests are added to +# an appropriate permission so views using those requests function. +class AccessPermissionsTest < ActiveSupport::TestCase + include AccessPermissionsTestBase + + check_routes(ForemanRescue::Engine.routes, []) +end