ops_script 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDQ4N2U3MDJmYjlkNTdhMzliZGMwYTE5MzAwYmQ4M2YzYTFhYmZmYw==
5
+ data.tar.gz: !binary |-
6
+ ZDUwYTY1ZDE0YTUyMjFmMGEzZTUwOWM2ZjgzNjM2ODY3Yjc1ODNiMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YjFhMzhmZWYzNmM3YzA3ZTkyZDU4MjI4NzA5ZDRhZDAwMTlmZWNkYzk0MzFk
10
+ NWMxNGY3YzEwYzBhMmQxZGY3ZjNlYjIzZjIxMzE3ZDAxNDBkYTI3ZjI3Nzlj
11
+ NWQ1ZGY0NTliMTAxODE3ZWU2ZGI3ODJiYzg5YzQxOGY4MmJjZjQ=
12
+ data.tar.gz: !binary |-
13
+ MzI4Y2FjZGFhMGI1ZDA0MTk0NmFkYzBjMGFhY2YwNTY2M2EwZTA1ZWM0ZGMz
14
+ NzU4YjBiZWViYzBmMTliODA1ZmM3MTllMjNhOTU5ZDhjY2UzNDE4NGMyNDk0
15
+ ZTI0MzU2NTIzMDI2YWExZGU0YjE4YjIyZTdlNzY2Nzk2NjJkOTI=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ops_script.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sanjiv Patel
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,37 @@
1
+ # OpsScript
2
+
3
+ Module to provide basic functionality common to many operations scripts. Specifically:
4
+ - Create and manage log file
5
+ - Use PID file to insure only one instance runs at a time
6
+ - Provide basic command line options such as --debug to log debug messages
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'ops_script'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install ops_script
21
+
22
+ ## Usage
23
+
24
+ require 'ops_script'
25
+ include OpsScript
26
+
27
+ OpsScript.run
28
+ log.info "This is my script running!"
29
+ end
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ module OpsScript
2
+ VERSION = "1.0.0"
3
+ end
data/lib/ops_script.rb ADDED
@@ -0,0 +1,172 @@
1
+ require "ops_script/version"
2
+
3
+ #
4
+ # This module provides some basic features common to many operational unix scripts
5
+ #
6
+ # * Log file
7
+ # * PID file used to insure only one instance is running, handy if running from cron multiple times
8
+ # * Options processing with basic options for overriding default log attributes and logfile and pidfile location
9
+ #
10
+ # == Example
11
+ #
12
+ # require 'ops_script'
13
+ # include OpsScript
14
+ # # Add your own custom options
15
+ # option_parser.on "--bar", "The bar option" do
16
+ # options[:bar] = true
17
+ # end
18
+ # # Run your code
19
+ # OpsScript.run do
20
+ # puts "BAR" if options[:bar]
21
+ # log.info "My informational message"
22
+ # log.debug "This is a debug message"
23
+ # # do some stuff
24
+ # end
25
+ #
26
+ # The module provides these options
27
+ # foo.rb -h
28
+ # Usage: foo.rb [options]
29
+ # --debug enable debug output
30
+ # --logfile log_file specify a different log file
31
+ # default is ../log/foo.log
32
+ # --log-retention retention Number of old log files to keep,
33
+ # or frequency of rotation (daily, weekly or monthly). Defaults to monthly.
34
+ # --log-size size Max size in bytes for logfile before rotating.
35
+ # Specify number of files to keep with log-retention option.
36
+ # --pidfile pid_file specify a different pid file
37
+ # default is ../tmp/foo.pid
38
+ # -h, -?, --help Display this help
39
+ #
40
+ #
41
+ module OpsScript
42
+
43
+ require 'date'
44
+ require 'fileutils'
45
+ require 'logger'
46
+ require 'optparse'
47
+
48
+ def script_basename
49
+ File.basename $0, '.*'
50
+ end
51
+ module_function :script_basename
52
+
53
+ def options
54
+ OpsScript.options
55
+ end
56
+
57
+ def self.options
58
+ @options ||= {}
59
+ end
60
+
61
+ def option_parser
62
+ OpsScript.option_parser
63
+ end
64
+
65
+ def self.option_parser
66
+ @option_parser ||= OptionParser.new
67
+ end
68
+
69
+ def process_options
70
+ options[:log_file] = File.expand_path("../../log/#{script_basename}.log", $0)
71
+ options[:pid_file] = File.expand_path("../../tmp/#{script_basename}.pid", $0)
72
+
73
+ option_parser.banner = "Usage: #{File.basename($0)} [options]"
74
+
75
+ option_parser.on_tail("-h","-?","--help", "Display this help") do
76
+ puts option_parser
77
+ exit
78
+ end
79
+
80
+ option_parser.on("--debug", "enable debug output") do
81
+ options[:debug] = true
82
+ end
83
+
84
+ option_parser.on("--logfile log_file", "specify a different log file", "default is #{options[:log_file]}") do |p|
85
+ options[:log_file] = p
86
+ end
87
+
88
+ option_parser.on("--log-retention retention", "Number of old log files to keep,",
89
+ "or frequency of rotation (daily, weekly or monthly). Defaults to monthly.") do |r|
90
+ options[:log_retention] = r
91
+ end
92
+
93
+ option_parser.on("--log-size size", "Max size in bytes for logfile before rotating.",
94
+ "Specify number of files to keep with log-retention option.") do |s|
95
+ options[:log_size] = s
96
+ end
97
+
98
+ option_parser.on_tail("-h","-?","--help", "Display this help") do
99
+ puts option_parser
100
+ exit
101
+ end
102
+
103
+ option_parser.on("--pidfile pid_file", "specify a different pid file", "default is #{options[:pid_file]}") do |p|
104
+ options[:pid_file] = p
105
+ end
106
+
107
+ option_parser.parse!
108
+ end
109
+ private :process_options
110
+
111
+ def create_directories
112
+ FileUtils.mkdir_p File.dirname(File.expand_path(options[:log_file]))
113
+ FileUtils.mkdir_p File.dirname(File.expand_path(options[:pid_file]))
114
+ end
115
+ private :create_directories
116
+
117
+ def already_running?
118
+ File.exists?(options[:pid_file])
119
+ end
120
+ private :already_running?
121
+
122
+ def create_pid
123
+ ::File.open(options[:pid_file], 'w' ){ |f| f.write("#{Process.pid}")}
124
+ end
125
+ private :create_pid
126
+
127
+ def remove_pid
128
+ File.unlink(options[:pid_file])
129
+ end
130
+ private :remove_pid
131
+
132
+ # Use log in your script to log messages
133
+ def log
134
+ OpsScript.log
135
+ end
136
+
137
+ def self.log
138
+ @log ||= Logger.new(options[:log_file], options[:log_retention] || 'monthly', options[:log_size])
139
+ end
140
+
141
+ def script
142
+ File.basename $0
143
+ end
144
+ private :script
145
+
146
+ def self.run(&block)
147
+ process_options
148
+ log.level = Logger::INFO unless options[:debug]
149
+ log.info "#{script} starting"
150
+ if already_running?
151
+ puts "Found #{options[:pid_file]} file. Must be running already. Exiting."
152
+ exit
153
+ else
154
+ create_directories
155
+ create_pid
156
+ end
157
+ begin
158
+ if block_given?
159
+ yield
160
+ else
161
+ raise "No block given. Nothing to run."
162
+ end
163
+ rescue => e
164
+ log.error e.message
165
+ log.debug e.backtrace.join('\n')
166
+ ensure
167
+ log.info "#{script} exiting"
168
+ log.close
169
+ remove_pid
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ops_script/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ops_script"
8
+ spec.version = OpsScript::VERSION
9
+ spec.authors = ["Sanjiv Patel"]
10
+ spec.email = ["sanjp2.0@gmail.com"]
11
+ spec.description = %q{Gem providing log, options, and pid file handling for operational scripts}
12
+ spec.summary = %q{Common features of operational scripts exctracted to a gem. Provides a log file, options processing and pid file with decent defaults.}
13
+ spec.homepage = "https://github.com/sanjp/ops_script"
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
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ops_script
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sanjiv Patel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Gem providing log, options, and pid file handling for operational scripts
42
+ email:
43
+ - sanjp2.0@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/ops_script.rb
54
+ - lib/ops_script/version.rb
55
+ - ops_script.gemspec
56
+ homepage: https://github.com/sanjp/ops_script
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.1
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Common features of operational scripts exctracted to a gem. Provides a log
80
+ file, options processing and pid file with decent defaults.
81
+ test_files: []