Skip to content

Instantly share code, notes, and snippets.

@kshcherban
Created January 26, 2017 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshcherban/dac50b715126f5eff0709da15d7474b2 to your computer and use it in GitHub Desktop.
Save kshcherban/dac50b715126f5eff0709da15d7474b2 to your computer and use it in GitHub Desktop.
start stop selenium grid in docker containers
#!/bin/bash
### Script to start selenium grid
function usage() {
echo -e "Usage: $0 [OPTION]...
Options:
<-a|--action start|stop>\tstart or stop cluster
[-c|--count 2]\tnumber of nodes to start
[-n|--node chrome|firefox]\tselenium node type
[-v|--version 3.0.1-fermium]\tselenium version
[--se-opts some-opts]\tadditional options to SE_OPTS
[--node-mem 300m]\tnode memory reservation
[--hub-mem 512m]\thub memory reservation
[--node-shm 300m]\tnode shm-size
[--log-driver syslog]\tdocker log-driver for containers
[--log-opt string]\tcustom log-opt
[-h|--help]"
exit 0
}
if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
fi
node="chrome"
count=2
selenium_version="3.0.1-fermium"
log_opt="true"
log_driver="syslog"
# Parse command line arguments
while [[ $# -ge 1 ]]; do
key="$1"
case $key in
-a|--action)
action="$2"
shift
;;
-c|--count)
count="$2"
shift
;;
-n|--node)
node="$2"
shift
;;
-v|--version)
selenium_version="$2"
shift
;;
--se-opts)
selenium_options="$2"
shift
;;
--node-mem)
node_mem="$2"
shift
;;
--hub-mem)
hub_mem="$2"
shift
;;
--node-shm)
node_shm="$2"
shift
;;
--log-driver)
log_driver="$2"
shift
;;
--log-opt)
log_opt="$2"
shift
;;
*)
# unknown option
;;
esac
shift
done
if [ -z $action ]; then
usage
fi
instance_ip="$(/sbin/ifconfig eth0 | sed -n 's/.*addr:\([0-9.]*\)\s*Bcast.*/\1/p')"
if [ "$node" == "chrome" ]; then
start_port=5655
else
start_port=5555
fi
if [ "$action" == "start" ]; then
# Start hub
hub_id="$(docker ps -q --filter name=selenium-hub)"
if [ "$log_opt" == "true" ]; then
log_opt="tag=docker/selenium-hub"
fi
if [ -z $hub_id ]; then
echo "*** Starting hub"
docker run -d --name=selenium-hub -p 4444:4444 \
--memory-reservation=$hub_mem \
-e 'HUB_PORT_4444_TCP_PORT=4444' \
-e "SE_OPTS=-host $instance_ip -port 4444 $selenium_options" \
-e "HUB_PORT_4444_TCP_ADDR=$instance_ip" \
-e "HUB_PORT_4444_TCP_PORT=4444" \
--log-opt="$log_opt" \
--log-driver=$log_driver \
selenium/hub:${selenium_version}
fi
# Start the horde
for i in $(seq $count); do
if [ "$log_opt" == "true" ]; then
log_opt="tag=docker/selenium-${node}-$i"
fi
echo "*** Starting selenium-${node}-$i"
port=$((start_port+i))
docker run -d --name=selenium-${node}-$i -p $port:$port \
--memory-reservation=$node_mem \
--shm-size=$node_shm \
-e "SE_OPTS=-host $instance_ip -port $port $selenium_options" \
-e "HUB_PORT_4444_TCP_ADDR=$instance_ip" \
-e "HUB_PORT_4444_TCP_PORT=4444" \
--log-opt="$log_opt" \
--log-driver=$log_driver \
selenium/node-${node}:${selenium_version}
done
elif [ "$action" == "stop" ]; then
echo "** Stopping and removing hub and nodes"
ids=$(docker ps -a -q --filter name=selenium-)
for i in $ids; do
docker stop $i
docker rm $i
done
else
echo "Unsupported action"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment