-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_installer.sh
executable file
·311 lines (283 loc) · 9.74 KB
/
get_installer.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
#! /bin/bash
# FIXME some super_user_do requests act oddly upon the params... test that 'super_user_do something >> somewhere' works
# even when failure to get super user happens...
function init_install_list {
puts "* Ensuring proper file structure in /etc/get_server_config"
super_user_do mkdir -p /etc/get_server_config/deb
super_user_do touch /etc/get_server_config/deb.list
super_user_do mkdir -p /etc/get_server_config/gem
super_user_do touch /etc/get_server_config/gem.list
super_user_do mkdir -p /etc/get_server_config/bin
puts "* Ensureing executable location"
super_user_do mkdir -p /usr/local/bin
}
function puts {
# make echo command expandable to allow verbosity.
echo -e "$@"
}
function display_version_info {
puts "get 0.0.2 alpha (http://github.com/joshaven/get_server_config)"
}
function display_help {
display_version_info
puts "Usage: get [package] "
puts "Install applications using apt package manager."
puts "Examples:"
puts " get ruby"
puts " get --purge ruby"
puts " get --rebuild"
puts
puts "Options:"
puts " --purge [package_name] This will uninstall package and purge config"
puts " --rebuild Use this option to install any missing applications"
puts " --version Display version info."
puts " --self_install Install get to your system"
puts " --self_uninstall Remove get from path, leave config files intact"
puts " --self_purge Remove get & config files (Destructive!)"
puts " --self_update Download and Install current version of get"
puts
puts "Any .deb packages locate in /etc/get_server_config/deb will take precedence over"
puts "any apt repositories. Be sure to use the following naming convention:"
puts "package_name-version.deb for any applications in the local repo."
puts "Report any bugs to [email protected]"
puts "If you find this script useful please paypal my email address a buck or two!"
puts " -- Joshaven Potter <[email protected]> --"
}
function self_installer {
self=$0
if [ -e /usr/local/bin/get ];then self_uninstaller;fi # Remove executable symlink
self_version_migrator # Run version migrations
init_install_list # Ensure file structure is built
# Ensure path exists and copy this script to the installation folder
super_user_do cp -f $self /etc/get_server_config/bin/get
# link script into path
if [ -e /etc/get_server_config/bin/get ]; then
super_user_do ln -s /etc/get_server_config/bin/get /usr/local/bin/get
else
puts "Could not find '/etc/get_server_config/bin/get'"
return 1
fi
# Set script as executable and symlink to a good location
if [ -e /usr/local/bin/get ];then
super_user_do chmod +x /usr/local/bin/get
fi
# Test install
if [ -e '/usr/local/bin/get' ];then
puts "SUCCESSFULLY installed 'get'."
else
puts "Could not locate '/usr/local/bin/get'. Erron in install."
fi
# Test path
if [[ ! $PATH =~ (^|:)/usr/local/bin($|:) ]]; then
puts -e "> It appears that '/usr/local/bin' is not part of your path.\n*** Please add '/usr/local/bin' root's path ***"
else
successful=1
fi
if [ $successful ];then
puts "Enjoy your (get)ting. For help try 'get --help'"
else
puts "Error installing get"
fi
}
function self_version_migrator {
if [ -e /etc/server_config ]; then
super_user_do mv /etc/server_config /etc/get_server_config
fi
}
function self_uninstaller {
if [ -e /usr/local/bin/get ];then
super_user_do rm /usr/local/bin/get
if [ $? ];then puts "Successfully destroyed: executable"; fi
else
echo "No execuatables to remove, already uninstalled."
fi
}
function self_updater {
if [ -e /tmp/get_installer.sh ];then
rm /tmp/get_installer.sh
fi
if which wget>/dev/null; then
wget -O /tmp/get_installer.sh -- http://github.com/joshaven/get_server_config/raw/master/get_installer.sh
elif which curl>/dev/null; then
curl -o /tmp/get_installer.sh -- http://github.com/joshaven/get_server_config/raw/master/get_installer.sh
else
return 1
fi
if [ -e /tmp/get_installer.sh ];then
puts "Downloaded updates"
self_uninstaller # this is needed to remove the exeutable
else
puts "Error Downloading new get."
return 1
fi
sh /tmp/get_installer.sh --self_install
if [ $? ];then puts "Updated Successfully"; else puts "Error Installing"; fi
rm /tmp/get_installer.sh
}
function self_purger {
self_uninstaller
if [ -e /etc/get_server_config ]; then
super_user_do rm -r /etc/get_server_config
if [ $? ];then puts "Successfully destroyed: config files"; fi
else
puts "No config files to delete, already purged."
fi
}
function super_user_do {
if [ $(id -u) == 0 ]; then
$@
else
if [ $(which sudo) ];then
sudo $@
else
puts "This command requires super user privilages, Please log in as root or install sudo and add $(id -un) to sudoers"
return 1
fi
fi
}
function rebuild_deb {
while read package
do
if [ -n "$package" ]; then # -n tests to see if the argument is non empty
installed=$(dpkg --list |grep "^ii $package "|awk '{ print $2 }')
if [ "$package" == "$installed" ]; then
puts "$package installation confirmed."
else
puts
puts "Installing $package..."
install_packages $package
fi
fi
done < "/etc/get_server_config/deb.list"
}
function install_packages {
packages=($@) #convert string into an array
for (( i=0; i<=${#packages[@]}-1; i++ )); do # step through array
package=${packages[i]}
installed=$(dpkg --list |grep "^ii $package "|awk '{ print $2 }')
if [ "$package" == "$installed" ]; then
puts "$package installation confirmed."
else
if install_from_local $package; then
super_user_do dpkg --install `ls /etc/get_server_config/deb/$package*.deb`
else
super_user_do apt-get install -y $package
fi
fi
done
}
function install_from_local {
ls -1 /etc/get_server_config/deb/$1*.deb > /dev/null 2>&1
return $?
}
function purge_package {
#convert string into an array
packages=($@)
for (( i=0; i<=${#packages[@]}-1; i++ )); do
##FIXME need feature
puts "This would purge: ${packages[$i]}... if it were written to do so"
puts "please manually edit /etc/get_server_config/deb.list"
done
}
function append_deb_install_list {
packages=($@)
#array of packages
for (( i=0; i<=$[${#packages[@]}-1]; i++ )); do
if ls /etc/get_server_config/deb/|grep "^${packages[$i]}-[0-9].*.deb$";then # if .deb is found in /deb folder
if package_untracked ${packages[$i]} 'deb';then
super_user_do puts "${packages[$i]}" >> /etc/get_server_config/deb.list
fi
elif apt-cache search ${packages[$i]}|awk '{print $1}'|grep "^${packages[$i]}$";then # else if package is in repo
if package_untracked ${packages[$i]} 'deb';then
super_user_do puts "${packages[$i]}" >> /etc/get_server_config/deb.list
fi
else
puts "ERROR::Can not find package >> ${packages[$i]}"
puts "If you are installing a custom .deb package copy it to /etc/get_server_config/deb/"
puts "be sure to name the .deb package like: name-version.deb (ie. ruby-1.8.2-p72.deb)"
puts
return 1
fi
done
}
function append_gem_install_list {
packages=($@) #array of packages
for (( i=0; i<=$[${#packages[@]}-1]; i++ )); do # step through array, i is index a is the entire array
if ls /etc/get_server_config/gem/|grep "^${packages[$i]}-[0-9].*.gem$";then # if an existing .gem is around
if package_untracked ${packages[$i]} 'gem';then
super_user_do puts "${packages[$i]}" >> /etc/get_server_config/gem.list
fi
elif gem list ${packages[$i]}|awk '{print $1}'|grep "^${packages[$i]}$";then # if package is on a repo
if package_untracked ${packages[$i]} 'gem'; then
super_user_do puts "${packages[$i]}" >> /etc/get_server_config/gem.list
fi
else
puts "ERROR::Can not find package >> ${packages[$i]}"
puts "If you are installing a custom .gem package copy it to /etc/get_server_config/gem/"
puts "be sure to name the .gem package like: name-version.deb (ie. rails-2.2.2.gem)"
puts
return 1
fi
done
}
function package_untracked {
while read package; do
if [ $1 == $package ];then return 1;fi
done < "/etc/get_server_config/$2.list"
return 0
}
##################################################################################################################
# Main
# Process: Allows self installation without further checks. Otherwise if already installed, processessing request
# or crying about needing to be installed.
##################################################################################################################
if [[ $@ == '--self_install' ]]; then
self_installer $0
else
if [ -e '/usr/local/bin/get' ]; then
case $@ in
'--version')
display_version_info
;;
'' | '--help')
display_help
;;
'--purge*')
shift
purge_package $@
;;
'--rebuild')
rebuild_deb
#rebuild_gem #FIXME Need this to work
;;
'--gem*')
if super_user_do gem install $@;then
append_gem_install_list $@
fi
;;
'--self_install')
puts "get is allready installed."
;;
'--self_uninstall')
self_uninstaller
;;
'--self_update')
self_updater
;;
'--self_purge')
self_purger
;;
*)
if append_deb_install_list $@; then
install_packages $@
else
return 2
fi
;;
esac
else
puts
puts "NOTICE: get is NOT installed!"
puts "** Please Install by running 'sh $0 --self_install' install get"
fi
fi