-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip3_automatizer.sh
248 lines (199 loc) · 5.05 KB
/
pip3_automatizer.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
#! /bin/bash
# pip3_automatizer.sh - Automate boring programs downloaded from pip, the python package manager.
function how_to {
verbose "Not implemented yet..." $INFO
return 0
}
function verbose {
local MEN=$1
local LEVEL_MEN=$2
if [ "$LEVEL_MEN" = 1 ]
then
echo -e "\e[31mERROR:$MEN\e[0m"
elif [ "$LEVEL_MEN" = 0 ]
then
echo -e "\e[34mINFO:$MEN\e[0m"
else
echo "$MEN"
fi
return 0
}
function check_requirements {
if (($(id -u) != 0))
then
verbose "This script need to be executed as root!" $ERROR
return 1
elif ! hash python 2>/dev/null
then
verbose "Python wasn't found on the system!" $ERROR
return 1
elif ! hash pip 2>/dev/null
then
verbose "Pip wasn't found on the system!" $ERROR
return 1
elif ! hash crontab 2>/dev/null
then
verbose "Crontab wasn't found on the system!" $ERROR
return 1
else
verbose "Seems ok in system requirements!" $INFO
return 0
fi
}
function is_it_installed {
local CONF_FOLDER="/etc/Pip_Updater"
local SCRIPT_SOURCE="pip_script.sh"
local VERIFY_FILES=("$CONF_FOLDER/" "$CONF_FOLDER/pip_script.sh" "$CONF_FOLDER/pip_updater.conf")
local steps=4
local steps_done=0
for FILES_VER in ${VERIFY_FILES[*]}
do
if [ -s $FILES_VER ]
then
steps_done=$(($steps_done + 1))
fi
done
if crontab -l | grep -q "$CONF_FOLDER/$SCRIPT_SOURCE"
then
steps_done=$(($steps_done + 1))
fi
if (($steps_done == $steps))
then
verbose "It seems that this program is already installed" $INFO
verbose "Not installing it again" $INFO
return 0
else
verbose "It seems that this program is not installed yet" $INFO
verbose "Numbers of steps that worked, meaning that it found traces of the program: $steps_done" $INFO
return 1
fi
}
function install_process {
local SCRIPT_SOURCE="pip_script.sh"
local CONF_FOLDER="/etc/Pip_Updater"
verbose "Creating the folder and configuration file" $INFO
verbose "Configuration folder will be created at $CONF_FOLDER" $INFO
if [ -s $CONF_FOLDER ]
then
verbose "The folder is already created!" $ERROR
return 1
else
mkdir $CONF_FOLDER
if [ -s $CONF_FOLDER ]
then
verbose "The folder was created!" $INFO
else
verbose "The folder wasn't created!" $ERROR
return 1
fi
fi
local CONF_FILE="$CONF_FOLDER/pip_updater.conf"
verbose "Creating the configuration file" $INFO
echo "# One program per line" >> $CONF_FILE
if [ -s $CONF_FILE ]
then
verbose "The configuration file was created!" $INFO
else
verbose "The configuraion file wasn't created!" $ERROR
return 1
fi
verbose "Copying script to the $CONF_FOLDER" $INFO
cp "$SCRIPT_SOURCE" "$CONF_FOLDER/$SCRIPT_SOURCE"
if [ -s "$CONF_FOLDER/$SCRIPT_SOURCE" ]
then
verbose "The file was copied correctly!" $INFO
else
verbose "The file wasn't copied correctly!" $ERROR
return 1
fi
verbose "Configuring crontab" $INFO
# This fixes the empty space that is created when cron doesn't have anything to put before
if [ ! -z "$(crontab -l)" ]
then
(crontab -l && echo "30 12,00 * * * /bin/bash $CONF_FOLDER/$SCRIPT_SOURCE") | crontab
else
echo "30 12,00 * * * /bin/bash $CONF_FOLDER/$SCRIPT_SOURCE" | crontab
fi
chmod +x "$CONF_FOLDER/$SCRIPT_SOURCE"
verbose "Done configuring crontab!" $INFO
verbose "End of the installation!" $INFO
return 0
}
function desinstall_process {
local CONF_FOLDER="/etc/Pip_Updater"
local SCRIPT_SOURCE="pip_script.sh"
local TMP_LOG="/tmp/pip_updater.log"
verbose "Deleting the pip script now" $INFO
# Rm -f is done to supress the error message
# Thus, the program seems to run as it's expected
if [ -s $CONF_FOLDER ] && rm -r "$CONF_FOLDER"
then
verbose "Deleted the program folder" $INFO
else
verbose "Folder couldn't be deleted, moving on" $ERROR
fi
verbose "Deleting the crontab configuration" $INFO
if crontab -l | grep -q "$CONF_FOLDER/$SCRIPT_SOURCE"
then
(crontab -l | grep -v "$CONF_FOLDER/$SCRIPT_SOURCE") | crontab
verbose "Deleted crontab configuration" $INFO
else
verbose "No crontab configuration was found, moving on!" $ERROR
fi
verbose "Deleting temporary log" $INFO
if [ -s $TMP_LOG ] && rm $TMP_LOG
then
verbose "Temporary log deleted" $INFO
else
verbose "Temporary log couldn't be deleted!" $ERROR
fi
return 0
}
# Default variables for message info and error
readonly ERROR=1
readonly INFO=0
readonly UNLABELLED=-1
readonly EXIT_MESSAGE="Leaving the program now"
verbose "Do you want to install or desinstall or leave the script? " $UNLABELLED
read -p "install-desinstall-leave : " OPTION
if [ $OPTION = "install" ]
then
if check_requirements
then
if is_it_installed
then
verbose $EXIT_MESSAGE $UNLABELLED
exit
else
if install_process
then
verbose "Exiting $0"
exit
else
verbose $EXIT_MESSAGE $UNLABELLED
exit
fi
fi
else
verbose $EXIT_MESSAGE $UNLABELLED
exit
fi
elif [ $OPTION = "desinstall" ]
then
if check_requirements
then
if desinstall_process
then
verbose "Desinstalled!" $INFO
exit
else
verbose "Couldn't desinstall it!" $ERROR
exit
fi
else
verbose $EXIT_MESSAGE $UNLABELLED
fi
else
verbose $EXIT_MESSAGE $UNLABELLED
exit
fi