forked from GuanglinDu/IPAMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththin.rb
executable file
·122 lines (108 loc) · 2.8 KB
/
thin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env ruby
# A quick and dirty thin init script
# Based on https://gist.github.com/flores/839739
# Run thin.rb to see its usage.
# The argument list:
#address: 0.0.0.0
#port: 3000
#ssl: true
#ssl-key-file: ./server.key
#ssl-cert-file: ./server.crt
#user: www-data
#group: www-data
#pid: ./tmp/pids/thin.pid
#timeout: 30
#wait: 30
#log: log/thin.log
#max_conns: 1024
#require: []
#environment: production
#max_persistent_conns: 512
#servers: 1
#threaded: true
#no-epoll: true
#daemonize: true
#socket: ./tmp/sockets/thin.sock
#chdir: ./
#tag: IPAMS thin service
# Which dir to run from
path = "./"
# The default IP address and port
address = "0.0.0.0"
port = "3000"
# The SSL key & cert files
key_file = "./server.key"
cert_file = "./server.crt"
# Let's keep it trackable
log = "./log/thin.log"
pid = "./tmp/thin.pid"
# http://goo.gl/glu6M9
# The backticks execute the command and return the output as a string.
def running?
str = `ps aux|grep thin`
str =~ /thin\s+server/i # Regexp
end
def get_rails_env
env = ENV["RAILS_ENV"]
if env =~ /development|test|production/
puts "Your RAILS_ENV was found: #{env}."
else
env = "development"
puts "Your RAILS_ENV was NOT found. Hence, it defaults to #{env}."
end
return env
end
def start_thin(env, pid, addr, port, kfile, cfile, log)
puts "Starting thin in SSL mode..."
system("thin start --environment #{env} -P #{pid}" \
" --address #{addr} --port #{port}" \
" --ssl --ssl-key-file #{kfile} --ssl-cert-file #{cfile}" \
" -l #{log} -d")
puts "Awesome, thin was started. Enjoy using it..."
end
def stop_thin(pid)
system("thin stop -P #{pid}")
#`rm -f #{pid}`
puts "Bye, thin was stopped."
end
def help
puts "Usage: #{__FILE__} status|start|stop|restart" \
"\nNote: This script starts thin in the SSL mode. Both the SSL key and" \
" certificate files have to be ready under the project root folder." \
" RAILS_ENV must be one of development, test, and production." \
" Otherwise, it defaults to development."
end
# The main part
# RAILS_ENV has to be determined first.
environment = get_rails_env
case ARGV.first
when 'status'
if running?
puts "Hi, thin is running joyfully..."
else
puts "Oops, thin is Not running at all. Sad! :)"
end
when 'start'
if running?
puts "Oops, thin was already started."
else
start_thin environment, pid, address, port, key_file, cert_file, log
end
when 'stop'
if running?
stop_thin pid
else
puts "It seems thin is not running. No further action."
end
when 'restart'
if running?
stop_thin pid
puts "Waiting a couple of seconds to let everybody bid farewell..."
sleep 5
else
puts "Oops, thin was not started yet & is going to be started."
end
start_thin environment, pid, address, port, key_file, cert_file, log
else
help
end