-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker.sh
executable file
·162 lines (130 loc) · 2.94 KB
/
docker.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
#!/bin/bash
# Default values
run_tests=0
LOCAL_PORT=""
# Help message
help_message() {
echo "Usage: docker.sh [OPTIONS]"
echo "Options:"
echo " --local-port Local port to bind for the GUI"
echo " --run_tests Run tests before starting"
echo " --help Show this help message"
}
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--run_tests)
run_tests=1
shift
;;
--local-port)
LOCAL_PORT="$2"
shift
;;
--help)
help_message
exit 0
;;
*)
echo "Error: Unknown option '$1'. Use --help for usage."
exit 1
;;
esac
shift
done
# Check for required parameters
if [[ -z $LOCAL_PORT ]]; then
echo "Error: Missing required parameter --local-port. Use --help for usage."
exit 1
fi
is_package_installed() {
dpkg-query -W -f='${Status}' "$1" 2>/dev/null | grep -c "ok installed"
}
UPDATED_PACKAGES=0
# Check if Docker is installed
if ! command -v docker &>/dev/null; then
echo "Docker not found. Installing Docker..."
# Enable non-free repository
sed -i 's/main$/main contrib non-free/g' /etc/apt/sources.list
# Update package lists
if [[ $UPDATED_PACKAGES == 0 ]]; then
sudo apt update || {
echo "apt-get update failed. Are you online?"
exit 2
}
UPDATED_PACKAGES=1
fi
# Install Docker
sudo apt install -y docker.io docker-compose || {
echo "sudo apt install -y docker.io failed"
exit 3
}
fi
if ! command -v wget &>/dev/null; then
# Update package lists
if [[ $UPDATED_PACKAGES == 0 ]]; then
sudo apt update || {
echo "apt-get update failed. Are you online?"
exit 3
}
UPDATED_PACKAGES=1
fi
sudo apt-get install -y wget || {
echo "sudo apt install -y wget failed"
exit 3
}
fi
if ! command -v git &>/dev/null; then
# Update package lists
if [[ $UPDATED_PACKAGES == 0 ]]; then
sudo apt update || {
echo "apt-get update failed. Are you online?"
exit 3
}
UPDATED_PACKAGES=1
fi
sudo apt-get install -y git || {
echo "sudo apt install -y git failed"
exit 4
}
fi
git rev-parse HEAD > git_hash
export LOCAL_PORT
# Write environment variables to .env file
echo "#!/bin/bash" > .env
echo "LOCAL_PORT=$LOCAL_PORT" >> .env
echo "=== Current git hash before auto-pulling ==="
git rev-parse HEAD
echo "=== Current git hash before auto-pulling ==="
git pull
function die {
echo $1
exit 1
}
SYNTAX_ERRORS=0
{ for i in $(ls *.php); do if ! php -l $i 2>&1; then SYNTAX_ERRORS=1; fi ; done } | 2>&1 grep -v mongodb
if [[ "$SYNTAX_ERRORS" -ne "0" ]]; then
echo "Tests failed";
exit 1
fi
if [[ "$run_tests" -eq "1" ]]; then
php testing.php && echo "Syntax checks for PHP Ok" || die "Syntax Checks for PHP failed"
fi
function docker_compose {
if command -v docker-compose 2>/dev/null >/dev/null; then
sudo docker-compose $*
else
sudo docker compose $*
fi
}
docker_compose build || {
rm git_hash
echo "Failed to build container"
exit 254
}
docker_compose up -d || {
rm git_hash
echo "Failed to build container"
exit 255
}
rm git_hash