logrotate 1.0.0 → 1.1.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/History.txt +9 -0
- data/Manifest.txt +3 -0
- data/README.txt +148 -26
- data/Rakefile +1 -1
- data/lib/logrotate.rb +2 -245
- data/lib/logrotate/impl.rb +416 -0
- data/lib/logrotate/logrotate.rb +86 -0
- data/lib/logrotate/rotateinfo.rb +166 -0
- data/test/test_logrotate.rb +78 -37
- metadata +6 -3
@@ -0,0 +1,166 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
module LogRotate
|
6
|
+
|
7
|
+
#
|
8
|
+
# This class is one of the possible return values from the
|
9
|
+
# LogRotate::rotate_file method. It contains information about the
|
10
|
+
# rotated files.
|
11
|
+
#
|
12
|
+
class RotateInfoDateExtension
|
13
|
+
|
14
|
+
#
|
15
|
+
# This field is an array of rotated file names and dates. Each
|
16
|
+
# element is a hash containing the following fields:
|
17
|
+
# +file+:: A rotated file name.
|
18
|
+
# +date_time+:: The associated date of the rotated file.
|
19
|
+
#
|
20
|
+
attr_accessor :rotated_files_and_dates
|
21
|
+
|
22
|
+
#
|
23
|
+
# An array of rotated file names. This list of file names will be
|
24
|
+
# the same as the file names listed in the rotated_files_and_dates
|
25
|
+
# field.
|
26
|
+
#
|
27
|
+
attr_accessor :rotated_files
|
28
|
+
|
29
|
+
#
|
30
|
+
# An array of file names that were deleted during the rotate_file
|
31
|
+
# method call.
|
32
|
+
#
|
33
|
+
attr_accessor :deleted_files
|
34
|
+
|
35
|
+
#
|
36
|
+
# The newly rotated file name created by the rotate_file method
|
37
|
+
# call.
|
38
|
+
#
|
39
|
+
attr_accessor :new_rotated_file
|
40
|
+
|
41
|
+
#
|
42
|
+
# ====Description:
|
43
|
+
# This method creates a RotateInfoDateExtension instance.
|
44
|
+
#
|
45
|
+
def initialize(rotated_files_and_dates,
|
46
|
+
rotated_files,
|
47
|
+
deleted_files,
|
48
|
+
new_rotated_file)
|
49
|
+
@rotated_files_and_dates = rotated_files_and_dates
|
50
|
+
@rotated_files = rotated_files
|
51
|
+
@deleted_files = deleted_files
|
52
|
+
@new_rotated_file = new_rotated_file
|
53
|
+
end
|
54
|
+
|
55
|
+
def ==(value)
|
56
|
+
return (@rotated_files_and_dates == value.rotated_files_and_dates &&
|
57
|
+
@rotated_files == value.rotated_files &&
|
58
|
+
@deleted_files == value.deleted_files &&
|
59
|
+
@new_rotated_file == value.new_rotated_file)
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s()
|
63
|
+
os = StringIO.new
|
64
|
+
os.print "Rotated Files And Dates:\n"
|
65
|
+
@rotated_files_and_dates.each do |element|
|
66
|
+
os.print " file -> ", element[:file], ", date_time -> ", element[:date_time], "\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
os.print "Rotated Files: \n"
|
70
|
+
if (@rotated_files.length() > 0)
|
71
|
+
os.print " ", @rotated_files.join("\n "), "\n"
|
72
|
+
end
|
73
|
+
|
74
|
+
os.print "Deleted Files:\n"
|
75
|
+
if (@deleted_files.length() > 0)
|
76
|
+
os.print " ", @deleted_files.join("\n "), "\n"
|
77
|
+
end
|
78
|
+
|
79
|
+
os.print "New Rotated File: \n"
|
80
|
+
os.print " ", @new_rotated_file, "\n"
|
81
|
+
|
82
|
+
return os.string()
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# This class is one of the possible return values from the
|
88
|
+
# LogRotate::rotate_file method. It contains information about the
|
89
|
+
# rotated files.
|
90
|
+
#
|
91
|
+
class RotateInfoIntegerExtension
|
92
|
+
|
93
|
+
#
|
94
|
+
# This field is an array of rotated file names and counts. Each
|
95
|
+
# element is a hash containing the following fields:
|
96
|
+
# +file+:: A rotated file name.
|
97
|
+
# +index+:: The associated index of the rotated file.
|
98
|
+
#
|
99
|
+
attr_accessor :rotated_files_and_counts
|
100
|
+
|
101
|
+
#
|
102
|
+
# An array of rotated file names. This list of file names will be
|
103
|
+
# the same as the file names listed in the rotated_files_and_counts
|
104
|
+
# field.
|
105
|
+
#
|
106
|
+
attr_accessor :rotated_files
|
107
|
+
|
108
|
+
#
|
109
|
+
# An array of file names that were deleted during the rotate_file
|
110
|
+
# method call.
|
111
|
+
#
|
112
|
+
attr_accessor :deleted_files
|
113
|
+
|
114
|
+
#
|
115
|
+
# The newly rotated file name created by the rotate_file method
|
116
|
+
# call.
|
117
|
+
#
|
118
|
+
attr_accessor :new_rotated_file
|
119
|
+
|
120
|
+
#
|
121
|
+
# ====Description:
|
122
|
+
# This method creates a RotateInfoIntegerExtension instance.
|
123
|
+
#
|
124
|
+
def initialize(rotated_files_and_counts,
|
125
|
+
rotated_files,
|
126
|
+
deleted_files,
|
127
|
+
new_rotated_file)
|
128
|
+
@rotated_files_and_counts = rotated_files_and_counts
|
129
|
+
@rotated_files = rotated_files
|
130
|
+
@deleted_files = deleted_files
|
131
|
+
@new_rotated_file = new_rotated_file
|
132
|
+
end
|
133
|
+
|
134
|
+
def ==(value)
|
135
|
+
return (@rotated_files_and_counts == value.rotated_files_and_counts &&
|
136
|
+
@rotated_files == value.rotated_files &&
|
137
|
+
@deleted_files == value.deleted_files &&
|
138
|
+
@new_rotated_file == value.new_rotated_file)
|
139
|
+
end
|
140
|
+
|
141
|
+
def to_s()
|
142
|
+
os = StringIO.new
|
143
|
+
os.print "Rotated Files And Counts:\n"
|
144
|
+
@rotated_files_and_counts.each do |element|
|
145
|
+
os.print " file -> ", element[:file], ", index -> ", element[:index], "\n"
|
146
|
+
end
|
147
|
+
|
148
|
+
os.print "Rotated Files: \n"
|
149
|
+
if (@rotated_files.length() > 0)
|
150
|
+
os.print " ", @rotated_files.join("\n "), "\n"
|
151
|
+
end
|
152
|
+
|
153
|
+
os.print "Deleted Files:\n"
|
154
|
+
if (@deleted_files.length() > 0)
|
155
|
+
os.print " ", @deleted_files.join("\n "), "\n"
|
156
|
+
end
|
157
|
+
|
158
|
+
os.print "New Rotated File: \n"
|
159
|
+
os.print " ", @new_rotated_file, "\n"
|
160
|
+
|
161
|
+
return os.string()
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
data/test/test_logrotate.rb
CHANGED
@@ -1,39 +1,22 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'logrotate'
|
4
|
+
require 'logrotate/impl'
|
5
|
+
require 'logrotate/rotateinfo'
|
6
|
+
|
7
|
+
require 'fileutils'
|
4
8
|
|
5
9
|
require 'test/unit'
|
6
10
|
|
7
11
|
class LogRotateTest < Test::Unit::TestCase
|
8
12
|
TEMP_DIR = "./temp"
|
9
13
|
|
10
|
-
def remove_directory(directory)
|
11
|
-
if (!File.directory?(directory)) then return end
|
12
|
-
|
13
|
-
entries = Dir.entries(directory).select {|entry| (entry != ".") && (entry != "..") }
|
14
|
-
entries.map! { |entry| File.join(directory, entry) }
|
15
|
-
|
16
|
-
entries.each do |entry|
|
17
|
-
if File.directory?(entry)
|
18
|
-
remove_directory(entry)
|
19
|
-
else
|
20
|
-
File.delete(entry)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
Dir.delete(directory)
|
25
|
-
end
|
26
|
-
|
27
|
-
def remove_temp_directory
|
28
|
-
remove_directory(TEMP_DIR)
|
29
|
-
end
|
30
|
-
|
31
14
|
def setup
|
32
|
-
|
15
|
+
teardown()
|
33
16
|
end
|
34
17
|
|
35
18
|
def teardown
|
36
|
-
|
19
|
+
FileUtils::rm_r(TEMP_DIR, :force => true)
|
37
20
|
end
|
38
21
|
|
39
22
|
def test_rotate_integer_extension()
|
@@ -46,7 +29,7 @@ class LogRotateTest < Test::Unit::TestCase
|
|
46
29
|
end
|
47
30
|
|
48
31
|
def test_rotate_date_extension()
|
49
|
-
date_time_format = "%
|
32
|
+
date_time_format = "%F"
|
50
33
|
|
51
34
|
output_dir = nil
|
52
35
|
rotate_date_extension(3, 10, gzip = false)
|
@@ -58,6 +41,22 @@ class LogRotateTest < Test::Unit::TestCase
|
|
58
41
|
rotate_date_extension(3, 10, gzip = true, output_dir)
|
59
42
|
rotate_date_extension(3, 10, gzip = true, output_dir, date_time_format)
|
60
43
|
end
|
44
|
+
|
45
|
+
def get_expected_results_integer_extension(iteration, base_name, count, gzip, output_dir)
|
46
|
+
expected_result = LogRotate::RotateInfoIntegerExtension.new(nil, nil, nil, nil)
|
47
|
+
expected_result.rotated_files_and_counts = []
|
48
|
+
|
49
|
+
1.upto(iteration > count ? count : iteration) do |i|
|
50
|
+
rotated_file = "#{output_dir}/#{base_name}.#{i}" + (gzip ? ".gz" : "")
|
51
|
+
expected_result.rotated_files_and_counts.push({ :index => i, :file => rotated_file })
|
52
|
+
end
|
53
|
+
|
54
|
+
expected_result.rotated_files = expected_result.rotated_files_and_counts.map { |entry| entry[:file] }
|
55
|
+
expected_result.deleted_files = iteration > count ? [ "#{output_dir}/#{base_name}.#{count}" + (gzip ? ".gz" : "") ] : []
|
56
|
+
expected_result.new_rotated_file = "#{output_dir}/#{base_name}.1" + (gzip ? ".gz" : "")
|
57
|
+
|
58
|
+
return expected_result
|
59
|
+
end
|
61
60
|
|
62
61
|
def rotate_integer_extension(count, rotations, gzip, output_dir = nil)
|
63
62
|
source_dir = "#{TEMP_DIR}"
|
@@ -75,26 +74,30 @@ class LogRotateTest < Test::Unit::TestCase
|
|
75
74
|
|
76
75
|
manipulate_garbage_files(output_dir, base_name, :insert)
|
77
76
|
|
78
|
-
#rotate the file
|
79
|
-
|
77
|
+
#rotate the file rotations times
|
78
|
+
1.upto(rotations) do |iteration|
|
79
|
+
data = rotations - iteration + 1
|
80
80
|
File.open(file, "w") do |file_stream|
|
81
|
-
file_stream.write("#{
|
81
|
+
file_stream.write("#{data}")
|
82
82
|
end
|
83
83
|
|
84
84
|
options = {:count => count}
|
85
85
|
if (gzip) then options[:gzip] = true end
|
86
86
|
if (is_output_dir) then options[:directory] = output_dir end
|
87
|
-
|
88
|
-
LogRotate.
|
87
|
+
|
88
|
+
result = LogRotate.rotate_file(file, options)
|
89
|
+
|
90
|
+
expected_result = get_expected_results_integer_extension(iteration, base_name, count, gzip, output_dir)
|
91
|
+
assert_equal(expected_result, result)
|
89
92
|
end
|
90
93
|
|
91
94
|
manipulate_garbage_files(output_dir, base_name, :delete)
|
92
|
-
|
95
|
+
|
93
96
|
#verify there are count rotated files.
|
94
97
|
entries = Dir.glob("#{output_dir}/*").map {|entry| File.split(entry)[1]}
|
95
98
|
assert_equal(entries.length, count)
|
96
99
|
|
97
|
-
#verify the contents
|
100
|
+
#verify the contents of the remaining files.
|
98
101
|
expected = (1..count).map { |i| "#{base_name}.#{i}" + (gzip ? ".gz" : "") }
|
99
102
|
assert_equal(entries.sort, expected)
|
100
103
|
|
@@ -109,11 +112,11 @@ class LogRotateTest < Test::Unit::TestCase
|
|
109
112
|
end
|
110
113
|
end
|
111
114
|
|
112
|
-
|
115
|
+
teardown()
|
113
116
|
end
|
114
117
|
|
115
118
|
def manipulate_garbage_files(dir, base_name, operation)
|
116
|
-
bogus_extensions = [ ".back", ".back123", ".2008-10-
|
119
|
+
bogus_extensions = [ ".back", ".back123", ".2008-10-04foo", ".123x" ]
|
117
120
|
|
118
121
|
bogus_extensions.each do |bogus_extension|
|
119
122
|
bogus_file_1 = File.join(dir, base_name + bogus_extension)
|
@@ -128,9 +131,40 @@ class LogRotateTest < Test::Unit::TestCase
|
|
128
131
|
end
|
129
132
|
end
|
130
133
|
|
134
|
+
def get_expected_result_date_extension(iteration, base_name, date_times, count, date_time_format, gzip, output_dir)
|
135
|
+
index = iteration - 1
|
136
|
+
|
137
|
+
expected_result = LogRotate::RotateInfoDateExtension.new(nil, nil, nil, nil)
|
138
|
+
expected_result.rotated_files_and_dates = []
|
139
|
+
oldest_existing_date_iteration = (iteration - count + 1 <= 1) ? 1 : (iteration - count + 1)
|
140
|
+
oldest_existing_date_index = oldest_existing_date_iteration - 1
|
141
|
+
index.downto(oldest_existing_date_index) do |i|
|
142
|
+
str_date_time = date_times[i].strftime(date_time_format)
|
143
|
+
reconstructed_date_time = DateTime.strptime(str_date_time, date_time_format)
|
144
|
+
rotated_file = "#{output_dir}/#{base_name}.#{str_date_time}" + (gzip ? ".gz" : "")
|
145
|
+
|
146
|
+
expected_result.rotated_files_and_dates.push({ :date_time => reconstructed_date_time,
|
147
|
+
:file => rotated_file })
|
148
|
+
end
|
149
|
+
|
150
|
+
expected_result.rotated_files = expected_result.rotated_files_and_dates.map { |entry| entry[:file] }
|
151
|
+
|
152
|
+
if (iteration > count)
|
153
|
+
str_date_time = date_times[index - count].strftime(date_time_format)
|
154
|
+
expected_result.deleted_files = [ "#{output_dir}/#{base_name}.#{str_date_time}" + (gzip ? ".gz" : "") ]
|
155
|
+
else
|
156
|
+
expected_result.deleted_files = []
|
157
|
+
end
|
158
|
+
|
159
|
+
str_date_time = date_times[index].strftime(date_time_format)
|
160
|
+
expected_result.new_rotated_file = "#{output_dir}/#{base_name}.#{str_date_time}" + (gzip ? ".gz" : "")
|
161
|
+
|
162
|
+
return expected_result
|
163
|
+
end
|
164
|
+
|
131
165
|
def rotate_date_extension(count, rotations, gzip,
|
132
166
|
output_dir = nil,
|
133
|
-
date_time_format = LogRotate::DEFAULT_DATE_TIME_FORMAT)
|
167
|
+
date_time_format = LogRotate::Impl::DEFAULT_DATE_TIME_FORMAT)
|
134
168
|
|
135
169
|
source_dir = "#{TEMP_DIR}"
|
136
170
|
base_name = "file.dat"
|
@@ -151,7 +185,10 @@ class LogRotateTest < Test::Unit::TestCase
|
|
151
185
|
now = DateTime.now
|
152
186
|
date_times = (0..(rotations - 1)).map { |i| now + i }
|
153
187
|
|
154
|
-
|
188
|
+
1.upto(rotations) do |iteration|
|
189
|
+
index = iteration - 1
|
190
|
+
date_time = date_times[index]
|
191
|
+
|
155
192
|
File.open(file, "w") do |file_stream|
|
156
193
|
file_stream.write(date_time.strftime(date_time_format))
|
157
194
|
end
|
@@ -161,7 +198,11 @@ class LogRotateTest < Test::Unit::TestCase
|
|
161
198
|
if (is_output_dir) then options[:directory] = output_dir end
|
162
199
|
if date_time_format then options[:date_time_format] = date_time_format end
|
163
200
|
|
164
|
-
LogRotate.
|
201
|
+
result = LogRotate.rotate_file(file, options)
|
202
|
+
|
203
|
+
expected_result = get_expected_result_date_extension(iteration, base_name, date_times, count,
|
204
|
+
date_time_format, gzip, output_dir)
|
205
|
+
assert_equal(expected_result, result)
|
165
206
|
end
|
166
207
|
|
167
208
|
#verify there are count rotated files.
|
@@ -190,7 +231,7 @@ class LogRotateTest < Test::Unit::TestCase
|
|
190
231
|
end
|
191
232
|
end
|
192
233
|
|
193
|
-
|
234
|
+
teardown()
|
194
235
|
end
|
195
236
|
|
196
237
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logrotate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DesigningPatterns
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-08-27 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -40,9 +40,12 @@ files:
|
|
40
40
|
- README.txt
|
41
41
|
- Rakefile
|
42
42
|
- lib/logrotate.rb
|
43
|
+
- lib/logrotate/impl.rb
|
44
|
+
- lib/logrotate/logrotate.rb
|
45
|
+
- lib/logrotate/rotateinfo.rb
|
43
46
|
- test/test_logrotate.rb
|
44
47
|
has_rdoc: true
|
45
|
-
homepage:
|
48
|
+
homepage: This package is a library of methods that perform log rotation. The
|
46
49
|
post_install_message:
|
47
50
|
rdoc_options:
|
48
51
|
- --main
|