adds openssh install and uninstall
This commit is contained in:
parent
c38d3722d3
commit
2ddd4303e5
258
src/install.sh
Executable file
258
src/install.sh
Executable file
@ -0,0 +1,258 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# BOT name
|
||||||
|
BOT_NAME="docker-deploy v1.0"
|
||||||
|
|
||||||
|
# only install/setup docker once
|
||||||
|
command -v docker >/dev/null 2>&1 || {
|
||||||
|
# remove old docker
|
||||||
|
sudo apt-get remove docker docker-engine docker.io containerd runc
|
||||||
|
# add docker repo
|
||||||
|
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
|
||||||
|
# make sure we have dependencies installed
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install \
|
||||||
|
ca-certificates \
|
||||||
|
curl \
|
||||||
|
gnupg \
|
||||||
|
lsb-release
|
||||||
|
# now install docker
|
||||||
|
sudo apt-get install docker-ce docker-ce-cli containerd.io
|
||||||
|
# start docker
|
||||||
|
sudo systemctl enable docker.service
|
||||||
|
# persist docker
|
||||||
|
udo systemctl enable containerd.service
|
||||||
|
# make sure the user is in the docker group
|
||||||
|
sudo groupadd docker
|
||||||
|
sudo usermod -aG docker "$USER"
|
||||||
|
}
|
||||||
|
|
||||||
|
# only install docker-compose once
|
||||||
|
command -v docker-compose >/dev/null 2>&1 || {
|
||||||
|
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
|
||||||
|
sudo chmod +x /usr/local/bin/docker-compose
|
||||||
|
}
|
||||||
|
|
||||||
|
# get script path
|
||||||
|
VDM_SRC_PATH="${BASH_SOURCE%/*}"
|
||||||
|
if [[ ! -d "$VDM_SRC_PATH" || "$VDM_SRC_PATH" == '.' ]]; then VDM_SRC_PATH="$PWD"; fi
|
||||||
|
|
||||||
|
# some globals
|
||||||
|
VDM_FORCE_INSTALL=false
|
||||||
|
VDM_UPDATE_HOST=false
|
||||||
|
|
||||||
|
# help message ʕ•ᴥ•ʔ
|
||||||
|
function show_help() {
|
||||||
|
cat <<EOF
|
||||||
|
Usage: ${0##*/:-} [OPTION...]
|
||||||
|
Options
|
||||||
|
======================================================
|
||||||
|
--src-path=<path>|--src=<path>
|
||||||
|
set path to the script source folder
|
||||||
|
example: ${0##*/:-} --src=/home/$USER/Docker/src
|
||||||
|
example: ${0##*/:-} --src-path=/home/$USER/Docker/src
|
||||||
|
======================================================
|
||||||
|
--repo-path=<path>|--repo=<path>
|
||||||
|
set path to the repository folder
|
||||||
|
example: ${0##*/:-} --repo=/home/$USER/Docker
|
||||||
|
example: ${0##*/:-} --repo-path=/home/$USER/Docker
|
||||||
|
======================================================
|
||||||
|
--project-path=<path>|--project=<path>
|
||||||
|
set path to the projects folder
|
||||||
|
example: ${0##*/:-} --project=/home/$USER/Projects
|
||||||
|
example: ${0##*/:-} --project-path=/home/$USER/Projects
|
||||||
|
======================================================
|
||||||
|
-f|--force
|
||||||
|
force installation
|
||||||
|
example: ${0##*/:-} -f
|
||||||
|
example: ${0##*/:-} --force
|
||||||
|
======================================================
|
||||||
|
--host
|
||||||
|
always update your host file
|
||||||
|
example: ${0##*/:-} --host
|
||||||
|
======================================================
|
||||||
|
-h|--help
|
||||||
|
display this help menu
|
||||||
|
example: ${0##*/:-} -h
|
||||||
|
example: ${0##*/:-} --help
|
||||||
|
======================================================
|
||||||
|
${BOT_NAME}
|
||||||
|
======================================================
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# check if we have options
|
||||||
|
while :; do
|
||||||
|
case $1 in
|
||||||
|
-h | --help)
|
||||||
|
show_help # Display a usage synopsis.
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
-f | --force)
|
||||||
|
VDM_FORCE_INSTALL=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--host)
|
||||||
|
VDM_UPDATE_HOST=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
--src-path | --src) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_SRC_PATH=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--src-path" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--src-path=?* | --src=?*)
|
||||||
|
VDM_SRC_PATH=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
--src-path= | --src=) # Handle the case of an empty --src-path=
|
||||||
|
echo '[error] "--src-path=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
--repo-path | --repo) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_REPO_PATH=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--repo-path" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--repo-path=?* | --repo=?*)
|
||||||
|
VDM_REPO_PATH=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
--repo-path= | --repo=) # Handle the case of an empty --repo-path=
|
||||||
|
echo '[error] "--repo-path=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
--project-path | --project) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_PROJECT_PATH=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--project-path" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--project-path=?* | --project=?*)
|
||||||
|
VDM_PROJECT_PATH=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
--project-path= | --project=) # Handle the case of an empty --project-path=
|
||||||
|
echo '[error] "---project-path=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
*) # Default case: No more options, so break out of the loop.
|
||||||
|
break ;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
# make source path available to other scripts
|
||||||
|
export VDM_SRC_PATH="${VDM_SRC_PATH}"
|
||||||
|
|
||||||
|
# now we get the repository path
|
||||||
|
[ -e "${VDM_REPO_PATH}" ] || {
|
||||||
|
echo "[notice] The repository full path is where the container .yml files will live."
|
||||||
|
echo -n "[enter:${VDM_SRC_PATH%/src}] Repository Full Path: "
|
||||||
|
read -r VDM_REPO_PATH
|
||||||
|
# make sure it exist
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ ${#VDM_REPO_PATH} -ge 1 ] && [ -d "${VDM_REPO_PATH}" ] || {
|
||||||
|
# we set the default
|
||||||
|
VDM_REPO_PATH="${VDM_SRC_PATH%/src}"
|
||||||
|
## check if this exist
|
||||||
|
[ -d "${VDM_REPO_PATH}" ] || {
|
||||||
|
echo "[error] Repository path (${VDM_REPO_PATH}) does not exist."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# we set the repo path (easy)
|
||||||
|
grep -q "VDM_REPO_PATH=\"${VDM_REPO_PATH}\"" "${VDM_SRC_PATH}/.env" || echo "export VDM_REPO_PATH=\"${VDM_REPO_PATH}\"" >"${VDM_SRC_PATH}/.env"
|
||||||
|
export VDM_REPO_PATH="${VDM_REPO_PATH}"
|
||||||
|
|
||||||
|
grep -q "VDM_UPDATE_HOST=${VDM_UPDATE_HOST}" "${VDM_SRC_PATH}/.env" || echo "export VDM_UPDATE_HOST=${VDM_UPDATE_HOST}" >>"${VDM_SRC_PATH}/.env"
|
||||||
|
export VDM_UPDATE_HOST
|
||||||
|
|
||||||
|
# notice about the repo path
|
||||||
|
echo "[notice] $VDM_REPO_PATH was set as the repository path in ${VDM_SRC_PATH}/.env"
|
||||||
|
|
||||||
|
# now we must set the Projects path
|
||||||
|
[ -e "${VDM_PROJECT_PATH}" ] || {
|
||||||
|
echo -n "[enter:/home/${USER}/Projects] Projects Full Path: "
|
||||||
|
read -r VDM_PROJECT_PATH
|
||||||
|
# make sure it exist
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ ${#VDM_PROJECT_PATH} -ge 1 ] && [ -d "${VDM_PROJECT_PATH}" ] || {
|
||||||
|
# we set the default
|
||||||
|
VDM_PROJECT_PATH="/home/${USER}/Projects"
|
||||||
|
## check if this exist
|
||||||
|
[ -d "${VDM_PROJECT_PATH}" ] || {
|
||||||
|
echo "[error] Project path (${VDM_PROJECT_PATH}) does not exist."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# we set the projects path (easy)
|
||||||
|
grep -q "VDM_PROJECT_PATH=\"${VDM_PROJECT_PATH}\"" "${VDM_SRC_PATH}/.env" || echo "export VDM_PROJECT_PATH=\"${VDM_PROJECT_PATH}\"" >>"${VDM_SRC_PATH}/.env"
|
||||||
|
export VDM_PROJECT_PATH="${VDM_PROJECT_PATH}"
|
||||||
|
|
||||||
|
# notice about the projects path
|
||||||
|
echo "[notice] $VDM_PROJECT_PATH was set as the project path in ${VDM_SRC_PATH}/.env"
|
||||||
|
|
||||||
|
# now we set the docker-deploy script in the executable path of this user
|
||||||
|
if [ ! $VDM_FORCE_INSTALL ]; then
|
||||||
|
[ -f /usr/local/bin/docker-deploy ] && {
|
||||||
|
echo "[error] /usr/local/bin/docker-deploy already exist! you can force installation:"
|
||||||
|
echo "[fix] $ ${0##*/:-} -f"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
sudo cp "${VDM_SRC_PATH}/docker-deploy.sh" /usr/local/bin/docker-deploy
|
||||||
|
else
|
||||||
|
sudo cp -f "${VDM_SRC_PATH}/docker-deploy.sh" /usr/local/bin/docker-deploy
|
||||||
|
fi
|
||||||
|
|
||||||
|
# we need to add helper for our docker-deploy script to find the .env file
|
||||||
|
sudo sed -i -e "s|/home/update/this/path/to/src|${VDM_SRC_PATH}|g" /usr/local/bin/docker-deploy
|
||||||
|
|
||||||
|
# notice of the src path
|
||||||
|
echo "[notice] ${VDM_SRC_PATH} is the src path to all scripts, and should not be deleted."
|
||||||
|
|
||||||
|
# we first make sure run is executable
|
||||||
|
echo "[sudo] making the /usr/local/bin/docker-deploy script executable"
|
||||||
|
sudo chmod +x "/usr/local/bin/docker-deploy"
|
||||||
|
|
||||||
|
# Note: If the command docker-deploy fails after installation, check your path.
|
||||||
|
# You can also create a symbolic link to /usr/bin or any other directory in your path.
|
||||||
|
# For example:
|
||||||
|
# $ sudo ln -s /usr/local/bin/docker-deploy /usr/bin/docker-deploy
|
||||||
|
|
||||||
|
# we load global environment values
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ -f "${VDM_SRC_PATH}/.env" ] && source "${VDM_SRC_PATH}/.env"
|
||||||
|
|
||||||
|
# check if we should setup traefik
|
||||||
|
echo -n "[enter:y] Setup Traefik (y/n): "
|
||||||
|
read -r VDM_SETUP_TRAEFIK
|
||||||
|
# make sure value was entered
|
||||||
|
if [ "${VDM_SETUP_TRAEFIK,,}" != 'n' ]; then
|
||||||
|
# run that task type script
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
source "${VDM_SRC_PATH}/setup-traefik.sh"
|
||||||
|
fi
|
||||||
|
# check if we should setup portainer
|
||||||
|
echo -n "[enter:y] Setup Portainer (y/n): "
|
||||||
|
read -r VDM_SETUP_PORTAINER
|
||||||
|
# make sure value was entered
|
||||||
|
if [ "${VDM_SETUP_PORTAINER,,}" != 'n' ]; then
|
||||||
|
# run that task type script
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
source "${VDM_SRC_PATH}/setup-portainer.sh"
|
||||||
|
fi
|
||||||
|
|
||||||
|
exit 0
|
440
src/setup-openssh.sh
Normal file
440
src/setup-openssh.sh
Normal file
@ -0,0 +1,440 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# check that our repository path is correct
|
||||||
|
[ -e "${VDM_REPO_PATH}" ] || {
|
||||||
|
echo "[error] Repository path (${VDM_REPO_PATH}) does not exist."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
# check that our projects path is correct
|
||||||
|
[ -e "${VDM_PROJECT_PATH}" ] || {
|
||||||
|
echo "[error] Projects path (${VDM_PROJECT_PATH}) does not exist."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
# be sure to create the container type path
|
||||||
|
# shellcheck disable=SC2174
|
||||||
|
mkdir -p -m 700 "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}"
|
||||||
|
# be sure to .ssh path exist
|
||||||
|
# shellcheck disable=SC2174
|
||||||
|
mkdir -p -m 700 "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.ssh"
|
||||||
|
|
||||||
|
# check if we have global env file
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ -f "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env" ] && source "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env" || {
|
||||||
|
touch "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env"
|
||||||
|
chmod 600 "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env"
|
||||||
|
}
|
||||||
|
|
||||||
|
# help message ʕ•ᴥ•ʔ
|
||||||
|
function show_help() {
|
||||||
|
cat <<EOF
|
||||||
|
Usage: ${0##*/:-} [OPTION...]
|
||||||
|
Options
|
||||||
|
======================================================
|
||||||
|
-u|--username <username>
|
||||||
|
set username of the container
|
||||||
|
example: ${0##*/:-} -u="ubuntu"
|
||||||
|
example: ${0##*/:-} --username="ubuntu"
|
||||||
|
======================================================
|
||||||
|
--uid <id>
|
||||||
|
set container user id
|
||||||
|
example: ${0##*/:-} --uid=1000
|
||||||
|
======================================================
|
||||||
|
--gid <id>
|
||||||
|
set container user group id
|
||||||
|
example: ${0##*/:-} --gid=1000
|
||||||
|
======================================================
|
||||||
|
-p|--port <port>
|
||||||
|
set ssh port to use
|
||||||
|
!! do not use 22 !!
|
||||||
|
example: ${0##*/:-} -p=2239
|
||||||
|
example: ${0##*/:-} --port=2239
|
||||||
|
======================================================
|
||||||
|
--ssh-dir <dir>
|
||||||
|
set ssh directory name found in the .ssh dir
|
||||||
|
of this repo for the container keys
|
||||||
|
This directory has separate files for
|
||||||
|
each public key allowed to access
|
||||||
|
the container
|
||||||
|
example: ${0##*/:-} --ssh-dir="teamname"
|
||||||
|
======================================================
|
||||||
|
--sudo
|
||||||
|
switch to add the container user to the
|
||||||
|
sudo group of the container
|
||||||
|
example: ${0##*/:-} --sudo
|
||||||
|
======================================================
|
||||||
|
-k|--key <key>
|
||||||
|
set key for the docker compose container naming
|
||||||
|
!! no spaces allowed in the key !!
|
||||||
|
example: ${0##*/:-} -k="vdm"
|
||||||
|
example: ${0##*/:-} --key="vdm"
|
||||||
|
======================================================
|
||||||
|
-e|--env-key <key>
|
||||||
|
set key for the environment variable naming
|
||||||
|
!! no spaces allowed in the key & must be UPPERCASE !!
|
||||||
|
example: ${0##*/:-} -e="VDM"
|
||||||
|
example: ${0##*/:-} --env-key="VDM"
|
||||||
|
======================================================
|
||||||
|
-d|--domain <domain.com>
|
||||||
|
set key website domain
|
||||||
|
!! must be domain.tld !!
|
||||||
|
example: ${0##*/:-} -d="vdm.dev"
|
||||||
|
example: ${0##*/:-} --domain="vdm.dev"
|
||||||
|
======================================================
|
||||||
|
-t|--time-zone <time/zone>
|
||||||
|
set time zone of the container
|
||||||
|
!! must valid time zone !!
|
||||||
|
example: ${0##*/:-} -t="Africa/Windhoek"
|
||||||
|
example: ${0##*/:-} --time-zone="Africa/Windhoek"
|
||||||
|
======================================================
|
||||||
|
-h|--help
|
||||||
|
display this help menu
|
||||||
|
example: ${0##*/:-} -h
|
||||||
|
example: ${0##*/:-} --help
|
||||||
|
======================================================
|
||||||
|
${BOT_NAME}
|
||||||
|
======================================================
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# set the local values
|
||||||
|
while :; do
|
||||||
|
case $1 in
|
||||||
|
-h | --help)
|
||||||
|
show_help # Display a usage synopsis.
|
||||||
|
exit
|
||||||
|
;;
|
||||||
|
--sudo)
|
||||||
|
VDM_SUDO_ACCESS=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-u | --username) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_USER_NAME=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--username" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-u=?* | --username=?*)
|
||||||
|
VDM_USER_NAME=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-u= | --username=) # Handle the case of an empty --username=
|
||||||
|
echo '[error] "--username=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
--uid) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_PUID=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--uid" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--uid=?*)
|
||||||
|
VDM_PUID=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
--uid=) # Handle the case of an empty --uid=
|
||||||
|
echo '[error] "--uid=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
--gid) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_PGID=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--gid" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--gid=?*)
|
||||||
|
VDM_PGID=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
--gid=) # Handle the case of an empty --gid=
|
||||||
|
echo '[error] "--gid=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
-p | --port) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_PORT=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--port" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-p=?* | --port=?*)
|
||||||
|
VDM_PORT=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-p= | --port=) # Handle the case of an empty --port=
|
||||||
|
echo '[error] "--port=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
--ssh-dir) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_PUBLIC_KEY_DIR=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--ssh-dir" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
--ssh-dir=?*)
|
||||||
|
VDM_PUBLIC_KEY_DIR=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-ssh-dir=) # Handle the case of an empty --ssh-dir=
|
||||||
|
echo '[error] "--ssh-dir=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
-k | --key) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_KEY=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--key" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-k=?* | --key=?*)
|
||||||
|
VDM_KEY=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-k= | --key=) # Handle the case of an empty --key=
|
||||||
|
echo '[error] "--key=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
-e | --env-key) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_ENV_KEY=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--env-key" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-e=?* | --env-key=?*)
|
||||||
|
VDM_ENV_KEY=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-e= | --env-key=) # Handle the case of an empty --env-key=
|
||||||
|
echo '[error] "--env-key=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
-d | --domain) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_DOMAIN=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--domain" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-d=?* | --domain=?*)
|
||||||
|
VDM_DOMAIN=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-d= | --domain=) # Handle the case of an empty --domain=
|
||||||
|
echo '[error] "--domain=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
-t | --time-zone) # Takes an option argument; ensure it has been specified.
|
||||||
|
if [ "$2" ]; then
|
||||||
|
VDM_TZ=$2
|
||||||
|
shift
|
||||||
|
else
|
||||||
|
echo '[error] "--time-zone" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
fi
|
||||||
|
;;
|
||||||
|
-t=?* | --time-zone=?*)
|
||||||
|
VDM_TZ=${1#*=} # Delete everything up to "=" and assign the remainder.
|
||||||
|
;;
|
||||||
|
-t= | --time-zone=) # Handle the case of an empty --time-zone=
|
||||||
|
echo '[error] "--time-zone=" requires a non-empty option argument.'
|
||||||
|
exit 17
|
||||||
|
;;
|
||||||
|
*) # Default case: No more options, so break out of the loop.
|
||||||
|
break ;;
|
||||||
|
esac
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
|
||||||
|
# check that we have what we need
|
||||||
|
[ ${#VDM_PORT} -ge 1 ] || {
|
||||||
|
echo -n "[enter] SSH Port: "
|
||||||
|
read -r VDM_PORT
|
||||||
|
# make sure value was entered
|
||||||
|
[ ${#VDM_PORT} -ge 1 ] || exit
|
||||||
|
}
|
||||||
|
[ ${#VDM_USER_NAME} -ge 1 ] || {
|
||||||
|
echo -n "[enter:ubuntu] UserName: "
|
||||||
|
read -r VDM_USER_NAME
|
||||||
|
}
|
||||||
|
[ ${#VDM_KEY} -ge 1 ] || {
|
||||||
|
echo -n "[enter:${VDM_USER_NAME:-ubuntu}] key: "
|
||||||
|
read -r VDM_KEY
|
||||||
|
# make sure value was entered
|
||||||
|
[ ${#VDM_KEY} -ge 1 ] || VDM_KEY="${VDM_USER_NAME:-ubuntu}"
|
||||||
|
}
|
||||||
|
[ ${#VDM_ENV_KEY} -ge 1 ] || {
|
||||||
|
echo -n "[enter:A] env key: "
|
||||||
|
read -r VDM_ENV_KEY
|
||||||
|
# make sure value was entered
|
||||||
|
[ ${#VDM_ENV_KEY} -ge 1 ] || VDM_ENV_KEY="A"
|
||||||
|
}
|
||||||
|
# we must get the global public key path
|
||||||
|
VDM_ENV_PUBLIC_KEY_GLOBAL_DIR="VDM_${VDM_ENV_KEY^^}_PUBLIC_KEY_GLOBAL_DIR"
|
||||||
|
VDM_PUBLIC_KEY_GLOBAL_DIR=${!VDM_ENV_PUBLIC_KEY_GLOBAL_DIR}
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ ${#VDM_PUBLIC_KEY_GLOBAL_DIR} -ge 1 ] && [ -d "${VDM_PUBLIC_KEY_GLOBAL_DIR}" ] || {
|
||||||
|
VDM_PUBLIC_KEY_GLOBAL_DIR="${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.ssh"
|
||||||
|
# make sure value was entered
|
||||||
|
[ ${#VDM_PUBLIC_KEY_GLOBAL_DIR} -ge 1 ] && [ -d "${VDM_PUBLIC_KEY_GLOBAL_DIR}" ] || exit
|
||||||
|
# add to env file
|
||||||
|
echo "${VDM_ENV_PUBLIC_KEY_GLOBAL_DIR}=${VDM_PUBLIC_KEY_GLOBAL_DIR}" >>"${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env"
|
||||||
|
}
|
||||||
|
# get available public dir
|
||||||
|
function getPublicKeyDir() {
|
||||||
|
# some house cleaning
|
||||||
|
PS3_old=$PS3
|
||||||
|
# some defaults
|
||||||
|
export PS3="[select] Public ssh key directory: "
|
||||||
|
# we start the selection array
|
||||||
|
local SELECTED
|
||||||
|
# Start our little Menu
|
||||||
|
select public_dir in $(ls "${VDM_PUBLIC_KEY_GLOBAL_DIR}"); do
|
||||||
|
case $REPLY in
|
||||||
|
*)
|
||||||
|
SELECTED="${public_dir}"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
break
|
||||||
|
done
|
||||||
|
# restore the default
|
||||||
|
export PS3=$PS3_old
|
||||||
|
# return selection
|
||||||
|
echo "${SELECTED}"
|
||||||
|
}
|
||||||
|
|
||||||
|
[ ${#VDM_PUBLIC_KEY_DIR} -ge 1 ] || {
|
||||||
|
VDM_PUBLIC_KEY_DIR=$(getPublicKeyDir)
|
||||||
|
# make sure value was entered
|
||||||
|
[ ${#VDM_PUBLIC_KEY_DIR} -ge 1 ] && [ -d "${VDM_PUBLIC_KEY_GLOBAL_DIR}/${VDM_PUBLIC_KEY_DIR}" ] || exit
|
||||||
|
}
|
||||||
|
# set the global key env string for the ssh keys
|
||||||
|
VDM_ENV_PUBLIC_KEY_U_DIR="VDM_${VDM_ENV_KEY^^}_PUBLIC_KEY_${VDM_PUBLIC_KEY_DIR^^}_DIR"
|
||||||
|
VDM_PUBLIC_KEY_U_DIR=${!VDM_ENV_PUBLIC_KEY_U_DIR}
|
||||||
|
# check if env is already set
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ -f "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env" ] && grep -q "${VDM_ENV_PUBLIC_KEY_U_DIR}=" "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env" || {
|
||||||
|
# add to env if not exist
|
||||||
|
[ ${#VDM_PUBLIC_KEY_U_DIR} -ge 1 ] && [ -d "${VDM_PUBLIC_KEY_U_DIR}" ] || {
|
||||||
|
echo "${VDM_ENV_PUBLIC_KEY_U_DIR}=${VDM_PUBLIC_KEY_GLOBAL_DIR}/${VDM_PUBLIC_KEY_DIR}" >>"${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
# we must get the project path
|
||||||
|
VDM_ENV_PROJECT_DIR="VDM_${VDM_ENV_KEY^^}_PROJECT_DIR"
|
||||||
|
VDM_PROJECT_DIR=${!VDM_ENV_PROJECT_DIR}
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ ${#VDM_PROJECT_DIR} -ge 1 ] && [ -d "${VDM_PROJECT_DIR}" ] || {
|
||||||
|
VDM_PROJECT_DIR="${VDM_PROJECT_PATH}"
|
||||||
|
# make sure value was entered
|
||||||
|
[ ${#VDM_PROJECT_DIR} -ge 1 ] && [ -d "${VDM_PROJECT_DIR}" ] || exit
|
||||||
|
# add to env file
|
||||||
|
echo "VDM_${VDM_ENV_KEY^^}_PROJECT_DIR=${VDM_PROJECT_DIR}" >>"${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/.env"
|
||||||
|
}
|
||||||
|
# return volume line
|
||||||
|
function getVolumeLine() {
|
||||||
|
# get the projects to mount
|
||||||
|
local line="$1"
|
||||||
|
# return line
|
||||||
|
cat <<EOF
|
||||||
|
|
||||||
|
- ${line}
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
# get available mounting projects
|
||||||
|
function getMountingProjects() {
|
||||||
|
# some house cleaning
|
||||||
|
PS3_old=$PS3
|
||||||
|
# some defaults
|
||||||
|
# some defaults
|
||||||
|
export PS3="[select] Projects to mount: "
|
||||||
|
# some buckets
|
||||||
|
FINISHED="n"
|
||||||
|
SELECTED=$(getVolumeLine "\${${VDM_ENV_PUBLIC_KEY_U_DIR}}:/config/ssh_public_keys")
|
||||||
|
until [ "${FINISHED}" = "y" ]; do
|
||||||
|
# Start our little Menu
|
||||||
|
select key in $(ls "${VDM_PROJECT_DIR}"); do
|
||||||
|
case $REPLY in
|
||||||
|
q | quit)
|
||||||
|
FINISHED="y"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
SELECTED+=$(getVolumeLine "\${VDM_${VDM_ENV_KEY^^}_PROJECT_DIR}/${key}/joomla:/app/${key}")
|
||||||
|
FINISHED="n"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
break
|
||||||
|
done
|
||||||
|
done
|
||||||
|
# restore the default
|
||||||
|
export PS3=$PS3_old
|
||||||
|
# return selection
|
||||||
|
echo "${SELECTED}"
|
||||||
|
}
|
||||||
|
# now load the projects
|
||||||
|
[ ${#VDM_MOUNT_PROJECTS} -ge 1 ] || {
|
||||||
|
echo "[enter] q to quit selecting"
|
||||||
|
# get the projects to mount
|
||||||
|
VDM_MOUNT_PROJECTS=$(getMountingProjects)
|
||||||
|
# get the mounting options
|
||||||
|
[ ${#VDM_MOUNT_PROJECTS} -ge 1 ] || exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# all values loaded notice
|
||||||
|
echo "[All value loaded]"
|
||||||
|
|
||||||
|
# build function
|
||||||
|
function buildContainer() {
|
||||||
|
# get the projects to mount
|
||||||
|
local mount_projects="$1"
|
||||||
|
# we build the yml file
|
||||||
|
# we use 33 as this is the www-data ID
|
||||||
|
cat <<EOF
|
||||||
|
version: "2.1"
|
||||||
|
services:
|
||||||
|
openssh-server-${VDM_KEY}:
|
||||||
|
image: lscr.io/linuxserver/openssh-server
|
||||||
|
container_name: openssh-server-${VDM_KEY}
|
||||||
|
restart: unless-stopped
|
||||||
|
hostname: ${VDM_DOMAIN:-vdm.dev}
|
||||||
|
environment:
|
||||||
|
- PUID=${VDM_PUID:-33}
|
||||||
|
- PGID=${VDM_PGID:-33}
|
||||||
|
- TZ=${VDM_TZ:-Africa/Windhoek}
|
||||||
|
- PUBLIC_KEY_DIR=/config/ssh_public_keys
|
||||||
|
- SUDO_ACCESS=${VDM_SUDO_ACCESS:-false}
|
||||||
|
- USER_NAME=${VDM_USER_NAME:-ubuntu}
|
||||||
|
volumes:${mount_projects}
|
||||||
|
ports:
|
||||||
|
- ${VDM_PORT}:2222
|
||||||
|
networks:
|
||||||
|
- openssh
|
||||||
|
|
||||||
|
networks:
|
||||||
|
openssh:
|
||||||
|
external:
|
||||||
|
name: openssh_gateway
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# create the directory if it does not yet already exist
|
||||||
|
# shellcheck disable=SC2174
|
||||||
|
mkdir -p -m 700 "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/available/${VDM_USER_NAME:-ubuntu}.${VDM_DOMAIN:-vdm.dev}"
|
||||||
|
# place this docker composer file in its place
|
||||||
|
buildContainer "${VDM_MOUNT_PROJECTS}" >"${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/available/${VDM_USER_NAME:-ubuntu}.${VDM_DOMAIN:-vdm.dev}/docker-compose.yml"
|
||||||
|
# set permissions
|
||||||
|
chmod 600 "${VDM_REPO_PATH}/${VDM_CONTAINER_TYPE}/available/${VDM_USER_NAME:-ubuntu}.${VDM_DOMAIN:-vdm.dev}/docker-compose.yml"
|
||||||
|
# saved the file
|
||||||
|
echo "[save] ${VDM_CONTAINER_TYPE}:docker-compose.yml"
|
||||||
|
echo "[setup] Completed!"
|
77
src/uninstall.sh
Normal file
77
src/uninstall.sh
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# BOT name
|
||||||
|
BOT_NAME="docker-deploy v1.0"
|
||||||
|
|
||||||
|
# we load global environment values
|
||||||
|
# shellcheck disable=SC2015
|
||||||
|
[ -f "${VDM_SRC_PATH}/.env" ] && source "${VDM_SRC_PATH}/.env" || {
|
||||||
|
echo "${VDM_SRC_PATH}/.env file not found, please run install."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# take down all containers
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
# ----------------------------------------------------- MULTI CONTAINERS
|
||||||
|
# check if we have possible joomla containers
|
||||||
|
if [ -e "${VDM_REPO_PATH}/joomla/enabled" ]; then
|
||||||
|
echo -n "[enter:y] Take down Joomla containers [y/n]: "
|
||||||
|
read -r VDM_DOWN_JOOMLA
|
||||||
|
# set the default if empty
|
||||||
|
VDM_DOWN_JOOMLA="${VDM_DOWN_JOOMLA:-y}"
|
||||||
|
# make sure it exist
|
||||||
|
if [ "${VDM_DOWN_JOOMLA,,}" != 'n' ]; then
|
||||||
|
export VDM_CONTAINER_TYPE="joomla"
|
||||||
|
source "${VDM_SRC_PATH}/down.sh"
|
||||||
|
fi
|
||||||
|
# remove all enabled
|
||||||
|
rm -fr "${VDM_REPO_PATH}/joomla/enabled"
|
||||||
|
fi
|
||||||
|
# check if we have possible openssh containers
|
||||||
|
if [ -e "${VDM_REPO_PATH}/openssh/enabled" ]; then
|
||||||
|
echo -n "[enter:y] Take down Openssh containers [y/n]: "
|
||||||
|
read -r VDM_DOWN_OPENSSH
|
||||||
|
# set the default if empty
|
||||||
|
VDM_DOWN_OPENSSH="${VDM_DOWN_OPENSSH:-y}"
|
||||||
|
# make sure it exist
|
||||||
|
if [ "${VDM_DOWN_OPENSSH,,}" != 'n' ]; then
|
||||||
|
export VDM_CONTAINER_TYPE="openssh"
|
||||||
|
source "${VDM_SRC_PATH}/down.sh"
|
||||||
|
fi
|
||||||
|
# remove all enabled
|
||||||
|
rm -fr "${VDM_REPO_PATH}/openssh/enabled"
|
||||||
|
fi
|
||||||
|
# ----------------------------------------------------- SINGLE CONTAINER
|
||||||
|
# check if we have possible portainer container
|
||||||
|
if [ -f "${VDM_REPO_PATH}/portainer/docker-compose.yml" ]; then
|
||||||
|
echo -n "[enter:y] Take down Portainer container [y/n]: "
|
||||||
|
read -r VDM_DOWN_PORTAINER
|
||||||
|
# set the default if empty
|
||||||
|
VDM_DOWN_PORTAINER="${VDM_DOWN_PORTAINER:-y}"
|
||||||
|
# make sure it exist
|
||||||
|
if [ "${VDM_DOWN_PORTAINER,,}" != 'n' ]; then
|
||||||
|
docker-compose --file "${VDM_REPO_PATH}/portainer/docker-compose.yml" down
|
||||||
|
fi
|
||||||
|
# we just have one yml file here (so dont remove it for now)
|
||||||
|
# rm -fr "${VDM_REPO_PATH}/portainer"
|
||||||
|
fi
|
||||||
|
# check if we have possible traefik container
|
||||||
|
if [ -f "${VDM_REPO_PATH}/traefik/docker-compose.yml" ]; then
|
||||||
|
echo -n "[enter:y] Take down Traefik container [y/n]: "
|
||||||
|
read -r VDM_DOWN_TRAEFIK
|
||||||
|
# set the default if empty
|
||||||
|
VDM_DOWN_TRAEFIK="${VDM_DOWN_TRAEFIK:-y}"
|
||||||
|
# make sure it exist
|
||||||
|
if [ "${VDM_DOWN_TRAEFIK,,}" != 'n' ]; then
|
||||||
|
docker-compose --file "${VDM_REPO_PATH}/traefik/docker-compose.yml" down
|
||||||
|
fi
|
||||||
|
# we just have one yml file here (so dont remove it for now)
|
||||||
|
# rm -fr "${VDM_REPO_PATH}/traefik"
|
||||||
|
fi
|
||||||
|
# ----------------------------------------------------- REMOVE SCRIPT
|
||||||
|
# now remove the docker-deploy script
|
||||||
|
sudo rm /usr/local/bin/docker-deploy
|
||||||
|
|
||||||
|
echo "[notice] ${BOT_NAME} has been uninstalled."
|
||||||
|
|
||||||
|
exit 0
|
Loading…
Reference in New Issue
Block a user