-
Notifications
You must be signed in to change notification settings - Fork 38
/
upgrade.sh
executable file
·150 lines (116 loc) · 5.43 KB
/
upgrade.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
#!/bin/bash
# Set different console colors
red=`tput setaf 1`
green=`tput setaf 2`
magenta=`tput setaf 5`
cyan=`tput setaf 6`
reset=`tput sgr0`
printf "\n ${cyan} NetConfig Upgrade Script \n\n ${reset}"
### Step 1 - Verify script is not running as root
PREFIX="sudo"
if [ "$(whoami)" = "root" ]; then
# Prevent user from running script directly as root
printf "\n ${red} NetConfig upgrade should not be run as root. Please run the upgrade script again as local user 'netconfig'.
You will be prompted for the user 'netconfig' password when a command needs to be run with elevated privileges. \n\n ${reset}"
exit
fi
### Step 2 - Get Current Version
CURRENTVERSION=`cat config.py | grep VERSION | cut -d \' -f 2`
printf "\n ${cyan} Current NetConfig version is ${CURRENTVERSION} \n\n ${reset}"
### Step 2 - Set working directory
cd /home/netconfig/netconfig
### Step 3 - Checkout master branch in git
COMMAND="git checkout master"
printf "\n ${cyan} Checking out master NetConfig branch in Git... ${reset} \n\n"
eval $COMMAND
### Step 4 - Verify Git status
printf "\n ${cyan} Verifying Git status... ${reset} \n"
if output=$(git status --porcelain) && [ -z "$output" ]; then
printf "\n ${green} The NetConfig installation is ready to be updated ${reset} \n\n"
else
printf "\n ${red} The NetConfig installation cannot be updated as there are Git merge conflicts.\n\n"
printf " Please delete or revert any changes made to the following files: \n\n ${magenta}"
git status --porcelain
printf "${reset} \n\n"
exit
fi
### Step 5 - Pull updated files from master branch
printf "\n ${cyan} Pulling any updated files from master NetConfig branch in Git... ${reset} \n\n"
git pull origin master | grep 'Already up-to-date'
if [ $? == 0 ]
then
printf "\n ${red} NetConfig files are already up to date with the most recent version on Git. Skipping pulling updated files from Git. ${reset} \n\n"
UPDATERESULT=false
else
printf "\n ${green} NetConfig files updated from master NetConfig branch in Git ${reset} \n\n"
UPDATERESULT=true
fi
### Step 6 - Delete stale bytecode
COMMAND="find . -name \"*.pyc\" -delete"
printf "\n ${cyan} Cleaning up stale Python bytecode... ${reset} \n\n"
eval $COMMAND
### Step 7 - Uninstall any old Python packages no longer used
COMMAND="${PREFIX} pip uninstall -r old_requirements.txt -y"
printf "\n ${cyan} Removing old Python packages... $(reset) \n\n"
eval $COMMAND
### Step 8 - Install any new Python packages
COMMAND="${PREFIX} pip install -r requirements.txt --upgrade"
printf "\n ${cyan} Updating required Python packages... ${reset} \n\n"
eval $COMMAND
### Step 9 - Verify Nginx NetConfig settings file is correct
MATCHFIELD="\$remote_addr;"
INSERTFIELD1=" proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;"
INSERTFIELD2=" proxy_set_header X-Forwarded-Proto \$scheme;"
CHECKCOMMAND=`cat /etc/nginx/sites-available/netconfig | grep proxy_set_header | wc -l`
FILE="/etc/nginx/sites-available/netconfig"
printf "\n ${cyan} Updating NGINX NetConfig settings file... ${reset} \n\n"
if [ "$CHECKCOMMAND" == "2" ]
then
${PREFIX} sed -i "s/$MATCHFIELD/$MATCHFIELD\n$INSERTFIELD1\n$INSERTFIELD2/" $FILE
printf "\n ${green} File updated successfully \n\n"
${PREFIX} service nginx restart
printf "\n ${green} NGINX service restarted successfully ${reset} \n\n"
else
printf "\n ${red} File does not need to be updated. Skipping. ${reset} \n\n"
fi
### Step 10 - Upgrade Database
COMMAND="python db_upgrade.py"
printf "\n ${cyan} Upgrading database schema to latest version... ${reset} \n\n"
FILE="app.db"
if [ -f "$FILE" ]
then
eval $COMMAND
printf "\n {$green} Database schema updated successfully."
else
printf "\n {$red} Database file app.db not found. Database schema not updated.
If using the local database for device inventory, please manually run the the following command:
${magenta} python /home/netconfig/netconfig/db_upgrade.sh ${reset} \n\n"
fi
### Step 11 - Update settings file with any new additions from settings template (if necessary)
if [[ $(awk 'FNR==NR{a[$1]=$0;next} !($1 in a)' instance/settings.py instance/settings_template.py) ]]; then
COMMAND="awk '{b=\$0;\$NF=\"\"}NR==FNR{a[\$0];print b;next}/^#/||!\$0{next}!(\$0 in a){print b}' instance/settings.py instance/settings_template.py > instance/settings.new"
eval $COMMAND
COMMAND="mv instance/settings.new instance/settings.py"
eval $COMMAND
printf "\n ${green} Settings file updated with new variables. Consult instance/settings_template.py or documentation for descriptions on new variables. \n\n ${reset}"
else
printf "\n ${magenta} Settings file does not need to be updated. Skipping. \n\n ${reset}"
fi
### Step 12 - Restart supervisorctl process
COMMAND="${PREFIX} supervisorctl restart netconfig"
printf "\n ${cyan} Restarting supervisorctl NetConfig process... ${reset} \n\n"
eval $COMMAND
### Step 13 - Notify of successful upgrade
NEWVERSION=`cat config.py | grep VERSION | cut -d \' -f 2`
if [ "$UPDATERESULT" == false ]
then
printf "\n ${green} NetConfig successfully upgraded to version ${NEWVERSION} \n\n ${reset}"
else
if [ "$CURRENTVERSION" == "$NEWVERSION" ]
then
printf "\n ${red} NetConfig was not upgraded. Please check your server's access to GitHub and the internet, and try again. \n"
printf " Current NetConfig Version: ${NEWVERSION} \n\n ${reset}"
else
printf "\n ${green} NetConfig successfully upgraded to version ${NEWVERSION} \n\n ${reset}"
fi
fi