lsb_init 0.0.1
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 +7 -0
- data/bin/lsb_init_ruby +5 -0
- data/lib/daemon +56 -0
- data/lib/lsb_init/configurator.rb +244 -0
- data/lib/lsb_init/main.rb +21 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 627bdfb260ef787997d681a8b76a1341dc423bf1
|
4
|
+
data.tar.gz: 5d81182274f5cfb6a03098c60e5df8111b65f5f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a61f95cd8696028a64c7cc056029ba1770495299b92c663a705d3ef1c34a60ec5172670cf803be4a32943fb19f6f66315e1843bed025f3de89452ff29629bccb
|
7
|
+
data.tar.gz: bc97c35a508a2b6571906109d43d7fbd225413a02750dba47e7b786e27d0f2d1c33c2b78b157da45dd7444c947ef8d7a1db5c653fba0491a1fa00e0aa9f048d8
|
data/bin/lsb_init_ruby
ADDED
data/lib/daemon
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require_relative './main'
|
4
|
+
|
5
|
+
class Daemon
|
6
|
+
|
7
|
+
include LsbInit::Main
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
|
11
|
+
@path = "#{File.expand_path('..', __FILE__)}"
|
12
|
+
@pidfile_path = "#{@path}/pidfile"
|
13
|
+
|
14
|
+
Process.daemon true
|
15
|
+
|
16
|
+
write_pid
|
17
|
+
at_exit{remove_pid}
|
18
|
+
|
19
|
+
Signal.trap('TERM'){ do_finish }
|
20
|
+
|
21
|
+
Signal.trap('USR1'){ do_reload }
|
22
|
+
|
23
|
+
do_main
|
24
|
+
|
25
|
+
rescue => e
|
26
|
+
puts e.message
|
27
|
+
exit e.errno
|
28
|
+
end
|
29
|
+
|
30
|
+
def write_pid
|
31
|
+
if File.exists?(@pidfile_path)
|
32
|
+
File.open(@pidfile_path, 'a'){|pidfile| pidfile << " #{Process.pid}" }
|
33
|
+
else
|
34
|
+
File.open(@pidfile_path, 'w'){|pidfile| pidfile.write(Process.pid.to_s) }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def remove_pid
|
39
|
+
if File.exists?(@pidfile_path)
|
40
|
+
pids = []
|
41
|
+
File.open(@pidfile_path, 'r+'){ |f|
|
42
|
+
f.flock File::LOCK_EX
|
43
|
+
pids = f.read.split(' ')
|
44
|
+
pids.delete(Process.pid.to_s)
|
45
|
+
f.rewind
|
46
|
+
f.write(pids.join(' '))
|
47
|
+
f.flush
|
48
|
+
f.truncate f.pos
|
49
|
+
}
|
50
|
+
File.delete(@pidfile_path) if pids.length==0
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
Daemon.new
|
@@ -0,0 +1,244 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module LsbInit
|
5
|
+
|
6
|
+
USAGE = <<-END
|
7
|
+
USAGE:
|
8
|
+
|
9
|
+
lsb_init_ruby command service_name
|
10
|
+
|
11
|
+
commands:
|
12
|
+
e - generate rc script (with disabling and removing of existing one), for project and places it into lsb_init folder.
|
13
|
+
r - disable and remove script
|
14
|
+
END
|
15
|
+
|
16
|
+
class ProjectIsNotUnderBundler < Exception
|
17
|
+
def note
|
18
|
+
<<-END
|
19
|
+
|
20
|
+
Please run `lsb_init_ruby` from directory (or tree) containing Gemfile
|
21
|
+
|
22
|
+
END
|
23
|
+
end
|
24
|
+
|
25
|
+
def message
|
26
|
+
"Cannot find proper project's root directory"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class InappropriateServiceName < Exception
|
31
|
+
|
32
|
+
def message
|
33
|
+
"Cannot generate service with name <#{@service_name}>"
|
34
|
+
end
|
35
|
+
|
36
|
+
def note
|
37
|
+
<<-END
|
38
|
+
|
39
|
+
Try to generate script with another service name:
|
40
|
+
lsb_init_ruby g <another_service_name>
|
41
|
+
|
42
|
+
END
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class BadArgumentPassed < Exception
|
47
|
+
def message
|
48
|
+
'Incorrect call'
|
49
|
+
end
|
50
|
+
def note
|
51
|
+
USAGE
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class Configurator
|
56
|
+
|
57
|
+
def initialize(args)
|
58
|
+
# defining @project_root, @service_name
|
59
|
+
raise BadArgumentPassed.new if args.length < 1
|
60
|
+
find_project
|
61
|
+
cmd = args.shift
|
62
|
+
define_name args.shift
|
63
|
+
cmd(cmd)
|
64
|
+
rescue ProjectIsNotUnderBundler, BadArgumentPassed, InappropriateServiceName => e
|
65
|
+
puts "ERROR: #{e.message}"
|
66
|
+
puts e.note
|
67
|
+
end
|
68
|
+
|
69
|
+
def cmd(command)
|
70
|
+
@command = command
|
71
|
+
case command
|
72
|
+
when 'g'
|
73
|
+
cleanup
|
74
|
+
generate
|
75
|
+
when 'd'
|
76
|
+
cleanup
|
77
|
+
else
|
78
|
+
puts USAGE
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def cleanup
|
83
|
+
if Dir.exists?(@lsb_init_dir) && Dir.exists?((_="#{@lsb_init_dir}/runner")) && Dir.entries(_).length==1
|
84
|
+
puts 'Removing old runner script'
|
85
|
+
runner_name = Dir.entries(_).first
|
86
|
+
`sudo rm /etc/init.d/#{runner_name}`
|
87
|
+
`sudo update-rc.d #{runner_name} remove`
|
88
|
+
end
|
89
|
+
|
90
|
+
puts "Removing project's lsb_init_ruby directory"
|
91
|
+
`rm -rf #{@lsb_init_dir}` if Dir.exists?(@lsb_init_dir)
|
92
|
+
end
|
93
|
+
|
94
|
+
def generate
|
95
|
+
gem_dir = File.expand_path('../..', File.expand_path(__FILE__))
|
96
|
+
puts "Preparing project's lsb_init folder"
|
97
|
+
|
98
|
+
[@lsb_init_dir].each{|d| Dir.mkdir d}
|
99
|
+
|
100
|
+
puts 'Generating scripts'
|
101
|
+
Dir.mkdir "#{@lsb_init_dir}/runner" unless Dir.exists?("#{@lsb_init_dir}/runner")
|
102
|
+
rc_filename = "#{@lsb_init_dir}/runner/#{@service_name}"
|
103
|
+
rc_file = File.new rc_filename, 'w'
|
104
|
+
|
105
|
+
rc_script = script_text
|
106
|
+
|
107
|
+
rc_file.write rc_script
|
108
|
+
rc_file.close
|
109
|
+
|
110
|
+
`cp #{gem_dir}/lsb_init/main.rb #{@lsb_init_dir}/main.rb`
|
111
|
+
`cp #{gem_dir}/daemon #{@lsb_init_dir}/daemon`
|
112
|
+
|
113
|
+
`chmod 755 #{rc_filename}`
|
114
|
+
`sudo chown root:root #{rc_filename}`
|
115
|
+
`sudo cp #{rc_filename} /etc/init.d/#{@service_name}`
|
116
|
+
`sudo update-rc.d #{@service_name} defaults`
|
117
|
+
end
|
118
|
+
|
119
|
+
def find_project
|
120
|
+
dir = Dir.pwd
|
121
|
+
while !File.exists?("#{dir}/Gemfile") do
|
122
|
+
dir = File.expand_path '..', dir
|
123
|
+
raise ProjectIsNotUnderBundler.new if dir.eql? '/'
|
124
|
+
end
|
125
|
+
@project_root = dir
|
126
|
+
@lsb_init_dir = "#{dir}/lsb_init"
|
127
|
+
@pidfile_path = "#{@lsb_init_dir}/pidfile"
|
128
|
+
puts "Used project's root: #{@project_root}"
|
129
|
+
puts "Used project's rc folder: #{@lsb_init_dir}"
|
130
|
+
end
|
131
|
+
|
132
|
+
def define_name(opts)
|
133
|
+
if !opts.nil?
|
134
|
+
@service_name = opts
|
135
|
+
elsif Dir.exists?(@lsb_init_dir) && Dir.exists?("#{@lsb_init_dir}/runner") && (_=Dir.entries("#{@lsb_init_dir}/runner")).length==1
|
136
|
+
@service_name = _[0]
|
137
|
+
else
|
138
|
+
@service_name = @project_root.split('/').last
|
139
|
+
end
|
140
|
+
|
141
|
+
raise InappropriateServiceName.new if @service_name.nil? || `which #{@service_name}`.length > 0
|
142
|
+
end
|
143
|
+
|
144
|
+
def script_text
|
145
|
+
text = <<-END
|
146
|
+
#!/bin/bash
|
147
|
+
|
148
|
+
### BEGIN INIT INFO
|
149
|
+
# Provides: #{@service_name}
|
150
|
+
# Required-Start: $local_fs $network
|
151
|
+
# Required-Stop: $local_fs $network
|
152
|
+
# Should-Start: $time
|
153
|
+
# Should-Stop: $time
|
154
|
+
# Default-Start: 2 3 4 5
|
155
|
+
# Default-Stop: 0 1 6
|
156
|
+
# Short-Description: Start and stop the service #{@service_name}
|
157
|
+
# Description: Controls the starting and stopping daemon #{@service_name}
|
158
|
+
#
|
159
|
+
### END INIT INFO
|
160
|
+
|
161
|
+
## Copyright: LAKSHMANAN GANAPATHY on MARCH 16, 2012
|
162
|
+
# Access: http://www.thegeekstuff.com/2012/03/lsbinit-script/
|
163
|
+
##
|
164
|
+
|
165
|
+
# Using the lsb functions to perform the operations.
|
166
|
+
. /lib/lsb/init-functions
|
167
|
+
# Process name ( For display )
|
168
|
+
NAME=#{@service_name}
|
169
|
+
# Daemon name, where is the actual executable
|
170
|
+
DAEMON=#{@lsb_init_dir}/daemon
|
171
|
+
# pid file for the daemon
|
172
|
+
PIDFILE=#{@pidfile_path}
|
173
|
+
|
174
|
+
# If the daemon is not there, then exit.
|
175
|
+
test -x $DAEMON || exit 5
|
176
|
+
|
177
|
+
case $1 in
|
178
|
+
start)
|
179
|
+
# Checked the PID file exists and check the actual status of process
|
180
|
+
if [ -e $PIDFILE ]; then
|
181
|
+
status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
|
182
|
+
# If the status is SUCCESS then don't need to start again.
|
183
|
+
if [ $status = "0" ]; then
|
184
|
+
exit # Exit
|
185
|
+
fi
|
186
|
+
fi
|
187
|
+
# Start the daemon.
|
188
|
+
log_daemon_msg "Starting the process" "$NAME"
|
189
|
+
# Start the daemon with the help of start-stop-daemon
|
190
|
+
# Log the message appropriately
|
191
|
+
if start-stop-daemon --start --quiet #{('--chuid '+Process.uid.to_s+':'+Process.gid.to_s) if Process.uid!=0 } --oknodo --pidfile $PIDFILE --exec $DAEMON ; then
|
192
|
+
log_end_msg 0
|
193
|
+
else
|
194
|
+
log_end_msg 1
|
195
|
+
fi
|
196
|
+
;;
|
197
|
+
stop)
|
198
|
+
# Stop the daemon.
|
199
|
+
if [ -e $PIDFILE ]; then
|
200
|
+
status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?"
|
201
|
+
if [ "$status" = 0 ]; then
|
202
|
+
start-stop-daemon --stop #{('--user '+Process.uid.to_s) if Process.uid!=0 } --quiet --oknodo --pidfile $PIDFILE
|
203
|
+
# /bin/rm -rf $PIDFILE
|
204
|
+
fi
|
205
|
+
else
|
206
|
+
log_daemon_msg "$NAME process is not running"
|
207
|
+
log_end_msg 0
|
208
|
+
fi
|
209
|
+
;;
|
210
|
+
restart)
|
211
|
+
# Restart the daemon.
|
212
|
+
$0 stop && sleep 2 && $0 start
|
213
|
+
;;
|
214
|
+
status)
|
215
|
+
# Check the status of the process.
|
216
|
+
if [ -e $PIDFILE ]; then
|
217
|
+
status_of_proc -p $PIDFILE $DAEMON "$NAME process" && exit 0 || exit $?
|
218
|
+
else
|
219
|
+
log_daemon_msg "$NAME Process is not running"
|
220
|
+
log_end_msg 0
|
221
|
+
fi
|
222
|
+
;;
|
223
|
+
reload)
|
224
|
+
# Reload the process. Basically sending some signal to a daemon to reload
|
225
|
+
# it configurations.
|
226
|
+
if [ -e $PIDFILE ]; then
|
227
|
+
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PIDFILE --name $NAME
|
228
|
+
log_success_msg "$NAME process reloaded successfully"
|
229
|
+
else
|
230
|
+
log_failure_msg "$PIDFILE does not exists"
|
231
|
+
fi
|
232
|
+
;;
|
233
|
+
*)
|
234
|
+
# For invalid arguments, print the usage message.
|
235
|
+
echo "Usage: $0 {start|stop|restart|reload|status}"
|
236
|
+
exit 2
|
237
|
+
;;
|
238
|
+
esac
|
239
|
+
END
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
243
|
+
|
244
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lsb_init
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anton Titkov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Gem provides commands to generate/remove `/etc/init.d/service_script`
|
14
|
+
for single-instanced daemon
|
15
|
+
email: atlascoder@gmail.com
|
16
|
+
executables:
|
17
|
+
- lsb_init_ruby
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- bin/lsb_init_ruby
|
22
|
+
- lib/daemon
|
23
|
+
- lib/lsb_init/configurator.rb
|
24
|
+
- lib/lsb_init/main.rb
|
25
|
+
homepage: http://github.com/atlascoder/lsb_init
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.6.4
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: lsb-init is a tool for generating LSB Init scripts for Ruby projects deploying
|
49
|
+
in Debian systems
|
50
|
+
test_files: []
|