runnode.sh 4 KB
Newer Older
Greg Rutz's avatar
Greg Rutz committed
1
#!/bin/bash -x
Greg Rutz's avatar
Greg Rutz committed
2

3 4
echo "Args = $@"

Greg Rutz's avatar
Greg Rutz committed
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
usage() {
  echo "Usage: runmetis [OPTION]..." 
  echo "Generate CCNx metis.cfg file according to the specified links and routes"
  echo "and then start the metis_daemon with that config.  Optionally, this script"
  echo "can start the metis_daemon with no links and start one or more ccnx_server"
  echo "processes to provide object data"
  echo ""
  echo "OPTIONS:"
  echo "  -h"        
  echo "             Show this help message"
  echo "  -r <LINKNAME>=<PREFIX>"
  echo "             Associates the given link alias <LINKNAME> with the routable"
  echo "             prefix <PREFIX>.  Can be specified more than once.  When this"
  echo "             option is present, all -s options will be ignored."
  echo "  -s <LCI_URI>=<SERVER_PROGRAM>"
  echo "             Run a ccnx_server process on this node that will return the"
  echo "             contents of the object identified by <LCI_URI> and run"
  echo "             the program <SERVER_PROGRAM> to generate the data"
  echo ""
}

declare -a servers
declare -A ROUTES

while getopts ":hs:r:" opt; do
  case $opt in
    h)
Greg Rutz's avatar
Greg Rutz committed
32 33
      usage
      exit 0
Greg Rutz's avatar
Greg Rutz committed
34 35
      ;;
    s)
36
      servers=("${servers[@]}" "$OPTARG")
Greg Rutz's avatar
Greg Rutz committed
37 38 39 40 41
      ;;
    r)
      IFS='=' read -ra ROUTE <<< $OPTARG
      link=${ROUTE[0]}
      route=${ROUTE[1]}
Greg Rutz's avatar
Greg Rutz committed
42 43 44
      # Convert link name to uppercase for matching with environment variables
      link=${link^^}
      # Either append or set the value in the associative array
Greg Rutz's avatar
Greg Rutz committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      if [ ${ROUTES[$link]+_} ]; then
        ROUTES[$link]=${ROUTES[$link]}" $route"
      else
        ROUTES[$link]=$route
      fi
      servers=()
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      usage
      exit 1
      ;;
    :)
      echo "Missing options argument for -$OPTARG" >&2
      usage
      exit 1
      ;;
  esac
done

shift $((OPTIND - 1))

# Get names of all links
Greg Rutz's avatar
Greg Rutz committed
68 69 70 71 72
declare -a links
names=$(env | grep "\(.*\)_NAME")
for name in $names; do
  links=("${links[@]}" $(echo $name | sed -e 's/\(.*\)_NAME=.*/\1/g'))
done
Greg Rutz's avatar
Greg Rutz committed
73

Greg Rutz's avatar
Greg Rutz committed
74 75 76
# Validate routes passed on command line against available links
for link in "${!ROUTES[@]}"; do 
  if [[ ! ${links[@]} =~ $link ]]; then
Greg Rutz's avatar
Greg Rutz committed
77 78 79 80 81 82 83 84 85 86 87 88
    echo "Link name specified on the command line ($link) is not availabe in this container"
    exit 1
  fi
done

# Metis Config File
metiscfg=metis.cfg
truncate -s 0 $metiscfg

# Get local ip
localip=$(ifconfig eth0 2>/dev/null | awk '/inet addr:/ {print $2}' | sed 's/addr://')

Greg Rutz's avatar
Greg Rutz committed
89 90 91 92 93 94
# Start out local ports for connections at 5000
localport=5000

# Add connection and route entries to our metis.cfg for each
# outgoing link
for link in "${links[@]}"; do
Greg Rutz's avatar
Greg Rutz committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108
  ports=$(env | grep "${link}_PORT_" | sed -e "s/${link}_PORT_\([[:digit:]]\+\).*/\1/g")  

  # Remove duplicates
  ports=$(echo $ports | xargs -n1 | sort -u | xargs)

  for port in $ports; do
    if (( $port == 2001 )); then
      # Ignore the metis control port
      continue
    else
      # Get IP Address of remote listener
      remoteip=$(printenv "${link}_PORT_${port}_TCP_ADDR")

      # Add the connection command
Greg Rutz's avatar
Greg Rutz committed
109
      echo "add connection tcp $link $remoteip $port $localip $((localport++))" >> $metiscfg
Greg Rutz's avatar
Greg Rutz committed
110 111 112 113 114 115 116 117 118 119 120

      # Add each route
      for route in ${ROUTES[$link]}; do
        echo "add route $link $route 10" >> $metiscfg
      done
    fi
  done
done

# Finally, add interest connection listener
echo "add listener tcp LISTENER $localip 9695" >> $metiscfg
Greg Rutz's avatar
Greg Rutz committed
121
echo "add listener tcp LISTENER 127.0.0.1 9695" >> $metiscfg
Greg Rutz's avatar
Greg Rutz committed
122 123 124 125 126 127

# For debugging, list all the connections and routes we added
echo "list connections" >> $metiscfg
echo "list routes" >> $metiscfg

echo "Running metis..."
Greg Rutz's avatar
Greg Rutz committed
128 129 130 131
metis_daemon --daemon --capacity 0 --log all=debug --log-file /var/log/metis.log --config $metiscfg & 

# Give a few seconds for metis to come up
sleep 2
Greg Rutz's avatar
Greg Rutz committed
132 133

# Run servers
Greg Rutz's avatar
Greg Rutz committed
134 135
echo "Running servers..."
for server in "${servers[@]}"; do
136
  echo "Server = $server"
Greg Rutz's avatar
Greg Rutz committed
137 138 139 140
  IFS='=' read -ra SERVER <<< $server
  lciuri=${SERVER[0]}
  prog=${SERVER[1]}
  echo "Running server for ( "$lciuri","$prog" )..."
141
  ccnx_server --identity /etc/ccnx/ccnx_keystore.p12 --password 1234 $lciuri "$prog" &
Greg Rutz's avatar
Greg Rutz committed
142 143
done

Greg Rutz's avatar
Greg Rutz committed
144
sleep infinity