-
Notifications
You must be signed in to change notification settings - Fork 15
/
dmcontext
executable file
·57 lines (48 loc) · 1.86 KB
/
dmcontext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
# This is a Bash script to create a Docker context that corresponds to
# the specified Docker Machine created/managed by docker-machine
# It is a context replacement for `eval $(docker-machine env name)`
# Test for sane inputs
if [ "$#" -ne 1 ]; then
echo "Incorrect number of parameters; please provide only the name of a machine to use to create a context"
exit 1
fi
if [ "$1" = "--help" ]; then
echo "Usage: dmcontext <name-of-machine-created-by-docker-machine>"
exit 0
fi
# Set the name of the machine against which the script will operate
MACHINE=$1
# Set full path to "docker-machine" command; error if not available
DM=$(command -v docker-machine)
if [ -z $DM ]; then
echo "Can't find docker-machine; exiting..."
exit 1
fi
# Set full path to "docker" command; error if not available
DCLI=$(command -v docker)
if [ -z $DCLI ]; then
echo "Can't find Docker CLI; exiting..."
exit 1
fi
# Test to see if specified Docker Machine exists
TEST=$($DM ls --format '{{ .Name }}' | grep $MACHINE -)
if [ -z $TEST ]; then
echo "Can't find specified machine; exiting..."
exit 1
fi
# Check for name collision with existing Docker context
TEST=$($DCLI context ls --format '{{ .Name }}' | grep $MACHINE -)
if [ ! -z $TEST ]; then
echo "Docker context with that name already exists; exiting..."
exit 1
fi
# Gather the necessary information about the machine
IPADDR=$($DM inspect $MACHINE --format '{{ .Driver.IPAddress }}')
CA=$($DM inspect $MACHINE --format '{{ .HostOptions.AuthOptions.CaCertPath }}')
CERT=$($DM inspect $MACHINE --format '{{ .HostOptions.AuthOptions.ClientCertPath }}')
KEY=$($DM inspect $MACHINE --format '{{ .HostOptions.AuthOptions.ClientKeyPath }}')
# Create the context
$DCLI context create $MACHINE \
--description "Docker context for $MACHINE" \
--docker "host=tcp://$IPADDR:2376,ca=$CA,cert=$CERT,key=$KEY"