From 9a1b72b6b0f1ad4ffb199d43b06586dc38c3766d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 10 Dec 2014 17:17:09 +0900 Subject: [PATCH 01/12] Add fluentd.agent#backup_config method and call it in #config_write and #config_append to backup config histories --- app/models/fluentd/agent/common.rb | 5 +++ app/models/fluentd/agent/local_common.rb | 25 +++++++++++ .../models/fluentd/agent/local_common_spec.rb | 45 +++++++++++++++++++ 3 files changed, 75 insertions(+) diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index e1e0a9c..1962a00 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -57,6 +57,11 @@ class Fluentd extra_options[:config_file] || self.class.default_options[:config_file] end + def config_backup_dir + dir = FluentdUI.data_dir + "/#{Rails.env}_confg_backups" + FileUtils.mkdir_p(dir) + dir + end # define these methods on each Agent class diff --git a/app/models/fluentd/agent/local_common.rb b/app/models/fluentd/agent/local_common.rb index a9b7e1e..1483fe2 100644 --- a/app/models/fluentd/agent/local_common.rb +++ b/app/models/fluentd/agent/local_common.rb @@ -1,6 +1,8 @@ class Fluentd class Agent module LocalCommon + MAX_BACKUP_FILE_NUM = 100 + def running? begin pid && Process.kill(0, pid) @@ -20,12 +22,14 @@ class Fluentd end def config_write(content) + backup_config File.open(config_file, "w") do |f| f.write content end end def config_append(content) + backup_config File.open(config_file, "a") do |f| f.write "\n" f.write content @@ -61,6 +65,27 @@ class Fluentd private + def backup_config + return unless File.exists? config_file + + FileUtils.cp config_file, config_backup_dir + "/#{Time.zone.now.strftime('%Y%m%d_%H%M%S')}.conf" + + remove_over_backup_files + end + + def remove_over_backup_files + over_file_count = backup_files.size - MAX_BACKUP_FILE_NUM + + #.times do nothing if over_file_count is negative or 0. + over_file_count.times do |i| + FileUtils.rm(backup_files.shift) + end + end + + def backup_files + Dir.glob("#{config_backup_dir}/*").sort + end + def logged_errors(&block) return [] unless File.exist?(log_file) buf = [] diff --git a/spec/models/fluentd/agent/local_common_spec.rb b/spec/models/fluentd/agent/local_common_spec.rb index 582804d..2665198 100644 --- a/spec/models/fluentd/agent/local_common_spec.rb +++ b/spec/models/fluentd/agent/local_common_spec.rb @@ -2,6 +2,10 @@ require 'spec_helper' require 'fileutils' describe 'Fluentd::Agent::LocalCommon' do + let!(:now) { Time.zone.now } + before { Timecop.freeze(now) } + after { Timecop.return } + subject { target_class.new.tap{|t| t.pid_file = pid_file_path} } let!(:target_class) { Struct.new(:pid_file){ include Fluentd::Agent::LocalCommon } } @@ -26,4 +30,45 @@ describe 'Fluentd::Agent::LocalCommon' do its(:pid) { should eq(9999) } end end + + describe '#config_write', stub: :daemon do + let(:config_contents) { <<-CONF.strip_heredoc } + + type forward + port 24224 + + CONF + + let(:new_config) { <<-CONF.strip_heredoc } + + type http + port 8899 + + CONF + + before do + Fluentd::Agent::LocalCommon::MAX_BACKUP_FILE_NUM.times do |i| + backpued_time = now - (i + 1).hours + FileUtils.touch daemon.agent.config_backup_dir + "/#{backpued_time.strftime('%Y%m%d_%H%M%S')}.conf" + end + + daemon.agent.config_write config_contents #add before conf + daemon.agent.config_write new_config #update conf + end + + after do + FileUtils.rm_r daemon.agent.config_backup_dir, force: true + end + + it 'backed up old conf' do + backup_file = daemon.agent.config_backup_dir + "/#{now.strftime('%Y%m%d_%H%M%S')}.conf" + expect(File.exists? backup_file).to be_truthy + expect(File.read(backup_file)).to eq config_contents + end + + it 'keep files num up to max' do + backup_files = Dir.glob("#{daemon.agent.config_backup_dir}/*").sort + expect(backup_files.size).to eq Fluentd::Agent::LocalCommon::MAX_BACKUP_FILE_NUM + end + end end From b87b2c8af4730864c6f5948dad0240e6b4a37da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Thu, 11 Dec 2014 16:30:28 +0900 Subject: [PATCH 02/12] Add #backup_running_config method on Fluentd::Agent --- app/models/fluentd/agent/common.rb | 25 ++++++++++++++ app/models/fluentd/agent/fluentd_gem.rb | 6 +++- app/models/fluentd/agent/td_agent/macosx.rb | 4 ++- app/models/fluentd/agent/td_agent/unix.rb | 4 ++- spec/models/fluentd/agent_spec.rb | 38 +++++++++++++++++++++ 5 files changed, 74 insertions(+), 3 deletions(-) diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index 1962a00..90a1fb7 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -63,6 +63,16 @@ class Fluentd dir end + def running_config_backup_dir + dir = FluentdUI.data_dir + "/#{Rails.env}_running_confg_backup" + FileUtils.mkdir_p(dir) + dir + end + + def running_config_backup_file + running_config_backup_dir + "/running.conf" + end + # define these methods on each Agent class %w(start stop restart).each do |method| @@ -76,6 +86,21 @@ class Fluentd raise NotImplementedError, "'#{method}' method is required to be defined" end end + + + private + + def backup_running_config + #back up config file only when start success + return unless yield + + return unless File.exists? config_file + + FileUtils.cp config_file, running_config_backup_file + + true + end + end end end diff --git a/app/models/fluentd/agent/fluentd_gem.rb b/app/models/fluentd/agent/fluentd_gem.rb index a44a088..34b6047 100644 --- a/app/models/fluentd/agent/fluentd_gem.rb +++ b/app/models/fluentd/agent/fluentd_gem.rb @@ -15,7 +15,10 @@ class Fluentd # return value is status_after_this_method_called == started def start return true if running? - actual_start + + backup_running_config do + actual_start + end end # return value is status_after_this_method_called == stopped @@ -67,6 +70,7 @@ class Fluentd Bundler.with_clean_env do spawn("fluentd #{options_to_argv}") end + wait_starting end diff --git a/app/models/fluentd/agent/td_agent/macosx.rb b/app/models/fluentd/agent/td_agent/macosx.rb index 84925d2..4d12099 100644 --- a/app/models/fluentd/agent/td_agent/macosx.rb +++ b/app/models/fluentd/agent/td_agent/macosx.rb @@ -4,7 +4,9 @@ class Fluentd module Macosx def start - detached_command("launchctl load #{plist}") && pid_from_launchctl + backup_running_config do + detached_command("launchctl load #{plist}") && pid_from_launchctl + end end def stop diff --git a/app/models/fluentd/agent/td_agent/unix.rb b/app/models/fluentd/agent/td_agent/unix.rb index e0f77d4..09f6353 100644 --- a/app/models/fluentd/agent/td_agent/unix.rb +++ b/app/models/fluentd/agent/td_agent/unix.rb @@ -3,7 +3,9 @@ class Fluentd class TdAgent module Unix def start - detached_command('/etc/init.d/td-agent start') + backup_running_config do + detached_command('/etc/init.d/td-agent start') + end end def stop diff --git a/spec/models/fluentd/agent_spec.rb b/spec/models/fluentd/agent_spec.rb index 6381c22..389096f 100644 --- a/spec/models/fluentd/agent_spec.rb +++ b/spec/models/fluentd/agent_spec.rb @@ -38,8 +38,19 @@ describe Fluentd::Agent do end context "actual start success" do + after do + FileUtils.rm_r instance.running_config_backup_dir, force: true + end + let(:start_result) { true } it { should be_truthy } + + it 'backed up running conf' do + subject + backup_file = instance.running_config_backup_file + expect(File.exists? backup_file).to be_truthy + expect(File.read(backup_file)).to eq File.read(instance.config_file) + end end context "actual start failed" do @@ -86,6 +97,33 @@ describe Fluentd::Agent do let(:described_class) { Fluentd::Agent::TdAgent } # override nested described_class behavior as https://github.com/rspec/rspec-core/issues/1114 it_should_behave_like "Fluentd::Agent has common behavior" + + describe "#backup_running_config" do + before do + instance.stub(:detached_command).and_return(true) + instance.stub(:pid_from_launchctl).and_return(true) + end + + after do + FileUtils.rm_r instance.running_config_backup_dir, force: true + end + + let(:options) do + { + :config_file => Rails.root.join('tmp', 'fluentd-test', 'fluentd.conf').to_s + } + end + + before do + instance.start + end + + it 'backed up running conf' do + backup_file = instance.running_config_backup_file + expect(File.exists? backup_file).to be_truthy + expect(File.read(backup_file)).to eq File.read(instance.config_file) + end + end end end From 36bd26a37d091de0b4ec73ba04952386ce46496a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Tue, 16 Dec 2014 14:18:51 +0900 Subject: [PATCH 03/12] Add UI to reuse backup config files --- .../fluentd/settings/histories_controller.rb | 31 +++++++++++ .../fluentd/settings_controller.rb | 3 ++ app/models/fluentd/agent/common.rb | 2 - app/models/fluentd/agent/local_common.rb | 12 +++-- app/models/fluentd/setting/backup_file.rb | 40 ++++++++++++++ .../settings/histories/_list.html.haml | 6 +++ .../settings/histories/index.html.haml | 3 ++ .../fluentd/settings/histories/show.html.haml | 11 ++++ app/views/fluentd/settings/show.html.haml | 7 +++ config/locales/translation_en.yml | 10 ++++ config/locales/translation_ja.yml | 10 ++++ config/routes.rb | 4 ++ .../fluentd/setting/hisotries_spec.rb | 52 +++++++++++++++++++ spec/features/setting_spec.rb | 15 ++++++ spec/spec_helper.rb | 1 + spec/support/config_histories.rb | 40 ++++++++++++++ 16 files changed, 241 insertions(+), 6 deletions(-) create mode 100644 app/controllers/fluentd/settings/histories_controller.rb create mode 100644 app/models/fluentd/setting/backup_file.rb create mode 100644 app/views/fluentd/settings/histories/_list.html.haml create mode 100644 app/views/fluentd/settings/histories/index.html.haml create mode 100644 app/views/fluentd/settings/histories/show.html.haml create mode 100644 spec/features/fluentd/setting/hisotries_spec.rb create mode 100644 spec/support/config_histories.rb diff --git a/app/controllers/fluentd/settings/histories_controller.rb b/app/controllers/fluentd/settings/histories_controller.rb new file mode 100644 index 0000000..6704e3d --- /dev/null +++ b/app/controllers/fluentd/settings/histories_controller.rb @@ -0,0 +1,31 @@ +class Fluentd::Settings::HistoriesController < ApplicationController + before_action :login_required + before_action :find_fluentd + before_action :find_backup_file, only: [:show, :reuse] + + def index + @backup_files = @fluentd.agent.backup_files_in_new_order.map do |file_path| + Fluentd::Setting::BackupFile.new(file_path: file_path) + 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 + + private + + def find_backup_file + #Do not use BackupFile.new(params[:id]) because params[:id] can be any path. + @backup_file = Fluentd::Setting::BackupFile.find_by_file_id( + { + backup_dir: @fluentd.agent.config_backup_dir, + file_id: params[:id] + } + ) + end +end diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index 520f494..c193083 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -6,6 +6,9 @@ class Fluentd::SettingsController < ApplicationController before_action :set_config, only: [:show, :edit, :update] def show + @backup_files = @fluentd.agent.backup_files_in_new_order[0..4].map do |file_path| + Fluentd::Setting::BackupFile.new(file_path: file_path) + end end def edit diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index 90a1fb7..af66b1f 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -87,7 +87,6 @@ class Fluentd end end - private def backup_running_config @@ -100,7 +99,6 @@ class Fluentd true end - end end end diff --git a/app/models/fluentd/agent/local_common.rb b/app/models/fluentd/agent/local_common.rb index 1483fe2..02162f1 100644 --- a/app/models/fluentd/agent/local_common.rb +++ b/app/models/fluentd/agent/local_common.rb @@ -63,6 +63,14 @@ class Fluentd File.read(pid_file).to_i rescue nil end + def backup_files + Dir.glob("#{config_backup_dir}/*").sort + end + + def backup_files_in_new_order + backup_files.reverse + end + private def backup_config @@ -82,10 +90,6 @@ class Fluentd end end - def backup_files - Dir.glob("#{config_backup_dir}/*").sort - end - def logged_errors(&block) return [] unless File.exist?(log_file) buf = [] diff --git a/app/models/fluentd/setting/backup_file.rb b/app/models/fluentd/setting/backup_file.rb new file mode 100644 index 0000000..8e39906 --- /dev/null +++ b/app/models/fluentd/setting/backup_file.rb @@ -0,0 +1,40 @@ +class Fluentd + module Setting + class BackupFile + attr_accessor :file_path + + def self.find_by_file_id(backup_dir: , file_id: ) + file_path = Pathname.new(backup_dir).join("#{file_id}.conf") + raise "No suce file #{file_path}" unless File.exist?(file_path) + + new(file_path: file_path) + end + + def initialize(file_path: nil) + @file_path = file_path + end + + def file_id + @file_id ||= with_file { name.gsub(/.conf\Z/,'') } + end + + def name + @name ||= with_file { File.basename(file_path) } + end + + def content + @content ||= with_file { File.open(file_path, "r") { |f| f.read } } + end + + def ctime + with_file { File.ctime(file_path) } + end + + private + def with_file + return nil unless file_path && File.exist?(file_path) + yield + end + end + end +end diff --git a/app/views/fluentd/settings/histories/_list.html.haml b/app/views/fluentd/settings/histories/_list.html.haml new file mode 100644 index 0000000..0393b28 --- /dev/null +++ b/app/views/fluentd/settings/histories/_list.html.haml @@ -0,0 +1,6 @@ +%ul + - @backup_files.each do |file| + %li + = link_to(file.name, daemon_setting_history_path(id: file.file_id)) + %label= t('terms.backup_time') + = file.ctime.strftime(I18n.t 'time.formats.default') diff --git a/app/views/fluentd/settings/histories/index.html.haml b/app/views/fluentd/settings/histories/index.html.haml new file mode 100644 index 0000000..cd86231 --- /dev/null +++ b/app/views/fluentd/settings/histories/index.html.haml @@ -0,0 +1,3 @@ +- page_title t('.page_title', label: @fluentd.label) + += render 'list' diff --git a/app/views/fluentd/settings/histories/show.html.haml b/app/views/fluentd/settings/histories/show.html.haml new file mode 100644 index 0000000..73992af --- /dev/null +++ b/app/views/fluentd/settings/histories/show.html.haml @@ -0,0 +1,11 @@ +- page_title t('.page_title', label: @fluentd.label) do + - link_to reuse_daemon_setting_history_path(id: @backup_file.file_id), method: 'post', class: "btn btn-primary pull-right" do + = icon('fa-pencil') + = t("terms.reuse") + +.row + .col-xs-12 + %pre + = preserve do + = @backup_file.content + diff --git a/app/views/fluentd/settings/show.html.haml b/app/views/fluentd/settings/show.html.haml index 45f0481..3c194a5 100644 --- a/app/views/fluentd/settings/show.html.haml +++ b/app/views/fluentd/settings/show.html.haml @@ -8,3 +8,10 @@ %pre = preserve do = @config + +.row + .col-xs-12 + %h3= t('fluentd.settings.history') + = render '/fluentd/settings/histories/list' + .link= link_to t('.link_to_histories'), daemon_setting_histories_path + diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index c097bf1..1552fb6 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -11,6 +11,7 @@ en: password_successfully_updated: "Your password has been changed successfully." fluentd_status_running: Running fluentd_status_stopped: Stopped + config_successfully_copied: "Config has been copeid successfully. Please restart %{brand} to use the new config" terms: &terms name: Name @@ -58,6 +59,8 @@ en: notice_restart_for_config_edit: "NOTICE: running %{brand} will restart after update config" lines: Lines languages: Language + backup_time: Backed up at + reuse: reuse plugins: view_on_rubygems_org: View on rubygems.org @@ -138,6 +141,7 @@ en: show: page_title: Config File in_out_head: In/Out setting + link_to_histories: See more histories edit: page_title: Edit Config File out_forward: @@ -245,6 +249,12 @@ en: page_title: "Config Parameters" confirm: page_title: "Confirmation" + history: "Setting History" + histories: + index: + page_title: "Setting Histories" + show: + page_title: "Reuse Setting History" misc: information: diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 64dc5e1..10d1ffe 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -11,6 +11,7 @@ ja: password_successfully_updated: "パスワードを変更しました。" fluentd_status_running: 稼働中 fluentd_status_stopped: 停止中 + config_successfully_copied: "設定をコピーしました。反映させるには、 %{brand}を再起動してください。" terms: &terms name: アカウント名 @@ -58,6 +59,8 @@ ja: notice_restart_for_config_edit: "※更新すると稼働中の%{brand}が再起動されます" lines: 行 languages: 言語 + backup_time: バックアップ日時 + reuse: 再利用 plugins: view_on_rubygems_org: rubygems.orgで見る @@ -136,6 +139,7 @@ ja: setting_empty: 設定されていません show: page_title: 設定ファイルの編集 + link_to_histories: さらに過去の履歴をみる edit: page_title: 設定ファイルの編集 out_forward: @@ -250,6 +254,12 @@ ja: page_title: "ファイル読み込み | その他の設定" confirm: page_title: "ファイル読み込み | 確認" + history: 設定履歴 + histories: + index: + page_title: "設定履歴 | 一覧" + show: + page_title: "設定履歴 | 詳細" misc: information: diff --git a/config/routes.rb b/config/routes.rb index ccf0cd6..94076aa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,10 @@ Rails.application.routes.draw do resource :out_elasticsearch, only: [:show], module: :settings, controller: :out_elasticsearch do post "finish" end + + resources :histories, only: [:index, :show], module: :settings, controller: :histories do + post "reuse", action: 'reuse', on: :member, as: 'reuse' + end end end end diff --git a/spec/features/fluentd/setting/hisotries_spec.rb b/spec/features/fluentd/setting/hisotries_spec.rb new file mode 100644 index 0000000..d6913e1 --- /dev/null +++ b/spec/features/fluentd/setting/hisotries_spec.rb @@ -0,0 +1,52 @@ +require "spec_helper" + +describe "histories", stub: :daemon do + let!(:exists_user) { build(:user) } + include_context 'daemon has some config histories' + + before do + login_with exists_user + end + + describe 'index' do + before do + visit '/daemon/setting/histories' + end + + after do + #remove backups on each to avoid deppending on spec execution order + FileUtils.rm_r daemon.agent.config_backup_dir, force: true + end + + it 'show histories#index' do + page.should have_css('h1', text: I18n.t('fluentd.settings.histories.index.page_title')) + expect(all('.row li').count).to eq 10 #links to hisotries#show + end + + it 'will go to histories#show' do + all('.row li a').first.click + + page.should have_css('h1', text: I18n.t('fluentd.settings.histories.show.page_title')) + end + end + + describe 'show' do + let(:last_backup_file) { Fluentd::Setting::BackupFile.new(file_path: daemon.agent.backup_files_in_new_order.first) } + before do + visit "/daemon/setting/histories/#{last_backup_file.file_id}" + end + + it 'show histories#show' do + page.should have_css('h1', text: I18n.t('fluentd.settings.histories.show.page_title')) + page.should have_text(last_backup_file.content) + end + + it 'update config and redirect to setting#show' do + click_link I18n.t("terms.reuse") + + page.should have_css('h1', text: I18n.t('fluentd.settings.show.page_title')) + page.should have_text(I18n.t('messages.config_successfully_copied', brand: 'fluentd') ) + page.should have_text(last_backup_file.content) + end + end +end diff --git a/spec/features/setting_spec.rb b/spec/features/setting_spec.rb index 34ab0ed..8672d64 100644 --- a/spec/features/setting_spec.rb +++ b/spec/features/setting_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe 'setting', stub: :daemon do let!(:exists_user) { build(:user) } + include_context 'daemon has some config histories' before do login_with exists_user @@ -15,6 +16,20 @@ describe 'setting', stub: :daemon do page.should have_css('h1', text: I18n.t('fluentd.settings.show.page_title')) page.should have_link(I18n.t('terms.edit')) page.should have_css('pre', text: 'GREAT CONFIG HERE') + expect(all('.row li').count).to eq 5 #links to hisotries#show + page.should have_link(I18n.t('fluentd.settings.show.link_to_histories')) + end + + it 'will go to histories#index' do + click_link I18n.t('fluentd.settings.show.link_to_histories') + + page.should have_css('h1', text: I18n.t('fluentd.settings.histories.index.page_title')) + end + + it 'will go to histories#show' do + all('.row li a').first.click + + page.should have_css('h1', text: I18n.t('fluentd.settings.histories.show.page_title')) end it 'edits setting' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 9f410ab..c6dee40 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -38,6 +38,7 @@ RSpec.configure do |config| config.include LoginMacro config.include JavascriptMacro config.include StubDaemon + config.include ConfigHistories # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of diff --git a/spec/support/config_histories.rb b/spec/support/config_histories.rb new file mode 100644 index 0000000..69cd031 --- /dev/null +++ b/spec/support/config_histories.rb @@ -0,0 +1,40 @@ +module ConfigHistories + shared_context 'daemon has some config histories' do + let!(:three_hours_ago) { Time.zone.now - 3.hours } + let(:config_contents) { <<-CONF.strip_heredoc } + + type forward + port 24224 + + CONF + + let(:new_config) { <<-CONF.strip_heredoc } + + type http + port 8899 + + CONF + + before do + Timecop.freeze(three_hours_ago) + + 7.times do |i| + backpued_time = three_hours_ago - (i + 1).hours + FileUtils.touch daemon.agent.config_backup_dir + "/#{backpued_time.strftime('%Y%m%d_%H%M%S')}.conf" + end + + Timecop.freeze(three_hours_ago + 1.hour) + daemon.agent.config_write config_contents #add before conf + + Timecop.freeze(three_hours_ago + 2.hour) + daemon.agent.config_write new_config #update conf + + Timecop.freeze(three_hours_ago + 3.hour) + end + + after do + FileUtils.rm_r daemon.agent.config_backup_dir, force: true + Timecop.return + end + end +end From 366fc6364dd419c74df5b3ac44f82819207c83ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Tue, 16 Dec 2014 18:17:08 +0900 Subject: [PATCH 04/12] Fix spec to ensure config_file --- spec/models/fluentd/agent_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/models/fluentd/agent_spec.rb b/spec/models/fluentd/agent_spec.rb index 389096f..c5fe633 100644 --- a/spec/models/fluentd/agent_spec.rb +++ b/spec/models/fluentd/agent_spec.rb @@ -2,7 +2,11 @@ require 'spec_helper' describe Fluentd::Agent do let(:instance) { described_class.new(options) } - let(:options) { {} } + let(:options) { + { + :config_file => Rails.root.join('tmp', 'fluentd-test', 'fluentd.conf').to_s + } + } describe "FluentdGem" do let(:described_class) { Fluentd::Agent::FluentdGem } # override nested described_class behavior as https://github.com/rspec/rspec-core/issues/1114 From 7c797d42b0761dd89ba9110072537ad606af0d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Tue, 16 Dec 2014 18:17:57 +0900 Subject: [PATCH 05/12] Fix spec to avoid depending on spec execution order --- spec/features/fluentd/setting/hisotries_spec.rb | 7 +------ spec/support/config_histories.rb | 3 +++ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/spec/features/fluentd/setting/hisotries_spec.rb b/spec/features/fluentd/setting/hisotries_spec.rb index d6913e1..d2faa32 100644 --- a/spec/features/fluentd/setting/hisotries_spec.rb +++ b/spec/features/fluentd/setting/hisotries_spec.rb @@ -13,14 +13,9 @@ describe "histories", stub: :daemon do visit '/daemon/setting/histories' end - after do - #remove backups on each to avoid deppending on spec execution order - FileUtils.rm_r daemon.agent.config_backup_dir, force: true - end - it 'show histories#index' do page.should have_css('h1', text: I18n.t('fluentd.settings.histories.index.page_title')) - expect(all('.row li').count).to eq 10 #links to hisotries#show + expect(all('.row li').count).to eq 9 #links to hisotries#show end it 'will go to histories#show' do diff --git a/spec/support/config_histories.rb b/spec/support/config_histories.rb index 69cd031..ff78f60 100644 --- a/spec/support/config_histories.rb +++ b/spec/support/config_histories.rb @@ -18,6 +18,9 @@ module ConfigHistories before do Timecop.freeze(three_hours_ago) + #remove backups on each to avoid depending on spec execution order + FileUtils.rm_r daemon.agent.config_backup_dir, force: true + 7.times do |i| backpued_time = three_hours_ago - (i + 1).hours FileUtils.touch daemon.agent.config_backup_dir + "/#{backpued_time.strftime('%Y%m%d_%H%M%S')}.conf" From 0786fafb9b98f703fc5f7eb8089aa54ed698c00f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Tue, 16 Dec 2014 19:03:06 +0900 Subject: [PATCH 06/12] Remove keyword argument from methods for Ruby 1.9 --- app/controllers/fluentd/settings/histories_controller.rb | 9 ++------- app/controllers/fluentd/settings_controller.rb | 2 +- app/models/fluentd/setting/backup_file.rb | 7 ++++--- spec/features/fluentd/setting/hisotries_spec.rb | 3 ++- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/controllers/fluentd/settings/histories_controller.rb b/app/controllers/fluentd/settings/histories_controller.rb index 6704e3d..e73a18f 100644 --- a/app/controllers/fluentd/settings/histories_controller.rb +++ b/app/controllers/fluentd/settings/histories_controller.rb @@ -5,7 +5,7 @@ class Fluentd::Settings::HistoriesController < ApplicationController def index @backup_files = @fluentd.agent.backup_files_in_new_order.map do |file_path| - Fluentd::Setting::BackupFile.new(file_path: file_path) + Fluentd::Setting::BackupFile.new(file_path) end end @@ -21,11 +21,6 @@ class Fluentd::Settings::HistoriesController < ApplicationController def find_backup_file #Do not use BackupFile.new(params[:id]) because params[:id] can be any path. - @backup_file = Fluentd::Setting::BackupFile.find_by_file_id( - { - backup_dir: @fluentd.agent.config_backup_dir, - file_id: params[:id] - } - ) + @backup_file = Fluentd::Setting::BackupFile.find_by_file_id(@fluentd.agent.config_backup_dir, params[:id]) end end diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index c193083..27776d5 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -7,7 +7,7 @@ class Fluentd::SettingsController < ApplicationController def show @backup_files = @fluentd.agent.backup_files_in_new_order[0..4].map do |file_path| - Fluentd::Setting::BackupFile.new(file_path: file_path) + Fluentd::Setting::BackupFile.new(file_path) end end diff --git a/app/models/fluentd/setting/backup_file.rb b/app/models/fluentd/setting/backup_file.rb index 8e39906..4005aa3 100644 --- a/app/models/fluentd/setting/backup_file.rb +++ b/app/models/fluentd/setting/backup_file.rb @@ -3,14 +3,14 @@ class Fluentd class BackupFile attr_accessor :file_path - def self.find_by_file_id(backup_dir: , file_id: ) + def self.find_by_file_id(backup_dir, file_id) file_path = Pathname.new(backup_dir).join("#{file_id}.conf") raise "No suce file #{file_path}" unless File.exist?(file_path) - new(file_path: file_path) + new(file_path) end - def initialize(file_path: nil) + def initialize(file_path) @file_path = file_path end @@ -31,6 +31,7 @@ class Fluentd end private + def with_file return nil unless file_path && File.exist?(file_path) yield diff --git a/spec/features/fluentd/setting/hisotries_spec.rb b/spec/features/fluentd/setting/hisotries_spec.rb index d2faa32..9ae1db7 100644 --- a/spec/features/fluentd/setting/hisotries_spec.rb +++ b/spec/features/fluentd/setting/hisotries_spec.rb @@ -26,7 +26,8 @@ describe "histories", stub: :daemon do end describe 'show' do - let(:last_backup_file) { Fluentd::Setting::BackupFile.new(file_path: daemon.agent.backup_files_in_new_order.first) } + let(:last_backup_file) { Fluentd::Setting::BackupFile.new(daemon.agent.backup_files_in_new_order.first) } + before do visit "/daemon/setting/histories/#{last_backup_file.file_id}" end From 7ec6c1b9fdd552f60880e0726c619fcda9dd8eee Mon Sep 17 00:00:00 2001 From: uu59 Date: Wed, 17 Dec 2014 13:21:58 +0900 Subject: [PATCH 07/12] Fix to write config file via Fleuntd::Agent --- app/models/fluentd/setting/config.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/fluentd/setting/config.rb b/app/models/fluentd/setting/config.rb index 45d0fed..09a7678 100644 --- a/app/models/fluentd/setting/config.rb +++ b/app/models/fluentd/setting/config.rb @@ -28,7 +28,8 @@ class Fluentd end def write_to_file - File.open(file, "w"){|f| f.write formatted } + return unless Fluentd.instance + Fluentd.instance.agent.config_write formatted end def formatted From 560035b185f12ab340b24ad2b3d892811e04cc0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 17 Dec 2014 14:07:35 +0900 Subject: [PATCH 08/12] Add UI for running bacukp config --- .../settings/running_backup_controller.rb | 19 ++++++++ .../fluentd/settings_controller.rb | 2 + app/models/fluentd/setting/backup_file.rb | 2 +- .../settings/running_backup/show.html.haml | 15 ++++++ app/views/fluentd/settings/show.html.haml | 8 +++- config/locales/translation_en.yml | 7 ++- config/locales/translation_ja.yml | 5 ++ config/routes.rb | 4 ++ .../fluentd/setting/ranning_backup_spec.rb | 48 +++++++++++++++++++ spec/features/setting_spec.rb | 2 + spec/support/config_histories.rb | 14 ++++++ 11 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 app/controllers/fluentd/settings/running_backup_controller.rb create mode 100644 app/views/fluentd/settings/running_backup/show.html.haml create mode 100644 spec/features/fluentd/setting/ranning_backup_spec.rb diff --git a/app/controllers/fluentd/settings/running_backup_controller.rb b/app/controllers/fluentd/settings/running_backup_controller.rb new file mode 100644 index 0000000..2cb4096 --- /dev/null +++ b/app/controllers/fluentd/settings/running_backup_controller.rb @@ -0,0 +1,19 @@ +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 + + private + + def find_backup_file + @backup_file = Fluentd::Setting::BackupFile.new(@fluentd.agent.running_config_backup_file) + end +end diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index 27776d5..c45618f 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -9,6 +9,8 @@ class Fluentd::SettingsController < ApplicationController @backup_files = @fluentd.agent.backup_files_in_new_order[0..4].map do |file_path| Fluentd::Setting::BackupFile.new(file_path) end + + @running_backedup_file = Fluentd::Setting::BackupFile.new(@fluentd.agent.running_config_backup_file) end def edit diff --git a/app/models/fluentd/setting/backup_file.rb b/app/models/fluentd/setting/backup_file.rb index 4005aa3..939182b 100644 --- a/app/models/fluentd/setting/backup_file.rb +++ b/app/models/fluentd/setting/backup_file.rb @@ -5,7 +5,7 @@ class Fluentd def self.find_by_file_id(backup_dir, file_id) file_path = Pathname.new(backup_dir).join("#{file_id}.conf") - raise "No suce file #{file_path}" unless File.exist?(file_path) + raise "No such a file #{file_path}" unless File.exist?(file_path) new(file_path) end diff --git a/app/views/fluentd/settings/running_backup/show.html.haml b/app/views/fluentd/settings/running_backup/show.html.haml new file mode 100644 index 0000000..7c3de25 --- /dev/null +++ b/app/views/fluentd/settings/running_backup/show.html.haml @@ -0,0 +1,15 @@ +- 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 + = icon('fa-pencil') + = t("terms.reuse") + +.row + .col-xs-12 + - if @backup_file.content + %pre + = preserve do + = @backup_file.content + - else + %p + =t('fluentd.common.never_started_yet', brand: fluentd_ui_brand) diff --git a/app/views/fluentd/settings/show.html.haml b/app/views/fluentd/settings/show.html.haml index 3c194a5..545c84f 100644 --- a/app/views/fluentd/settings/show.html.haml +++ b/app/views/fluentd/settings/show.html.haml @@ -9,9 +9,15 @@ = preserve do = @config +.row + .col-xs-12 + %h3= link_to(t('fluentd.settings.running_backup.title'), daemon_setting_running_backup_path) + %p + %label= t('terms.backup_time') + = @running_backedup_file.ctime.try(:strftime, I18n.t('time.formats.default')) || t('fluentd.common.never_started_yet', brand: fluentd_ui_brand) + .row .col-xs-12 %h3= t('fluentd.settings.history') = render '/fluentd/settings/histories/list' .link= link_to t('.link_to_histories'), daemon_setting_histories_path - diff --git a/config/locales/translation_en.yml b/config/locales/translation_en.yml index 1552fb6..e29beaf 100644 --- a/config/locales/translation_en.yml +++ b/config/locales/translation_en.yml @@ -11,7 +11,7 @@ en: password_successfully_updated: "Your password has been changed successfully." fluentd_status_running: Running fluentd_status_stopped: Stopped - config_successfully_copied: "Config has been copeid successfully. Please restart %{brand} to use the new config" + config_successfully_copied: "Config has been copied successfully. Please restart %{brand} to use the new config" terms: &terms name: Name @@ -120,6 +120,7 @@ en: Delete %{brand} setting.

Running %{brand} will be stopped, but log and config file are still exists.

+ never_started_yet: "%{brand} had never been started yet." show: page_title: Dashboard new: @@ -255,6 +256,10 @@ en: page_title: "Setting Histories" show: page_title: "Reuse Setting History" + running_backup: + title: "Running Config" + show: + page_title: "Running Config" misc: information: diff --git a/config/locales/translation_ja.yml b/config/locales/translation_ja.yml index 10d1ffe..3e3d63d 100644 --- a/config/locales/translation_ja.yml +++ b/config/locales/translation_ja.yml @@ -119,6 +119,7 @@ ja: %{brand}の設定を削除します。

起動中の%{brand}は停止し、ログや設定ファイルはそのまま残存します。

+ never_started_yet: "%{brand} は起動されたことがありません。" show: page_title: "ダッシュボード" new: @@ -260,6 +261,10 @@ ja: page_title: "設定履歴 | 一覧" show: page_title: "設定履歴 | 詳細" + running_backup: + title: "使用中の設定" + show: + page_title: "使用中の設定" misc: information: diff --git a/config/routes.rb b/config/routes.rb index 94076aa..1f86f7a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -67,6 +67,10 @@ Rails.application.routes.draw do resources :histories, only: [:index, :show], module: :settings, controller: :histories do post "reuse", action: 'reuse', on: :member, as: 'reuse' end + + resource :running_backup, only: [:show], module: :settings, controller: :running_backup do + post "reuse", action: 'reuse', on: :member, as: 'reuse' + end end end end diff --git a/spec/features/fluentd/setting/ranning_backup_spec.rb b/spec/features/fluentd/setting/ranning_backup_spec.rb new file mode 100644 index 0000000..11da20d --- /dev/null +++ b/spec/features/fluentd/setting/ranning_backup_spec.rb @@ -0,0 +1,48 @@ +require "spec_helper" + +describe "running_backup", stub: :daemon do + let!(:exists_user) { build(:user) } + include_context 'daemon has some config histories' + + before do + login_with exists_user + end + + context 'has running backup file' do + before do + visit '/daemon/setting/running_backup' + end + + describe 'show' do + it 'has no content, no reuse bottun' do + expect(page).to have_text(I18n.t('fluentd.common.never_started_yet', brand: 'fluentd')) + expect(page).not_to have_css('pre') + expect(page).not_to have_text(I18n.t("terms.reuse")) + end + end + end + + context 'has running backup file' do + include_context 'daemon had been started once' + + before do + visit '/daemon/setting/running_backup' + end + + describe 'show' do + it 'has content, reuse bottun' do + expect(page).not_to have_text(I18n.t('fluentd.common.never_started_yet', brand: 'fluentd')) + expect(page).to have_text(backup_content) + expect(page).to have_text(I18n.t("terms.reuse")) + end + + it 'update config and redirect to setting#show' do + click_link I18n.t("terms.reuse") + + page.should have_css('h1', text: I18n.t('fluentd.settings.show.page_title')) + page.should have_text(I18n.t('messages.config_successfully_copied', brand: 'fluentd') ) + page.should have_text(backup_content) + end + end + end +end diff --git a/spec/features/setting_spec.rb b/spec/features/setting_spec.rb index 8672d64..c1b5b8a 100644 --- a/spec/features/setting_spec.rb +++ b/spec/features/setting_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe 'setting', stub: :daemon do let!(:exists_user) { build(:user) } include_context 'daemon has some config histories' + include_context 'daemon had been started once' before do login_with exists_user @@ -18,6 +19,7 @@ describe 'setting', stub: :daemon do page.should have_css('pre', text: 'GREAT CONFIG HERE') expect(all('.row li').count).to eq 5 #links to hisotries#show page.should have_link(I18n.t('fluentd.settings.show.link_to_histories')) + page.should have_text(I18n.t('fluentd.settings.running_backup.title')) end it 'will go to histories#index' do diff --git a/spec/support/config_histories.rb b/spec/support/config_histories.rb index ff78f60..a41581d 100644 --- a/spec/support/config_histories.rb +++ b/spec/support/config_histories.rb @@ -40,4 +40,18 @@ module ConfigHistories Timecop.return end end + + shared_context 'daemon had been started once' do + let!(:backup_content){ "Running backup file content" } + + before do + File.open(daemon.agent.running_config_backup_file, "w") do |file| + file.write(backup_content) + end + end + + after do + FileUtils.rm_r daemon.agent.running_config_backup_dir, force: true + end + end end From a61ba6f506142ec7014ebdec337d2819f48b52a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 17 Dec 2014 17:08:30 +0900 Subject: [PATCH 09/12] Change to use array.first(5) instead of array[0..4] --- app/controllers/fluentd/settings_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index c45618f..3fad700 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -6,7 +6,7 @@ class Fluentd::SettingsController < ApplicationController before_action :set_config, only: [:show, :edit, :update] def show - @backup_files = @fluentd.agent.backup_files_in_new_order[0..4].map do |file_path| + @backup_files = @fluentd.agent.backup_files_in_new_order.first(5).map do |file_path| Fluentd::Setting::BackupFile.new(file_path) end From c4eb06f5ba9447a7731cc41a6b2e56da70c86300 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 17 Dec 2014 18:09:04 +0900 Subject: [PATCH 10/12] Chamge method name #backup_files -> #backup_files_in_old_order to make its sort function clearer, and Refactor #remove_over_backup_files by using it --- app/models/fluentd/agent/local_common.rb | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/models/fluentd/agent/local_common.rb b/app/models/fluentd/agent/local_common.rb index 02162f1..a4b6cbd 100644 --- a/app/models/fluentd/agent/local_common.rb +++ b/app/models/fluentd/agent/local_common.rb @@ -64,11 +64,15 @@ class Fluentd end def backup_files - Dir.glob("#{config_backup_dir}/*").sort + Dir.glob("#{config_backup_dir}/*") + end + + def backup_files_in_old_order + backup_files.sort end def backup_files_in_new_order - backup_files.reverse + backup_files_in_old_order.reverse end private @@ -84,9 +88,11 @@ class Fluentd def remove_over_backup_files over_file_count = backup_files.size - MAX_BACKUP_FILE_NUM - #.times do nothing if over_file_count is negative or 0. - over_file_count.times do |i| - FileUtils.rm(backup_files.shift) + return if over_file_count <= 0 + + backup_files_in_old_order.first(over_file_count).each do |file| + next unless File.exist? file + FileUtils.rm(file) end end From 7b65e03cc82e06cce3ad34f0fe791cab13011cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 17 Dec 2014 18:19:01 +0900 Subject: [PATCH 11/12] Change to use File.join instead of string concatenation because File.join makes the purpose of code clear and do it safely --- app/models/fluentd/agent/common.rb | 6 +++--- app/models/fluentd/agent/local_common.rb | 4 ++-- spec/models/fluentd/agent/local_common_spec.rb | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/fluentd/agent/common.rb b/app/models/fluentd/agent/common.rb index af66b1f..1b2609a 100644 --- a/app/models/fluentd/agent/common.rb +++ b/app/models/fluentd/agent/common.rb @@ -58,19 +58,19 @@ class Fluentd end def config_backup_dir - dir = FluentdUI.data_dir + "/#{Rails.env}_confg_backups" + dir = File.join(FluentdUI.data_dir, "#{Rails.env}_confg_backups") FileUtils.mkdir_p(dir) dir end def running_config_backup_dir - dir = FluentdUI.data_dir + "/#{Rails.env}_running_confg_backup" + dir = File.join(FluentdUI.data_dir, "#{Rails.env}_running_confg_backup") FileUtils.mkdir_p(dir) dir end def running_config_backup_file - running_config_backup_dir + "/running.conf" + File.join(running_config_backup_dir, "running.conf") end # define these methods on each Agent class diff --git a/app/models/fluentd/agent/local_common.rb b/app/models/fluentd/agent/local_common.rb index a4b6cbd..6157e04 100644 --- a/app/models/fluentd/agent/local_common.rb +++ b/app/models/fluentd/agent/local_common.rb @@ -64,7 +64,7 @@ class Fluentd end def backup_files - Dir.glob("#{config_backup_dir}/*") + Dir.glob(File.join("#{config_backup_dir}", "*.conf")) end def backup_files_in_old_order @@ -80,7 +80,7 @@ class Fluentd def backup_config return unless File.exists? config_file - FileUtils.cp config_file, config_backup_dir + "/#{Time.zone.now.strftime('%Y%m%d_%H%M%S')}.conf" + FileUtils.cp config_file, File.join(config_backup_dir, "#{Time.zone.now.strftime('%Y%m%d_%H%M%S')}.conf") remove_over_backup_files end diff --git a/spec/models/fluentd/agent/local_common_spec.rb b/spec/models/fluentd/agent/local_common_spec.rb index 2665198..d6a0990 100644 --- a/spec/models/fluentd/agent/local_common_spec.rb +++ b/spec/models/fluentd/agent/local_common_spec.rb @@ -49,7 +49,7 @@ describe 'Fluentd::Agent::LocalCommon' do before do Fluentd::Agent::LocalCommon::MAX_BACKUP_FILE_NUM.times do |i| backpued_time = now - (i + 1).hours - FileUtils.touch daemon.agent.config_backup_dir + "/#{backpued_time.strftime('%Y%m%d_%H%M%S')}.conf" + FileUtils.touch File.join(daemon.agent.config_backup_dir , "#{backpued_time.strftime('%Y%m%d_%H%M%S')}.conf") end daemon.agent.config_write config_contents #add before conf @@ -61,7 +61,7 @@ describe 'Fluentd::Agent::LocalCommon' do end it 'backed up old conf' do - backup_file = daemon.agent.config_backup_dir + "/#{now.strftime('%Y%m%d_%H%M%S')}.conf" + backup_file = File.join(daemon.agent.config_backup_dir, "#{now.strftime('%Y%m%d_%H%M%S')}.conf") expect(File.exists? backup_file).to be_truthy expect(File.read(backup_file)).to eq config_contents end From 61058867057e84090d592577070d7523fb3f0815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B3=A5=E4=BA=95=20=E9=9B=AA?= Date: Wed, 17 Dec 2014 18:44:17 +0900 Subject: [PATCH 12/12] Change to use application.yml setting instead of hard coded number --- app/controllers/fluentd/settings_controller.rb | 2 +- app/models/fluentd/agent/local_common.rb | 4 +--- config/application.yml | 2 ++ spec/features/setting_spec.rb | 2 +- spec/models/fluentd/agent/local_common_spec.rb | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/fluentd/settings_controller.rb b/app/controllers/fluentd/settings_controller.rb index 3fad700..ec9d5a1 100644 --- a/app/controllers/fluentd/settings_controller.rb +++ b/app/controllers/fluentd/settings_controller.rb @@ -6,7 +6,7 @@ class Fluentd::SettingsController < ApplicationController before_action :set_config, only: [:show, :edit, :update] def show - @backup_files = @fluentd.agent.backup_files_in_new_order.first(5).map do |file_path| + @backup_files = @fluentd.agent.backup_files_in_new_order.first(Settings.histories_count_in_preview).map do |file_path| Fluentd::Setting::BackupFile.new(file_path) end diff --git a/app/models/fluentd/agent/local_common.rb b/app/models/fluentd/agent/local_common.rb index 6157e04..14823e4 100644 --- a/app/models/fluentd/agent/local_common.rb +++ b/app/models/fluentd/agent/local_common.rb @@ -1,8 +1,6 @@ class Fluentd class Agent module LocalCommon - MAX_BACKUP_FILE_NUM = 100 - def running? begin pid && Process.kill(0, pid) @@ -86,7 +84,7 @@ class Fluentd end def remove_over_backup_files - over_file_count = backup_files.size - MAX_BACKUP_FILE_NUM + over_file_count = backup_files.size - ::Settings.max_backup_files_count return if over_file_count <= 0 diff --git a/config/application.yml b/config/application.yml index 423a228..ee5fc90 100644 --- a/config/application.yml +++ b/config/application.yml @@ -1,6 +1,8 @@ defaults: &defaults default_password: changeme default_log_tail_count: 30 + histories_count_in_preview: 5 + max_backup_files_count: 100 recommended_plugins: - category: filter name: "rewrite-tag-filter" diff --git a/spec/features/setting_spec.rb b/spec/features/setting_spec.rb index c1b5b8a..0f557af 100644 --- a/spec/features/setting_spec.rb +++ b/spec/features/setting_spec.rb @@ -17,7 +17,7 @@ describe 'setting', stub: :daemon do page.should have_css('h1', text: I18n.t('fluentd.settings.show.page_title')) page.should have_link(I18n.t('terms.edit')) page.should have_css('pre', text: 'GREAT CONFIG HERE') - expect(all('.row li').count).to eq 5 #links to hisotries#show + expect(all('.row li').count).to eq Settings.histories_count_in_preview #links to hisotries#show page.should have_link(I18n.t('fluentd.settings.show.link_to_histories')) page.should have_text(I18n.t('fluentd.settings.running_backup.title')) end diff --git a/spec/models/fluentd/agent/local_common_spec.rb b/spec/models/fluentd/agent/local_common_spec.rb index d6a0990..8b9db1c 100644 --- a/spec/models/fluentd/agent/local_common_spec.rb +++ b/spec/models/fluentd/agent/local_common_spec.rb @@ -47,7 +47,7 @@ describe 'Fluentd::Agent::LocalCommon' do CONF before do - Fluentd::Agent::LocalCommon::MAX_BACKUP_FILE_NUM.times do |i| + ::Settings.max_backup_files_count.times do |i| backpued_time = now - (i + 1).hours FileUtils.touch File.join(daemon.agent.config_backup_dir , "#{backpued_time.strftime('%Y%m%d_%H%M%S')}.conf") end @@ -68,7 +68,7 @@ describe 'Fluentd::Agent::LocalCommon' do it 'keep files num up to max' do backup_files = Dir.glob("#{daemon.agent.config_backup_dir}/*").sort - expect(backup_files.size).to eq Fluentd::Agent::LocalCommon::MAX_BACKUP_FILE_NUM + expect(backup_files.size).to eq ::Settings.max_backup_files_count end end end