-
Notifications
You must be signed in to change notification settings - Fork 65
/
installOpenVZWebPanel.sh
330 lines (267 loc) · 8.93 KB
/
installOpenVZWebPanel.sh
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/sh
# global variables
VERSION="2.4"
DOWNLOAD_URL="http://ovz-web-panel.googlecode.com/files/ovz-web-panel-$VERSION.tgz"
RUBYGEMS_URL="http://production.cf.rubygems.org/rubygems/rubygems-1.3.5.tgz"
RUBY_SQLITE3_CMD="ruby -e \"require 'rubygems'\" -e \"require 'sqlite3'\""
LOG_FILE="/tmp/ovz-web-panel.log"
INSTALL_DIR="/opt/ovz-web-panel/"
FORCE=0 # force installation to the same directory
PRESERVE_ARCHIVE=0
AUTOSOLVER=1 # automatic solving of dependencies
DISTRIB_ID=""
DEBUG=0
UPGRADE=0
UNINSTALL=0
ERR_FATAL=1
for PARAM in $@; do
eval $PARAM
done
[ "x$DEBUG" = "x1" ] && set -xv
log() {
echo `date` $1 >> $LOG_FILE
}
puts() {
echo $1
log "$1"
}
puts_separator() {
puts "-----------------------------------"
}
puts_spacer() {
puts
}
exec_cmd() {
TITLE=$1
COMMAND=$2
puts "$TITLE $COMMAND"
`$COMMAND`
}
fatal_error() {
puts "Fatal error: $1"
exit $ERR_FATAL
}
is_command_present() {
puts "Checking presence of the command: $1"
CMD=`whereis -b $1 | awk '{ print $2 }'`
[ -n "$CMD" ] && return 0 || return 1
}
detect_os() {
puts "Detecting distrib ID..."
is_command_present "lsb_release"
if [ $? -eq 0 ]; then
puts "LSB info: `lsb_release -a`"
DISTRIB_ID=`lsb_release -si`
return 0
fi
[ -f /etc/redhat-release ] && DISTRIB_ID="RedHat"
[ -f /etc/fedora-release ] && DISTRIB_ID="Fedora"
[ -f /etc/debian_version ] && DISTRIB_ID="Debian"
}
resolve_deps() {
puts "Resolving dependencies..."
if [ "$DISTRIB_ID" = "Ubuntu" -o "$DISTRIB_ID" = "Debian" ]; then
apt-get update
apt-get -y install ruby rubygems libsqlite3-ruby libopenssl-ruby rake
fi
if [ "$DISTRIB_ID" = "RedHat" -o "$DISTRIB_ID" = "CentOS" ]; then
yum -y install ruby
is_command_present gem
if [ $? -ne 0 ]; then
yum -y install ruby-devel ruby-docs ruby-ri ruby-irb ruby-rdoc
wget -nc -P /tmp/ $RUBYGEMS_URL
ARCHIVE_NAME=`echo $RUBYGEMS_URL | sed 's/.\+\///g'`
DIR_NAME=`echo $ARCHIVE_NAME | sed 's/.tgz//g'`
tar -C /tmp/ -xzf /tmp/$ARCHIVE_NAME
ruby /tmp/$DIR_NAME/setup.rb
rm -f /tmp/$ARCHIVE_NAME
rm -rf /tmp/$DIR_NAME
fi
gem list rake -i
[ $? -ne 0 ] && gem install rake
gem list rdoc -i
[ $? -ne 0 ] && gem install rdoc
sh -c "$RUBY_SQLITE3_CMD" > /dev/null 2>&1
if [ $? -ne 0 ]; then
yum -y install sqlite-devel make gcc
gem install sqlite3
fi
fi
if [ "$DISTRIB_ID" = "Fedora" ]; then
yum -y install ruby rubygems ruby-sqlite3 rubygem-rake
fi
}
check_environment() {
puts "Checking environment..."
[ "`whoami`" != "root" ] && fatal_error "Installer should be executed under root user."
puts "System info: `uname -a`"
detect_os
[ "x$DISTRIB_ID" != "x" ] && puts "Detected distrib ID: $DISTRIB_ID"
detect_openvz
}
check_dependencies() {
[ "x$AUTOSOLVER" = "x1" ] && resolve_deps
is_command_present ruby
if [ $? -eq 0 ]; then
RUBY_VERSION=`ruby -v | awk '{ print $2 }'`
if [ "1.8" != "`echo $RUBY_VERSION | awk -F. '{ print $1"."$2 }'`" ]; then
fatal_error "Panel requires Ruby 1.8 (Ruby 1.9 is not supported)."
fi
if [ `echo $RUBY_VERSION | awk -F. '{ print $3 }'` -lt 5 ]; then
fatal_error "Panel requires Ruby 1.8.5 or higher."
fi
puts "Ruby version: $RUBY_VERSION"
else
fatal_error "Ruby 1.8 is not installed. Please install it first."
fi
is_command_present gem
if [ $? -eq 0 ]; then
puts "RubyGems version: `gem -v`"
else
fatal_error "RubyGems is not installed. Please install it first."
fi
puts "Checking Ruby SQLite3 support: $RUBY_SQLITE3_CMD"
sh -c "$RUBY_SQLITE3_CMD" > /dev/null 2>&1
[ $? -ne 0 ] && fatal_error "Ruby SQLite3 support not found. Please install it first."
puts_spacer
}
detect_openvz() {
if [ -f /proc/vz/version ]; then
ENVIRONMENT="HW-NODE"
puts "OpenVZ hardware node detected."
elif [ -d /proc/vz ]; then
ENVIRONMENT="VPS"
puts "OpenVZ virtual environment detected."
else
ENVIRONMENT="STANDALONE"
puts "Standalone environment detected."
fi
}
install_product() {
puts "Installation..."
[ -f $INSTALL_DIR/config/database.yml ] && UPGRADE=1
mkdir -p $INSTALL_DIR
if [ -f "$DOWNLOAD_URL" ]; then
ARCHIVE_NAME=$DOWNLOAD_URL
puts "Local archive: $ARCHIVE_NAME"
PRESERVE_ARCHIVE=1
else
exec_cmd "Downloading:" "wget -nc -P $INSTALL_DIR $DOWNLOAD_URL"
[ $? -ne 0 ] && fatal_error "Failed to download distribution."
ARCHIVE_NAME="$INSTALL_DIR/"`echo $DOWNLOAD_URL | sed 's/.\+\///g'`
fi
EXCLUDE_LIST=""
if [ "x$UPGRADE" = "x1" ]; then
EXCLUDE_LIST="--exclude=*.log --exclude=config/database.yml --exclude=db/*.sqlite3"
[ -f "$INSTALL_DIR/config/certs/server.crt" ] && EXCLUDE_LIST="$EXCLUDE_LIST --exclude=config/certs/*"
[ -f "$INSTALL_DIR/utils/hw-daemon/certs/server.crt" ] && EXCLUDE_LIST="$EXCLUDE_LIST --exclude=hw-daemon/certs/*"
fi
exec_cmd "Unpacking:" "tar --strip 2 -C $INSTALL_DIR -xzf $ARCHIVE_NAME $EXCLUDE_LIST"
if [ "x$PRESERVE_ARCHIVE" != "x1" ]; then
exec_cmd "Removing downloaded archive:" "rm -f $ARCHIVE_NAME"
fi
if [ "x$UPGRADE" = "x1" ]; then
puts "Removing deprecated files..."
[ -f $INSTALL_DIR/app/controllers/admin_controller.rb ] && rm $INSTALL_DIR/app/controllers/admin_controller.rb
puts "Upgrading database..."
CURRENT_DIR=`pwd`
cd $INSTALL_DIR
rake db:migrate RAILS_ENV="production"
cd $CURRENT_DIR
[ $? -ne 0 ] && fatal_error "Failed to upgrade database to new version."
puts "Syncing physical servers state..."
ruby $INSTALL_DIR/script/runner -e production "HardwareServer.all.each { |server| server.sync }"
puts "Reset remember_me tokens..."
ruby $INSTALL_DIR/script/runner -e production "User.all.each{ |user| user.remember_token = ''; user.save }"
fi
[ ! -x $INSTALL_DIR/script/owp ] && chmod +x $INSTALL_DIR/script/owp
if [ "$DISTRIB_ID" = "Ubuntu" -o "$DISTRIB_ID" = "Debian" -o "$DISTRIB_ID" = "RedHat" -o "$DISTRIB_ID" = "CentOS" -o "$DISTRIB_ID" = "Fedora" ]; then
cp $INSTALL_DIR/script/owp /etc/init.d/owp
chmod 755 /etc/init.d/owp
if [ "$DISTRIB_ID" = "Ubuntu" -o "$DISTRIB_ID" = "Debian" ]; then
update-rc.d -f owp remove
update-rc.d owp defaults 30
else
/sbin/chkconfig --add owp
fi
fi
if [ -f $INSTALL_DIR/script/owp.cron -a -d /etc/cron.daily ]; then
cp $INSTALL_DIR/script/owp.cron /etc/cron.daily/owp.cron
chmod 755 /etc/cron.daily/owp.cron
fi
if [ -f $INSTALL_DIR/config/owp.conf.sample -a ! -f /etc/owp.conf ]; then
cp $INSTALL_DIR/config/owp.conf.sample /etc/owp.conf
sed -i "s|^INSTALL_DIR=.*|INSTALL_DIR=$INSTALL_DIR|g" /etc/owp.conf
fi
puts "Installation finished."
puts "Product was installed into: $INSTALL_DIR"
puts_spacer
}
stop_services() {
puts "Stopping services..."
$INSTALL_DIR/script/owp stop
}
start_services() {
[ "x$UPGRADE" = "x1" ] && stop_services
puts "Starting services..."
if [ "x$UPGRADE" = "x0" ]; then
if [ "$ENVIRONMENT" = "HW-NODE" ]; then
HW_DAEMON_CONFIG="$INSTALL_DIR/utils/hw-daemon/hw-daemon.ini"
if [ ! -f $HW_DAEMON_CONFIG ]; then
echo "address = 127.0.0.1" >> $HW_DAEMON_CONFIG
echo "port = 7767" >> $HW_DAEMON_CONFIG
RAND_KEY=`head -c 200 /dev/urandom | md5sum | awk '{ print \$1 }'`
echo "key = $RAND_KEY" >> $HW_DAEMON_CONFIG
fi
$INSTALL_DIR/script/owp start
puts "Adding localhost to the list of controlled servers..."
ruby $INSTALL_DIR/script/runner -e production "HardwareServer.new(:host => 'localhost', :auth_key => '$RAND_KEY').connect"
[ $? -ne 0 ] && puts "Failed to add local server."
else
$INSTALL_DIR/script/owp start
puts "Place hardware daemon on machine with OpenVZ."
puts "To start hardware daemon run:"
puts "sudo ruby $INSTALL_DIR/utils/hw-daemon/hw-daemon.rb start"
fi
else
$INSTALL_DIR/script/owp start
fi
}
print_access_info() {
puts "Panel should be available at:"
puts "http://`hostname -f`:3000"
puts "Default credentials: admin/admin"
}
uninstall_product() {
if [ ! -d "$INSTALL_DIR" -o "$INSTALL_DIR" = "" -o "$INSTALL_DIR" = "/" ]; then
puts "Panel not found. Nothing to uninstall."
return 1
fi
stop_services
rm -rf $INSTALL_DIR
if [ "$DISTRIB_ID" = "Ubuntu" -o "$DISTRIB_ID" = "Debian" ]; then
update-rc.d -f owp remove
elif [ "$DISTRIB_ID" = "RedHat" -o "$DISTRIB_ID" = "CentOS" -o "$DISTRIB_ID" = "Fedora" ]; then
/sbin/chkconfig --del owp
fi
[ -f /etc/owp.conf ] && rm /etc/owp.conf
[ -f /etc/init.d/owp ] && rm /etc/init.d/owp
[ -f /etc/cron.daily/owp.cron ] && rm /etc/cron.daily/owp.cron
puts "Panel was uninstalled. Goto http://localhost:3000"
}
main() {
puts_separator
puts "OpenVZ Web Panel Installer."
puts_separator
check_environment
if [ "x$UNINSTALL" = "x1" ]; then
uninstall_product
else
check_dependencies
install_product
start_services
print_access_info
puts_separator
fi
}
main