PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tmpl/start

https://github.com/clearpathrobotics/robot_upstart
#! | 93 lines | 82 code | 11 blank | 0 comment | 0 complexity | dc9d8a81f8c84f4f34477f356f268207 MD5 | raw file
  1. #!/bin/bash
  2. #
  3. # Author: Mike Purvis <mpurvis@clearpathrobotics.com>
  4. # Copyright (c) 2013, Clearpath Robotics, Inc.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * Neither the name of Clearpath Robotics, Inc. nor the
  14. # names of its contributors may be used to endorse or promote products
  15. # derived from this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. # DISCLAIMED. IN NO EVENT SHALL CLEARPATH ROBOTICS, INC. BE LIABLE FOR ANY
  21. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #
  28. # Please send comments, questions, or patches to code@clearpathrobotics.com
  29. function log() {{
  30. logger -s -p user.$1 ${{@:2}}
  31. }}
  32. log info "{job}: Using workspace setup file {setup}"
  33. source {setup}
  34. JOB_FOLDER=/etc/ros/{rosdistro}/{job}.d
  35. logdir={logdir}
  36. export ROS_IP=`rosrun robot_upstart getifip {interface}`
  37. export ROS_MASTER_URI={master}
  38. if [ "$ROS_IP" = "" ]; then
  39. log err "{job}: No IP address on {interface}, cannot roslaunch."
  40. exit 1
  41. fi
  42. if [[ ! -d $logdir ]]; then
  43. log warn "{job}: The log directory you specified ($logdir) does not exist. Defaulting to /tmp!"
  44. logdir="/tmp"
  45. fi
  46. log info "{job}: Launching on interface {interface}, ROS_IP=$ROS_IP, ROS_MASTER_URI=$ROS_MASTER_URI, ROS_LOG_DIR=$logdir"
  47. # If xacro files are present in job folder, generate and expand an amalgamated urdf.
  48. XACRO_FILENAME=$logdir/{job}.xacro
  49. XACRO_ROBOT_NAME=$(echo "{job}" | cut -d- -f1)
  50. rosrun robot_upstart mkxacro $JOB_FOLDER $XACRO_ROBOT_NAME > $XACRO_FILENAME
  51. if [[ "$?" == "0" ]]; then
  52. URDF_FILENAME=$logdir/{job}.urdf
  53. rosrun xacro xacro $XACRO_FILENAME -o $URDF_FILENAME
  54. if [[ "$?" == "0" ]]; then
  55. log info "{job}: Generated URDF: $URDF_FILENAME"
  56. else
  57. log warn "{job}: URDF macro expansion failure. Robot description will not function."
  58. fi
  59. export ROBOT_URDF_FILENAME=$URDF_FILENAME
  60. fi
  61. # Assemble amalgamated launchfile.
  62. LAUNCH_FILENAME=$logdir/{job}.launch
  63. rosrun robot_upstart mklaunch $JOB_FOLDER > $LAUNCH_FILENAME
  64. if [[ "$?" != "0" ]]; then
  65. log err "{job}: Unable to generate amalgamated launchfile."
  66. exit 1
  67. fi
  68. log info "{job}: Generated launchfile: $LAUNCH_FILENAME"
  69. # Warn and exit if setuidgid is missing from the system.
  70. which setuidgid > /dev/null
  71. if [ "$?" != "0" ]; then
  72. log err "{job}: Can't launch as unprivileged user without setuidgid. Please install daemontools package."
  73. exit 1
  74. fi
  75. # Punch it.
  76. export ROS_HOME=$(echo ~{user})/.ros
  77. export ROS_LOG_DIR=$logdir
  78. setuidgid {user} roslaunch $LAUNCH_FILENAME &
  79. PID=$!
  80. log info "{job}: Started roslaunch as background process, PID $PID, ROS_LOG_DIR=$ROS_LOG_DIR"
  81. echo "$PID" > $logdir/{job}.pid
  82. wait "$PID"