-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.sh
executable file
·167 lines (156 loc) · 5.9 KB
/
update.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
#!/bin/bash
CHMOD_FILES=0460
CHMOD_DIRECTORIES=2570
CHMOD_PROJECT=0700
OWNER_USER=www-data
OWNER_GROUP=www-developer
CLI_USER=root
CLI_GROUP=root
CHMOD_CLI=0700
CLIENT_USER=root
CLIENT_GROUP=root
CHMOD_CLIENT=0700
function usageParam() {
echo -e " \e[32m$1\e[33m $2"
echo -e " \e[39m$3"
if [ $4 != ".nodefault" ]; then
echo -e " Default: \e[94m$4"
fi
echo -e "\e[39m"
}
function usage() {
echo -e "\e[41m -- DYNDNS UPDATER USAGE -- \e[49m"
echo -e ""
echo -e "Parameters:"
usageParam "-ou|--ouser" "USER" "Owner user for web files and directories" $OWNER_USER
usageParam "-og|--ogroup" "GROUP" "Owner group for web files and directories" $OWNER_GROUP
usageParam "-cu|--cuser" "USER" "Owner user for CLI script files and directory" $CLI_USER
usageParam "-cg|--cgroup" "GROUP" "Owner group for CLI script files and directory" $CLI_GROUP
usageParam "-du|--duser" "USER" "Owner user for client script files and directory" $CLIENT_USER
usageParam "-dg|--dgroup" "GROUP" "Owner group for client script files and directory" $CLIENT_GROUP
usageParam "-mf|--mfile" "MODE" "Permission mode for web files" $CHMOD_FILES
usageParam "-md|--mdir" "MODE" "Permission mode for web directories" $CHMOD_DIRECTORIES
usageParam "-mp|--mproject" "MODE" "Permission mode for project files (e.g. .gitignore)" $CHMOD_PROJECT
usageParam "-mc|--mcli" "MODE" "Permission mode for CLI scripts" $CHMOD_CLI
usageParam "-md|--mclient" "MODE" "Permission mode for client scripts" $CHMOD_CLIENT
usageParam "-h|--help" "" "Show this usage help" ".nodefault"
echo -e ""
}
while [ "$1" != "" ]; do
case $1 in
-mf | --mfile ) shift
CHMOD_FILES=$1
;;
-md | --mdir ) shift
CHMOD_DIRECTORIES=$1
;;
-mp | --mproject ) shift
CHMOD_PROJECT=$1
;;
-mc | --mcli ) shift
CHMOD_CLI=$1
;;
-md | --mclient ) shift
CHMOD_CLIENT=$1
;;
-ou | --ouser ) shift
OWNER_USER=$1
;;
-og | --ogroup ) shift
OWNER_GROUP=$1
;;
-cu | --cuser ) shift
CLI_USER=$1
;;
-cg | --cgroup ) shift
CLI_GROUP=$1
;;
-du | --duser ) shift
CLIENT_USER=$1
;;
-dg | --dgroup ) shift
CLIENT_GROUP=$1
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
# args: text, back, text
function printWithColor() {
echo -e "\e[$2m\e[$3m[updater] $1\e[49m\e[39m"
}
function cdBack() {
printWithColor "Switching back to $CW_DIR..." 46 30
cd $CW_DIR
}
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
CW_DIR=`pwd`
printWithColor "Switching to $SCRIPT_DIR..." 46 30
cd $SCRIPT_DIR
printWithColor "Checking differences from remote repository..." 45 30
WCL_LOG=`git log origin/master..HEAD | wc -l`
if [ $WCL_LOG -gt 0 ]; then
printWithColor "Found pending commits." 41 30
printWithColor "Please push them or reset branch before continuing the update." 41 30
cdBack
printWithColor "Exit." 41 30
exit 1
fi
WCL_ADD=`git diff | egrep '^\+' | wc -l`
WCL_REMOVE=`git diff | egrep '^\-' | wc -l`
if [ $WCL_ADD -gt 0 ] || [ $WCL_REMOVE -gt 0 ]; then
printWithColor "Found difference in the content of files." 41 30
printWithColor "Please commit and push or reset them before continuing the update." 41 30
cdBack
printWithColor "Exit." 41 30
exit 2
fi
WCL_GITDIFF=`git diff | wc -l`
if [ "$WCL_GITDIFF" -eq 0 ]; then
printWithColor "No difference found." 42 30
else
WCL_DIFF=`git diff | egrep '^diff' | wc -l`
WCL_NEWMODE=`git diff | egrep '^new mode' | wc -l`
WCL_OLDMODE=`git diff | egrep '^old mode' | wc -l`
WCL_TOTAL=$((WCL_DIFF + WCL_NEWMODE + WCL_OLDMODE))
if [ "$WCL_TOTAL" -eq "$WCL_GITDIFF" ]; then
printWithColor "Difference only found in file modes." 42 30
printWithColor "Reseting local repository..." 45 30
git reset --hard
else
printWithColor "Unknown difference found or error occurred." 41 30
printWithColor "Please check with 'git diff' and 'git status' before continuing the update." 41 30
cdBack
printWithColor "Exit." 41 30
exit 3
fi
fi
printWithColor "git pull" 45 30
git pull
printWithColor "Set owners" 45 30
printWithColor "Owner user: $OWNER_USER" 103 30
printWithColor "Owner group: $OWNER_GROUP" 103 30
printWithColor "Owner of CLI scripts: ${CLI_USER}:${CLI_GROUP}" 103 30
printWithColor "Owner of client scripts: ${CLIENT_USER}:${CLIENT_GROUP}" 103 30
chown -R $OWNER_USER:$OWNER_GROUP .
chown -R $CLI_USER:$CLI_GROUP ./cli
chown -R $CLIENT_USER:$CLIENT_GROUP ./client
printWithColor "Set permissions" 45 30
printWithColor "Mode for files: $CHMOD_FILES" 103 30
printWithColor "Mode for directories: $CHMOD_DIRECTORIES" 103 30
printWithColor "Mode for CLI scripts: $CHMOD_CLI" 103 30
printWithColor "Mode for client scripts: $CHMOD_CLIENT" 103 30
printWithColor "Mode for project files: $CHMOD_PROJECT" 103 30
find . -type f -exec chmod $CHMOD_FILES {} \;
find . -type d -exec chmod $CHMOD_DIRECTORIES {} \;
find cli -type f -exec chmod $CHMOD_CLI {} \;
find client -type f -exec chmod $CHMOD_CLIENT {} \;
chmod $CHMOD_PROJECT .gitignore update.sh
chmod +x update.sh
cdBack
printWithColor "Ready." 42 30
exit 0