rinit 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +8 -0
- data/TODO +4 -0
- data/bin/rinit +42 -0
- data/examples/init +42 -0
- data/examples/monit +8 -0
- data/lib/rinit.rb +8 -0
- data/lib/rinit/commands.rb +41 -0
- data/lib/rinit/exceptions.rb +4 -0
- data/lib/rinit/process_utils.rb +80 -0
- data/lib/rinit/template_builder.rb +38 -0
- data/lib/rinit/version.rb +3 -0
- data/rinit.gemspec +27 -0
- data/test/rinit_commands_test.rb +52 -0
- data/test/support/foo_daemon.rb +8 -0
- data/test/support/sample_init.rb +41 -0
- data/test/template_builder_test.rb +29 -0
- data/test/test_helper.rb +4 -0
- metadata +156 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ea0acfa0eb74979cb10df4d21486576f600304e2
|
4
|
+
data.tar.gz: 5b130fbf83ac7b8b82955c6be348a86a6a88d36c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 389a152befef38018ca332c82fbbcc8527febaada6fc4c84784a889e989f46f2f2df0ea00d98de0a5be321498922a3a18e7b93d5b51f8d8d308b4b4981bc7e1d
|
7
|
+
data.tar.gz: 571ba2186bab9183b14cb484ca549de3e3d2b746d083e389328a5d4828cd5dfaed089fc3db0674e3aece20ef3e0872de4c7cd6b4c5324be9de136468432abca8
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
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/foo_daemon.rb
|
17
|
+
test/test_init.rb
|
18
|
+
test/version_tmp
|
19
|
+
tmp
|
20
|
+
tags
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 YOUR NAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Brad Smith
|
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,120 @@
|
|
1
|
+
# Rinit
|
2
|
+
|
3
|
+
Rinit is a `init-like` script witten in ruby.
|
4
|
+
|
5
|
+
### This only works for Debian based systems currently.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'rinit'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install rinit
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Let's say you have a daemon script called `foodaemon.rb`.
|
24
|
+
You want to be able to `start, stop, restart, and get the status` of said daemon.
|
25
|
+
Better yet, you need a pid file so you can use monit to monitor the process.
|
26
|
+
Create a file in `/etc/init.d/` (or where ever you like). Name it what you want and make sure it is executable.
|
27
|
+
|
28
|
+
### your awesome damon script
|
29
|
+
```ruby
|
30
|
+
loop do
|
31
|
+
puts "I am a daemon...and I loop loop loop and I loop loop loop!"
|
32
|
+
sleep 10
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
### a init script to control said awesomeness
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
#!/usr/bin/env ruby
|
40
|
+
require 'rinit'
|
41
|
+
|
42
|
+
extend Rinit
|
43
|
+
# app_name This is a startup script for use in /etc/init.d
|
44
|
+
#
|
45
|
+
# description: This will start, stop, and restart foo daemon
|
46
|
+
|
47
|
+
# this is an example of using ruby for a init script
|
48
|
+
|
49
|
+
APP_NAME = "foodaemon"
|
50
|
+
PIDFILE = "/tmp/#{APP_NAME}.pid"
|
51
|
+
PATH = "/home/bradleyd/Projects/rinit/test/"
|
52
|
+
DAEMON = "#{PATH}/foo_daemon.rb"
|
53
|
+
USER = ENV["USER"]
|
54
|
+
|
55
|
+
case ARGV.first
|
56
|
+
when 'status'
|
57
|
+
puts "#{APP_NAME} is #{Rinit.status(PIDFILE)}"
|
58
|
+
when 'start'
|
59
|
+
result = Rinit.start :cmd => "#{DAEMON}",
|
60
|
+
:chuid => USER,
|
61
|
+
:pidfile => PIDFILE
|
62
|
+
|
63
|
+
puts "#{APP_NAME} is started" unless result.nil?
|
64
|
+
when 'stop'
|
65
|
+
Rinit.stop(PIDFILE)
|
66
|
+
puts "#{APP_NAME} is stopped"
|
67
|
+
when 'restart'
|
68
|
+
puts "#{APP_NAME} is restarting..."
|
69
|
+
Rinit.restart(PIDFILE, :cmd => "#{DAEMON}",
|
70
|
+
:chuid => USER,
|
71
|
+
:pidfile => PIDFILE)
|
72
|
+
end
|
73
|
+
|
74
|
+
unless %w{start stop restart status}.include? ARGV.first
|
75
|
+
puts "Usage: #{APP_NAME} {start|stop|restart}"
|
76
|
+
exit
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
Then you can do this
|
81
|
+
|
82
|
+
```bash
|
83
|
+
./foo status
|
84
|
+
foo is stoppped
|
85
|
+
```
|
86
|
+
|
87
|
+
```bash
|
88
|
+
./foo start
|
89
|
+
foo is running
|
90
|
+
```
|
91
|
+
You get the idea...
|
92
|
+
|
93
|
+
### Simple Template Builder
|
94
|
+
|
95
|
+
`rinit` also has a simple template builder. For example,
|
96
|
+
to generate a init script to be used in `init.d/` one could do this
|
97
|
+
|
98
|
+
* only monit and init templates are supported as of now
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
-t, --template type generator for init template
|
102
|
+
-n, --daemon-name name name of the dameon
|
103
|
+
-h, --help Show this message
|
104
|
+
```
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
rinit -t init -n foobar
|
108
|
+
```
|
109
|
+
|
110
|
+
This will create a file in the current directory where you ran `rinit`
|
111
|
+
|
112
|
+
* your milage may vary
|
113
|
+
|
114
|
+
## Contributing
|
115
|
+
|
116
|
+
1. Fork it
|
117
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
118
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
119
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
120
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/TODO
ADDED
data/bin/rinit
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
|
4
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
|
5
|
+
require "bundler/setup"
|
6
|
+
require 'rinit'
|
7
|
+
require 'optparse'
|
8
|
+
|
9
|
+
options = {}
|
10
|
+
banner = nil
|
11
|
+
|
12
|
+
# set the default option is none are given
|
13
|
+
#
|
14
|
+
OptionParser.new do |opts|
|
15
|
+
opts.banner = "Usage: #{__FILE__} [options]"
|
16
|
+
|
17
|
+
opts.on("-t ", "--template type", "generator for init template") do |t|
|
18
|
+
options[:template] = t
|
19
|
+
end
|
20
|
+
|
21
|
+
opts.on("-n ", "--daemon-name name", "name of the dameon") do |n|
|
22
|
+
options[:name] = n
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
opts.on_tail("-h", "--help", "Show this message") do
|
28
|
+
puts opts
|
29
|
+
exit
|
30
|
+
end
|
31
|
+
#set current options
|
32
|
+
banner = opts
|
33
|
+
|
34
|
+
end.parse!
|
35
|
+
|
36
|
+
if options.empty?
|
37
|
+
puts banner
|
38
|
+
exit
|
39
|
+
end
|
40
|
+
|
41
|
+
template = Rinit::TemplateBuilder.new(options)
|
42
|
+
template.build
|
data/examples/init
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rinit'
|
3
|
+
|
4
|
+
extend Rinit
|
5
|
+
|
6
|
+
# app_name This is a startup script for use in /etc/init.d
|
7
|
+
#
|
8
|
+
# description: This will start, stop, and restart <%= @name %>
|
9
|
+
|
10
|
+
# this is an example of using ruby for a init script
|
11
|
+
|
12
|
+
APP_NAME = <%= @name %>
|
13
|
+
PIDFILE = "/tmp/#{APP_NAME}.pid"
|
14
|
+
# change PATH to point at your daemon path
|
15
|
+
PATH = File.expand_path File.dirname(__FILE__)
|
16
|
+
DAEMON = File.join(PATH, <%= @name %>)
|
17
|
+
# @todo change this to the user you want the daemon to run as
|
18
|
+
USER = ENV["USER"]
|
19
|
+
|
20
|
+
case ARGV.first
|
21
|
+
when "status"
|
22
|
+
puts "#{APP_NAME} is #{Rinit.status(PIDFILE) ? "running" : "stopped"}"
|
23
|
+
when "start"
|
24
|
+
result = Rinit.start(cmd: DAEMON,
|
25
|
+
chuid: USER,
|
26
|
+
pidfile: PIDFILE)
|
27
|
+
|
28
|
+
puts "#{APP_NAME} has started" unless result.nil?
|
29
|
+
when "stop"
|
30
|
+
Rinit.stop(PIDFILE)
|
31
|
+
puts "#{APP_NAME} is stopped"
|
32
|
+
when "restart"
|
33
|
+
puts "#{APP_NAME} is restarting..."
|
34
|
+
Rinit.restart(PIDFILE, cmd: DAEMON,
|
35
|
+
chuid: USER,
|
36
|
+
pidfile: PIDFILE)
|
37
|
+
end
|
38
|
+
|
39
|
+
unless %w{start stop restart status}.include? ARGV.first
|
40
|
+
puts "Usage: #{APP_NAME} {start|stop|restart}"
|
41
|
+
exit
|
42
|
+
end
|
data/examples/monit
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
check process <%= @name %>
|
2
|
+
with pidfile /var/run/<%= @name %>.pid
|
3
|
+
start program = "/etc/init.d/<%= @name %> start"
|
4
|
+
stop program = "/etc/init.d/<%= @name %> stop"
|
5
|
+
if cpu > 60% for 2 cycles then alert
|
6
|
+
if failed host 127.0.0.1 port 3000 protocol http
|
7
|
+
then alert
|
8
|
+
|
data/lib/rinit.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "open4"
|
2
|
+
module Rinit
|
3
|
+
class << self
|
4
|
+
include ProcessUtils
|
5
|
+
|
6
|
+
# @param opts [Hash] opts :cmd, :chuid, :pidfile
|
7
|
+
# @return [nil]
|
8
|
+
# @example
|
9
|
+
# {cmd: "/tmp/foo_daemon.rb", chuid: "foo", pidfile: "/tmp/foo.pid"}
|
10
|
+
#
|
11
|
+
def start(opts={})
|
12
|
+
command = opts.fetch(:cmd) { raise Rinit::CommandException.new "No command given" }
|
13
|
+
user = opts.fetch(:chuid) { raise Rinit::CommandException.new "No user given" }
|
14
|
+
pidfile = opts.fetch(:pidfile) { raise Rinit::CommandException.new "No pidfile was given" }
|
15
|
+
may_the_fork_be_with_you(command, pidfile)
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param pidfile [String] the full pidfile path
|
19
|
+
# @return [nil] if there were not any errors
|
20
|
+
def stop(pidfile)
|
21
|
+
kill_process(pidfile)
|
22
|
+
pidfile_cleanup(pidfile)
|
23
|
+
end
|
24
|
+
|
25
|
+
# @param pidfile [String] the full pidfile path
|
26
|
+
# @return process_status [String] Started or Stopped
|
27
|
+
def status pidfile
|
28
|
+
is_process_running?(pidfile)
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param pidfile [String] the full pidfile path
|
32
|
+
# @param opts [Hash] see #start
|
33
|
+
# @return process_status [String] Started or Stopped
|
34
|
+
# @example
|
35
|
+
# "/tmp/foo.pid", {cmd: "/tmp/foo_daemon.rb", chuid: "foo", pidfile: "/tmp/foo.pid"}
|
36
|
+
def restart(pidfile, opts={})
|
37
|
+
stop(pidfile)
|
38
|
+
start(opts)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require "sys/proctable"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
module Rinit
|
5
|
+
module ProcessUtils
|
6
|
+
include Sys
|
7
|
+
|
8
|
+
# @private
|
9
|
+
def may_the_fork_be_with_you(command, pidfile)
|
10
|
+
rd, wr = IO.pipe
|
11
|
+
pid = fork do
|
12
|
+
rd.close
|
13
|
+
begin
|
14
|
+
exec(command)
|
15
|
+
rescue SystemCallError
|
16
|
+
wr.write('!')
|
17
|
+
exit 1
|
18
|
+
end
|
19
|
+
end
|
20
|
+
wr.close
|
21
|
+
command_result = if rd.eof?
|
22
|
+
write_pidfile(pid, pidfile)
|
23
|
+
else
|
24
|
+
nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @private
|
29
|
+
def get_pid_from_file(filename)
|
30
|
+
begin
|
31
|
+
pid = IO.readlines(filename)
|
32
|
+
rescue Errno::ENOENT => e
|
33
|
+
puts e.message + "---Are you sure it is running?"
|
34
|
+
exit 1
|
35
|
+
end
|
36
|
+
# if the file is there but no pid was written to_i returns 0
|
37
|
+
pid.empty? ? -1 : pid[0].to_i
|
38
|
+
end
|
39
|
+
|
40
|
+
# @private
|
41
|
+
def is_process_running?(pidfile)
|
42
|
+
pid = get_pid_from_file(pidfile)
|
43
|
+
ProcTable.ps{ |process|
|
44
|
+
return true if process.pid == pid
|
45
|
+
}
|
46
|
+
false
|
47
|
+
end
|
48
|
+
|
49
|
+
# @private
|
50
|
+
def pidfile_cleanup pidfile
|
51
|
+
begin
|
52
|
+
FileUtils.rm(pidfile)
|
53
|
+
rescue Errno::ENOENT => e
|
54
|
+
puts e.message
|
55
|
+
exit 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# @private
|
60
|
+
def kill_process(pidfile)
|
61
|
+
pid = get_pid_from_file(pidfile)
|
62
|
+
# should never be negative unless there is an issue with writing the pid
|
63
|
+
return if pid < 0
|
64
|
+
begin
|
65
|
+
Process.kill(9,pid)
|
66
|
+
rescue Errno::ESRCH => e
|
67
|
+
puts e.message
|
68
|
+
puts "The pid file may not have been cleaned up properly."
|
69
|
+
exit 1
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# @private
|
74
|
+
def write_pidfile(pid, pidfile)
|
75
|
+
File.open(pidfile, 'w') { |f| f.write(pid) }
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "erb"
|
2
|
+
module Rinit
|
3
|
+
# simple template generator
|
4
|
+
#
|
5
|
+
# @note only supports init and monit right now
|
6
|
+
class TemplateBuilder
|
7
|
+
|
8
|
+
def initialize(opts={})
|
9
|
+
@template = opts.fetch(:template) { 'init' }
|
10
|
+
@name = opts.fetch(:name) { 'foobar' }
|
11
|
+
end
|
12
|
+
|
13
|
+
# @params nil [nil]
|
14
|
+
# @return [nil]
|
15
|
+
def build
|
16
|
+
@erb_temp = ERB.new(File.read(template_file)) #.result(binding)
|
17
|
+
write_out_file
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
# @private
|
22
|
+
def write_out_file
|
23
|
+
File.open(example_name, 'w') do |f|
|
24
|
+
f.write @erb_temp.result(binding)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# @private
|
29
|
+
def example_name
|
30
|
+
"#{@name}.#{@template}"
|
31
|
+
end
|
32
|
+
|
33
|
+
# @private
|
34
|
+
def template_file
|
35
|
+
File.join(File.expand_path(File.dirname(__FILE__)), "../../", "examples", @template)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/rinit.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rinit/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rinit"
|
8
|
+
spec.version = Rinit::VERSION
|
9
|
+
spec.authors = ["Bradley Smith"]
|
10
|
+
spec.email = ["bradleydsmith@gmail.com"]
|
11
|
+
spec.description = %q{ Provides init-like script structure for ruby}
|
12
|
+
spec.summary = %q{ Provides init-like script structure for ruby so you can control your daemons. }
|
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_dependency "sys-proctable"
|
22
|
+
spec.add_dependency "open4"
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "mocha"
|
26
|
+
spec.add_development_dependency "minitest"
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
require 'tempfile'
|
3
|
+
require 'sys/proctable'
|
4
|
+
require "ostruct"
|
5
|
+
|
6
|
+
class RinitCommandsTest < MiniTest::Unit::TestCase
|
7
|
+
extend Rinit
|
8
|
+
include Sys
|
9
|
+
|
10
|
+
DAEMON = File.join(File.expand_path(File.dirname(__FILE__)), "support", "foo_daemon.rb")
|
11
|
+
USER = ENV["USER"]
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@rinit = Rinit
|
15
|
+
@pidfile = Tempfile.new("foo.pid")
|
16
|
+
@pidfile.rewind
|
17
|
+
end
|
18
|
+
|
19
|
+
def teardown
|
20
|
+
#@pidfile.unlink
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_that_respond_to_start
|
24
|
+
assert_respond_to @rinit, :start
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_that_respond_to_stop
|
28
|
+
assert_respond_to @rinit, :stop
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_that_respond_to_status
|
32
|
+
assert_respond_to @rinit, :status
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_that_respond_to_restart
|
36
|
+
assert_respond_to @rinit, :restart
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_start_daemon_and_stop_daemon
|
40
|
+
assert(@rinit.start(:cmd => DAEMON,
|
41
|
+
:chuid => USER,
|
42
|
+
:pidfile => @pidfile.path))
|
43
|
+
|
44
|
+
assert(@rinit.status(@pidfile.path))
|
45
|
+
assert(@rinit.stop(@pidfile.path))
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_status_should_be_stopped
|
49
|
+
refute(@rinit.status(@pidfile.path))
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rinit'
|
3
|
+
|
4
|
+
extend Rinit
|
5
|
+
|
6
|
+
# app_name This is a startup script for use in /etc/init.d
|
7
|
+
#
|
8
|
+
# description: This will start, stop, and restart foo daemon
|
9
|
+
|
10
|
+
# this is an example of using ruby for a init script
|
11
|
+
|
12
|
+
APP_NAME = "Foodaemon"
|
13
|
+
PIDFILE = "/tmp/#{APP_NAME}.pid"
|
14
|
+
PATH = File.expand_path File.dirname(__FILE__)
|
15
|
+
DAEMON = File.join(PATH, "foo_daemon.rb")
|
16
|
+
USER = ENV["USER"]
|
17
|
+
|
18
|
+
case ARGV.first
|
19
|
+
when "status"
|
20
|
+
puts "#{APP_NAME} is #{Rinit.status(PIDFILE) ? "running" : "stopped"}"
|
21
|
+
when "start"
|
22
|
+
result = Rinit.start(cmd: DAEMON,
|
23
|
+
chuid: USER,
|
24
|
+
pidfile: PIDFILE)
|
25
|
+
|
26
|
+
puts "#{APP_NAME} has started" unless result.nil?
|
27
|
+
when "stop"
|
28
|
+
Rinit.stop(PIDFILE)
|
29
|
+
puts "#{APP_NAME} is stopped"
|
30
|
+
when "restart"
|
31
|
+
puts "#{APP_NAME} is restarting..."
|
32
|
+
Rinit.restart(PIDFILE, cmd: DAEMON,
|
33
|
+
chuid: USER,
|
34
|
+
pidfile: PIDFILE)
|
35
|
+
end
|
36
|
+
|
37
|
+
unless %w{start stop restart status}.include? ARGV.first
|
38
|
+
puts "Usage: #{APP_NAME} {start|stop|restart}"
|
39
|
+
exit
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative "test_helper"
|
2
|
+
require_relative './test_helper'
|
3
|
+
|
4
|
+
class RinitTemplateBuilderTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@name = 'foobar'
|
8
|
+
@template = 'init'
|
9
|
+
@example_file = File.join(File.expand_path(File.dirname(__FILE__)), "../",
|
10
|
+
"#{@name}.#{@template}")
|
11
|
+
@generator = Rinit::TemplateBuilder.new(template: @template, name: @name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_respond_to_build
|
15
|
+
assert_respond_to(@generator, :build)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_create_template
|
19
|
+
@generator.build
|
20
|
+
assert(File.exist?(@example_file), "cant find file")
|
21
|
+
end
|
22
|
+
|
23
|
+
def teardown
|
24
|
+
begin
|
25
|
+
FileUtils.rm(@example_file)
|
26
|
+
rescue
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rinit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bradley Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-04-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sys-proctable
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: open4
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: ' Provides init-like script structure for ruby'
|
98
|
+
email:
|
99
|
+
- bradleydsmith@gmail.com
|
100
|
+
executables:
|
101
|
+
- rinit
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- TODO
|
112
|
+
- bin/rinit
|
113
|
+
- examples/init
|
114
|
+
- examples/monit
|
115
|
+
- lib/rinit.rb
|
116
|
+
- lib/rinit/commands.rb
|
117
|
+
- lib/rinit/exceptions.rb
|
118
|
+
- lib/rinit/process_utils.rb
|
119
|
+
- lib/rinit/template_builder.rb
|
120
|
+
- lib/rinit/version.rb
|
121
|
+
- rinit.gemspec
|
122
|
+
- test/rinit_commands_test.rb
|
123
|
+
- test/support/foo_daemon.rb
|
124
|
+
- test/support/sample_init.rb
|
125
|
+
- test/template_builder_test.rb
|
126
|
+
- test/test_helper.rb
|
127
|
+
homepage: ''
|
128
|
+
licenses:
|
129
|
+
- MIT
|
130
|
+
metadata: {}
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - '>='
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - '>='
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.0.0
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Provides init-like script structure for ruby so you can control your daemons.
|
151
|
+
test_files:
|
152
|
+
- test/rinit_commands_test.rb
|
153
|
+
- test/support/foo_daemon.rb
|
154
|
+
- test/support/sample_init.rb
|
155
|
+
- test/template_builder_test.rb
|
156
|
+
- test/test_helper.rb
|