nginx_utils 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +9 -0
- data/lib/nginx_utils/version.rb +3 -0
- data/lib/nginx_utils.rb +190 -0
- data/nginx_utils.gemspec +25 -0
- data/spec/lib/nginx_utils_spec.rb +380 -0
- data/spec/spec_helper.rb +2 -0
- metadata +129 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 i2bskn
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# NginxUtils
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/i2bskn/nginx_utils.png?branch=master)](https://travis-ci.org/i2bskn/nginx_utils)
|
4
|
+
|
5
|
+
Nginx utilities.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'nginx_utils'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nginx_utils
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require 'nginx_utils'
|
25
|
+
```
|
26
|
+
|
27
|
+
Nginx log rotate:
|
28
|
+
|
29
|
+
```
|
30
|
+
params = {
|
31
|
+
debug: false,
|
32
|
+
script_log: "/tmp/nginx_rotate.log",
|
33
|
+
log_level: :debug,
|
34
|
+
root_dir: "/usr/local/nginx",
|
35
|
+
target_logs: "*.log",
|
36
|
+
prefix: Time.now.strftime("%Y%m%d%H%M%S"),
|
37
|
+
retention: 90,
|
38
|
+
pid_file: "/usr/local/nginx/logs/nginx.pid"
|
39
|
+
}
|
40
|
+
|
41
|
+
rotate = NginxUtils::Logrotate.new(params)
|
42
|
+
rotate.execute
|
43
|
+
```
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/nginx_utils.rb
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "nginx_utils/version"
|
4
|
+
require "logger"
|
5
|
+
|
6
|
+
module NginxUtils
|
7
|
+
class Logrotate
|
8
|
+
attr_accessor :logger, :rename_logs, :delete_logs
|
9
|
+
|
10
|
+
def initialize(options={})
|
11
|
+
# Debug
|
12
|
+
# debug: false => Not execute. Only output of logs to STDOUT.
|
13
|
+
# debug: true => Execute rotate logs Nginx. (default)
|
14
|
+
if options[:debug]
|
15
|
+
debug
|
16
|
+
else
|
17
|
+
@execute = true
|
18
|
+
|
19
|
+
# Script log file
|
20
|
+
# not specified => /tmp/nginx_rotate.log (default)
|
21
|
+
# script_log: false => Not output of logs.
|
22
|
+
# script_log: "/path/to/nginx_rotate.log" => /path/to/nginx_rotate.log
|
23
|
+
# script_log: STDOUT => STDOUT
|
24
|
+
set_logger options[:script_log]
|
25
|
+
|
26
|
+
# Script log level
|
27
|
+
# not specified => Logger::DEBUG (default)
|
28
|
+
# log_level: [:fatal|:error|:info|:warn]
|
29
|
+
set_log_level options[:log_level]
|
30
|
+
end
|
31
|
+
|
32
|
+
# Target logs
|
33
|
+
# Log of rename target is "#{root_dir}/**/#{target_logs}"
|
34
|
+
# Log of delete target is "#{root_dir}/**/#{target_logs}.*"
|
35
|
+
# Default parameters are as follows:
|
36
|
+
# - root_dir => /usr/local/nginx
|
37
|
+
# - target_logs => *.log
|
38
|
+
@root_dir = options[:root_dir] || "/usr/local/nginx"
|
39
|
+
@target_logs = options[:target_logs] || "*.log"
|
40
|
+
@rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
|
41
|
+
@delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
|
42
|
+
|
43
|
+
# Rename prefix
|
44
|
+
# Log of rename target to add the prefix.
|
45
|
+
# File name of the renamed after: "#{filename}.#{prefix}"
|
46
|
+
# Current time default. (YYYYmmddHHMMSS)
|
47
|
+
@prefix = options[:prefix] || Time.now.strftime("%Y%m%d%H%M%S")
|
48
|
+
|
49
|
+
# Retention period
|
50
|
+
# Delete log last modification time has passed the retention period.
|
51
|
+
# Specified unit of day.
|
52
|
+
dates = options[:retention] || 90
|
53
|
+
@retention = Time.now - (dates.to_i * 3600 * 24)
|
54
|
+
|
55
|
+
# PID file of Nginx
|
56
|
+
# The default is "#{root_dir}/logs/nginx.pid".
|
57
|
+
@pid_file = options[:pid_file] || File.join(@root_dir, "logs", "nginx.pid")
|
58
|
+
end
|
59
|
+
|
60
|
+
def config(options={})
|
61
|
+
# Debug
|
62
|
+
unless options[:debug].nil?
|
63
|
+
if options[:debug]
|
64
|
+
debug
|
65
|
+
else
|
66
|
+
debug false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Script log file
|
71
|
+
unless options[:script_log].nil?
|
72
|
+
set_logger options[:script_log]
|
73
|
+
end
|
74
|
+
|
75
|
+
# Script log level
|
76
|
+
unless options[:log_level].nil?
|
77
|
+
set_log_level options[:log_level]
|
78
|
+
end
|
79
|
+
|
80
|
+
# Target logs
|
81
|
+
reglog = false
|
82
|
+
unless options[:root_dir].nil?
|
83
|
+
@root_dir = options[:root_dir]
|
84
|
+
reglob = true
|
85
|
+
end
|
86
|
+
|
87
|
+
unless options[:target_logs].nil?
|
88
|
+
@target_logs = options[:target_logs]
|
89
|
+
reglob = true
|
90
|
+
end
|
91
|
+
|
92
|
+
if reglob
|
93
|
+
@rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
|
94
|
+
@delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
|
95
|
+
end
|
96
|
+
|
97
|
+
# Rename prefix
|
98
|
+
unless options[:prefix].nil?
|
99
|
+
@prefix = options[:prefix]
|
100
|
+
end
|
101
|
+
|
102
|
+
# Retention period
|
103
|
+
unless options[:retention].nil?
|
104
|
+
@retention = Time.now - (options[:retention].to_i * 3600 * 24)
|
105
|
+
end
|
106
|
+
|
107
|
+
# PID file of Nginx
|
108
|
+
unless options[:pid_file].nil?
|
109
|
+
@pid_file = options[:pid_file]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def rename
|
114
|
+
@rename_logs.each do |log|
|
115
|
+
after = "#{log}.#{@prefix}"
|
116
|
+
if File.exists? after
|
117
|
+
@logger.warn "File already exists: #{after}" if @logger
|
118
|
+
else
|
119
|
+
File.rename(log, after) if @execute
|
120
|
+
@logger.debug "Rename log file: #{log} to #{after}" if @logger
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def delete
|
126
|
+
@delete_logs.each do |log|
|
127
|
+
if File.stat(log).mtime < @retention
|
128
|
+
File.unlink(log) if @execute
|
129
|
+
@logger.debug "Delete log file: #{log}" if @logger
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def restart
|
135
|
+
if File.exists? @pid_file
|
136
|
+
cmd = "kill -USR1 `cat #{@pid_file}`"
|
137
|
+
@logger.debug "Nginx restart command: #{cmd}" if @logger
|
138
|
+
if @execute
|
139
|
+
if system(cmd)
|
140
|
+
@logger.info "Nginx restart is successfully!" if @logger
|
141
|
+
else
|
142
|
+
@logger.error "Nginx restart failed!" if @logger
|
143
|
+
raise "Nginx restart failed!" if @logger == false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
else
|
147
|
+
@logger.warn "Pid file is not found. not restart nginx. (#{@pid_file})" if @logger
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def execute
|
152
|
+
@logger.info "Nginx logrotate is started!" if @logger
|
153
|
+
rename
|
154
|
+
delete
|
155
|
+
@logger.info "Nginx logrotate is successfully!" if @logger
|
156
|
+
restart
|
157
|
+
end
|
158
|
+
|
159
|
+
private
|
160
|
+
def debug(set=true)
|
161
|
+
if set
|
162
|
+
@execute = false
|
163
|
+
@logger = Logger.new(STDOUT)
|
164
|
+
else
|
165
|
+
@execute = true
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def set_logger(log)
|
170
|
+
case log
|
171
|
+
when nil then @logger = Logger.new("/tmp/nginx_rotate.log")
|
172
|
+
when false then @logger = false
|
173
|
+
else @logger = Logger.new(log)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def set_log_level(level)
|
178
|
+
if @logger
|
179
|
+
case level
|
180
|
+
when :fatal then @logger.level = Logger::FATAL
|
181
|
+
when :error then @logger.level = Logger::ERROR
|
182
|
+
when :warn then @logger.level = Logger::WARN
|
183
|
+
when :info then @logger.level = Logger::INFO
|
184
|
+
when :debug then @logger.level = Logger::DEBUG
|
185
|
+
else @logger.level = Logger::DEBUG
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
data/nginx_utils.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nginx_utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nginx_utils"
|
8
|
+
spec.version = NginxUtils::VERSION
|
9
|
+
spec.authors = ["i2bskn"]
|
10
|
+
spec.email = ["i2bskn@gmail.com"]
|
11
|
+
spec.description = %q{The various utilities on nginx}
|
12
|
+
spec.summary = %q{Nginx utilities}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "fakefs"
|
25
|
+
end
|
@@ -0,0 +1,380 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe NginxUtils do
|
6
|
+
include FakeFS::SpecHelpers
|
7
|
+
|
8
|
+
def create_files(options={retention: 90})
|
9
|
+
root_dir = options[:root_dir] || "/usr/local/nginx/logs"
|
10
|
+
FileUtils.mkdir_p root_dir
|
11
|
+
not_del = Time.now - ((options[:retention].to_i - 1) * 3600 * 24)
|
12
|
+
do_del = Time.now - ((options[:retention].to_i + 1) * 3600 * 24)
|
13
|
+
not_del_file = "access.log.#{not_del.strftime('%Y%m%d%H%M%S')}"
|
14
|
+
do_del_file = "access.log.#{do_del.strftime('%Y%m%d%H%M%S')}"
|
15
|
+
files = [
|
16
|
+
"access.log",
|
17
|
+
"error.log",
|
18
|
+
"nginx.pid",
|
19
|
+
not_del_file,
|
20
|
+
do_del_file
|
21
|
+
]
|
22
|
+
files.each{|f| File.open(File.join(root_dir, f), "w").close}
|
23
|
+
File.utime(not_del, not_del, File.join(root_dir, not_del_file))
|
24
|
+
File.utime(do_del, do_del, File.join(root_dir, do_del_file))
|
25
|
+
{
|
26
|
+
not_del_file: File.join(root_dir, not_del_file),
|
27
|
+
do_del_file: File.join(root_dir, do_del_file)
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
before(:each) do
|
32
|
+
FileUtils.mkdir_p "/tmp"
|
33
|
+
@script_log = File.open("/tmp/nginx_rotate.log", "a")
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "Logrotate" do
|
37
|
+
describe "#initialize" do
|
38
|
+
before(:each) do
|
39
|
+
@time_now = Time.now
|
40
|
+
Time.stub!(:now).and_return(@time_now)
|
41
|
+
end
|
42
|
+
|
43
|
+
context "with default params" do
|
44
|
+
before(:each) do
|
45
|
+
create_files
|
46
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "@execute should be true" do
|
50
|
+
expect(@rotate.instance_eval{@execute}).to eq(true)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "@logger should be a instance of Logger class" do
|
54
|
+
expect(@rotate.instance_eval{@logger}.is_a? Logger).to eq(true)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "@logger.level should be debug(0)" do
|
58
|
+
expect(@rotate.instance_eval{@logger}.level).to eq(Logger::DEBUG)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "@root_dir should be default install prefix of Nginx" do
|
62
|
+
expect(@rotate.instance_eval{@root_dir}).to eq("/usr/local/nginx")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "@target logs should be *.log" do
|
66
|
+
expect(@rotate.instance_eval{@target_logs}).to eq("*.log")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "@rename_logs size should be 2" do
|
70
|
+
expect(@rotate.instance_eval{@rename_logs}.size).to eq(2)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "@delete_logs size should be 2" do
|
74
|
+
expect(@rotate.instance_eval{@delete_logs}.size).to eq(2)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "@prefix should be a some as @time_now" do
|
78
|
+
expect(@rotate.instance_eval{@prefix}).to eq(@time_now.strftime("%Y%m%d%H%M%S"))
|
79
|
+
end
|
80
|
+
|
81
|
+
it "@retention should be 90 days" do
|
82
|
+
expect(@rotate.instance_eval{@retention}).to eq(@time_now - (90 * 3600 * 24))
|
83
|
+
end
|
84
|
+
|
85
|
+
it "@pid_file should be /usr/local/nginx/logs/nginx.pid" do
|
86
|
+
expect(@rotate.instance_eval{@pid_file}).to eq("/usr/local/nginx/logs/nginx.pid")
|
87
|
+
end
|
88
|
+
|
89
|
+
it "@rename_logs should be created" do
|
90
|
+
expect(@rotate.instance_eval{@rename_logs}).not_to eq(nil)
|
91
|
+
end
|
92
|
+
|
93
|
+
it "@delete_logs should be created" do
|
94
|
+
expect(@rotate.instance_eval{@delete_logs}).not_to eq(nil)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "with debug" do
|
99
|
+
before(:each) do
|
100
|
+
@rotate = NginxUtils::Logrotate.new(debug: true, script_log: @script_log)
|
101
|
+
end
|
102
|
+
|
103
|
+
it "@execute should be false" do
|
104
|
+
expect(@rotate.instance_eval{@execute}).to eq(false)
|
105
|
+
end
|
106
|
+
|
107
|
+
it "@logger should be a instance of Logger class" do
|
108
|
+
expect(@rotate.instance_eval{@logger}.is_a? Logger).to eq(true)
|
109
|
+
end
|
110
|
+
|
111
|
+
it "other parameters should be default" do
|
112
|
+
expect(@rotate.instance_eval{@logger}.level).to eq(Logger::DEBUG)
|
113
|
+
expect(@rotate.instance_eval{@root_dir}).to eq("/usr/local/nginx")
|
114
|
+
expect(@rotate.instance_eval{@target_logs}).to eq("*.log")
|
115
|
+
expect(@rotate.instance_eval{@prefix}).to eq(@time_now.strftime("%Y%m%d%H%M%S"))
|
116
|
+
expect(@rotate.instance_eval{@retention}).to eq(@time_now - (90 * 3600 * 24))
|
117
|
+
expect(@rotate.instance_eval{@pid_file}).to eq("/usr/local/nginx/logs/nginx.pid")
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context "with custom params" do
|
122
|
+
it "@logger should be false if script_log is false" do
|
123
|
+
rotate = NginxUtils::Logrotate.new(script_log: false)
|
124
|
+
expect(rotate.instance_eval{@logger}).to eq(false)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "@logger.level should be a specified level" do
|
128
|
+
rotate = NginxUtils::Logrotate.new(log_level: :warn, script_log: @script_log)
|
129
|
+
expect(rotate.instance_eval{@logger}.level).to eq(Logger::WARN)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "@root_dir should be a specified parameter" do
|
133
|
+
rotate = NginxUtils::Logrotate.new(root_dir: "/var/log/nginx", script_log: @script_log)
|
134
|
+
expect(rotate.instance_eval{@root_dir}).to eq("/var/log/nginx")
|
135
|
+
end
|
136
|
+
|
137
|
+
it "@prefix should be a specified parameter" do
|
138
|
+
rotate = NginxUtils::Logrotate.new(prefix: "20130518", script_log: @script_log)
|
139
|
+
expect(rotate.instance_eval{@prefix}).to eq("20130518")
|
140
|
+
end
|
141
|
+
|
142
|
+
it "@retention should be a specified period" do
|
143
|
+
rotate = NginxUtils::Logrotate.new(retention: 30, script_log: @script_log)
|
144
|
+
expect(rotate.instance_eval{@retention}).to eq(@time_now - (30 * 3600 * 24))
|
145
|
+
end
|
146
|
+
|
147
|
+
it "@pid_file should be a specified parameter" do
|
148
|
+
rotate = NginxUtils::Logrotate.new(pid_file: "/var/run/nginx.pid", script_log: @script_log)
|
149
|
+
expect(rotate.instance_eval{@pid_file}).to eq("/var/run/nginx.pid")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "#config" do
|
155
|
+
before(:each) do
|
156
|
+
@time_now = Time.now
|
157
|
+
Time.stub!(:now).and_return(@time_now)
|
158
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
159
|
+
end
|
160
|
+
|
161
|
+
it "@execute should be a false if debug is true" do
|
162
|
+
old_execute = @rotate.instance_eval{@execute}
|
163
|
+
@rotate.config debug: true
|
164
|
+
expect(@rotate.instance_eval{@execute}).to eq(false)
|
165
|
+
expect(@rotate.instance_eval{@execute}).not_to eq(old_execute)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "@execute should be a true if debug is false" do
|
169
|
+
rotate = NginxUtils::Logrotate.new(debug: true)
|
170
|
+
old_execute = rotate.instance_eval{@execute}
|
171
|
+
rotate.config debug: false
|
172
|
+
expect(rotate.instance_eval{@execute}).to eq(true)
|
173
|
+
expect(rotate.instance_eval{@execute}).not_to eq(old_execute)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "@logger should be a false if script_log is false" do
|
177
|
+
old_logger = @rotate.instance_eval{@logger}
|
178
|
+
@rotate.config script_log: false
|
179
|
+
expect(@rotate.instance_eval{@logger}).to eq(false)
|
180
|
+
expect(@rotate.instance_eval{@logger}).not_to eq(old_logger)
|
181
|
+
end
|
182
|
+
|
183
|
+
it "@logger.level should be a specified level" do
|
184
|
+
old_level = @rotate.instance_eval{@logger}.level
|
185
|
+
@rotate.config log_level: :warn
|
186
|
+
expect(@rotate.instance_eval{@logger}.level).to eq(Logger::WARN)
|
187
|
+
expect(@rotate.instance_eval{@logger}.level).not_to eq(old_level)
|
188
|
+
end
|
189
|
+
|
190
|
+
it "@root_dir should be a specified parameter" do
|
191
|
+
old_root_dir = @rotate.instance_eval{@root_dir}
|
192
|
+
@rotate.config root_dir: "/var/log/nginx"
|
193
|
+
expect(@rotate.instance_eval{@root_dir}).to eq("/var/log/nginx")
|
194
|
+
expect(@rotate.instance_eval{@root_dir}).not_to eq(old_root_dir)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "@target_logs should be a specified parameter" do
|
198
|
+
old_target_logs = @rotate.instance_eval{@target_logs}
|
199
|
+
@rotate.config target_logs: "*_log"
|
200
|
+
expect(@rotate.instance_eval{@target_logs}).to eq("*_log")
|
201
|
+
expect(@rotate.instance_eval{@target_logs}).not_to eq(old_target_logs)
|
202
|
+
end
|
203
|
+
|
204
|
+
it "@prefix should be a specified parameter" do
|
205
|
+
old_prefix = @rotate.instance_eval{@prefix}
|
206
|
+
@rotate.config prefix: "20130518"
|
207
|
+
expect(@rotate.instance_eval{@prefix}).to eq("20130518")
|
208
|
+
expect(@rotate.instance_eval{@prefix}).not_to eq(old_prefix)
|
209
|
+
end
|
210
|
+
|
211
|
+
it "@retention should be a specified period" do
|
212
|
+
old_retention = @rotate.instance_eval{@retention}
|
213
|
+
@rotate.config retention: 30
|
214
|
+
expect(@rotate.instance_eval{@retention}).to eq(@time_now - (30 * 3600 * 24))
|
215
|
+
expect(@rotate.instance_eval{@retention}).not_to eq(old_retention)
|
216
|
+
end
|
217
|
+
|
218
|
+
it "@pid_file should be a specified parameter" do
|
219
|
+
old_pid_file = @rotate.instance_eval{@pid_file}
|
220
|
+
@rotate.config pid_file: "/var/run/nginx.pid"
|
221
|
+
expect(@rotate.instance_eval{@pid_file}).to eq("/var/run/nginx.pid")
|
222
|
+
expect(@rotate.instance_eval{@pid_file}).not_to eq(old_pid_file)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "#rename" do
|
227
|
+
before(:each) do
|
228
|
+
create_files
|
229
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
230
|
+
end
|
231
|
+
|
232
|
+
it "rename target logs" do
|
233
|
+
prefix = Time.now.strftime('%Y%m%d%H%M%S')
|
234
|
+
@rotate.config prefix: prefix
|
235
|
+
@rotate.rename
|
236
|
+
@rotate.rename_logs.each do |log|
|
237
|
+
expect(File.exists? log).to eq(false)
|
238
|
+
expect(File.exists? "#{log}.#{prefix}").to eq(true)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
it "output log file" do
|
243
|
+
@rotate.rename
|
244
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
245
|
+
expect(log_lines.size).to eq(@rotate.rename_logs.size)
|
246
|
+
end
|
247
|
+
|
248
|
+
it "do not rename if a file with the same name exists" do
|
249
|
+
prefix = Time.now.strftime('%Y%m%d%H%M%S')
|
250
|
+
@rotate.config prefix: prefix
|
251
|
+
File.open("#{@rotate.rename_logs.first}.#{prefix}", "w").close
|
252
|
+
@rotate.rename
|
253
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
254
|
+
expect(File.exists? @rotate.rename_logs.first).to eq(true)
|
255
|
+
expect(log_lines.select{|line| /File already exists/ =~ line}.size).to eq(1)
|
256
|
+
end
|
257
|
+
|
258
|
+
it "do not rename if not executable" do
|
259
|
+
prefix = Time.now.strftime('%Y%m%d%H%M%S')
|
260
|
+
@rotate.config debug: true, script_log: false, prefix: prefix
|
261
|
+
@rotate.rename
|
262
|
+
@rotate.rename_logs.each do |log|
|
263
|
+
expect(File.exists? log).to eq(true)
|
264
|
+
expect(File.exists? "#{log}.#{prefix}").to eq(false)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
it "do not output log if script_log is false" do
|
269
|
+
@rotate.config script_log: false
|
270
|
+
@rotate.rename
|
271
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
272
|
+
expect(log_lines.size).to eq(0)
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
describe "#delete" do
|
277
|
+
before(:each) do
|
278
|
+
@time_now = Time.now
|
279
|
+
Time.stub!(:now).and_return(@time_now)
|
280
|
+
@retention = @time_now - (90 * 3600 * 24)
|
281
|
+
@created = create_files
|
282
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
283
|
+
end
|
284
|
+
|
285
|
+
it "delete target logs" do
|
286
|
+
@rotate.delete
|
287
|
+
expect(File.exists? @created[:do_del_file]).to eq(false)
|
288
|
+
expect(File.exists? @created[:not_del_file]).to eq(true)
|
289
|
+
end
|
290
|
+
|
291
|
+
it "output log file" do
|
292
|
+
@rotate.delete
|
293
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
294
|
+
expect(log_lines.size).to eq(1)
|
295
|
+
end
|
296
|
+
|
297
|
+
it "do not delete if not executable" do
|
298
|
+
@rotate.config debug: true, script_log: false
|
299
|
+
@rotate.delete
|
300
|
+
expect(File.exists? @created[:do_del_file]).to eq(true)
|
301
|
+
end
|
302
|
+
|
303
|
+
it "do not output log if script_log is false" do
|
304
|
+
@rotate.config script_log: false
|
305
|
+
@rotate.delete
|
306
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
307
|
+
expect(log_lines.size).to eq(0)
|
308
|
+
end
|
309
|
+
end
|
310
|
+
|
311
|
+
describe "#restart" do
|
312
|
+
before(:each) do
|
313
|
+
@command = "kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`"
|
314
|
+
create_files
|
315
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
316
|
+
end
|
317
|
+
|
318
|
+
it "should execute command" do
|
319
|
+
Object.any_instance.should_receive(:system).with(@command).and_return(true)
|
320
|
+
@rotate.restart
|
321
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
322
|
+
expect(log_lines.select{|line| /Nginx restart is successfully!/ =~ line}.size).to eq(1)
|
323
|
+
end
|
324
|
+
|
325
|
+
it "do not execute command if not exists pid file" do
|
326
|
+
File.stub!(:exists?).and_return(false)
|
327
|
+
@rotate.restart
|
328
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
329
|
+
expect(log_lines.select{|line| /Pid file is not found/ =~ line}.size).to eq(1)
|
330
|
+
end
|
331
|
+
|
332
|
+
it "do not execute commando if not executable" do
|
333
|
+
@rotate.config debug: true, script_log: @script_log
|
334
|
+
@rotate.restart
|
335
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
336
|
+
expect(log_lines.size).to eq(1)
|
337
|
+
end
|
338
|
+
|
339
|
+
it "should outputs error log if it fails" do
|
340
|
+
Object.any_instance.should_receive(:system).with(@command).and_return(false)
|
341
|
+
@rotate.restart
|
342
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
343
|
+
expect(log_lines.select{|line| /Nginx restart failed!/ =~ line}.size).to eq(1)
|
344
|
+
end
|
345
|
+
|
346
|
+
it "should generate an exception if it fails" do
|
347
|
+
@rotate.config script_log: false
|
348
|
+
Object.any_instance.should_receive(:system).with(@command).and_return(false)
|
349
|
+
expect(proc{@rotate.restart}).to raise_error
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
describe "#execute" do
|
354
|
+
before(:each) do
|
355
|
+
NginxUtils::Logrotate.any_instance.should_receive(:rename).and_return(true)
|
356
|
+
NginxUtils::Logrotate.any_instance.should_receive(:delete).and_return(true)
|
357
|
+
NginxUtils::Logrotate.any_instance.should_receive(:restart).and_return(true)
|
358
|
+
create_files
|
359
|
+
@rotate = NginxUtils::Logrotate.new(script_log: @script_log)
|
360
|
+
end
|
361
|
+
|
362
|
+
it "should call methods" do
|
363
|
+
@rotate.execute
|
364
|
+
end
|
365
|
+
|
366
|
+
it "output log file" do
|
367
|
+
@rotate.execute
|
368
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
369
|
+
expect(log_lines.size).to eq(2)
|
370
|
+
end
|
371
|
+
|
372
|
+
it "do not output log if script_log is false" do
|
373
|
+
@rotate.config script_log: false
|
374
|
+
@rotate.execute
|
375
|
+
log_lines = File.open("/tmp/nginx_rotate.log").read.split("\n")
|
376
|
+
expect(log_lines.size).to eq(0)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nginx_utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- i2bskn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakefs
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: The various utilities on nginx
|
79
|
+
email:
|
80
|
+
- i2bskn@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .travis.yml
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/nginx_utils.rb
|
92
|
+
- lib/nginx_utils/version.rb
|
93
|
+
- nginx_utils.gemspec
|
94
|
+
- spec/lib/nginx_utils_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
homepage: ''
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: -1747986619663830752
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
hash: -1747986619663830752
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.23
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Nginx utilities
|
127
|
+
test_files:
|
128
|
+
- spec/lib/nginx_utils_spec.rb
|
129
|
+
- spec/spec_helper.rb
|