From 3a145ab4647e2da66f72fab243a406ae3fdf0a3b Mon Sep 17 00:00:00 2001 From: Alexander Olofsson <alexander.olofsson@liu.se> Date: Wed, 2 Nov 2022 14:09:59 +0100 Subject: [PATCH] Add CI and clean up code slightly --- .gitignore | 4 + .gitlab-ci.yml | 101 ++++++++++++++++++ .rubocop.yml | 13 +++ .travis.yml | 5 - .../foreman_template_tasks/template_action.rb | 6 +- .../template_export_action.rb | 1 - .../template_import_action.rb | 1 - foreman_template_tasks.gemspec | 5 + lib/foreman_template_tasks/engine.rb | 9 -- lib/foreman_template_tasks/version.rb | 2 +- lib/tasks/foreman_template_tasks.rake | 66 ++++++------ 11 files changed, 160 insertions(+), 53 deletions(-) create mode 100644 .gitlab-ci.yml create mode 100644 .rubocop.yml delete mode 100644 .travis.yml diff --git a/.gitignore b/.gitignore index 9106b2a..272689d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,12 @@ /.bundle/ +/vendor/ /.yardoc +/Gemfile.lock /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ +/public/ +/*.gem diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..524ea57 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,101 @@ +--- +variables: + CI_FOREMAN_PLUGIN: foreman_template_tasks + # CI_FOREMAN_WEB: true + + POSTGRES_DB: test + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + +services: + - name: postgres:13-alpine + alias: db + +image: ruby:2.7 + +lint: + cache: + paths: + - vendor/ruby + when: always + + script: + - gem install bundler -N + - bundle config set path vendor + - bundle install -j $(nproc) --retry=3 + - bundle exec rubocop app/ lib/ + +test: + parallel: + matrix: + - FOREMAN_VERSION: + # - 2.5-stable + - 3.0-stable + # - 3.1-stable + # - 3.2-stable + # - 3.3-stable + - 3.4-stable + + cache: + key: foreman-$FOREMAN_VERSION + paths: + - vendor/ruby + - node_modules + when: always + + variables: + RAILS_ENV: test + DATABASE_URL: postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@db/$POSTGRES_DB + DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL: "true" + + before_script: + # Set up Ruby build dependencies + - apt-get update -yqq + - apt-get install -yqq build-essential libcurl4-openssl-dev postgresql-client-13 zlib1g-dev libpq-dev + - apt-get install -yqq --no-install-recommends npm + - gem install bundler -N + + # Install foreman + - FOREMAN_DIR="$(dirname "$CI_PROJECT_DIR")/foreman" + - git clone -b "$FOREMAN_VERSION" --depth=1 -- https://github.com/theforeman/foreman "$FOREMAN_DIR" + - cd "$FOREMAN_DIR" + - bundle config set path "$CI_PROJECT_DIR/vendor" + - bundle config set without console development ec2 gce journald libvirt openstack ovirt sqlite vmware + - bundle install -j $(nproc) --retry=3 + + # Configure database + - bundle exec rake db:create + - bundle exec rake db:migrate + + # Install plugin + - "echo \"gem '$CI_FOREMAN_PLUGIN', path: '$CI_PROJECT_DIR'\" > bundler.d/local.rb" + - bundle install -j $(nproc) --retry=3 + - bundle exec rake db:migrate + + - | + if [ -n "$CI_FOREMAN_WEB" ]; then + mkdir -p "$CI_PROJECT_DIR/node_modules" + ln -s "$CI_PROJECT_DIR/node_modules" node_modules + npm install --legacy-peer-deps + bundle exec rake webpack:compile + fi + script: + # Foreman-side tasks + - cd "$FOREMAN_DIR" + - | + if [ -d "$CI_PROJECT_DIR/test" ]; then + bundle exec rake "test:$CI_FOREMAN_PLUGIN" + [ -d 'jenkins/reports' ] && mv jenkins/reports "$CI_PROJECT_DIR" + fi + - '[ -d "$CI_PROJECT_DIR/app/assets" ] && RAILS_ENV=production bundle exec rake "plugin:assets:precompile[$CI_FOREMAN_PLUGIN]"' + + # Plugin-side tasks + - cd "$CI_PROJECT_DIR" + - gem build $CI_FOREMAN_PLUGIN.gemspec + + artifacts: + paths: + - '*.gem' + - 'public' + reports: + junit: reports/unit/*.xml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..a420c76 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,13 @@ +--- +AllCops: + NewCops: enable + +Style/Documentation: + Enabled: false + +Metrics/AbcSize: + Enabled: false +Metrics/CyclomaticComplexity: + Enabled: false +Metrics/PerceivedComplexity: + Enabled: false diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f723cc2..0000000 --- a/.travis.yml +++ /dev/null @@ -1,5 +0,0 @@ -sudo: false -language: ruby -rvm: - - 2.3.8 -before_install: gem install bundler -v 1.16.1 diff --git a/app/lib/actions/foreman_template_tasks/template_action.rb b/app/lib/actions/foreman_template_tasks/template_action.rb index 8594f2f..5fb765b 100644 --- a/app/lib/actions/foreman_template_tasks/template_action.rb +++ b/app/lib/actions/foreman_template_tasks/template_action.rb @@ -30,6 +30,7 @@ module Actions plan_self end + # rubocop:disable Metrics/MethodLength def humanized_output return unless output[:results] @@ -52,17 +53,18 @@ module Actions end out += if exceptions.any? - ", #{output[:results].count { |r| !r[:exception].nil? }} templates skipped;\n- " + exceptions.join("\n- ") + ", #{output[:results].count { |r| !r[:exception].nil? }} templates skipped;\n- #{exceptions.join("\n- ")}" # rubocop:disable Layout/LineLength else '.' end if changes.any? - out += "\n\nTemplates changed:\n- #{changes.map { |ch| "#{ch[:type].camelcase} | #{ch[:name]}" }.join("\n- ")}" + out += "\n\nTemplates changed:\n- #{changes.map { |ch| "#{ch[:type].camelcase} | #{ch[:name]}" }.join("\n- ")}" # rubocop:disable Layout/LineLength end out end + # rubocop:enable Metrics/MethodLength def humanized_action raise NotImplementedError diff --git a/app/lib/actions/foreman_template_tasks/template_export_action.rb b/app/lib/actions/foreman_template_tasks/template_export_action.rb index 0b97739..50af5e1 100644 --- a/app/lib/actions/foreman_template_tasks/template_export_action.rb +++ b/app/lib/actions/foreman_template_tasks/template_export_action.rb @@ -3,7 +3,6 @@ module Actions module ForemanTemplateTasks class TemplateExportAction < TemplateAction - def run exporter = ForemanTemplates::TemplateExporter.new(input[:task_params]) output[:results] = exporter.export![:results].map(&:to_h) diff --git a/app/lib/actions/foreman_template_tasks/template_import_action.rb b/app/lib/actions/foreman_template_tasks/template_import_action.rb index c405f2b..61d6fff 100644 --- a/app/lib/actions/foreman_template_tasks/template_import_action.rb +++ b/app/lib/actions/foreman_template_tasks/template_import_action.rb @@ -3,7 +3,6 @@ module Actions module ForemanTemplateTasks class TemplateImportAction < TemplateAction - def run importer = ForemanTemplates::TemplateImporter.new(input[:task_params]) output[:results] = importer.import![:results].map(&:to_h) diff --git a/foreman_template_tasks.gemspec b/foreman_template_tasks.gemspec index 0f44cbd..ded4135 100644 --- a/foreman_template_tasks.gemspec +++ b/foreman_template_tasks.gemspec @@ -16,4 +16,9 @@ Gem::Specification.new do |spec| spec.add_runtime_dependency 'foreman-tasks' spec.add_runtime_dependency 'foreman_templates' + + spec.add_development_dependency 'rubocop' + spec.add_development_dependency 'rubocop-minitest' + spec.add_development_dependency 'rubocop-performance' + spec.add_development_dependency 'rubocop-rails' end diff --git a/lib/foreman_template_tasks/engine.rb b/lib/foreman_template_tasks/engine.rb index 4f5a734..03de41f 100644 --- a/lib/foreman_template_tasks/engine.rb +++ b/lib/foreman_template_tasks/engine.rb @@ -22,14 +22,5 @@ module ForemanTemplateTasks initializer 'foreman_template_tasks.dynflow_world', before: 'foreman_tasks.initialize_dynflow' do |_app| ::ForemanTasks.dynflow.require! end - - # Include concerns in this config.to_prepare block - config.to_prepare do - begin - # :: - rescue StandardError => e - Rails.logger.warn "ForemanTemplateTasks: skipping engine hook (#{e})" - end - end end end diff --git a/lib/foreman_template_tasks/version.rb b/lib/foreman_template_tasks/version.rb index 28d0071..177ca13 100644 --- a/lib/foreman_template_tasks/version.rb +++ b/lib/foreman_template_tasks/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ForemanTemplateTasks - VERSION = '0.2.1'.freeze + VERSION = '0.2.1' end diff --git a/lib/tasks/foreman_template_tasks.rake b/lib/tasks/foreman_template_tasks.rake index 30be783..3f6e6ff 100644 --- a/lib/tasks/foreman_template_tasks.rake +++ b/lib/tasks/foreman_template_tasks.rake @@ -1,24 +1,22 @@ # frozen_string_literal: true -require 'pp' - namespace :template_tasks do # rubocop:disable Metrics/BlockLength desc 'Start a template import according to settings' task import: [:environment, 'dynflow:client'] do User.current = User.anonymous_admin - context = ENV['context'] + context = ENV.fetch('context', nil) r = ForemanTasks.delay Actions::ForemanTemplateTasks::TemplateImportAction, { start_at: Time.zone.now }, { context: context, - repo: ENV['repo'], - branch: ENV['branch'], - prefix: ENV['prefix'], - dirname: ENV['dirname'], - filter: ENV['filter'], - associate: ENV['associate'], - lock: ENV['lock'] + repo: ENV.fetch('repo', nil), + branch: ENV.fetch('branch', nil), + prefix: ENV.fetch('prefix', nil), + dirname: ENV.fetch('dirname', nil), + filter: ENV.fetch('filter', nil), + associate: ENV.fetch('associate', nil), + lock: ENV.fetch('lock', nil) }.compact url = Rails.application.routes.url_helpers.foreman_tasks_task_url(r, host: SETTINGS[:fqdn]) @@ -29,18 +27,18 @@ namespace :template_tasks do # rubocop:disable Metrics/BlockLength desc 'Start a template export according to settings' task export: [:environment, 'dynflow:client'] do User.current = User.anonymous_admin - context = ENV['context'] + context = ENV.fetch('context', nil) r = ForemanTasks.delay Actions::ForemanTemplateTasks::TemplateExportAction, { start_at: Time.zone.now }, { context: context, - repo: ENV['repo'], - branch: ENV['branch'], - prefix: ENV['prefix'], - dirname: ENV['dirname'], - filter: ENV['filter'], - metadata_export_mode: ENV['metadata_export_mode'] + repo: ENV.fetch('repo', nil), + branch: ENV.fetch('branch', nil), + prefix: ENV.fetch('prefix', nil), + dirname: ENV.fetch('dirname', nil), + filter: ENV.fetch('filter', nil), + metadata_export_mode: ENV.fetch('metadata_export_mode', nil) }.compact url = Rails.application.routes.url_helpers.foreman_tasks_task_url(r, host: SETTINGS[:fqdn]) @@ -51,8 +49,8 @@ namespace :template_tasks do # rubocop:disable Metrics/BlockLength namespace :import do desc 'Schedule a recurring template import' task schedule_recurring: [:environment, 'dynflow:client'] do - context = ENV['context'] - cronline = ENV['cronline'] + context = ENV.fetch('context', nil) + cronline = ENV.fetch('cronline', nil) raise 'Need to specify a cronline' unless cronline @@ -62,13 +60,13 @@ namespace :template_tasks do # rubocop:disable Metrics/BlockLength r.start Actions::ForemanTemplateTasks::TemplateImportAction, { context: context, - repo: ENV['repo'], - branch: ENV['branch'], - prefix: ENV['prefix'], - dirname: ENV['dirname'], - filter: ENV['filter'], - associate: ENV['associate'], - lock: ENV['lock'] + repo: ENV.fetch('repo', nil), + branch: ENV.fetch('branch', nil), + prefix: ENV.fetch('prefix', nil), + dirname: ENV.fetch('dirname', nil), + filter: ENV.fetch('filter', nil), + associate: ENV.fetch('associate', nil), + lock: ENV.fetch('lock', nil) }.compact url = Rails.application.routes.url_helpers.foreman_tasks_recurring_logic_url(r, host: SETTINGS[:fqdn]) @@ -80,8 +78,8 @@ namespace :template_tasks do # rubocop:disable Metrics/BlockLength namespace :export do desc 'Schedule a recurring template import' task schedule_recurring: [:environment, 'dynflow:client'] do - context = ENV['context'] - cronline = ENV['cronline'] + context = ENV.fetch('context', nil) + cronline = ENV.fetch('cronline', nil) raise 'Need to specify a cronline' unless cronline @@ -91,12 +89,12 @@ namespace :template_tasks do # rubocop:disable Metrics/BlockLength r.start Actions::ForemanTemplateTasks::TemplateExportAction, { context: context, - repo: ENV['repo'], - branch: ENV['branch'], - prefix: ENV['prefix'], - dirname: ENV['dirname'], - filter: ENV['filter'], - metadata_export_mode: ENV['metadata_export_mode'] + repo: ENV.fetch('repo', nil), + branch: ENV.fetch('branch', nil), + prefix: ENV.fetch('prefix', nil), + dirname: ENV.fetch('dirname', nil), + filter: ENV.fetch('filter', nil), + metadata_export_mode: ENV.fetch('metadata_export_mode', nil) }.compact url = Rails.application.routes.url_helpers.foreman_tasks_recurring_logic_url(r, host: SETTINGS[:fqdn]) -- GitLab