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..265af1a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,15 +1,10 @@ -AllCops: - TargetRubyVersion: 2.2 - TargetRailsVersion: 5.1 - Exclude: - - '*.spec' - - 'Rakefile' +inherit_gem: + theforeman-rubocop: + - default.yml -Rails: - Enabled: true - -Rails/ActionFilter: - EnforcedStyle: action +Style/FrozenStringLiteralComment: + Exclude: + - Rakefile # Don't enforce documentation Style/Documentation: @@ -22,10 +17,6 @@ Metrics/ClassLength: Exclude: - 'test/**/*' -Performance/FixedSize: - Exclude: - - 'test/**/*' - Metrics/BlockLength: Exclude: - 'test/**/*' @@ -47,7 +38,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..0ed8e76 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 => _e 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/setting/rescue.rb b/app/models/setting/rescue.rb index 602c956..6df1031 100644 --- a/app/models/setting/rescue.rb +++ b/app/models/setting/rescue.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Setting class Rescue < ::Setting BLANK_ATTRS.concat ['rescue_pxegrub_tftp_template', 'rescue_pxegrub2_tftp_template'] @@ -5,17 +7,17 @@ class Rescue < ::Setting 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') }), + 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') }), + 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') }) + N_('PXEGrub2 template used when booting rescue system'), + '', N_('PXEGrub2 rescue template'), nil, + :collection => proc { Setting::Rescue.templates('PXEGrub2') }), ] 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/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..6cc28ab 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.5', '< 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..3523df6 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' @@ -13,19 +15,19 @@ class Engine < ::Rails::Engine end initializer 'foreman_monitoring.load_default_settings', - :before => :load_config_initializers do |_app| + :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__) + require_dependency File.expand_path('../../app/models/setting/rescue.rb', __dir__) end end initializer 'foreman_rescue.register_plugin', :before => :finisher_hook do |_app| Foreman::Plugin.register :foreman_rescue do - requires_foreman '>= 1.21' + requires_foreman '>= 3.9' # Add permissions security_block :foreman_rescue do @@ -35,13 +37,11 @@ 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 + rescue StandardError => e + Rails.logger.warn "ForemanRescue: skipping engine hook (#{e})" end rake_tasks do @@ -51,7 +51,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..38d4f8a 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 @@ -13,7 +15,7 @@ namespace :test do end namespace :foreman_rescue do - task :rubocop do + task rubocop: :environment do begin require 'rubocop/rake_task' RuboCop::RakeTask.new(:rubocop_foreman_rescue) do |task| diff --git a/locale/gemspec.rb b/locale/gemspec.rb index 14cbc6a..111f485 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.') 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..816cc97 100644 --- a/test/models/host_test.rb +++ b/test/models/host_test.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'test_plugin_helper' class HostTest < ActiveSupport::TestCase diff --git a/test/test_plugin_helper.rb b/test/test_plugin_helper.rb index ddf429b..3c113aa 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'