Skip to content

Instantly share code, notes, and snippets.

@jceloria
Created October 3, 2018 21:36
Show Gist options
  • Save jceloria/c227ed45e34c6f9dcfbb289b1f00214c to your computer and use it in GitHub Desktop.
Save jceloria/c227ed45e34c6f9dcfbb289b1f00214c to your computer and use it in GitHub Desktop.
setup nginx-rtmp in docker/systemd
#!/usr/bin/env bash
SELF=${0##*/} SDIR=${0%/*}
########################################################################################################################
: '
The MIT License (MIT)
Copyright © 2018 by John Celoria.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'
######################################################## config ########################################################
# Set some defaults
VERSION=0.1
DEFAULT_CONTAINER_NAME="nginx-rtmp"
DEFAULT_CONTAINER_REPO="alfg/nginx-rtmp"
DEFAULT_DATA_DIR="/opt/data"
DEFAULT_NGINX_CONF='https://raw.githubusercontent.com/alfg/docker-nginx-rtmp/master/nginx.conf'
####################################################### functions ######################################################
# Print usage information
function help() {
cat << EOF
Usage: ${SELF} [OPTION]...
Setup nginx-rtmp using Docker
-h Display this help message and exit
-q Quiet output
EOF
return
}
########################################################################################################################
# Logging function
function log() {
local level levels=(notice warning crit)
level="+($(IFS='|';echo "${levels[*]}"))"
shopt -s extglob; case ${1} in
${level}) level=${1}; shift ;;
*) level=notice ;;
esac; shopt -u extglob
[[ -z ${RETVAL} ]] && { for RETVAL in "${!levels[@]}"; do
[[ ${levels[${RETVAL}]} = "${level}" ]] && break
done }
logger -s -p ${level} -t "[${SELF}:${FUNCNAME[1]}()]" -- $@;
}
########################################################################################################################
# Log and then exit
function die() { local retval=${RETVAL:-$?}; log "$@"; exit ${retval}; }
########################################################################################################################
# Sanity checks
while getopts ":hq" opt; do
case ${opt} in
h) help >&2; exit 1 ;;
q) QUIET=1 ;;
\?) echo "Invalid option: -${OPTARG}" >&2 ;;
:) echo "Option -${OPTARG} requires an argument." >&2; exit 1 ;;
esac
done; shift $((${OPTIND} - 1))
req_progs=(logger)
for p in ${req_progs[@]}; do
hash "${p}" 2>&- || \
{ die "Required program \"${p}\" not found in \${PATH}."; }
done
######################################################### main #########################################################
function main() {
[[ ${QUIET:-0} -eq 1 ]] && exec >${LOGFILE:-/dev/null} 2>&1
# Use sudo if we're not root
[[ ${USER} != "root" ]] && SUDO="$(type -P sudo) -i"
# confirm options
read -p "What do you want to name this container? [${DEFAULT_CONTAINER_NAME}]: " CONTAINER_NAME
CONTAINER_NAME=${CONTAINER_NAME:-${DEFAULT_CONTAINER_NAME}}
read -p "Which docker repository are we pulling from? [${DEFAULT_CONTAINER_REPO}]: " CONTAINER_REPO
CONTAINER_REPO=${CONTAINER_REPO:-${DEFAULT_CONTAINER_REPO}}
read -p "The local path to the data directory? [${DEFAULT_DATA_DIR}/${CONTAINER_NAME}]: " DATA_DIR
DATA_DIR=${DATA_DIR:-${DEFAULT_DATA_DIR}/${CONTAINER_NAME}}
# setup directories
${SUDO} mkdir -p ${DATA_DIR}/{hls,nginx}
# grab a copy of nginx.conf if it doesn't already exist
echo ${DATA_DIR}/nginx/nginx.conf
ls -al ${DATA_DIR}/nginx/nginx.conf
if [[ ! -e ${DATA_DIR}/nginx/nginx.conf ]]; then
echo "Fetching ${DEFAULT_NGINX_CONF}"
curl -sL ${DEFAULT_NGINX_CONF} | ${SUDO} tee ${DATA_DIR}/nginx/nginx.conf >/dev/null
fi
# build the systemd service file
read -r -d '' service <<-_EOF_
[Unit]
Description=nginx-rtmp in Docker container
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker stop ${CONTAINER_NAME}
ExecStartPre=-/usr/bin/docker rm ${CONTAINER_NAME}
ExecStartPre=-/usr/bin/docker pull ${CONTAINER_REPO}
ExecStart=/usr/bin/docker run --rm -t \\
-v ${DATA_DIR}/hls:/opt/data/hls \\
-v ${DATA_DIR}/nginx/logs:/opt/nginx/logs \\
-v ${DATA_DIR}/nginx/nginx.conf:/opt/nginx/nginx.conf \\
-p 1935:1935 \\
-p 8080:80 \\
--name ${CONTAINER_NAME} ${CONTAINER_REPO}
ExecStop=-/usr/bin/docker stop -t 3 ${CONTAINER_NAME}
ExecStop=-/usr/bin/docker rm ${CONTAINER_NAME}
Restart=always
RestartSec=10s
[Install]
WantedBy=multi-user.target
_EOF_
# write the systemd service file
echo "${service}" | ${SUDO} tee /etc/systemd/system/container-${CONTAINER_NAME}.service >/dev/null
# Ensure permissions are copacetic
${SUDO} chown root:root /etc/systemd/system/container-${CONTAINER_NAME}.service
${SUDO} chmod 0644 /etc/systemd/system/container-${CONTAINER_NAME}.service
# reload systemd
${SUDO} systemctl daemon-reload
# (re)start the service
echo "running: ${SUDO} systemctl restart container-${CONTAINER_NAME}.service"
(${SUDO} systemctl restart container-${CONTAINER_NAME}.service &)
read -p "Hit [Enter] to follow the systemd log, Hit Ctrl-C at anytime"
# follow the systemd service log
${SUDO} journalctl -fu container-${CONTAINER_NAME}.service
exit 0
}
main $@
########################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment