rbatch 1.8.2 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -41,3 +41,7 @@ initial
41
41
  * add log_send_mail option
42
42
  * add cmd_timeout option
43
43
 
44
+ 1.9.0(2013/02/11)
45
+ ----
46
+ * add "<host>" keyword to log_name option
47
+ * Environmental variables ($TMPDIR and $HOSTNAME) are checked strictly
data/README.ja.md CHANGED
@@ -16,7 +16,7 @@ RBatchについて
16
16
  * ファイル名・ディレクトリ構造制約
17
17
  * 二重起動チェック
18
18
 
19
- このフレームワークはRuby 1.9.x以降のみで動作します。
19
+ このフレームワークはRuby 1.9.x以降で動作します。また、Rubyプラットフォームは"linux","mswin","mingw","cygwin"で動作します。
20
20
 
21
21
  ### 自動ログ出力
22
22
  Logging blockを使うことで、自動的にログファイルに出力することができます。
@@ -223,6 +223,7 @@ $ cat log/YYYYMMDD_HHMMSS_backup.log
223
223
  # <data> --> YYYYMMDDの日付形式に置換されます
224
224
  # <time> --> hhmmssの時刻形式に置換されます
225
225
  # <prog> --> 拡張子を除いたファイル名に置換されます
226
+ # <host> --> ホスト名に置換されます
226
227
  #
227
228
  #log_name : "<date>_<time>_<prog>.log"
228
229
  #log_name : "<date>_<prog>.log"
data/README.md CHANGED
@@ -15,7 +15,7 @@ There are following functions.
15
15
  * Directory Structure convention
16
16
  * Double Run Check
17
17
 
18
- This work on only Ruby 1.9.x or more later.
18
+ This work on Ruby 1.9.x or later, and on Ruby platform of "linux","mswin","mingw","cygwin".
19
19
 
20
20
  ### Auto Logging
21
21
  Use Auto Logging block, RBatch automatically write to logfile.
@@ -218,9 +218,10 @@ Config Sample
218
218
  #
219
219
  # Default is "<date>_<time>_<prog>.log".
220
220
  # Reservation words are follows.
221
- # <data> --> replace to YYYYMMDD date string
222
- # <time> --> replace to hhmmss time string
223
- # <prog> --> Program file base name (except extention)
221
+ # <data> --> Replace to YYYYMMDD date string
222
+ # <time> --> Replace to HHMMSS time string
223
+ # <prog> --> Replace to Program file base name (except extention).
224
+ # <host> --> Replace to Hostname.
224
225
  #
225
226
  #log_name : "<date>_<time>_<prog>.log"
226
227
  #log_name : "<date>_<prog>.log"
data/lib/rbatch/log.rb CHANGED
@@ -80,7 +80,7 @@ module RBatch
80
80
  #
81
81
  # ==== Params
82
82
  # +opt+ = Option hash object. Hash keys is follows.
83
- # - +:name+ (String) = log file name. Default is "<date>_<time>_<prog>.log"
83
+ # - +:name+ (String) = log file name. Default is "<date>_<time>_<prog>.log".Reservation words are "<date>","<time>","<prog>","<host>".
84
84
  # - +:dir+ (String) = log direcotry path. Default is "../log"
85
85
  # - +:level+ (String) = log level. ["debug"|"info"|"warn"|"error"|"fatal"] . Default is "info".
86
86
  # - +:append+ (Boolean) = appned to log or not(=overwrite). Default is ture.
@@ -115,6 +115,7 @@ module RBatch
115
115
  @file_name.gsub!("<date>", Time.now.strftime("%Y%m%d"))
116
116
  @file_name.gsub!("<time>", Time.now.strftime("%H%M%S"))
117
117
  @file_name.gsub!("<prog>", @prog_base)
118
+ @file_name.gsub!("<host>", RBatch::hostname)
118
119
  path = File.join(@opt[:dir],@file_name)
119
120
  # create Logger instance
120
121
  begin
@@ -1,3 +1,3 @@
1
1
  module Rbatch
2
- VERSION = "1.8.2"
2
+ VERSION = "1.9.0"
3
3
  end
data/lib/rbatch.rb CHANGED
@@ -5,12 +5,28 @@ module RBatch
5
5
  module_function
6
6
  def program_name=(f) ; @@program_name = f ; end
7
7
  def program_name ; @@program_name ; end
8
+ # Hostname
9
+ def hostname
10
+ case RUBY_PLATFORM
11
+ when /mswin|mingw/
12
+ return ENV["COMPUTERNAME"] ? ENV["COMPUTERNAME"] : "unknownhost"
13
+ when /cygwin|linux/
14
+ return ENV["HOSTNAME"] ? ENV["HOSTNAME"] : "unknownhost"
15
+ else
16
+ raise "Unknown RUBY_PRATFORM : " + RUBY_PLATFORM
17
+ end
18
+ end
19
+ # tmp dir
8
20
  def tmp_dir
9
21
  case RUBY_PLATFORM
10
22
  when /mswin|mingw/
11
- return ENV["TEMP"]
23
+ if ENV["TEMP"].nil?
24
+ raise "Cannot use temporary directory, because ENV[\"TEMP\"] is not defined"
25
+ else
26
+ return ENV["TEMP"]
27
+ end
12
28
  when /cygwin|linux/
13
- return "/tmp/"
29
+ return ENV["TMPDIR"] ? ENV["TMPDIR"] : "/tmp"
14
30
  else
15
31
  raise "Unknown RUBY_PRATFORM : " + RUBY_PLATFORM
16
32
  end
@@ -0,0 +1,20 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'rbatch'
3
+ require 'fileutils'
4
+ require 'date'
5
+
6
+ RBatch::Log.new do |log|
7
+ log.info("Start---------------")
8
+ target_dir = RBatch::config["target_dir"]
9
+ RBatch::config["file_list"].each do |file_wildcard|
10
+ Dir::glob(file_wildcard).each do |file|
11
+ if ! File.exists?(File.join(target_dir,File.basename(file)))
12
+ log.info("Copy " + file + " to " + target_dir)
13
+ FileUtils.cp(file,target_dir)
14
+ else
15
+ log.info("Skip " + file + " (already backuped)")
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,4 @@
1
+ file_list:
2
+ - /var/log/hoge*
3
+ - /var/log/messages
4
+ target_dir: /tmp
@@ -459,6 +459,15 @@ class LoggerTest < Test::Unit::TestCase
459
459
  }
460
460
  end
461
461
 
462
+ def test_i_change_name_by_opt_with_host
463
+ log = RBatch::Log.new({:name => "<prog><date><host>name.log" })
464
+ log.info("test_change_name_by_opt_with_host")
465
+ log.close
466
+ File::open(File.join(@dir , "test_log" + Time.now.strftime("%Y%m%d") + RBatch::hostname + "name.log")) {|f|
467
+ assert_match /test_change_name_by_opt_with_host/, f.read
468
+ }
469
+ end
470
+
462
471
 
463
472
  def test_i_log_level_default
464
473
  log = RBatch::Log.new({ :name => "test_level" })
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rbatch
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.8.2
5
+ version: 1.9.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - fetaro
@@ -80,12 +80,14 @@ files:
80
80
  - rbatch.gemspec
81
81
  - sample/bin/apache_log_insert.rb
82
82
  - sample/bin/file_batch_copy.rb
83
+ - sample/bin/log_backup.rb
83
84
  - sample/bin/mysql_data_backup.rb
84
85
  - sample/bin/openam_log_insert.rb
85
86
  - sample/bin/openldap_backup.rb
86
87
  - sample/bin/webagent_log_insert.rb
87
88
  - sample/conf/apache_log_insert.yaml
88
89
  - sample/conf/file_batch_copy.yaml
90
+ - sample/conf/log_backup.yaml
89
91
  - sample/conf/mysql_data_backup.yaml
90
92
  - sample/conf/openam_log_insert.yaml
91
93
  - sample/conf/openldap_backup.yaml