Last active
August 29, 2015 14:14
-
-
Save xemoe/416a43fd3959b60ded37 to your computer and use it in GitHub Desktop.
Kylemanna/docker-openvpn helper script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
################################# | |
## Boostrap and Initial parameters | |
################################# | |
# Require realpath | |
type realpath; | |
if [ $? -eq 1 ]; then | |
echo "realpath command not found, wait for install" | |
sudo apt-get update && sudo apt-get install realpath -y; | |
fi | |
################################# | |
## Configurations | |
################################# | |
SCRIPT_PATH=$(dirname $(realpath -s $0)) | |
OVPN_DATA=dockers_ovpndata_1 | |
OVPN_SERVICE=dockers_ovpnserv_1 | |
DOCKER=/usr/bin/docker | |
OVPN_IMAGE=kylemanna/openvpn | |
################################# | |
## Functions | |
################################# | |
function available_command { | |
echo "Available Command is" | |
echo "1) start_data_container" | |
echo "2) initialize_configurations" | |
echo "3) start_vpn" | |
echo "4) generate_client_cert" | |
echo "5) retrieve_client_cert" | |
echo "6) clean" | |
} | |
function start_data_container { | |
${DOCKER} run -d --name ${OVPN_DATA} -v /etc/openvpn busybox tail -f /dev/null | |
} | |
function initialize_configurations { | |
if [ -z ${1} ]; then | |
echo "Require parameter {2} tobe hostname or ipaddress" | |
exit; | |
fi | |
HOST_NAME=${1} | |
${DOCKER} run --volumes-from ${OVPN_DATA} --rm ${OVPN_IMAGE} ovpn_genconfig -u udp://${HOST_NAME} | |
${DOCKER} run --volumes-from ${OVPN_DATA} --rm -it ${OVPN_IMAGE} ovpn_initpki | |
} | |
function start_vpn { | |
${DOCKER} run --name ${OVPN_SERVICE} --volumes-from ${OVPN_DATA} -d -p 1194:1194/udp --privileged --cap-add=NET_ADMIN ${OVPN_IMAGE} | |
} | |
function generate_client_cert { | |
if [ -z ${1} ]; then | |
echo "Require parameter {2} tobe CLIENT_NAME" | |
exit; | |
fi | |
CLIENT_NAME=${1} | |
${DOCKER} run --volumes-from ${OVPN_DATA} --rm -it ${OVPN_IMAGE} easyrsa build-client-full ${CLIENT_NAME} nopass | |
} | |
function retrieve_client_cert { | |
if [ -z ${1} ]; then | |
echo "Require parameter {2} tobe CLIENT_NAME" | |
exit; | |
fi | |
if [ ! -d ${SCRIPT_PATH}/files ]; then | |
mkdir ${SCRIPT_PATH}/files | |
fi | |
CLIENT_NAME=${1} | |
${DOCKER} run --volumes-from ${OVPN_DATA} --rm ${OVPN_IMAGE} ovpn_getclient ${CLIENT_NAME} > ${SCRIPT_PATH}/files/${CLIENT_NAME}.ovpn | |
} | |
function clean { | |
${DOCKER} ps -a | grep Exit | cut -d ' ' -f 1 | xargs ${DOCKER} rm | |
} | |
################################# | |
## Argument | |
################################# | |
ARG=${1} | |
################################# | |
## Start commands | |
################################# | |
case ${ARG} in | |
"start_data_container"|"1") | |
start_data_container | |
;; | |
"initialize_configurations"|"2") | |
initialize_configurations ${2} | |
;; | |
"start_vpn"|"3") | |
start_vpn | |
;; | |
"generate_client_cert"|"4") | |
generate_client_cert ${2} | |
;; | |
"retrieve_client_cert"|"5") | |
retrieve_client_cert ${2} | |
;; | |
"clean"|"6") | |
clean | |
;; | |
*) | |
available_command | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment