Skip to content

Instantly share code, notes, and snippets.

@perj
Created February 16, 2016 21:20
Show Gist options
  • Save perj/79a253c875f88cc73cc6 to your computer and use it in GitHub Desktop.
Save perj/79a253c875f88cc73cc6 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Copyright 2016 Per Johansson
#
# 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.
if [ "$(uname -s)" != Darwin ] || [ "$(uname -r | cut -d . -f 1)" -lt 15 ]; then
echo "OS X El Capitan or greater required."
exit 1
fi
if ! type -p docker-machine > /dev/null || ! type -p docker > /dev/null || ! type -p jq > /dev/null; then
echo "Prerequisites: docker-machine, docker and jq."
echo "Use your favourite package manager."
exit 1
fi
DIGITALOCEAN_REGION="$1"
export DIGITALOCEAN_REGION
DIGITALOCEAN_ACCESS_TOKEN="$(< .token)"
export DIGITALOCEAN_ACCESS_TOKEN
if [ -z "$DIGITALOCEAN_REGION" ] || [ -z "$DIGITALOCEAN_ACCESS_TOKEN" ]; then
echo "Usage:"
echo " echo my-digital-ocean-token > .token ; $0 region"
echo "Valid regions (as of writing, check digital ocean for updates): nyc1 sfo1 nyc2 ams2 sgp1 lon1 nyc3 ams3 fra1 tor1"
exit 1
fi
machine="do-vpn-$DIGITALOCEAN_REGION"
docker-machine create --driver digitalocean "$machine" || exit $?
eval $(docker-machine env "$machine")
machine_ip=$(docker-machine inspect "$machine" | jq -r .Driver.IPAddress)
profid="do.vpn.$DIGITALOCEAN_REGION"
image="gaomd/ikev2-vpn-server:0.2.2"
docker run -d --name ikev2-vpn-server --privileged -p 500:500/udp -p 4500:4500/udp "$image"
srun=$?
docker run -i -t --rm --volumes-from ikev2-vpn-server \
-e "HOST=$machine_ip" \
-e "PROFILE_NAME=Profile for VPN $machine" \
-e "PROFILE_IDENTIFIER=$profid" \
-e "CONN_NAME=VPN $machine" \
"$image" generate-mobileconfig | profiles -I -F -
sconfig=$?
if [ "$srun" != 0 ] || [ "$sconfig" != 0 ]; then
echo "Failed to start VPN or to install configuration. Shutting down."
docker-machine rm "$machine"
profiles -R -p "$profid"
exit 1
fi
echo
echo "VPN running."
echo "Press enter to shut down"
read
docker-machine rm "$machine"
profiles -R -p "$profid"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment