rbatch 1.4.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.
Files changed (62) hide show
  1. data/CHANGELOG +25 -0
  2. data/LICENSE +0 -0
  3. data/README.ja.md +280 -0
  4. data/README.md +280 -0
  5. data/Rakefile +66 -0
  6. data/VERSION +1 -0
  7. data/doc/rdoc/CHANGELOG.html +160 -0
  8. data/doc/rdoc/LICENSE.html +94 -0
  9. data/doc/rdoc/RBatch.html +476 -0
  10. data/doc/rdoc/RBatch/Cmd.html +329 -0
  11. data/doc/rdoc/RBatch/CmdException.html +154 -0
  12. data/doc/rdoc/RBatch/CmdResult.html +492 -0
  13. data/doc/rdoc/RBatch/Log.html +739 -0
  14. data/doc/rdoc/created.rid +8 -0
  15. data/doc/rdoc/images/brick.png +0 -0
  16. data/doc/rdoc/images/brick_link.png +0 -0
  17. data/doc/rdoc/images/bug.png +0 -0
  18. data/doc/rdoc/images/bullet_black.png +0 -0
  19. data/doc/rdoc/images/bullet_toggle_minus.png +0 -0
  20. data/doc/rdoc/images/bullet_toggle_plus.png +0 -0
  21. data/doc/rdoc/images/date.png +0 -0
  22. data/doc/rdoc/images/find.png +0 -0
  23. data/doc/rdoc/images/loadingAnimation.gif +0 -0
  24. data/doc/rdoc/images/macFFBgHack.png +0 -0
  25. data/doc/rdoc/images/package.png +0 -0
  26. data/doc/rdoc/images/page_green.png +0 -0
  27. data/doc/rdoc/images/page_white_text.png +0 -0
  28. data/doc/rdoc/images/page_white_width.png +0 -0
  29. data/doc/rdoc/images/plugin.png +0 -0
  30. data/doc/rdoc/images/ruby.png +0 -0
  31. data/doc/rdoc/images/tag_green.png +0 -0
  32. data/doc/rdoc/images/wrench.png +0 -0
  33. data/doc/rdoc/images/wrench_orange.png +0 -0
  34. data/doc/rdoc/images/zoom.png +0 -0
  35. data/doc/rdoc/index.html +124 -0
  36. data/doc/rdoc/js/darkfish.js +116 -0
  37. data/doc/rdoc/js/jquery.js +32 -0
  38. data/doc/rdoc/js/quicksearch.js +114 -0
  39. data/doc/rdoc/js/thickbox-compressed.js +10 -0
  40. data/doc/rdoc/lib/rbatch/cmd_rb.html +56 -0
  41. data/doc/rdoc/lib/rbatch/config_rb.html +56 -0
  42. data/doc/rdoc/lib/rbatch/log_rb.html +58 -0
  43. data/doc/rdoc/lib/rbatch_rb.html +58 -0
  44. data/doc/rdoc/rdoc.css +706 -0
  45. data/lib/rbatch.rb +51 -0
  46. data/lib/rbatch/cmd.rb +118 -0
  47. data/lib/rbatch/config.rb +29 -0
  48. data/lib/rbatch/log.rb +205 -0
  49. data/sample/bin/file_batch_copy.rb +9 -0
  50. data/sample/bin/openldap_backup.rb +7 -0
  51. data/sample/bin/test.rb +12 -0
  52. data/sample/config/file_batch_copy.yaml +5 -0
  53. data/sample/config/openldap_backup.yaml +2 -0
  54. data/sample/config/rbatch.yaml +68 -0
  55. data/sample/log/empty +0 -0
  56. data/test/cases/test_cmd.rb +120 -0
  57. data/test/cases/test_config.rb +34 -0
  58. data/test/cases/test_log.rb +594 -0
  59. data/test/config/rbatch.yaml +0 -0
  60. data/test/mocks/PrintArgs.exe +0 -0
  61. data/test/mocks/win_cmd.exe +0 -0
  62. metadata +173 -0
@@ -0,0 +1,25 @@
1
+ 1.0.0
2
+ -----
3
+ initial
4
+
5
+ 1.1.0(2012/11/9)
6
+ -----
7
+ * make common config file
8
+ * change log option
9
+
10
+ 1.2.0(2012/11/13)
11
+ -----
12
+ * change log level option format
13
+ * make log append option
14
+
15
+ 1.3.0(2012/12/30)
16
+ -----
17
+ * add RBatch::Log to instance interface
18
+ * add double run check option
19
+
20
+ 1.4.0(2013/1/5)
21
+ ----
22
+ * add log_stdout option
23
+ * add sample code
24
+ * add cmd_raise option
25
+ * add log_delete_old_log option
data/LICENSE ADDED
File without changes
@@ -0,0 +1,280 @@
1
+ [[English]](https://github.com/fetaro/rbatch/blob/master/README.md "english") [[Japanese]](https://github.com/fetaro/rbatch/blob/master/README.ja.md "japanese")
2
+
3
+ RBatch:Ruby-base バッチスクリプトフレームワーク
4
+ =============
5
+
6
+ RBatchについて
7
+ --------------
8
+ これはRubyで書かれたバッチスクリプトのフレームワークです。
9
+ バッチスクリプト(バックアップやプロセスリロード等)を書く際に便利な機能をフレームワークとして提供しています。
10
+
11
+ 主な機能は以下のとおり。
12
+
13
+ * 自動ログ出力
14
+ * 自動設定ファイル読み込み
15
+ * 外部コマンド実行
16
+ * ファイル名・ディレクトリ構造制約
17
+ * 二重起動チェック
18
+
19
+ このフレームワークはRuby 1.9.x以降のみで動作します。
20
+
21
+ ### 自動ログ出力
22
+ Logging blockを使うことで、自動的にログファイルに出力することができます。
23
+ ログファイルはデフォルトで"../log/YYYYMMDD_HHMMSS_${PROG_NAME}.log"に出力されます。
24
+ 例外が発生した場合でも、ログにスタックトレースを出力することができます。
25
+
26
+ サンプル
27
+
28
+ スクリプト : ./bin/sample1.rb
29
+ ```
30
+ require 'rbatch'
31
+
32
+ RBatch::Log.new(){ |log| # Logging block
33
+ log.info "info string"
34
+ log.error "error string"
35
+ raise "exception"
36
+ }
37
+ ```
38
+
39
+ ログファイル : ./log/20121020_005953_sample1.log
40
+ ```
41
+ # Logfile created on 2012-10-20 00:59:53 +0900 by logger.rb/25413
42
+ I, [2012-10-20T00:59:53.895528 #3208] INFO -- : info string
43
+ E, [2012-10-20T00:59:53.895582 #3208] ERROR -- : error string
44
+ F, [2012-10-20T00:59:53.895629 #3208] FATAL -- : Caught exception; existing 1
45
+ F, [2012-10-20T00:59:53.895667 #3208] FATAL -- : exception (RuntimeError)
46
+ test.rb:6:in `block in <main>'
47
+ /usr/local/lib/ruby192/lib/ruby/gems/1.9.1/gems/rbatch-1.0.0/lib/rbatch/auto_logger.rb:37:in `initialize'
48
+ test.rb:3:in `new'
49
+ test.rb:3:in `<main>'
50
+ ```
51
+
52
+ ### 自動設定ファイル読み込み
53
+
54
+ RBatchは簡単にデフォルトの位置の設定ファイルを読み込めます。
55
+ デフォルトの位置は"../config/${PROG_NAME}.yaml"です。
56
+
57
+ サンプル
58
+
59
+ 設定ファイル : ./config/sample2.yaml
60
+ ```
61
+ key: value
62
+ array:
63
+ - item1
64
+ - item2
65
+ - item3
66
+ ```
67
+
68
+ スクリプト : ./bin/sample2.rb
69
+ ```
70
+ require 'rbatch'
71
+ p RBatch::config
72
+ => {"key" => "value", "array" => ["item1", "item2", "item3"]}
73
+ ```
74
+
75
+ ### 外部コマンド実行
76
+ RBatchは外部コマンド(たとえば"ls -l")を実行するラッパー関数を提供します。
77
+
78
+ この関数は、実行結果をオブジェクトで返し、そのオブジェクトは標準出力、標準エラー出力、およびexitステータスを含みます。
79
+
80
+ サンプル
81
+ ```
82
+ require 'rbatch'
83
+ r = RBatch::cmd("ls")
84
+ p r.stdout
85
+ => "fileA\nfileB\n"
86
+ p r.stderr
87
+ => ""
88
+ p r.status
89
+ => 0
90
+ ```
91
+
92
+ ### ファイル名・ディレクトリ構造制約
93
+
94
+ RBatchでは、「設定より規約」(convention over configuration)という原則に従い、バッチスクリプトで必要になるファイル群に、ファイル名とディレクトリ構造の制約をもたせます。
95
+
96
+ 具体的には、"bin/hoge.rb"というバッチスクリプトでは、"conf/hoge.yaml"という設定ファイルを読み、
97
+ "log/YYYYMMDD_HHMMSS_hoge.rb"というログを出力するという規則です。
98
+
99
+ これにより、バッチスクリプトの可読性・保守性が向上します。
100
+
101
+ ```
102
+ ./
103
+ |-bin
104
+ | |- hoge.rb
105
+ | |- bar.rb
106
+ |-config
107
+ | |- hoge.yaml
108
+ | |- bar.yaml
109
+ |-log
110
+ |- YYYYMMDD_HHMMSS_hoge.log
111
+ |- YYYYMMDD_HHMMSS_bar.log
112
+ ```
113
+
114
+ ### 二重起動チェック
115
+
116
+ RBatchの共通設定ファイルに"forbid_double_run: true"の設定を書けば、RBatchを利用したスクリプトの二重起動チェックができます。
117
+
118
+ クイックスタート
119
+ --------------
120
+ ### ステップ1: インストール
121
+
122
+ ```
123
+ # git clone https://github.com/fetaro/rbatch.git
124
+ # cd rbatch
125
+ # rake package
126
+ # gem install pkg/rbatch-1.0.0
127
+ ```
128
+
129
+ ### ステップ2: ディレクトリ作成
130
+
131
+ ```
132
+ $ mkdir bin log config
133
+ ```
134
+
135
+ ### ステップ3: バッチスクリプト作成
136
+
137
+ bin/backup.rbは以下の通り。
138
+ ```
139
+ require 'rbatch'
140
+
141
+ RBatch::Log.new(){|log|
142
+ log.info( "start backup" )
143
+ result = RBatch::cmd( "cp -p /var/log/message /backup")
144
+ log.info( result )
145
+ log.error ( "backup failed") if result.status != 0
146
+ }
147
+ ```
148
+
149
+ ### ステップ4: 実行
150
+
151
+ ```
152
+ $ ruby bin/backup.rb
153
+ ```
154
+
155
+ ### ステップ5: 確認
156
+
157
+ 自動的にlog/YYYYMMDD_HHMMSS_backup.logにログファイルが出ます。
158
+
159
+ ```
160
+ $ cat log/YYYYMMDD_HHMMSS_backup.log
161
+
162
+ # Logfile created on 2012-10-20 00:19:23 +0900 by logger.rb/25413
163
+ I, [2012-10-20T00:19:23.422876 #2357] INFO -- : start backup
164
+ I, [2012-10-20T00:19:23.424773 #2357] INFO -- : {:stdout=>"", :stderr=>"cp: cannot stat `/var/log/message': No such file or directory\n", :status=>1}
165
+ E, [2012-10-20T00:19:23.424882 #2357] ERROR -- : backup failed
166
+ ```
167
+
168
+
169
+ マニュアル
170
+ --------------
171
+
172
+ ### RBatch全体設定ファイル
173
+
174
+ 以下の場所にRBatch全体設定ファイルを配置すると、全てのスクリプトにてオプションが適用される。
175
+
176
+ ```
177
+ (スクリプトのパス)/../config/rbatch.yaml
178
+ ```
179
+
180
+ 設定ファイルのサンプルは以下の通り
181
+ ```
182
+ # RBatch 全体設定
183
+ #
184
+ # 設定ファイルの形式はYAML形式です
185
+ #
186
+
187
+ # -------------------
188
+ # 全体
189
+ # -------------------
190
+
191
+ # スクリプトの二重起動を可能にするかどうか
192
+ #
193
+ # trueにすると、requireしたタイミングで例外を投げます。
194
+ #
195
+ # Default : false
196
+ #
197
+ #forbid_double_run: true
198
+
199
+ # -------------------
200
+ # 外部コマンド実行関連
201
+ # -------------------
202
+
203
+ # 例外発生機能を有効にするかどうか
204
+ #
205
+ # Default : false
206
+ #
207
+ # trueの場合、コマンドの終了ステータスが0でない場合に例外を発生する。
208
+ #
209
+ #cmd_raise: true
210
+
211
+ # -------------------
212
+ # ログ関連
213
+ # -------------------
214
+
215
+ # ログファイル名
216
+ #
217
+ # Default : "<date>_<time>_<prog>.log"
218
+ #
219
+ # 予約語
220
+ # <data> --> YYYYMMDDの日付形式に置換されます
221
+ # <time> --> hhmmssの時刻形式に置換されます
222
+ # <prog> --> 拡張子を除いたファイル名に置換されます
223
+ #
224
+ #log_name : "<date>_<time>_<prog>.log"
225
+ #log_name : "<date>_<prog>.log"
226
+
227
+ # ログ出力ディレクトリ
228
+ #
229
+ # Default : "(スクリプトの配置パス)/../log"
230
+ #
231
+ #log_dir : "/tmp/log"
232
+
233
+ # ログを追記するかどうか
234
+ #
235
+ # Default : ture
236
+ #
237
+ #log_append : false
238
+
239
+ # ログレベル
240
+ #
241
+ # Default : "info"
242
+ # Value : "debug","info","wran","error","fatal"
243
+ #
244
+ #log_level : "debug"
245
+
246
+ # 標準出力とログの両方に文字列を出力するかどうか
247
+ #
248
+ # Default : false
249
+ #
250
+ #log_stdout : true
251
+
252
+ # 古いログを削除するかどうか
253
+ #
254
+ # trueの場合、RBatch::Log.newを呼んだタイミングで、古いログを削除する。
255
+ # 削除対象のログは、そのRBatch::Logのインスタンスが出力するログファイルと
256
+ # 同じファイル名フォーマットであり、かつログファイル名のフォーマットに<date>が
257
+ # 含まれるもの。
258
+ # 例えば、RBatch::Logで出力するログファイルが「20120105_hoge.log」だった場合、
259
+ # 削除対象のログは「YYYYMMDD_hoge.log」のログとなる。
260
+ #
261
+ # Default : false
262
+ #
263
+ #log_delete_old_log: true
264
+
265
+ # 古いログの残す日数
266
+ #
267
+ # Default : 7
268
+ #
269
+ #log_delete_old_log_date: 14
270
+
271
+ ```
272
+
273
+
274
+ テスト方法
275
+ --------------
276
+ ```
277
+ ruby -I lib test/cases/test_config.rb
278
+ ruby -I lib test/cases/test_cmd.rb
279
+ ruby -I lib test/cases/test_log.rb
280
+ ```
@@ -0,0 +1,280 @@
1
+ [[English]](https://github.com/fetaro/rbatch/blob/master/README.md "english") [[Japanese]](https://github.com/fetaro/rbatch/blob/master/README.ja.md "japanese")
2
+
3
+ RBatch:Ruby-base Batch Script Framework
4
+ =============
5
+
6
+ About RBatch
7
+ --------------
8
+ This is a Ruby-base Batch Script Framework. RBatch offer a convenient function as a framework, when you write a batch script such as "data backup script" or "proccess start script".
9
+
10
+ There are following functions.
11
+
12
+ * Auto Logging
13
+ * Auto Config Reading
14
+ * External Command Wrapper
15
+ * Directory Structure convention
16
+ * Double Run Check
17
+
18
+ This work on only Ruby 1.9.x or more later.
19
+
20
+ ### Auto Logging
21
+ Use Auto Logging block, RBatch automatically write to logfile.
22
+ Log file default location is "../log/YYYYMMDD_HHMMSS_${PROG_NAME}.log" .
23
+ If exception occuerd, then RBatch write stack trace to logfile.
24
+
25
+ sample
26
+
27
+ script : ./bin/sample1.rb
28
+ ```
29
+ require 'rbatch'
30
+
31
+ RBatch::Log.new(){ |log| # Logging block
32
+ log.info "info string"
33
+ log.error "error string"
34
+ raise "exception"
35
+ }
36
+ ```
37
+
38
+ logfile : ./log/20121020_005953_sample1.log
39
+ ```
40
+ # Logfile created on 2012-10-20 00:59:53 +0900 by logger.rb/25413
41
+ I, [2012-10-20T00:59:53.895528 #3208] INFO -- : info string
42
+ E, [2012-10-20T00:59:53.895582 #3208] ERROR -- : error string
43
+ F, [2012-10-20T00:59:53.895629 #3208] FATAL -- : Caught exception; existing 1
44
+ F, [2012-10-20T00:59:53.895667 #3208] FATAL -- : exception (RuntimeError)
45
+ test.rb:6:in `block in <main>'
46
+ /usr/local/lib/ruby192/lib/ruby/gems/1.9.1/gems/rbatch-1.0.0/lib/rbatch/auto_logger.rb:37:in `initialize'
47
+ test.rb:3:in `new'
48
+ test.rb:3:in `<main>'
49
+ ```
50
+
51
+ ### Auto Config Reading
52
+
53
+ RBatch easy to read config file (located on "../config/${PROG_NAME}.yaml")
54
+
55
+ sample
56
+
57
+ config : ./config/sample2.yaml
58
+ ```
59
+ key: value
60
+ array:
61
+ - item1
62
+ - item2
63
+ - item3
64
+ ```
65
+
66
+ script : ./bin/sample2.rb
67
+ ```
68
+ require 'rbatch'
69
+ p RBatch::config
70
+ => {"key" => "value", "array" => ["item1", "item2", "item3"]}
71
+ ```
72
+
73
+ ### External Command Wrapper
74
+ RBatch provide a function which wrap external command (such as 'ls').
75
+
76
+ This function return a result object which contain command's STDOUT, STDERR ,and exit status.
77
+
78
+ sample
79
+ ```
80
+ require 'rbatch'
81
+ r = RBatch::cmd("ls")
82
+ p r.stdout
83
+ => "fileA\nfileB\n"
84
+ p r.stderr
85
+ => ""
86
+ p r.status
87
+ => 0
88
+ ```
89
+
90
+ ### Directory Structure Convention
91
+
92
+ Follow the axiom of "convention over configuration", RBatch restrict file naming rule and directory structure.
93
+
94
+ For exsample, If you make "bin/hoge.rb", you should name config file to "conf/hoge.yaml". And the name of log file is decided on "log/YYYYMMDD_HHMMSS_hoge.rb"
95
+
96
+ In this way, maintainability and readability of batch script get higher.
97
+
98
+ ```
99
+ ./
100
+ |-bin
101
+ | |- hoge.rb
102
+ | |- bar.rb
103
+ |-config
104
+ | |- hoge.yaml
105
+ | |- bar.yaml
106
+ |-log
107
+ |- YYYYMMDD_HHMMSS_hoge.log
108
+ |- YYYYMMDD_HHMMSS_bar.log
109
+ ```
110
+
111
+ ### Double Run Check
112
+
113
+ Forbit double run of the RBatch script by writing option "forbid_double_run: true" to the common configuration file.
114
+
115
+ Quick Start
116
+ --------------
117
+ ### Step1: Install
118
+
119
+ ```
120
+ # git clone https://github.com/fetaro/rbatch.git
121
+ # cd rbatch
122
+ # rake package
123
+ # gem install pkg/rbatch-1.0.0
124
+ ```
125
+
126
+ ### Step2: Make directories
127
+
128
+ ```
129
+ $ mkdir bin log config
130
+ ```
131
+
132
+ ### Step3: Write batch script
133
+
134
+ for bin/backup.rb
135
+ ```
136
+ require 'rbatch'
137
+
138
+ RBatch::Log.new(){|log|
139
+ log.info( "start backup" )
140
+ result = RBatch::cmd( "cp -p /var/log/message /backup")
141
+ log.info( result )
142
+ log.error ( "backup failed") if result.status != 0
143
+ }
144
+ ```
145
+
146
+ ### Step4: Run
147
+
148
+ ```
149
+ $ ruby bin/backup.rb
150
+ ```
151
+
152
+ ### Step5: Check
153
+
154
+ Log file is generated automatically.
155
+
156
+ ```
157
+ $ cat log/YYYYMMDD_HHMMSS_backup.log
158
+
159
+ # Logfile created on 2012-10-20 00:19:23 +0900 by logger.rb/25413
160
+ I, [2012-10-20T00:19:23.422876 #2357] INFO -- : start backup
161
+ I, [2012-10-20T00:19:23.424773 #2357] INFO -- : {:stdout=>"", :stderr=>"cp: cannot stat `/var/log/message': No such file or directory\n", :status=>1}
162
+ E, [2012-10-20T00:19:23.424882 #2357] ERROR -- : backup failed
163
+ ```
164
+
165
+
166
+ Manual
167
+ --------------
168
+
169
+ ### Common Config File
170
+
171
+ If you make follow config file, option value effect to all scripts.
172
+
173
+ ```
174
+ (script file path)/../config/rbatch.yaml
175
+ ```
176
+
177
+ Config Sample
178
+ ```
179
+ # RBatch Common Config
180
+ #
181
+ # This file format is YAML
182
+ #
183
+
184
+ # -------------------
185
+ # Global setting
186
+ # -------------------
187
+
188
+ # Forbit Double Run
189
+ #
190
+ # Default : false
191
+ #
192
+ #forbid_double_run: true
193
+
194
+ # -------------------
195
+ # Cmd setting
196
+ # -------------------
197
+
198
+ # Raise Exception
199
+ #
200
+ # Default : false
201
+ #
202
+ # If command exit status is not 0, raise exception.
203
+ #
204
+ #cmd_raise : true
205
+
206
+
207
+ # -------------------
208
+ # Log setting
209
+ # -------------------
210
+
211
+ # Log File Name
212
+ #
213
+ # Default : "<date>_<time>_<prog>.log"
214
+ #
215
+ # Reservation words
216
+ # <data> --> replace to YYYYMMDD date string
217
+ # <time> --> replace to hhmmss time string
218
+ # <prog> --> Program file base name (except extention)
219
+ #
220
+ #log_name : "<date>_<time>_<prog>.log"
221
+ #log_name : "<date>_<prog>.log"
222
+
223
+ # Log Output Directory
224
+ #
225
+ # Default : "(Script path)/../log"
226
+ #
227
+ #log_dir : "/tmp/log"
228
+
229
+ # Append log or not
230
+ #
231
+ # Default : ture
232
+ #
233
+ #log_append : false
234
+
235
+ # Log Level
236
+ #
237
+ # Default : "info"
238
+ # Value : "debug","info","wran","error","fatal"
239
+ #
240
+ #log_level : "debug"
241
+
242
+ # Print log-string both file and STDOUT
243
+ #
244
+ # Default : false
245
+ #
246
+ #log_stdout : true
247
+
248
+ # 標準出力とログの両方に文字列を出力するかどうか
249
+ #
250
+ # Default : false
251
+ #
252
+ #log_stdout : true
253
+
254
+ # Delete old log files
255
+ #
256
+ # If this is true, delete old log file when RBatch::Log.new is called.
257
+ # A log file to delete is a log file which was made by the RBatch::Log instance,
258
+ # and log filename format include "<date>".
259
+ #
260
+ # Default : false
261
+ #
262
+ #log_delete_old_log: true
263
+
264
+ # The day of leaving log files
265
+ #
266
+ # Default : 7
267
+ #
268
+ #log_delete_old_log_date: 14
269
+
270
+
271
+ ```
272
+
273
+
274
+ How to Test
275
+ --------------
276
+ ```
277
+ ruby -I lib test/cases/test_config.rb
278
+ ruby -I lib test/cases/test_cmd.rb
279
+ ruby -I lib test/cases/test_log.rb
280
+ ```