Skip to content

Instantly share code, notes, and snippets.

@twang2218
Created July 12, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twang2218/08bee95be386e241be2620a91e653c0b to your computer and use it in GitHub Desktop.
Save twang2218/08bee95be386e241be2620a91e653c0b to your computer and use it in GitHub Desktop.
Script to create an OpenVPN service
#!/bin/sh
# Before run this script, a docker host in a public cloud should be available
# The host can be created by the following commands:
#
# docker-machine create -d digitalocean dev
# eval $(docker-machine env dev)
#
# Then you can run this command simply by :
#
# ./prepare-openvpn-service.sh all <server_address> <username>
#
function create {
if [ -z "$1" ]; then
echo "Usage: $0 create <server_address>"
exit 1
fi
VPN_SERVER=$1
docker volume create --name openvpn
docker run -v openvpn:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u udp://${VPN_SERVER}
docker run -v openvpn:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki nopass
}
function destroy {
docker rm --volumes --force openvpn
}
function run {
docker run -v openvpn:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN --name openvpn kylemanna/openvpn
}
function generate_config {
if [ -z "$1" ]; then
echo "Usage: $0 generate <username>"
exit 1
fi
USER_NAME=$1
docker run -v openvpn:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full ${USER_NAME} nopass
}
function export_config {
if [ -z "$1" ]; then
echo "Usage: $0 export <username>"
exit 1
fi
USER_NAME=$1
docker run -v openvpn:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient ${USER_NAME} > ${USER_NAME}.ovpn
}
function usage {
echo "Usage: $0 {create|generate|export|run|destroy}"
}
function run_all {
if [ ! "$#" -eq "2" ]; then
echo "Usage: $0 all <server_address> <username>"
exit 1
fi
VPN_SERVER=$1
USER_NAME=$2
create ${VPN_SERVER}
run
generate_config ${USER_NAME}
export_config ${USER_NAME}
}
function main {
Command=$1
shift
case "${Command}" in
create) create $@ ;;
destroy) destroy ;;
run) run ;;
generate) generate_config $@ && export_config $@ ;;
export) export_config $@ ;;
all) run_all $@ ;;
*) usage ;;
esac
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment