Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Iveson committed Jul 27, 2017
0 parents commit 28c3d2f
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# General Aliases
alias apk='apk --progress'
alias ll="ls -ltan"

alias hosts='cat /etc/hosts'
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias untar="tar xzvkf"
alias mv="mv -nv"
alias cp="cp -i"
alias ip4="ip -4 addr"
alias ip6="ip -6 addr"

COL_YEL="\[\e[1;33m\]"
COL_GRA="\[\e[0;37m\]"
COL_WHI="\[\e[1;37m\]"
COL_GRE="\[\e[1;32m\]"
COL_RED="\[\e[1;31m\]"

# Bash Prompt
if test "$UID" -eq 0 ; then
_COL_USER=$COL_RED
_p=" #"
else
_COL_USER=$COL_GRE
_p=">"
fi
COLORIZED_PROMPT="${_COL_USER}\u${COL_WHI}@${COL_YEL}\h${COL_WHI}:\w${_p} \[\e[m\]"
case $TERM in
*term | rxvt | screen )
PS1="${COLORIZED_PROMPT}\[\e]0;\u@\h:\w\007\]" ;;
linux )
PS1="${COLORIZED_PROMPT}" ;;
* )
PS1="\u@\h:\w${_p} " ;;
esac
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM alpine:latest
LABEL maintainer "Steven Iveson <[email protected]>"
COPY Dockerfile /Dockerfile

RUN apk add -U -v nfs-utils bash iproute2 && \
rm -rf /var/cache/apk/* /tmp/* && \
rm -f /sbin/halt /sbin/poweroff /sbin/reboot && \
mkdir -p /var/lib/nfs/rpc_pipefs && \
mkdir -p /var/lib/nfs/v4recovery && \
echo "rpc_pipefs /var/lib/nfs/rpc_pipefs rpc_pipefs defaults 0 0" >> /etc/fstab && \
echo "nfsd /proc/fs/nfsd nfsd defaults 0 0" >> /etc/fstab

COPY confd-binary /usr/bin/confd
COPY confd/confd.toml /etc/confd/confd.toml
COPY confd/toml/* /etc/confd/conf.d/
COPY confd/tmpl/* /etc/confd/templates/

COPY nfsd.sh /usr/bin/nfsd.sh
COPY .bashrc /root/.bashrc

RUN chmod +x /usr/bin/nfsd.sh /usr/bin/confd

ENTRYPOINT ["/usr/bin/nfsd.sh"]
Binary file added confd-binary
Binary file not shown.
1 change: 1 addition & 0 deletions confd/tmpl/exports.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{getenv "SHARED_DIRECTORY"}} *(rw,fsid=0,async,no_subtree_check,no_auth_nlm,insecure,no_root_squash)
7 changes: 7 additions & 0 deletions confd/toml/exports.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[template]
src = "exports.tmpl"
dest = "/etc/exports"
mode = "0644"
keys = [
"SHARED_DIRECTORY",
]
95 changes: 95 additions & 0 deletions nfsd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/bin/bash

# Make sure we react to these signals by running stop() when we see them - for clean shutdown
# And then exiting
trap "stop; exit 0;" SIGTERM SIGINT

stop()
{
# We're here because we've seen SIGTERM, likely via a Docker stop command or similar
# Let's shutdown cleanly
echo "SIGTERM caught, terminating NFS process(es)..."
/usr/sbin/exportfs -ua
pid1=$(pidof rpc.nfsd)
pid2=$(pidof rpc.mountd)
kill -TERM $pid1 $pid2 > /dev/null 2>&1
echo "Terminated."
exit
}

if [ -z "$SHARED_DIRECTORY" ]; then
echo "The SHARED_DIRECTORY environment variable is null, exiting..."
exit 1
fi

# This loop runs till until we've started up successfully
while true; do

# Check if NFS is running by recording it's PID (if it's not running $pid will be null):
pid=$(pidof rpc.mountd)

# If $pid is null, do this to start or restart NFS:
while [ -z "$pid" ]; do
echo "Starting Confd population of files..."
/usr/bin/confd -version
/usr/bin/confd -onetime
echo ""
echo "Displaying /etc/exports contents..."
cat /etc/exports
echo ""

# Only required if v3 will be used
# echo "Starting rpcbind..."
#/sbin/rpcbind -w
# echo "Displaying rpcbind status..."
#/sbin/rpcinfo

# Only required if v3 will be used
# /usr/sbin/rpc.idmapd
# /usr/sbin/rpc.gssd -v
# /usr/sbin/rpc.statd

echo "Starting NFS in the background..."
/usr/sbin/rpc.nfsd --debug 8 --no-udp --no-nfs-version 2 --no-nfs-version 3
echo "Exporting File System..."
/usr/sbin/exportfs -rv
echo "Starting Mountd in the background..."
/usr/sbin/rpc.mountd --debug all --no-udp --no-nfs-version 2 --no-nfs-version 3
# --exports-file /etc/exports

# Check if NFS is now running by recording it's PID (if it's not running $pid will be null):
pid=$(pidof rpc.mountd)

# If $pid is null, startup failed; log the fact and sleep for 2s
# We'll then automatically loop through and try again
if [ -z "$pid" ]; then
echo "Startup of NFS failed, sleeping for 2s, then retrying..."
sleep 2
fi

done

# Break this outer loop once we've started up successfully
# Otherwise, we'll silently restart and Docker won't know
break

done

while true; do

# Check if NFS is STILL running by recording it's PID (if it's not running $pid will be null):
pid=$(pidof rpc.mountd)
# If it is not, lets kill our PID1 process (this script) by breaking out of this while loop:
# This ensures Docker observes the failure and handles it as necessary
if [ -z "$pid" ]; then
echo "NFS has failed, exiting, so Docker can restart the container..."
break
fi

# If it is, give the CPU a rest
sleep 1

done

sleep 1
exit 1

0 comments on commit 28c3d2f

Please sign in to comment.