From 3071b3d2c48abf02110e982bc9be6e17385e67d9 Mon Sep 17 00:00:00 2001 From: uu59 Date: Wed, 14 Jan 2015 17:55:30 +0900 Subject: [PATCH] Add config test button to running backup --- .../concerns/setting_history_concern.rb | 28 +++++++++++++++ .../fluentd/settings/histories_controller.rb | 26 +++----------- .../settings/running_backup_controller.rb | 16 +++------ .../settings/running_backup/show.html.haml | 11 ++++-- config/routes.rb | 1 + .../fluentd/setting/ranning_backup_spec.rb | 36 +++++++++++++++++++ 6 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 app/controllers/concerns/setting_history_concern.rb diff --git a/app/controllers/concerns/setting_history_concern.rb b/app/controllers/concerns/setting_history_concern.rb new file mode 100644 index 0000000..deabda1 --- /dev/null +++ b/app/controllers/concerns/setting_history_concern.rb @@ -0,0 +1,28 @@ +module SettingHistoryConcern + extend ActiveSupport::Concern + + included do + before_action :login_required + before_action :find_fluentd + before_action :find_backup_file, only: [:show, :reuse, :configtest] + end + + def show + end + + def reuse + @fluentd.agent.config_write @backup_file.content + redirect_to daemon_setting_path, flash: { success: t('messages.config_successfully_copied', brand: fluentd_ui_brand) } + end + + def configtest + @fluentd.config_file = @backup_file.file_path + if @fluentd.agent.dryrun + flash = { success: t('messages.dryrun_is_passed') } + else + flash = { danger: @fluentd.agent.log_tail(1).first } + end + after_dryrun_redirect(flash) + end + +end diff --git a/app/controllers/fluentd/settings/histories_controller.rb b/app/controllers/fluentd/settings/histories_controller.rb index a02a622..5cac72a 100644 --- a/app/controllers/fluentd/settings/histories_controller.rb +++ b/app/controllers/fluentd/settings/histories_controller.rb @@ -1,7 +1,5 @@ class Fluentd::Settings::HistoriesController < ApplicationController - before_action :login_required - before_action :find_fluentd - before_action :find_backup_file, only: [:show, :reuse, :configtest] + include SettingHistoryConcern def index @backup_files = @fluentd.agent.backup_files_in_new_order.map do |file_path| @@ -9,28 +7,14 @@ class Fluentd::Settings::HistoriesController < ApplicationController end end - def show - end - - def reuse - @fluentd.agent.config_write @backup_file.content - redirect_to daemon_setting_path, flash: { success: t('messages.config_successfully_copied', brand: fluentd_ui_brand) } - end - - def configtest - @fluentd.config_file = @backup_file.file_path - if @fluentd.agent.dryrun - flash = { success: t('messages.dryrun_is_passed') } - else - flash = { danger: @fluentd.agent.log_tail(1).first } - end - redirect_to daemon_setting_history_path(params[:id]), flash: flash - end - private def find_backup_file #Do not use BackupFile.new(params[:id]) because params[:id] can be any path. @backup_file = Fluentd::SettingArchive::BackupFile.find_by_file_id(@fluentd.agent.config_backup_dir, params[:id]) end + + def after_dryrun_redirect(flash) + redirect_to daemon_setting_history_path(params[:id]), flash: flash + end end diff --git a/app/controllers/fluentd/settings/running_backup_controller.rb b/app/controllers/fluentd/settings/running_backup_controller.rb index 7fe33db..9a5c1a6 100644 --- a/app/controllers/fluentd/settings/running_backup_controller.rb +++ b/app/controllers/fluentd/settings/running_backup_controller.rb @@ -1,19 +1,13 @@ class Fluentd::Settings::RunningBackupController < ApplicationController - before_action :login_required - before_action :find_fluentd - before_action :find_backup_file, only: [:show, :reuse] - - def show - end - - def reuse - @fluentd.agent.config_write @backup_file.content - redirect_to daemon_setting_path, flash: { success: t('messages.config_successfully_copied', brand: fluentd_ui_brand) } - end + include SettingHistoryConcern private def find_backup_file @backup_file = Fluentd::SettingArchive::BackupFile.new(@fluentd.agent.running_config_backup_file) end + + def after_dryrun_redirect(flash) + redirect_to daemon_setting_running_backup_path, flash: flash + end end diff --git a/app/views/fluentd/settings/running_backup/show.html.haml b/app/views/fluentd/settings/running_backup/show.html.haml index 7c3de25..77e6770 100644 --- a/app/views/fluentd/settings/running_backup/show.html.haml +++ b/app/views/fluentd/settings/running_backup/show.html.haml @@ -1,6 +1,11 @@ -- page_title t('.page_title', label: @fluentd.label) do - - if @backup_file.content - - link_to reuse_daemon_setting_running_backup_path, method: 'post', class: "btn btn-primary pull-right" do +- page_title t('.page_title', label: @fluentd.label) + +- if @backup_file.content + %p{class: "pull-right"} + = link_to configtest_daemon_setting_running_backup_path, method: 'post', class: "btn btn-default" do + = icon('fa-legal') + = t("terms.configtest") + = link_to reuse_daemon_setting_running_backup_path, method: 'post', class: "btn btn-primary" do = icon('fa-pencil') = t("terms.reuse") diff --git a/config/routes.rb b/config/routes.rb index f54dbd4..90d43dc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -73,6 +73,7 @@ Rails.application.routes.draw do resource :running_backup, only: [:show], module: :settings, controller: :running_backup do post "reuse", action: 'reuse', on: :member, as: 'reuse' + post "configtest" , action: "configtest", on: :member, as: "configtest" end end end diff --git a/spec/features/fluentd/setting/ranning_backup_spec.rb b/spec/features/fluentd/setting/ranning_backup_spec.rb index 11da20d..2219c3d 100644 --- a/spec/features/fluentd/setting/ranning_backup_spec.rb +++ b/spec/features/fluentd/setting/ranning_backup_spec.rb @@ -43,6 +43,42 @@ describe "running_backup", stub: :daemon do page.should have_text(I18n.t('messages.config_successfully_copied', brand: 'fluentd') ) page.should have_text(backup_content) end + + describe "configtest" do + let(:backup_content){ config } + let(:daemon) { build(:fluentd, variant: "fluentd_gem") } # To use fluentd_gem for real dry-run checking + before do + click_link I18n.t("terms.configtest") + end + + context "invalid configfile" do + let(:config) { <<-CONFIG } + + type aaaaaaaaaaaa + + CONFIG + + it do + page.should_not have_css('.alert-success') + page.should have_css('.alert-danger') + page.should have_text(%Q|Unknown input plugin 'aaaaaaaaaaaa'|) + end + end + + context "valid configfile" do + let(:config) { <<-CONFIG } + + type syslog + tag syslog + + CONFIG + + it do + page.should have_css('.alert-success') + page.should_not have_css('.alert-danger') + end + end + end end end end