-
Notifications
You must be signed in to change notification settings - Fork 0
/
vesl
executable file
·223 lines (209 loc) · 7.32 KB
/
vesl
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
#!/bin/bash
# This file is part of vesl.
# (c) Jim Fisk <[email protected]>
# For the full copyright and license information, please view the MIT
# license located at https://github.com/Jantcu/vesl/blob/master/LICENSE.
# Draws "vesl" in CLI block letters.
function veslbrand() {
echo -e "${LightCyan} _ ${LightPurple} _~ ${NC} "
echo -e "${LightCyan}__ _____ ___| | ${LightPurple} _~ )_)_~ ${NC} "
echo -e "${LightCyan}\ \ / / _ \/ __| | ${LightPurple} )_))_))_) ${NC} "
echo -e "${LightCyan} \ V / __/\__ \ | ${LightPurple} _!__!__!_ ${NC} "
echo -e "${LightCyan} \_/ \___||___/_| ${LightPurple} \_______/ ${NC} "
echo -e "${LightBlue}"
echo "~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~ "
echo "^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^ "
echo -e "${NC}"
}
# Define colors
Black='\033[0;30m'
DarkGray='\033[1;30m'
Red='\033[0;31m'
LightRed='\033[1;31m'
Green='\033[0;32m'
LightGreen='\033[1;32m'
BrownOrange='\033[0;33m'
Yellow='\033[1;33m'
Blue='\033[0;34m'
LightBlue='\033[1;34m'
Purple='\033[0;35m'
LightPurple='\033[1;35m'
Cyan='\033[0;36m'
LightCyan='\033[1;36m'
LightGray='\033[0;37m'
White='\033[1;37m'
NC='\033[0m' # No Color
# Prints the current version of the project
function veslversion() {
echo -e "${Green}Version ${Yellow}0.0.SeriouslyThisIsExperimental${NC}"
}
# Defines the available commands
function vesloptions() {
echo -e "${Yellow}Options:${NC}"
echo -e "${Green} --help${NC},${Green} -h${NC} Documentation providing additional context for commands."
echo -e "${Green} --version${NC},${Green} -v${NC} Prints release details of the vesl that you are using."
echo -e "${Green} --all${NC},${Green} -a${NC} Add to the 'list' command to display all containers available to vesl."
}
# Defines the available commands
function veslcommands() {
echo -e "${Yellow}Commands:${NC}"
echo -e "${Green} load${NC} Downloads a vesl blueprint (docker image)."
#echo -e "${Green} use${NC} Sets the blueprint (docker image) for creating a new vesl (docker container)."
echo -e "${Green} release${NC} Deletes a blueprint (docker image) using the 'rmi' command."
echo -e "${Green} list${NC},${Green} ls${NC} Displays all vesls (docker containers) created using a vesl blueprint."
echo -e "${Green} cast${NC},${Green} create${NC} Creates a new vesl (docker container) from the vesl blueprint in 'use'."
echo -e "${Green} sink${NC},${Green} destroy${NC} Deletes all references to an existing vesl (docker container)."
echo -e "${Green} sail${NC},${Green} start${NC} Starts a vesl (docker container) that has already been created."
echo -e "${Green} dock${NC},${Green} stop${NC} Stops a vesl (docker container) that is currently running."
}
# Start an existing vesl.
function veslstart() {
if [[ -n "$arg" ]]; then
container=$arg
echo -ne "Starting vesl "
docker start $container
docker exec -it $container service apache2 start
docker exec -it $container service mysql start
docker exec -it $container script -q -c "fish" /dev/null
else
echo "Please specify a vesl"
fi
}
# Stop a running vesl.
function veslstop() {
if [[ -n "$arg" ]]; then
container=$arg
echo -ne "Stopping vesl ${Green}$container${NC}"
echo $'\r'
if docker stop $container >/dev/null; then
echo -ne "Vesl ${Green}$container${NC} was stopped."
else
echo -ne "Vesl ${Green}$container${NC} could not be stopped."
fi
else
echo "Please specify a vesl"
fi
}
# Load a blueprint.
function veslload() {
if [[ -n "$arg" ]]; then
image=$arg
docker pull vesl/$image
else
echo "Please specify a vesl"
fi
}
# Delete a blueprint.
function veslrelease() {
if [[ -n "$arg" ]]; then
image=$arg
docker rmi vesl/$image
else
echo "Please specify a vesl"
fi
}
# Create a new vesl.
function veslcreate() {
# Check that the user actually supplied an argument.
if [[ -n "$arg" ]]; then
image=$arg
# Check if the user input is a valid docker image.
if ! docker inspect --type=image vesl/$image >/dev/null; then
# If the image supplied by user doesn't exist, tell them and stop script.
echo -e "${Green}$image${NC} is NOT a valid vesl blueprint"
echo "Available blueprints: "
docker images --format "table{{ .Repository }}" | grep vesl | awk -F'/' '{print $2}'
exit
fi
# Ask the user to name the container they are creating.
read -p "Name your vesl: " container
echo $'\r'
# Define an iterator
n=0
# Start a while loop that tries 5 times to get an open port.
until [ $n -ge 5 ]
do
# Create a random number between 1 and 50.
variant=$((1 + RANDOM % 50))
# Add the random number to the standard host port.
port=$(expr "80" + "$variant")
# Add the random number to the SSL/TLS port.
ssl_port=$(expr "443" + "$variant")
# Try to create the container with ports that were randomly created.
docker run -dit --name $container -v $(pwd):/var/www/drupal -v $HOME/.gitconfig:/root/.gitconfig -v $HOME/.ssh:/root/.ssh -p 0.0.0.0:$ssl_port:443 -p 0.0.0.0:$port:80 vesl/$image /bin/bash && break
# Increase the iterator.
n=$[$n+1]
sleep 15
done
else
echo "Please specify a blueprint"
echo "Available blueprints: "
docker images --format "table{{ .Repository }}" | grep vesl | awk -F'/' '{print $2}'
fi
}
# Delete a vesl.
function vesldestroy() {
if [[ -n "$arg" ]]; then
container=$arg
read -p "Are you sure you want to permanently delete vesl $container (y/n)? " -n 1 -r
echo $'\r'
if [[ $REPLY =~ ^[Yy]$ ]]; then
if docker rm $container >/dev/null; then
echo -e "Vesl ${Green}$container${NC} has been deleted."
else
echo -e "Vesl ${Green}$container${NC} can NOT be deleted."
fi
fi
else
echo "Please specify a vesl"
fi
}
# List all existing docker containers.
function vesllist() {
if [[ $arg == "--all" ]] || [[ $arg == "-a" ]]; then
docker ps -a --format "table{{ .Image }}\\t{{ .Names }}\\t{{ .Status }}\\t{{ .Ports }}"
else
echo -e "${LightBlue}Drupal Vesls${NC}"
docker ps -a --format "table{{ .Image }}\\t{{ .Names }}\\t{{ .Status }}\\t{{ .Ports }}" --filter "ancestor=vesl/drupal"
echo -e "${LightBlue}Wordpress Vesls${NC}"
docker ps -a --format "table{{ .Image }}\\t{{ .Names }}\\t{{ .Status }}\\t{{ .Ports }}" --filter "ancestor=vesl/wordpress"
fi
}
# Get the user input
command=$1
arg=$2
if [[ -n "$command" ]]; then
if [[ $command == "sail" ]] || [[ $command == "start" ]]; then
veslstart
elif [[ $command == "dock" ]] || [[ $command == "stop" ]]; then
veslstop
elif [[ $command == "cast" ]] || [[ $command == "create" ]]; then
veslcreate
elif [[ $command == "sink" ]] || [[ $command == "destroy" ]]; then
vesldestroy
elif [[ $command == "list" ]] || [[ $command == "ls" ]]; then
vesllist
elif [[ $command == "load" ]]; then
veslload
elif [[ $command == "release" ]]; then
veslrelease
elif [[ $command == "--help" ]] || [[ $command == "-h" ]]; then
vesloptions
veslcommands
elif [[ $command == "--version" ]] || [[ $command == "-v" ]]; then
veslversion
else
echo "$command is not a valid command. Please use \"vesl --help\" to see what is available."
fi
# The user did not give any input.
else
echo $'\r'
veslbrand
echo $'\r'
veslversion
echo $'\r'
vesloptions
echo $'\r'
veslcommands
echo $'\r'
fi