add docker-hosts plugin

This commit is contained in:
Buck Brady 2017-01-01 14:29:03 -07:00
commit b311d6814b
No known key found for this signature in database
GPG key ID: EB55519CCA3014F3
3 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 Buck Brady
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.

View file

@ -0,0 +1,21 @@
### docker-hosts
docker hosts is a simple zsh plugin to make managing multiple docker hosts easily.
#### Usage
dh-init: initializes the plugin dir and files for use
dh-add (nickname) (host IP/FQDN): adds a host with a nickname to the hosts list
dh-delete (nickname/IP/FQDN): removes the given host from the list
dh-list: shows all hosts in the hostlist
dh-set (nickname): sets the $DOCKER_HOST variable to the given host
dh-set-default (nickname): sets the host that will be automaticly loaded on shell startup
dh-current: shows the current $DOCKER_HOST vaule
dh-clear: clears the DOCKER_HOST and allows docker to connect to localhost

View file

@ -0,0 +1,88 @@
# MIT License
#
# Copyright (c) 2016 Buck Brady
#
# 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.
# initilizes the plugin dir
function dh-init() {
if [ ! -d ~/.docker-hosts ]
then
mkdir -p ~/.docker-hosts
touch ~/.docker-hosts/hosts
touch ~/.docker-hosts/default
printf "docker-host plugin intialized.\n"
fi
}
# lists all hosts in hosts file
function dh-list() {
dh-init
printf '%-20s %s\n' '-- Nickname --' '-- Host IP/FQDN --'
awk -F',' '{printf "%-20s %s\n", $1, $2}' \
$HOME/.docker-hosts/hosts | grep -v "#"
}
# adds host to hostfile
function dh-add() {
dh-init
echo "$1,$2" >> ~/.docker-hosts/hosts
printf 'Host "%s" added with IP/FQDN "%s"\n' $1 $2
}
# removes host from host file
function dh-remove() {
mv $HOME/.docker-hosts/hosts $HOME/.docker-hosts/hosts.old
grep -vw "$1" $HOME/.docker-hosts/hosts.old > $HOME/.docker-hosts/hosts
rm -f $HOME/.docker-hosts/hosts.old
}
# sets $DOCKER_HOST to given host in hostfile
function dh-set() {
dh-init
host=`grep -w $1 $HOME/.docker-hosts/hosts | awk -F',' '{print $2}'`
export DOCKER_HOST=tcp://$host
}
# loads the default host
function dh-get-default() {
host=`cat $HOME/.docker-hosts/default`
export DOCKER_HOST=tcp://$host
}
# sets host to be loaded as the default
function dh-set-default() {
grep -w $1 $HOME/.docker-hosts/hosts \
| awk -F',' '{print $2}' > $HOME/.docker-hosts/default
}
# list the current docker host
function dh-current() {
printf 'Current Docker Host: %s\n' $DOCKER_HOST
}
# clears the $DOCKER_HOST env
function dh-clear() {
export DOCKER_HOST=''
print 'DOCKER_HOST is now empty, docker will try to connect to localhost socket.'
}
# load plugin
dh-init
dh-get-default