-
Notifications
You must be signed in to change notification settings - Fork 10
/
hda-create-server
executable file
·162 lines (145 loc) · 3.77 KB
/
hda-create-server
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env ruby
#
# Amahi Home Server
# Copyright (C) 2007-2009 Amahi Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License v3
# (29 June 2007), as published in the COPYING file.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# file COPYING for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Amahi
# team at http://www.amahi.org/ under "Contact Us."
require 'optparse'
require 'pp'
SCRIPT_NAME = File.basename($0)
def initscript_file(name, visible_name, user = "apache")
file = ["#! /bin/sh",
"#",
"# #{name}: Starts the #{visible_name} Daemon",
"#",
"# chkconfig: 345 96 03",
"# description: This is the #{name} daemon, which runs on the amahi server",
"#",
"# processname: #{name}",
"# config: ",
"",
"# Source function library.",
". /etc/init.d/functions",
"",
"DAEMON=#{name}",
"PROGRAM_NAME=\"#{visible_name}\"",
"PROGRAM_DIR=#{Dir.pwd}",
"PROGRAM_BIN=$PROGRAM_DIR/server/$DAEMON",
"LOCKFILE=/var/lock/subsys/$DAEMON",
"PIDFILE=/var/run/$DAEMON.pid",
"DAEMON_USER=#{user}",
"",
"if [[ ! -e $PROGRAM_DIR ]]; then",
" mkdir -p $PROGRAM_DIR",
"fi",
"",
"if [ \"$1\" == 'status' ]; then",
" test -x $PROGRAM_BIN || exit 4",
"else ",
" test -x $PROGRAM_BIN || exit 5",
"fi",
"",
"base=${0##*/}",
"",
"start() {",
" echo -n $\"Starting $PROGRAM_NAME daemon... \"",
" daemon --user $DAEMON_USER $PROGRAM_BIN",
" RETVAL=$?",
" if [ $RETVAL = 0 ]; then",
" touch $LOCKFILE",
" pidof $DAEMON > $PIDFILE",
" success $\"$base startup\"",
" else",
" failure $\"$base startup\"",
" fi",
" echo",
" return $RETVAL",
"}",
"",
"stop() {",
" echo -n $\"Shutting down $PROGRAM_NAME daemon: \"",
" killproc $DAEMON",
" RETVAL=$?",
" [ $RETVAL = 0 ] && success $\"$base shutdown\" || failure $\"$base shutdown\"",
" rm -f $LOCKFILE $PIDFILE",
" echo",
" return $RETVAL",
"}",
"",
"restart() {",
" stop",
" start",
"}",
"",
"RETVAL=0",
"",
"# See how we were called.",
"case \"$1\" in",
" start)",
" start",
" ;;",
" stop)",
" stop",
" ;;",
" status)",
" status $DAEMON",
" RETVAL=$?",
" ;;",
" restart)",
" restart",
" ;;",
" condrestart)",
" [ ! -f $lockfile ] || restart",
" ;;",
" *)",
" echo $\"Usage: $0 {start|stop|status|restart}\"",
" exit 2",
" ;;",
"esac",
"",
"exit $RETVAL",
"" ]
file.join "\n"
end
class OptParser
def self.parse(args)
options = { }
options[:visible_name] = nil
options[:service_name] = nil
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{SCRIPT_NAME} [options] name"
opts.separator ""
opts.on("-n", "--name ServerName", "Name of the server is ServerName.") do |lib|
options[:visible_name] = lib
options[:service_name] ||= lib.downcase
end
opts.on("-s", "--service servicename", "Name of the service is 'servicename'.") do |lib|
options[:service_name] = lib
end
opts.separator ""
opts.on("-h", "--help",
"Show this help message.") { $stderr.puts opts; exit }
end
opts.parse!(args)
options
end
end
options = OptParser.parse(ARGV)
pp options
puts initscript_file("adito", "OpenVPN ALS")
if options[:visible_name].nil?
$stderr.puts "error: cannot parse arguments. please run '#{SCRIPT_NAME} -h' for help."
exit 1
end
exit