forked from philcryer/lipsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·295 lines (263 loc) · 10.8 KB
/
setup.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
#!/bin/bash
# Name : setup.sh
# Author : Phil Cryer <[email protected]>
# Site : http://github.com/philcryer/lipsync
# Desc : This script sets up the lipsync service
clear
stty erase '^?'
echo "lipsync setup script"
###############################################################################
# Check if the user is root, or if they have user has sudo privileges
###############################################################################
echo -n "* Checking that user is root or has sudo access..."
if [ "$(id -u)" != "0" ]; then
sudo -v >/dev/null 2>&1 || { echo; echo " ERROR: User $(whoami) is not root, and does not have sudo privileges" ; exit 1; }
else
echo "ok"
fi
###############################################################################
###############################################################################
# Checking if this system is either Debian or Ubuntu
###############################################################################
echo -n "* Checking if the installer supports this system..."
if [ `cat /etc/issue.net | cut -d' ' -f1` == "Debian" ] || [ `cat /etc/issue.net | cut -d' ' -f1` == "Ubuntu" ];then
echo "ok"
else
echo; echo " ERROR: this installer currently does not support your system,"
echo " but you can try, it could work (tm) - let us know if it does"
fi
###############################################################################
###############################################################################
# Test if required applications are installed, die if not
###############################################################################
echo -n "* Checking that required software is installed..."
type -P ssh &>/dev/null || { echo; echo " ERROR: lipsync requires ssh-client but it's not installed" >&2; exit 1; }
type -P ssh-copy-id &>/dev/null || { echo; echo " ERROR: lipsync requires ssh-copy-id but it's not installed" >&2; exit 1; }
type -P rsync &>/dev/null || { echo; echo " ERROR: lipsync requires rsync but it's not installed" >&2; exit 1; }
type -P lsyncd &>/dev/null || { echo; echo " ERROR: lipsync requires lsyncd but it's not installed" >&2; exit 1; }
type -P unison &>/dev/null || { echo; echo " ERROR: lipsync requires unison but it's not installed" >&2; exit 1; }
echo "ok"
###############################################################################
###############################################################################
# Define functions
###############################################################################
questions(){
echo -n " - IP or domainname for server: "
read remote_server
echo -n " - SSH port on server (default is 22): "
read port
if [ -z "$port" ]
then
port="22"
fi
echo -n " - username (must exist on the client and server): "
read username
echo -n " - local directory to be synced (default is /home/$username/sync): "
read lipsync_dir_local
if [ -z "$lipsync_dir_local" ]
then
lipsync_dir_local="/home/$username/sync"
fi
echo -n " - remote directory to be synced (default is /home/$username/sync): "
read lipsync_dir_remote
if [ -z "$lipsync_dir_remote" ]
then
lipsync_dir_remote="/home/$username/sync"
fi
}
ssh.keygen(){
if [ -f '/home/${username}/.ssh/id_dsa' ]; then
echo "* Existing SSH key found for ${username} backing up..."
mv /home/${username}/.ssh/id_dsa /home/${username}/.ssh/id_dsa-OLD
if [ $? -eq 0 ]; then
echo "done"
else
echo; echo " ERROR: there was an error backing up the SSH key"; exit 1
fi
fi
echo -n "* Creating new SSH key for ${username}..."
ssh-keygen -q -N '' -f /home/${username}/.ssh/id_dsa
if [ $? -eq 0 ]; then
chown -R $username:$username /home/${username}/.ssh
echo "done"
else
echo; echo " ERROR: there was an error generating the ssh key"; exit 1
fi
echo "* Transferring ssh key for ${username} to ${remote_server} on port ${port} (login as $username now)...";
#echo -n " NOTE: you will be prompted to login..."
#su ${username} -c "ssh-copy-id ${remote_server}" >> /dev/null
# ssh-copy-id -i id_rsa.pub terry@host2
su ${username} -c "ssh-copy-id -i /home/${username}/.ssh/id_dsa.pub '-p ${port} ${username}@${remote_server}'" >> /dev/null
# ssh-copy-id ${remote_server}
if [ $? -eq 0 ]; then
X=0 #echo "done"
else
echo; echo " ERROR: there was an error transferring the ssh key"; exit 1
fi
echo -n "* Setting permissions on the ssh key for ${username} on ${remote_server} on port ${port}...";
#echo -n " NOTE: you should not be prompted to login..."
su ${username} -c "SSH_AUTH_SOCK=0 ssh ${remote_server} -p ${port} 'chmod 700 .ssh'"
#ssh ${remote_server} 'chmod 700 .ssh'
if [ $? -eq 0 ]; then
echo "done"
else
echo; echo " ERROR: there was an error setting permissions on the ssh key for ${username} on ${remote_server} on port ${port}..."; exit 1
fi
}
create.group(){
echo -n "* Creating group lipsync..."
grep lipsync /etc/group >>/dev/null
if [ $? -eq 0 ]; then
echo; echo " NOTICE: existing group lipsync found, not creating"
else
groupadd lipsync
echo "done"
fi
}
create.user(){
echo -n "* Creating user lipsync..."
grep lipsync /etc/passwd
if [ $? -eq 0 ]; then
echo; echo " NOTICE: existing user lipsync found, not creating"
else
useradd -g lipsync -s /bin/false lipsync
echo "done"
fi
}
build.conf(){
echo -n "* Creating lipsyncd.conf.xml for ${username}..."
sed 's|LSLOCDIR|'$lipsync_dir_local/'|g' etc/lipsyncd.conf.xml > /tmp/lipsyncd.conf.xml.01
sed 's|LSUSER|'$username'|g' /tmp/lipsyncd.conf.xml.01 > /tmp/lipsyncd.conf.xml.02
sed 's|LPORT|'$port'|g' /tmp/lipsyncd.conf.xml.02 > /tmp/lipsyncd.conf.xml.03
sed 's|LSREMSERV|'$remote_server'|g' /tmp/lipsyncd.conf.xml.03 > /tmp/lipsyncd.conf.xml.04
sed 's|LSREMDIR|'$lipsync_dir_remote'|g' /tmp/lipsyncd.conf.xml.04 > /tmp/lipsyncd.conf.xml
echo "done"
}
deploy(){
#echo -n "* Enabling lipsync for $username..."
#touch /home/$username/.lipsyncd; chown $username:$username /home/$username/.lipsyncd
#echo "done"
echo -n "* Adding new lipsync user to lipsync group..."
adduser $username lipsync >> /dev/null
echo "done"
########################
echo "* Deploying lipsync..."
echo -n " > installing /usr/bin/lipsync..."
cp usr/bin/lipsync /usr/bin; chown root:root /usr/bin/lipsync; chmod 755 /usr/bin/lipsync
echo "done"
########################
echo -n " > installing /usr/bin/lipsyncd..."
cp usr/bin/lipsyncd /usr/bin; chown root:root /usr/bin/lipsyncd; chmod 755 /usr/bin/lipsyncd
echo "done"
########################
echo -n " > installing /etc/init.d/lipsyncd..."
sed 's|LSUSER|'$username'|g' etc/init.d/lipsyncd > /etc/init.d/lipsyncd; chown root:root /etc/init.d/lipsyncd; chmod 755 /etc/init.d/lipsyncd
echo "done"
########################
echo -n " > installing /etc/cron.d/lipsync..."
sed 's|LSUSER|'$username'|g' etc/cron.d/lipsync > /etc/cron.d/lipsync
#sed 's|PORT|'$port'|g' /tmp/lipsync.01 > /tmp/lipsync.02
#sed 's|LSLOCDIR|'$lipsync_dir_local'|g' /tmp/lipsync.01 > /tmp/lipsync.02
#sed 's|LSREMDIR|'$lipsync_dir_remote'|g' /tmp/lipsync.02 > /tmp/lipsync.03
#sed 's|LSREMSERV|'$remote_server'|g' /tmp/lipsync.03 > /etc/cron.d/lipsync
#rm /tmp/lipsync.*
echo "done"
########################
echo -n " > installing /etc/lipsyncd.conf.xml..."
mv /tmp/lipsyncd.conf.xml /etc
rm /tmp/lipsyncd.conf.*
echo "done"
########################
echo -n " > installing docs /usr/share/doc/lipsync..."
mkdir /usr/share/doc/lipsync
cp README* INSTALL* LICENSE /usr/share/doc/lipsync
echo "done"
########################
echo -n " > preparing logfile /var/log/lipsyncd.log..."
touch /var/log/lipsyncd.log
# chmod 640 /var/log/lipsyncd.log
chmod g+w /var/log/lipsyncd.log
chown lipsync:lipsync /var/log/lipsyncd.log
echo "done"
########################
echo -n " > enabling unison for $username..."
sed 's|LSLOCDIR|'$lipsync_dir_local/'|g' unison/lipsync.prf > /tmp/lipsync.prf.01
sed 's|LSUSER|'${username}'|g' /tmp/lipsync.prf.01 > /tmp/lipsync.prf.02
sed 's|LSREMDIR|'$lipsync_dir_remote'|g' /tmp/lipsync.prf.02 > /tmp/lipsync.prf.03
sed 's|LPORT|'$port'|g' /tmp/lipsync.prf.03 > /tmp/lipsync.prf.04
sed 's|LSREMSERV|'$remote_server'|g' /tmp/lipsync.prf.04 > /tmp/lipsync.prf
if [ ! -d "/home/${username}/.unison" ]; then
mkdir /home/${username}/.unison
else
rm -fR /home/${username}/.unison-old
mv /home/${username}/.unison /home/${username}/.unison-old
mkdir /home/${username}/.unison
fi
cp /tmp/lipsync.prf /home/${username}/.unison/
chown -R ${username}:${username} /home/${username}/.unison/
rm /tmp/lipsync.*
echo "done"
########################
echo -n " > creating repair_state.sh in ~/$username..."
sed 's|LSLOCDIR|'$lipsync_dir_local/'|g' extras/repair_state.sh > /tmp/repair_state.sh.01
sed 's|LSUSER|'${username}'|g' /tmp/repair_state.sh.01 > /tmp/repair_state.sh.02
sed 's|LSREMDIR|'$lipsync_dir_remote'|g' /tmp/repair_state.sh.02 > /tmp/repair_state.sh.03
sed 's|LPORT|'$port'|g' /tmp/repair_state.sh.03 > /tmp/repair_state.sh.04
sed 's|LSREMSERV|'$remote_server'|g' /tmp/repair_state.sh.04 > /home/${username}/repair_state.sh
########################
echo "lipsync installed `date`" > /var/log/lipsyncd.log
}
uninstall(){
echo -n " NOTICE: stopping lipsync service..."
/etc/init.d/lipsyncd stop >> /dev/null
echo "done"
echo -n " NOTICE: disabling lipsync for user..."
rm -f /home/$username/.unison
echo "done"
echo -n " NOTICE: removing lipsync user and group..."
userdel lipsync
groupdel lipsync
echo "done"
echo -n " NOTICE: removing lipsync files..."
rm -rf /etc/init.d/lipsyncd
rm -rf /etc/lipsyncd.conf.xml
rm -rf /usr/bin/lipsync*
rm -rf /var/log/lipsync*
rm -rf /usr/share/doc/lipsync
echo "done"
}
###############################################################################
###############################################################################
# Install or uninstall the lipsync service
###############################################################################
if [ "${1}" = "uninstall" ]; then
echo " ALERT: Uninstall option chosen, all lipsync files and configuration will be purged!"
echo -n " ALERT: To continue press enter to continue, otherwise hit ctrl-c now to bail..."
read continue
uninstall
exit 0
else
questions
ssh.keygen
create.group
create.user
build.conf
deploy
fi
###############################################################################
###############################################################################
# Startup lipsync and exit
###############################################################################
#echo -n "lipsync setup complete, starting lipsync..."
echo "lipsync setup complete, starting lipsync..."
/etc/init.d/lipsyncd start
# echo "done"
if [ -f /var/run/lipsyncd.pid ]; then
echo " NOTICE: lipsyncd is running as pid `cat /var/run/lipsyncd.pid`"
echo " Check /var/log/lipsyncd.log for details"
else
echo " NOTICE: lipsyncd failed to start..."
echo " Check /var/log/lipsyncd.log for details"
fi
###############################################################################
exit 0