refactor: replace some 'if's with && and || expressions

This commit is contained in:
Matthias Radestock committed 2015-12-31 15:36:18 +00:00
1 parent c1e8c9c537
commit 9c31de9cb9
1 file changed
+25 -78
+25 -78
View File
@@ -4,11 +4,8 @@ set -e
[ -n "$WEAVE_DEBUG" ] && set -x [ -n "$WEAVE_DEBUG" ] && set -x
SCRIPT_VERSION="(unreleased version)" SCRIPT_VERSION="(unreleased version)"
if [ "$SCRIPT_VERSION" = "(unreleased version)" ] ; then IMAGE_VERSION=latest
IMAGE_VERSION=latest [ "$SCRIPT_VERSION" = "(unreleased version)" ] || IMAGE_VERSION=$SCRIPT_VERSION
else
IMAGE_VERSION=$SCRIPT_VERSION
fi
IMAGE_VERSION=${WEAVE_VERSION:-$IMAGE_VERSION} IMAGE_VERSION=${WEAVE_VERSION:-$IMAGE_VERSION}
# - Docker versions prior to 1.6 cannot pull images from DockerHub # - Docker versions prior to 1.6 cannot pull images from DockerHub
@@ -271,10 +268,7 @@ extra_hosts_args() {
check_docker_version check_docker_version
if [ "$1" = "--local" ] ; then [ "$1" = "--local" ] && shift 1 && IS_LOCAL=1
shift 1
IS_LOCAL=1
fi
if [ "$1" = "--help" -o "$1" = "help" ] ; then if [ "$1" = "--help" -o "$1" = "help" ] ; then
# "--help|help" are special because we always want to process them # "--help|help" are special because we always want to process them
@@ -303,9 +297,7 @@ elif [ "$1" = "run" -a -z "$IS_LOCAL" ] ; then
# we can't rely on our baked-in docker to support those arguments. # we can't rely on our baked-in docker to support those arguments.
shift 1 shift 1
if [ "$1" != "--without-dns" ] ; then [ "$1" = "--without-dns" ] || DNS_ARGS=$(exec_remote dns-args "$@" || true)
DNS_ARGS=$(exec_remote dns-args "$@" || true)
fi
shift $(dns_arg_count "$@") shift $(dns_arg_count "$@")
REWRITE_HOSTS=1 REWRITE_HOSTS=1
@@ -325,14 +317,8 @@ elif [ "$1" = "run" -a -z "$IS_LOCAL" ] ; then
shift shift
done done
if [ -n "$REWRITE_HOSTS" ] ; then [ -n "$REWRITE_HOSTS" ] && extra_hosts_args "$@" && DNS_EXTRA_HOSTS_ARGS="--rewrite-hosts $DNS_EXTRA_HOSTS_ARGS"
extra_hosts_args "$@" [ -n "$NO_MULTICAST_ROUTE" ] && ATTACH_ARGS="--no-multicast-route"
DNS_EXTRA_HOSTS_ARGS="--rewrite-hosts $DNS_EXTRA_HOSTS_ARGS"
fi
if [ -n "$NO_MULTICAST_ROUTE" ] ; then
ATTACH_ARGS="--no-multicast-route"
fi
collect_cidr_args "$@" collect_cidr_args "$@"
shift $CIDR_ARG_COUNT shift $CIDR_ARG_COUNT
@@ -368,9 +354,7 @@ HTTP_PORT=6784
PROXY_PORT=12375 PROXY_PORT=12375
PROXY_CONTAINER_NAME=weaveproxy PROXY_CONTAINER_NAME=weaveproxy
COVERAGE_ARGS="" COVERAGE_ARGS=""
if [ -n "$COVERAGE" ] ; then [ -n "$COVERAGE" ] && COVERAGE_ARGS="-test.coverprofile=/home/weave/cover.prof --"
COVERAGE_ARGS="-test.coverprofile=/home/weave/cover.prof --"
fi
###################################################################### ######################################################################
# general helpers; independent of docker and weave # general helpers; independent of docker and weave
@@ -400,9 +384,7 @@ fractional_sleep() {
run_iptables() { run_iptables() {
# -w is recent addition to iptables # -w is recent addition to iptables
if [ -z "$CHECKED_IPTABLES_W" ] ; then if [ -z "$CHECKED_IPTABLES_W" ] ; then
if iptables -S -w >/dev/null 2>&1 ; then iptables -S -w >/dev/null 2>&1 && IPTABLES_W=-w
IPTABLES_W=-w
fi
CHECKED_IPTABLES_W=1 CHECKED_IPTABLES_W=1
fi fi
@@ -447,9 +429,7 @@ configure_arp_cache() {
arp_update() { arp_update() {
# It's not the end of the world if this doesn't run - we configure # It's not the end of the world if this doesn't run - we configure
# ARP caches so that stale entries will be noticed quickly. # ARP caches so that stale entries will be noticed quickly.
if command_exists arping ; then ! command_exists arping || $3 arping -U -q -I $1 -c 1 ${2%/*}
$3 arping -U -q -I $1 -c 1 ${2%/*}
fi
} }
# Generate a random MAC value # Generate a random MAC value
@@ -572,9 +552,7 @@ EOF
MTU=${WEAVE_MTU:-$(cat /sys/class/net/$BRIDGE/mtu)} MTU=${WEAVE_MTU:-$(cat /sys/class/net/$BRIDGE/mtu)}
fi fi
if [ "$1" != "--without-ethtool" ] ; then [ "$1" = "--without-ethtool" ] || ethtool_tx_off_$BRIDGE_TYPE $BRIDGE
ethtool_tx_off_$BRIDGE_TYPE $BRIDGE
fi
ip link set dev $BRIDGE up ip link set dev $BRIDGE up
@@ -597,9 +575,7 @@ init_fastdp() {
init_bridge() { init_bridge() {
# Observe any MTU that is already set # Observe any MTU that is already set
if [ -z "$MTU" ] ; then [ -n "$MTU" ] || MTU=${WEAVE_MTU:-65535}
MTU=${WEAVE_MTU:-65535}
fi
ip link add name $BRIDGE type bridge ip link add name $BRIDGE type bridge
@@ -834,9 +810,8 @@ setup_router_iface_bridge() {
return 1 return 1
fi fi
# No-op if already attached # No-op if already attached
if netnsenter ip link show $CONTAINER_IFNAME >/dev/null 2>&1 ; then netnsenter ip link show $CONTAINER_IFNAME >/dev/null 2>&1 && return 0
return 0
fi
connect_container_to_bridge $CONTAINER_IFNAME && connect_container_to_bridge $CONTAINER_IFNAME &&
netnsenter ethtool -K eth0 tx off >/dev/null && netnsenter ethtool -K eth0 tx off >/dev/null &&
netnsenter ip link set $CONTAINER_IFNAME up netnsenter ip link set $CONTAINER_IFNAME up
@@ -943,9 +918,7 @@ check_not_running() {
} }
stop() { stop() {
if ! docker stop $1 >/dev/null 2>&1 ; then docker stop $1 >/dev/null 2>&1 || echo "$2 is not running." >&2
echo "$2 is not running." >&2
fi
docker rm -f $1 >/dev/null 2>&1 || true docker rm -f $1 >/dev/null 2>&1 || true
} }
@@ -1202,9 +1175,7 @@ check_running() {
# Execute $@ only if the weave container is running # Execute $@ only if the weave container is running
when_weave_running() { when_weave_running() {
if check_running $CONTAINER_NAME 2>/dev/null ; then ! check_running $CONTAINER_NAME 2>/dev/null || "$@"
"$@"
fi
} }
# Iff the container in $1 has an FQDN, invoke $2 as a command passing # Iff the container in $1 has an FQDN, invoke $2 as a command passing
@@ -1265,21 +1236,13 @@ delete_dns_fqdn() {
collect_dns_add_remove_args() { collect_dns_add_remove_args() {
collect_ip_args "$@" collect_ip_args "$@"
shift $IP_COUNT shift $IP_COUNT
if [ $# -gt 0 -a "$1" != "-h" ] ; then [ $# -gt 0 -a "$1" != "-h" ] && C="$1" && shift 1
C="$1" [ $# -eq 2 -a "$1" = "-h" ] && FQDN="$2" && shift 2
shift
fi
if [ $# -eq 2 -a "$1" = "-h" ] ; then
FQDN="$2"
shift 2
fi
[ $# -eq 0 -a \( -n "$C" -o \( $IP_COUNT -gt 0 -a -n "$FQDN" \) \) ] || usage [ $# -eq 0 -a \( -n "$C" -o \( $IP_COUNT -gt 0 -a -n "$FQDN" \) \) ] || usage
check_running $CONTAINER_NAME check_running $CONTAINER_NAME
if [ -n "$C" ] ; then if [ -n "$C" ] ; then
CONTAINER=$(container_id $C) CONTAINER=$(container_id $C)
if [ $IP_COUNT -eq 0 ] ; then [ $IP_COUNT -gt 0 ] || IP_ARGS=$(with_container_addresses echo_ips $CONTAINER)
IP_ARGS=$(with_container_addresses echo_ips $CONTAINER)
fi
fi fi
} }
@@ -1622,10 +1585,7 @@ launch_router() {
fi fi
fi fi
if [ "$BRIDGE_TYPE" != bridge ] ; then [ "$BRIDGE_TYPE" != bridge ] && NETHOST_OPT="--net=host" && HTTP_IP=127.0.0.1
NETHOST_OPT="--net=host"
HTTP_IP=127.0.0.1
fi
# Set WEAVE_DOCKER_ARGS in the environment in order to supply # Set WEAVE_DOCKER_ARGS in the environment in order to supply
# additional parameters, such as resource limits, to docker # additional parameters, such as resource limits, to docker
@@ -1904,16 +1864,11 @@ EOF
;; ;;
config|proxy-config) config|proxy-config)
[ "$COMMAND" = "config" ] || deprecation_warning "$COMMAND" "'weave config'" [ "$COMMAND" = "config" ] || deprecation_warning "$COMMAND" "'weave config'"
if PROXY_ADDR=$(proxy_addr) ; then PROXY_ADDR=$(proxy_addr) && echo "-H=$PROXY_ADDR"
echo "-H=$PROXY_ADDR"
fi
;; ;;
connect) connect)
[ $# -gt 0 ] || usage [ $# -gt 0 ] || usage
if [ "$1" = "--replace" ] ; then [ "$1" = "--replace" ] && replace="-d replace=true" && shift
replace="-d replace=true"
shift
fi
call_weave POST /connect $replace -d $(peer_args "$@") call_weave POST /connect $replace -d $(peer_args "$@")
;; ;;
forget) forget)
@@ -1991,10 +1946,7 @@ EOF
CONTAINER=$(docker run -e WEAVE_CIDR=none $DNS_ARGS -d "$@") CONTAINER=$(docker run -e WEAVE_CIDR=none $DNS_ARGS -d "$@")
create_bridge create_bridge
ipam_cidrs_or_die allocate $CONTAINER $CIDR_ARGS ipam_cidrs_or_die allocate $CONTAINER $CIDR_ARGS
if [ -n "$REWRITE_HOSTS" ] ; then [ -n "$REWRITE_HOSTS" ] && extra_hosts_args "$@" && rewrite_etc_hosts $DNS_EXTRA_HOSTS
extra_hosts_args "$@"
rewrite_etc_hosts $DNS_EXTRA_HOSTS
fi
with_container_netns_or_die $CONTAINER attach $ALL_CIDRS with_container_netns_or_die $CONTAINER attach $ALL_CIDRS
when_weave_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS when_weave_running with_container_fqdn $CONTAINER put_dns_fqdn $ALL_CIDRS
echo $CONTAINER echo $CONTAINER
@@ -2074,10 +2026,7 @@ EOF
dns-add) dns-add)
collect_dns_add_remove_args "$@" collect_dns_add_remove_args "$@"
FN=put_dns_fqdn FN=put_dns_fqdn
if [ -z "$CONTAINER" ] ; then [ -z "$CONTAINER" ] && CONTAINER=weave:extern && FN=put_dns_fqdn_no_check_alive
FN=put_dns_fqdn_no_check_alive
CONTAINER=weave:extern
fi
if [ -n "$FQDN" ] ; then if [ -n "$FQDN" ] ; then
$FN $CONTAINER $FQDN $IP_ARGS $FN $CONTAINER $FQDN $IP_ARGS
else else
@@ -2086,7 +2035,7 @@ EOF
;; ;;
dns-remove) dns-remove)
collect_dns_add_remove_args "$@" collect_dns_add_remove_args "$@"
[ -n "$CONTAINER" ] || CONTAINER=weave:extern [ -z "$CONTAINER" ] && CONTAINER=weave:extern
if [ -n "$FQDN" ] ; then if [ -n "$FQDN" ] ; then
delete_dns_fqdn $CONTAINER $FQDN $IP_ARGS delete_dns_fqdn $CONTAINER $FQDN $IP_ARGS
else else
@@ -2116,9 +2065,7 @@ EOF
add_iptables_rule nat WEAVE -d $CIDR ! -s $CIDR -j MASQUERADE add_iptables_rule nat WEAVE -d $CIDR ! -s $CIDR -j MASQUERADE
add_iptables_rule nat WEAVE -s $CIDR ! -d $CIDR -j MASQUERADE add_iptables_rule nat WEAVE -s $CIDR ! -d $CIDR -j MASQUERADE
fi fi
if [ -n "$FQDN" ] ; then [ -n "$FQDN" ] && when_weave_running put_dns_fqdn_no_check_alive weave:expose $FQDN $CIDR
when_weave_running put_dns_fqdn_no_check_alive weave:expose $FQDN $CIDR
fi
done done
show_addrs $ALL_CIDRS show_addrs $ALL_CIDRS
;; ;;