-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-up
executable file
·179 lines (164 loc) · 7.91 KB
/
docker-up
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
#!/usr/bin/env bash
# Vars
UBUNTU_VERSION=$(<ubuntu-version)
if [ -f "override-ubuntu-version" ]; then
UBUNTU_VERSION=$(<override-ubuntu-version)
fi
# Config Files
## DockerFile-Template -> Dockerfile-Computed if does not exist
if [ ! -f Dockerfile-Computed ]; then
cp Dockerfile-Template Dockerfile-Computed
fi
## docker-compose-template.yml -> docker-compose-computed.yml if does not exist
if [ ! -f docker-compose-computed.yml ]; then
cp docker-compose-template.yml docker-compose-computed.yml
fi
## Replace "FROM ubuntu:xx.xx" with FROM ubuntu:$UBUNTU_VERSION in Dockerfile-Computed where each x is any single digit
sed -i '' "s/FROM ubuntu:[1-9][0-9]\.[0-9][0-9]/FROM ubuntu:$UBUNTU_VERSION/g" Dockerfile-Computed
# if pre-run does not exist, then copy the default
if [ ! -f pre-run/pre-run ]; then
cp pre-run/pre-run-default pre-run/pre-run
fi
H=$(sed -e 's#.*\-h=\([^[:space:]]*\?\).*#\1#' <<< "$*")
C=$(sed -e 's#.*\-c=\([^[:space:]]*\?\).*#\1#' <<< "$*")
D=$(sed -e 's#.*\-d=\([^[:space:]]*\?\).*#\1#' <<< "$*")
if [ "$H" == "-h" ]; then
printf "Use -c to specify building with no-cache eg. -c=true; default is false\n"
printf "Use -d to specify building with no-cache & verbosely eg. -d=true; default is false\n"
exit 1;
fi
# if -c=true, then build with no-cache
if [ "$C" == "-c=true" ]; then
printf "============ Docker Build - with forcing re-cache\n"
docker-compose -p "dockerlocal3090" -f docker-compose-computed.yml build --no-cache
elif [ "$D" == "-d=true" ]; then
printf "============ Docker Pull\n"
docker-compose -p "dockerlocal3090" -f docker-compose-computed.yml build --no-cache --progress=plain
fi
printf "============ Docker Build\n"
docker-compose -p "dockerlocal3090" -f docker-compose-computed.yml up --build -d
status=$?
if [ $status -eq 0 ]; then
printf "============= ✅ Docker is running!\n"
printf "============= ℹ️ This simple docker does not have a webserver, but if you did install & configure one, you'd see your site here: http://localhost:3090\n"
printf "============= 👉 If you want to SSH: "
echo "./docker-ssh" | sed -e "s/.*/"$'\e[32m'"&"$'\e[m'"/"
printf "\n"
# prompt the user to select a post action
printf "\n============ POST-ACTION: \n"
printf "1. Run a script\n"
printf "2. SSH\n"
printf "3. View logs\n"
printf "4. Create a script\n"
read -p "Enter the number of the post action you want to perform (ctrl+d to exit): " -r
echo
if [[ $REPLY =~ ^[1-4]$ ]]; then
case $REPLY in
1)
# Scan the /scripts/ directory for folders starting with "server-"
# and prompt the user to select one
# so that a scripts/server-<server-name> would be an option 1. <server-name>
# and then run ./docker-run -n <server-name>
SCRIPTS=$(find scripts/ -maxdepth 1 -type d -name "server-*" -exec basename {} \;)
SCRIPTS_ARRAY=($SCRIPTS)
SCRIPTS_ARRAY_LENGTH=${#SCRIPTS_ARRAY[@]}
if [ $SCRIPTS_ARRAY_LENGTH -gt 0 ]; then
printf "\n============ SELECT SCRIPT: \n"
for (( i=0; i<${SCRIPTS_ARRAY_LENGTH}; i++ ));
do
printf "$((i+1)). ${SCRIPTS_ARRAY[$i]}\n"
done
printf "\n"
read -p "Enter the number of the script you want to run: " -r
echo
if [[ $REPLY =~ ^[0-9]+$ ]]
then
if [ $REPLY -gt 0 ] && [ $REPLY -le $SCRIPTS_ARRAY_LENGTH ]; then
# GET SCRIPT NAME FROM ARRAY
INDEX=$((REPLY-1))
SCRIPT_NAME=${SCRIPTS_ARRAY[$INDEX]}
printf "============= 🏃♀️ Running script: $SCRIPT_NAME\n"
# remove the "server-" prefix
SCRIPT_NAME=${SCRIPT_NAME:7}
./docker-run -n $SCRIPT_NAME
else
printf "============= ❌ Invalid number.\n"
fi
else
printf "============= ❌ Invalid number.\n"
fi
else
printf "============= ❌ No scripts found.\n"
fi
;;
2)
printf "============= 👉 SSH: "
echo "./docker-ssh" | sed -e "s/.*/"$'\e[32m'"&"$'\e[m'"/"
printf "\n"
./docker-ssh
;;
3)
printf "============ VIEW LOGS: \n"
echo "� ./docker-log" | sed -e "s/.*/"$'\e[32m'"&"$'\e[m'"/"
printf "\n"
./docker-log
;;
4)
printf "============ CREATE SCRIPT: \n"
# ask if we need to use a custom template
printf "============ Would you like to use a custom template? (y/n)\n"
read -r USE_CUSTOM_TEMPLATE
# if using custom template, the command is ./docker-script -n <script-name> -t <template-name>
if [ "$USE_CUSTOM_TEMPLATE" = "y" ]; then
# Scan the /scripts/templates directory for files starting with "template-"
# and prompt the user to select one
# so that a scripts/templates/template-<template-name> would be an option 1. <template-name>
# and then run ./docker-script -n <script-name> -t <template-name>
TEMPLATES=$(find scripts/templates/ -maxdepth 1 -type f -name "template-*" -exec basename {} \;)
TEMPLATES_ARRAY=($TEMPLATES)
TEMPLATES_ARRAY_LENGTH=${#TEMPLATES_ARRAY[@]}
if [ $TEMPLATES_ARRAY_LENGTH -gt 0 ]; then
printf "\n============ SELECT TEMPLATE: \n"
for (( i=0; i<${TEMPLATES_ARRAY_LENGTH}; i++ ));
do
printf "$((i+1)). ${TEMPLATES_ARRAY[$i]}\n"
done
printf "\n"
read -p "Enter the number of the template you want to use: " -r
echo
if [[ $REPLY =~ ^[0-9]+$ ]]
then
if [ $REPLY -gt 0 ] && [ $REPLY -le $TEMPLATES_ARRAY_LENGTH ]; then
# GET TEMPLATE NAME FROM ARRAY
INDEX=$((REPLY-1))
TEMPLATE_NAME=${TEMPLATES_ARRAY[$INDEX]}
printf "============= 🏃♀️ Creating script with template: $TEMPLATE_NAME\n"
# remove the "template-" prefix
TEMPLATE_NAME=${TEMPLATE_NAME:9}
read -p "Enter the script name: " -r
echo
./docker-script -n $REPLY -t $TEMPLATE_NAME
else
printf "============= ❌ Invalid number.\n"
fi
else
printf "============= ❌ Invalid number.\n"
fi
else
printf "============= ❌ No templates found.\n"
fi
else
# else the command is ./docker-script -n <script-name>
echo "🔨 ./docker-script -n <script-name>" | sed -e "s/.*/"$'\e[32m'"&"$'\e[m'"/"
read -p "Enter the script name: " -r
echo
./docker-script -n $REPLY
fi
;;
esac
else
printf "============= ❌ Invalid number.\n"
fi
else
printf "============= ❌ Docker failed to build.\n"
fi