Skip to content

Instantly share code, notes, and snippets.

@dabumana
Last active November 26, 2022 02:17
Show Gist options
  • Save dabumana/f19a9870bf9147da3698d69e0cfa8c1d to your computer and use it in GitHub Desktop.
Save dabumana/f19a9870bf9147da3698d69e0cfa8c1d to your computer and use it in GitHub Desktop.
This tool help you to generate an initial structure and *configuration* file, you can setup your own environment, according to the requirements of your project please follow the instructions to complete the process of initial setup and then you can *build* your services for production or test purposes.
#!/bin/bash
#
# Author: @dabumana
# Copyright BSD
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
#1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
#2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
#3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
function stack_conf() {
echo -ne "\e[1;31m
Stack configuration\e[1;33m
-------------------\e[1;36m
Define the path of the config file (Kubernetes or Docker-compose)
\e[1;35m1. \e[1;33mKubernetes
\e[1;35m2. \e[1;33mDocker-compose
\e[1;35m
*Selection:\e[1;31m"
read -n 1 select
case "$select" in
1)
echo -ne "\e[1;36m
Stack name (Kubernetes):\e[1;31m"
read name
echo -ne "\e[1;36m
Define the path of the file (dir/to/file):\e[1;31m"
read path
docker stack deploy --compose-file "$path" "$name"
;;
2)
echo -ne "\e[1;36m
Stack name (Docker-compose):\e[1;31m"
read name
echo -ne "\e[1;36m
Define the path of the file (dir/to/file):\e[1;31m"
read path
docker stack deploy --kubeconfig "$path" "$name"
;;
[!1-2]*)
echo -ne "
*** UNKNOW OPTION... LEAVING PROGRAM. ***
"
exit 1
esac
}
function compose() {
[ -f "docker-compose.yml" ] && rm docker-compose.yml || echo -en "
\e[1;31m
Docker-Compose\e[1;36m
--------------\e[0m
"
[ -d "services" ] && rm -rf services || mkdir services
echo -en "\e[1;33m
Fill the details of your config file.
\e[1;36m
Version:\e[1;31m"
read version
echo "
version: '"$version"'" >> docker-compose.yml
echo -en "\e[1;35m
*Remember that some options are functional just for the version.\e[0m
"
echo "
networks:" >> docker-compose.yml
echo -en "\e[1;36m
Networks (number of networks to parent):\e[1;31m"
read -n 1 numnet
if [[ "$numnet" =~ ^[1-9]+$ ]]
then
for i in $(seq 1 "$numnet");do
echo -en "\e[1;36m
Network name:\e[1;31m"
read name
echo "
"$name":
driver: bridge" >> docker-compose.yml
echo -en "\e[1;32m
*** NETWORK ENABLE [ \e[1;31m"$name"\e[1;32m ] ***
\e[0m"
done
else
echo -en "\e[1;31m
*** NETWORK NOT DEFINED ***
\e[0m"
fi
echo "
default:
driver: host" >> docker-compose.yml
echo "
volumes:" >> docker-compose.yml
echo -en "\e[1;36m
Volumes (number of volumes to parent with nodes):\e[1;31m"
read -n 1 numvol
if [[ "$numvol" =~ ^[1-9]+$ ]]
then
for i in $(seq 1 "$numvol");do
echo -en "\e[1;36m
Volume data name:\e[1;31m"
read name
echo "
"$name": " >> docker-compose.yml
echo -en "\e[1;32m
*** VOLUME ENABLE [ \e[1;31m"$name"\e[1;32m ] ***
\e[0m"
done
else
echo -en "\e[1;31m
*** VOLUMES NOT DEFINED ***
\e[0m"
fi
echo -en "\e[1;36m
Service nodes (number of nodes with services to define):\e[1;31m"
read -n 1 quantity
echo "
services:" >> docker-compose.yml
if [[ "$quantity" =~ ^[1-9]+$ ]]
then
for i in $(seq 1 "$quantity");do
echo -en "\e[1;36m
Container name:\e[1;31m"
read name
echo -en "\e[1;36m
Container image:\e[1;31m"
read image
ENV=services/"$name"/
[ ! -d ""$ENV"" ] && mkdir -p "$ENV" || echo -ne "\e[1;35m
*Skiping folder creation
\e[1;31m*** SERVICE DOCKER FOLDER EXIST ***
\e[0m"
echo -ne "\e[1;31m
Provide the details of build and context.\e[1;36m
-----------------------------------------\e[1;32m
**BUILD\e[1;36m
Context: \e[1;31m"$ENV"\e[0m"
echo -ne "\e[1;36m
Dockerfile:\e[1;31m"
read dockerfile
echo "
FROM "$image"
#COPY
#RUN
#CMD
" >> services/"$name"/"$dockerfile"
echo -ne "\e[1;36m
SHM size (xG):\e[1;31m"
read shm
echo "
"$name":
image: "$image"
build:
context: "$ENV"
dockerfile: "$dockerfile"" >> docker-compose.yml
echo -ne "\e[1;32m
**DEPLOY
\e[1;36m
Select mode:
\e[1;35m1.\e[1;33m global (One container per swarm mode)
\e[1;35m2.\e[1;33m replicated
\e[1;35m
*Selection:\e[1;31m"
read -n 1 mode
case "$mode" in
1)
echo "
deploy:
mode: 'global' " >> docker-compose.yml
echo -ne "\e[1;32m
*** DEPLOY MODE ENABLE [ \e[1;31mglobal\e[1;32m ] ***
\e[0m"
;;
2)
echo -ne "\e[1;36m
Number of instances:\e[1;31m"
read -n 1 instances
echo -ne "\e[1;35m
Set the resources of each replica, limits and reservations.
\e[1;33m
### Limits\e[1;36m
cpu processors:\e[1;31m"
read -n 1 cpus
echo -ne "\e[1;36m
memory (xG):\e[1;31m"
read memory
echo "
deploy:
mode: '"$mode"'
replicas: "$instances"
resources:
limits:
cpus: '"$cpus"'
memory: "$memory"" >> docker-compose.yml
if [[ "$version" == '3.7' ]]
then
echo -ne "\e[1;33m
### Reservations\e[1;36m
cpu processors:\e[1;31m"
read -n 1 cpusB
echo -ne "\e[1;36m
memory (xG):\e[1;31m"
read memoryB
echo "
reservations:
cpus: '"$cpusB"'
memory: "$memoryB"" >> docker-compose.yml
echo -ne "\e[1;32m
*** DEPLOY MODE ENABLE [ \e[1;31m"$mode"\e[1;32m ] ***
\e[0m"
else
echo -ne "\e[1;31m
*** VERSION WITHOUT RESOURCE RESERVATION ***
\e[0m"
fi
echo -ne "\e[1;36m
Please select your restart policy in case of OOME (Out Of Memory Exceptions):
\e[1;35m1. \e[1;33mnone
\e[1;35m2. \e[1;33mon-failure
\e[1;35m3. \e[1;33many
\e[1;35m
*Select the concurrence:\e[1;31m"
read -n 1 policy
case "$policy" in
1)
echo "
restart_policy:
condition: none " >> docker-compose.yml
;;
2)
echo "
restart_policy:
condition: on-failure
delay: 15s
max_attempts: 3
window: 180s " >> docker-compose.yml
;;
3)
echo "
restart_policy:
condition: any
delay: 60s
max_attempts: 3
window: 120s " >> docker-compose-yml
;;
[!1-3]*)
echo -ne "\e[1;31m
*** UNKNOW OPTION ***
\e[0m"
;;
esac
;;
[!1-2]*)
echo -ne "\e[1;31m
*** Unknow command... default values added! ***
\e[0m"
echo "
deploy:
mode: 'global'" >> docker-compose.yml
;;
esac
echo -ne "\e[1;36m
Enter the DNS address for this container:\e[1;31m"
read dns
echo "
container_name: "$name"
dns: "$dns"
dns_search:" >> docker-compose.yml
echo -ne "\e[1;36m
Number of DNS resolvers:\e[1;31m"
read -n 1 resolver
if [[ "$resolver" =~ ^[1-9]+$ ]]
then
for i in $(seq 1 "$resolver");do
echo -ne "\e[1;36m
Address of DNS resolver:\e[1;31m"
read addr
echo "
- "$addr"" >> docker-compose.yml
echo -ne "\e[1;32m
*** DNS RESOLVER ENABLE [ \e[1;31m"$addr"\e[1;32m ] ***
\e[0m"
done
else
echo -ne "\e[1;31m
*** DNS RESOLVER NOT DEFINED ***
\e[0m"
fi
echo '''
volumes: ''' >> docker-compose.yml
echo -ne "\e[1;36m
Number of internal volumes:\e[1;31m"
read -n 1 volumes
if [[ "$volumes" =~ ^[1-9]+$ ]]
then
for x in $(seq 1 "$volumes");do
echo -ne "\e[1;36m
Select the type of volume:
\e[1;35m1. \e[1;33mvolume
\e[1;35m2. \e[1;33mbind
\e[1;35m
*Selection:\e[1;31m"
read -n 1 type
case "$type" in
1)
echo -ne "\e[1;36m
Define the source path or name:\e[1;31m"
read source
echo -ne "\e[1;36m
Define the destiny path:\e[1;31m"
read path
echo "
- type: 'volume'
source: "$source"
target: "$path"
volume:
nocopy: true" >> docker-compose.yml
echo -ne "\e[1;32m
*** INTERNAL VOLUME ENABLE [ \e[1;31m"$source"\e[1;32m ] ***
\e[0m"
;;
2)
echo -ne "\e[1;36m
Define the source path or name:\e[1;31m"
read source
echo -ne "\e[1;36m
Define the destiny path:\e[1;31m"
read path
echo "
- type: 'bind'
source: "$source"
target: "$path"" >> docker-compose.yml
echo -ne "\e[1;32m
*** INTERNAL VOLUME ENABLE [ \e[1;31m"$source"\e[1;32m ] ***
\e[0m"
;;
[!1-2]*)
echo -ne "\e[1;31m
*** UNKNOW TYPE VOLUME CANCELED ***
\e[0m"
;;
esac
done
else
echo -ne "\e[31m
*** NO VOLUME SET DEFINED ***
\e[0m"
fi
echo -ne "\e[1;36m
PID number process:\e[1;31m"
read -n 3 pid
echo "
pid: '"$pid"'
ports:" >> docker-compose.yml
echo -ne "\e[1;36m
How many ports do you need for this service:\e[1;31m"
read -n 1 many
if [[ "$many" =~ ^[1-9]+$ ]]
then
for x in $(seq 1 "$many");do
echo -ne "\e[1;36m
Target port:\e[1;31m"
read -n 4 target
echo -ne "\e[1;36m
Published port:\e[1;31m"
read -n 4 pub
echo -ne "\e[1;36m
Protocol of port (tcp / udp):\e[1;31m"
read pro
echo -ne "\e[1;36m
Port mode (host / ingress):\e[1;31m"
read mode
echo "
- target: "$target"
published: "$pub"
protocol: "$pro"
mode: "$mode"" >> docker-compose.yml
echo -ne "\e[1;32m
*** SERVICE PORT ENABLE [ \e[1;31m"$target"\e[1;32m ] ***
\e[0m"
done
else
echo -ne "\e[1;31m
*** NO PORT SET DEFINED ***
\e[0m"
fi
echo '''
expose: ''' >> docker-compose.yml
echo -ne "\e[1;36m
Number of ports exposed in this service:\e[1;31m"
read -n 1 porq
if [[ "$porq" =~ ^[1-9]+$ ]]
then
for x in $(seq 1 "$porq");do
echo -ne "\e[1;36m
Exposed port:\e[1;31m"
read -n 4 expo
echo "
- "$expo"" >> docker-compose.yml
echo -ne "\e[1;32m
*** PORT ENABLE EXPOSED [ \e[1;31m"$expo"\e[1;32m ] ***
\e[0m"
done
else
echo -ne "\e[1;31m
*** NO PORTS EXPOSED ***
\e[0m"
fi
echo '''
networks: ''' >> docker-compose.yml
echo -ne "\e[1;36m
Number of networks exposed in this service:\e[1;31m"
read -n 1 netq
if [[ "$netq" =~ ^[1-9]+$ ]]
then
for x in $(seq 1 "$netq");do
echo -ne "\e[1;36m
Enter the name of the network:\e[1;31m"
read netn
echo "
- "$netn"" >> docker-compose.yml
echo -ne "\e[1;32m
*** NETWORK ENABLE [ \e[1;31m"$netn"\e[1;32m ] ***
\e[0m"
done
else
echo -ne "\e[1;31m
*** NO NETWORK SET DEFINED ***
\e[0m"
fi
echo -ne "\e[1;36m
Number of dependant services:\e[1;31m"
read -n 1 serq
if [[ "$serq" =~ ^[1-9]+$ ]]
then
echo '''
depends_on: ''' >> docker-compose.yml
for x in $(seq 1 "$serq");do
echo -ne "\e[1;36m
Enter the dependant service name:\e[1;31m"
read sern
echo "
- "$sern"" >> docker-compose.yml
echo -ne "\e[1;32m
*** DEPENDENCY ENABLE [ \e[1;31m"$sern"\e[1;32m ] ***
\e[0m"
done
else
echo -ne "\e[1;31m
*** NO DEPENDANT SERVICES ***
\e[0m"
fi
echo -ne "\e[1;32m
*** NODE ENABLE [ \e[1;33m"$name"\e[1;32m ] ***
\e[0m"
done
else
echo -ne "\e[1;31m
*** NO SERVICE SET DEFINED ***
\e[0m"
fi
sed -i 's/[[,A-Z]//g; s/[^[:print:]\t]//g; /^$/d' docker-compose.yml
exit 1
}
function editor() {
echo -en "\e[1;31m
Editor\e[1;36m
======\e[1;33m
Please select the values and provide details about your stack and fill the
information to generate the config file.\e[0m"
compose
}
function main(){
echo -en "\e[1;36m
####::.#######:::
. ##::.##.... ##:
: ##:: ##:::: ##:
: ##:: ##:::: ##:
: ##:: ##:::: ##:
: ##:: ##:::: ##:
.####:. #######::
....:::.......:::
\e[0m
\e[1;33mInitial Orchestation\e[0m
\e[1;33mCommand Line Interface \e[0m
\e[1;33m(Docker/Compose)\e[0m
\e[1;32m
This tool help you to generate an initial structure and *configuration*
file, you can setup your own environment, according to the requirements
of your project please follow the instructions to complete the process
of initial setup and then you can *build* your services for production
or test purposes.
\e[1;36m
=========================================================================
\e[1;31m
Read the options below and select the operation that fits to your project.
\e[1;36m
Instructions:
\e[1;33m
- Enter the key number [ \e[1;35m1\e[33m ] > Default run for a single instance
(This configures a single running instance.)
- Enter the key number [ \e[1;35m2\e[33m ] > Build and run
(This configure the initial steps and build.)
- Enter the key number [ \e[1;35m3\e[33m ] > Deploy swarm
(Deploy swarm.)
- Enter the key number [ \e[1;35m4\e[33m ] > Deploy stack
(Deploy stack.)
- Enter the key number [ \e[1;35m5\e[33m ] > Prune system container
(WARNING: This option removes all the content on system container.)
- Enter the key number [ \e[1;35m0\e[33m ] > Create and edit a compose config file
(Defined environment with manual number of services with their
preferences.)
\e[1;35m
*Please remember that some options are just for initial configuration.
\e[34m
\e[1;36mEnter your option:\e[1;35m"
read -n 1 key
case "$key" in
0)
echo -ne "\e[1;36m
Please define the values and parameters in the editor to generate a new config
file for your deployment.\e[0m"
editor
;;
1)
echo -ne "\e[1;36m
Please define the path of the dockerfile to be executed (path/to/dockerfile):\e[1;31m"
read path
docker run -f "$path"
;;
2)
docker-compose build
docker-compose up
echo -ne "\e[1;32m
*** COMPOSE ENABLED ***
\e[0m"
;;
3)
docker swarm ca
docker swarm init
echo -ne "\e[1;32m
*** SWARM ENABLED ***
\e[0m"
;;
4)
stack_conf
echo -ne "\e[1;32m
*** STACK ENABLED ***
\e[0m"
;;
5)
docker-compose down -v --rm all --remove-orphans
docker system prune --volumes --all
docker images prune
docker container prune
echo -ne "\e[1;32m
*** DOCKER PRUNE ***
\e[0m"
exit 1
;;
[!0-5]*)
echo -ne "\e[1,31m
*** UNKNOW OPTION... LEAVING PROGRAM. ***
\e[0m"
exit 1
esac
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment