github.com/mccv1r0/cni@v0.7.0-alpha1/scripts/exec-plugins.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  if [[ ${DEBUG} -gt 0 ]]; then set -x; fi
     4  
     5  NETCONFPATH=${NETCONFPATH-/etc/cni/net.d}
     6  
     7  function exec_plugins() {
     8  	i=0
     9  	contid=$2
    10  	netns=$3
    11  	export CNI_COMMAND=$(echo $1 | tr '[:lower:]' '[:upper:]')
    12  	export PATH=$CNI_PATH:$PATH
    13  	export CNI_CONTAINERID=$contid
    14  	export CNI_NETNS=$netns
    15  
    16  	for netconf in $(echo $NETCONFPATH/*.conf | sort); do
    17  		name=$(jq -r '.name' <$netconf)
    18  		plugin=$(jq -r '.type' <$netconf)
    19  		export CNI_IFNAME=$(printf eth%d $i)
    20  
    21  		res=$($plugin <$netconf)
    22  		if [ $? -ne 0 ]; then
    23  			errmsg=$(echo $res | jq -r '.msg')
    24  			if [ -z "$errmsg" ]; then
    25  				errmsg=$res
    26  			fi
    27  
    28  			echo "${name} : error executing $CNI_COMMAND: $errmsg"
    29  			exit 1
    30  		elif [[ ${DEBUG} -gt 0 ]]; then
    31  			echo ${res} | jq -r .
    32  		fi
    33  
    34  		let "i=i+1"
    35  	done
    36  }
    37  
    38  if [ $# -ne 3 ]; then
    39  	echo "Usage: $0 add|del CONTAINER-ID NETNS-PATH"
    40  	echo "  Adds or deletes the container specified by NETNS-PATH to the networks"
    41  	echo "  specified in \$NETCONFPATH directory"
    42  	exit 1
    43  fi
    44  
    45  exec_plugins $1 $2 $3