Skip to content

Commit

Permalink
Added update feature and removed uncessary files.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mekacher-Anis committed Mar 25, 2022
1 parent 7c19097 commit a6a0836
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 586 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Bash (debian) script for setting up nginx with docker.

# basic commands
The script must be ran with root previliges.\
The script needs a username and a domain name specified using `-u` and `-d` options
The script needs a username specified using `-u` option. \
After the script is installed you can run it from any directory using `sudo ndss` instead of `sudo ./setup.sh`.
### Add static file server
`sudo ./setup.sh -u user -d example.com`
### Add proxy server
Expand All @@ -28,6 +29,15 @@ This script will then be avaible by calling `ndss` from the command line
### make a backup of the configuration
`sudo ./setup.sh -u user -b`

### udpate nginx / php version
`sudo ./setup.sh -u user --update`

### shutdown running containers
`sudo ./setup.sh -u user --shutdown`

### stop containers and remove built images
`sudo ./setup.sh -u user --clean`

# command line arguments
|argument|default|description|
|:---: | :--: | :---------: |
Expand All @@ -44,6 +54,9 @@ This script will then be avaible by calling `ndss` from the command line
| -l --list | - | list all the configured servers. |
| -b --backup | - | creates a gzipped tar file and saves it in the user's home directory |
| -i --install | - | install this script to make it more accessible using `ndss` command |
| --update | - | backup the old configuration and update the nginx base images |
| --shutdown | - | shutsdown running containers |
| --clean | - | shutsdown running containers and removes the built images. |

# Todo
- [x] update readme file (because it's way too fucking old)
Expand All @@ -58,5 +71,8 @@ This script will then be avaible by calling `ndss` from the command line
- [x] add systemd timer for checking certifcate renewals.
- [x] make the script installable
- [x] add option to easily make compressed backups
- [ ] add the functionality to easily update nginx to latest version.
- [x] add the functionality to easily update nginx to latest version.
- [ ] build release package and update instructions on how to install the script
- [ ] make it easier to add foreign container to the nginx network
- [ ] add a check to stop the script if one of the actions requires the container to be already running but it's not
- [ ] clean the directory structure
Binary file removed nginx-setup.tar.gz
Binary file not shown.
Binary file removed nginx.zip
Binary file not shown.
117 changes: 105 additions & 12 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,31 @@ create_server_files() {
fi
}

# create the required nginx image
# create the required nginx images
create_images() {
if [[ "$(docker images -q $NGINX_IMG_NAME 2> /dev/null)" == "" ]]; then
if [ ! $1 ]; then
echo "[LOG] building nginx image..."
docker build -f nginx-dockerfile -t $NGINX_IMG_NAME . 1>/dev/null
fi
if [[ "$(docker images -q anismk/php-fpm 2> /dev/null)" == "" ]]; then
echo "[LOG] building php image..."
docker build -f php-dockerfile -t anismk/php-fpm . 1>/dev/null
else
echo "[LOG] building nginx image (force pull)..."
docker build --pull -f nginx-dockerfile -t $NGINX_IMG_NAME . 1>/dev/null
echo "[LOG] building php image (force pull)..."
docker build --pull -f php-dockerfile -t anismk/php-fpm . 1>/dev/null

fi
}

# generate docker compose configuration and start server
start_docker_compose() {
build_config_start_docker_compose() {
# export project name to be used by docker compose
COMPOSE_PROJECT_NAME='ndss'
export COMPOSE_PROJECT_NAME

# if nginx image doesn't exist then build it
create_images
if [[ "$(docker images -q $NGINX_IMG_NAME 2> /dev/null)" == "" || "$(docker images -q anismk/php-fpm 2> /dev/null)" == "" ]]; then
create_images
fi

# if the image is already running, then simply reload the configuration
if [[ $(docker ps | grep anismk-nginx-server) ]]; then
Expand Down Expand Up @@ -165,7 +170,7 @@ request_certificate() {
parse_cmd_args() {
# parse command line arguments
OPTIONS=u:p:d:t:s:m:lib
LONGOPTS=user:,path:,domain:,type:,proxiedServer:,delete,enable,disable,ssl,email:,list,install,backup
LONGOPTS=user:,path:,domain:,type:,proxiedServer:,delete,enable,disable,ssl,email:,list,install,backup,update,shutdown,clean

! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
Expand Down Expand Up @@ -244,6 +249,22 @@ parse_cmd_args() {
ACTION='backup'
shift
;;
--update)
ACTION='update'
shift
;;
--restore)
ACTION='restore'
shift
;;
--shutdown)
ACTION='shutdown'
shift
;;
--clean)
ACTION='clean'
shift
;;
--)
shift
break
Expand All @@ -261,7 +282,7 @@ parse_cmd_args() {
exit 1
fi

if [[ $ACTION != 'list' && $ACTION != 'install' && $ACTION != 'backup' && -z $SERVER_NAME ]]; then
if [[ $ACTION != 'list' && $ACTION != 'install' && $ACTION != 'backup' && $ACTION != 'update' && $ACTION != 'shutdown' && $ACTION != 'clean' && -z $SERVER_NAME ]]; then
echo "Please specify the domain name of the service using the '-d | --domain' option"
exit 1
fi
Expand Down Expand Up @@ -293,6 +314,46 @@ parse_cmd_args() {
# echo -e "[CONFIG]\nselected user\t: ${USERNAME}\nroot folder\t: ${NGINX_ROOT_FOLDER}\nnew server name\t: ${SERVER_NAME}\n"
}


# update existing containers
update() {
if [ -f $NGINX_ROOT_FOLDER/docker-compose.yml ]; then
echo -e "!! WARNING !!\nThe server will be unavailable for a short period of time while switching between the old and new instance.\nDo you want to continue [y/n]:"
read -n 1 ans
if [ $ans = 'y' ]; then
# make a backup of the old configuration
echo -e "\nA backup of the old configuration has been made at /home/$USERNAME/nginx-backup.tar.gz.\n You can restore this version by running this script with '--restore' command"
tar -C $NGINX_ROOT_FOLDER -czf /home/$USERNAME/nginx-backup.tar.gz .

# build the new images
create_images 1

# respawn containers
CWD=$(pwd)
cd $NGINX_ROOT_FOLDER

COMPOSE_PROJECT_NAME='ndss'
export COMPOSE_PROJECT_NAME

docker-compose down
docker-compose up -d 1>/dev/null # will automatically rebuild images if needed

if [ ! $? ]; then
echo "[ERROR] Update failed. Images need to be rebuilt manually and servers restored from backup."
cd $CWD
exit 1
else
echo "Containers have been updated successfully."
fi

cd $CWD
fi
else
echo "[ERROR] Can't upgrade images, as no previous configuration was found."
exit 1
fi
}

####################################################
################### main script ####################
####################################################
Expand All @@ -305,9 +366,11 @@ fi
parse_cmd_args $@

# always check if all required packages are installed
check_packages
# and the directory structure exists
# check_packages
check_dirs
if [ $ACTION != 'clean' ]; then
check_dirs
fi

# hmmmm pure functions, aren't my thing, that's why the code is a bit of a mess :D
case $ACTION in
Expand All @@ -326,7 +389,7 @@ create)
create_server_files
;;
esac
if start_docker_compose; then
if build_config_start_docker_compose; then
if [ $SSL == true ]; then
echo "Waiting for nginx to start..."
while ! nc -z 127.0.0.1 80; do
Expand Down Expand Up @@ -405,4 +468,34 @@ install)
backup)
tar -C $NGINX_ROOT_FOLDER -czf /home/$USERNAME/nginx-backup.tar.gz .
echo "The archive is located at /home/$USERNAME/nginx-backup.tar.gz"
;;

update)
update
;;

shutdown | clean)
# stop and remove containers
if [ -d $NGINX_ROOT_FOLDER ]; then
echo "[LOG] Shutting down containers..."
CWD=$(pwd)
cd $NGINX_ROOT_FOLDER
COMPOSE_PROJECT_NAME='ndss'
export COMPOSE_PROJECT_NAME
docker-compose down
cd $CWD
fi
# remove images if requested
if [ $ACTION == 'clean' ]; then
if [[ "$(docker images -q $NGINX_IMG_NAME 2> /dev/null)" != "" ]]; then
echo "[LOG] Removing nginx image..."
docker image rm $NGINX_IMG_NAME 1>/dev/null
fi
if [[ "$(docker images -q anismk/php-fpm 2> /dev/null)" != "" ]]; then
echo "[LOG] Removing php-fpm image..."
docker image rm anismk/php-fpm 1>/dev/null
fi
echo "[LOG] All clean."
fi
;;
esac
46 changes: 0 additions & 46 deletions templates/nginx/conf.d/default.conf

This file was deleted.

25 changes: 0 additions & 25 deletions templates/nginx/fastcgi_params

This file was deleted.

Loading

0 comments on commit a6a0836

Please sign in to comment.