refactor: extract helper for and simplify get_ipam_cidr
This commit is contained in:
1 parent
6f57bcb477
commit
79eb4c298c
1 file changed
+29
-26
@@ -528,6 +528,26 @@ container_ip() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Given a container name or short ID in $1, ensure the specified
|
||||||
|
# container is running and then print its full ID to stdout. If
|
||||||
|
# it's not running or doesn't exist, print an error to stderr and
|
||||||
|
# return with an indicative non-zero exit code.
|
||||||
|
container_id() {
|
||||||
|
if ! STATUS=$(docker inspect --format='{{.State.Running}} {{.Id}}' $1 2>/dev/null) ; then
|
||||||
|
echo "Error: No such container: $1" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
case "$STATUS" in
|
||||||
|
true*)
|
||||||
|
echo "${STATUS#true }"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "$1 container is not running." >&2
|
||||||
|
return 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
# Call url $4 with http verb $3 on container $1 at port $2
|
# Call url $4 with http verb $3 on container $1 at port $2
|
||||||
http_call() {
|
http_call() {
|
||||||
container_ip $1 \
|
container_ip $1 \
|
||||||
@@ -693,31 +713,13 @@ ipam_claim() {
|
|||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
# If we don't already have any CIDRs passed from the command-line, get one from IPAM
|
# If we don't already have any CIDRs passed from the command-line, get
|
||||||
# $1 is the name of the container and $2, if set, is the ID to use inside IPAM
|
# one from IPAM. $1 is the full container id.
|
||||||
get_ipam_cidr() {
|
get_ipam_cidr() {
|
||||||
if [ -n "$CIDR_ARGS" ] ; then
|
if [ -n "$CIDR_ARGS" ] ; then
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
if [ -z "$2" ] ; then
|
CIDR_ARGS="$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$1)"
|
||||||
# get the long form of the container ID
|
|
||||||
if ! status=$(docker inspect --format='{{.State.Running}} {{.Id}}' $1 2>/dev/null) ; then
|
|
||||||
echo "Error: No such container: $1" >&2
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
case "$status" in
|
|
||||||
true*)
|
|
||||||
CONTAINER="${status#true }"
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
echo "$1 container is not running." >&2
|
|
||||||
return 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
else
|
|
||||||
CONTAINER="$2"
|
|
||||||
fi
|
|
||||||
CIDR_ARGS="$(http_call $CONTAINER_NAME $HTTP_PORT POST /ip/$CONTAINER)"
|
|
||||||
if [ "$CIDR_ARGS" = "404 page not found" ] ; then
|
if [ "$CIDR_ARGS" = "404 page not found" ] ; then
|
||||||
echo "No IP address supplied (use the -iprange option on 'weave launch' to enable IP address allocation)" >&2
|
echo "No IP address supplied (use the -iprange option on 'weave launch' to enable IP address allocation)" >&2
|
||||||
return 1
|
return 1
|
||||||
@@ -920,7 +922,7 @@ case "$COMMAND" in
|
|||||||
shift $CIDR_COUNT
|
shift $CIDR_COUNT
|
||||||
CONTAINER=$(docker run $DNS_ARGS -d "$@")
|
CONTAINER=$(docker run $DNS_ARGS -d "$@")
|
||||||
create_bridge
|
create_bridge
|
||||||
get_ipam_cidr $CONTAINER $CONTAINER
|
get_ipam_cidr $CONTAINER
|
||||||
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null
|
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null
|
||||||
tell_dns PUT $CONTAINER $CIDR_ARGS
|
tell_dns PUT $CONTAINER $CIDR_ARGS
|
||||||
echo $CONTAINER
|
echo $CONTAINER
|
||||||
@@ -940,7 +942,7 @@ case "$COMMAND" in
|
|||||||
CONTAINER_ID="$1"
|
CONTAINER_ID="$1"
|
||||||
create_bridge
|
create_bridge
|
||||||
CONTAINER=$(docker start $CONTAINER_ID)
|
CONTAINER=$(docker start $CONTAINER_ID)
|
||||||
get_ipam_cidr $CONTAINER_ID $CONTAINER
|
get_ipam_cidr $CONTAINER
|
||||||
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null
|
with_container_netns $CONTAINER attach $CIDR_ARGS >/dev/null
|
||||||
tell_dns PUT $CONTAINER_ID $CIDR_ARGS
|
tell_dns PUT $CONTAINER_ID $CIDR_ARGS
|
||||||
echo $CONTAINER
|
echo $CONTAINER
|
||||||
@@ -950,8 +952,9 @@ case "$COMMAND" in
|
|||||||
shift $CIDR_COUNT
|
shift $CIDR_COUNT
|
||||||
[ $# -eq 1 ] || usage
|
[ $# -eq 1 ] || usage
|
||||||
CONTAINER_ID="$1"
|
CONTAINER_ID="$1"
|
||||||
|
CONTAINER=$(container_id $CONTAINER_ID)
|
||||||
create_bridge
|
create_bridge
|
||||||
get_ipam_cidr $CONTAINER_ID
|
get_ipam_cidr $CONTAINER
|
||||||
with_container_netns $CONTAINER_ID attach $CIDR_ARGS >/dev/null
|
with_container_netns $CONTAINER_ID attach $CIDR_ARGS >/dev/null
|
||||||
tell_dns PUT $CONTAINER_ID $CIDR_ARGS
|
tell_dns PUT $CONTAINER_ID $CIDR_ARGS
|
||||||
;;
|
;;
|
||||||
@@ -986,7 +989,7 @@ case "$COMMAND" in
|
|||||||
expose)
|
expose)
|
||||||
collect_cidr_args "$@"
|
collect_cidr_args "$@"
|
||||||
shift $CIDR_COUNT
|
shift $CIDR_COUNT
|
||||||
get_ipam_cidr $CONTAINER_NAME weave:expose
|
get_ipam_cidr weave:expose
|
||||||
if [ $# -eq 0 ] ; then
|
if [ $# -eq 0 ] ; then
|
||||||
FQDN=""
|
FQDN=""
|
||||||
else
|
else
|
||||||
@@ -1009,7 +1012,7 @@ case "$COMMAND" in
|
|||||||
hide)
|
hide)
|
||||||
collect_cidr_args "$@"
|
collect_cidr_args "$@"
|
||||||
shift $CIDR_COUNT
|
shift $CIDR_COUNT
|
||||||
get_ipam_cidr $CONTAINER_NAME weave:expose
|
get_ipam_cidr weave:expose
|
||||||
create_bridge --without-ethtool
|
create_bridge --without-ethtool
|
||||||
for CIDR in $CIDR_ARGS ; do
|
for CIDR in $CIDR_ARGS ; do
|
||||||
if ip addr show dev $BRIDGE | grep -qF $CIDR ; then
|
if ip addr show dev $BRIDGE | grep -qF $CIDR ; then
|
||||||
|
|||||||
Reference in new issue
Block a user