########################################################
# T-Pot                                                #
# Cowrie upstart script                                #
#                                                      #
# v16.03.4 by av / mo, DTAG, 2016-03-03                #
########################################################

description "Cowrie"
author "av"
start on started docker and filesystem
stop on runlevel [!2345]
respawn
pre-start script
  # Remove any existing cowrie containers
  myCID=$(docker ps -a | grep cowrie | awk '{ print $1 }')
  if [ "$myCID" != "" ];
    then docker rm -v $myCID;
  fi
  # Remove any data from previous container if persistence is not enabled 
  if ! [ -f /data/persistence.on ];
    then
      rm -rf /data/cowrie/* || true
      mkdir -p /data/cowrie/log/tty/ /data/cowrie/downloads/ /data/cowrie/keys/ /data/cowrie/misc/
      chmod 760 /data/cowrie -R
      chown tpot:tpot /data/cowrie -R
  fi
end script
script
  /usr/bin/docker run --name cowrie --rm=true -p 22:2222 -v /data/cowrie:/data/cowrie -v /data/ews:/data/ews dtagdevsec/cowrie:latest1603
end script
post-start script
  # Delay next start to avoid rapid respawning
  sleep 2
end script
