You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.2 KiB

  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=30
  4. EXTRA_COMMANDS="get_key probeid log create_backup load_backup"
  5. EXTRA_HELP=" get_key print probe public key (used for probe registration)
  6. probeid print probe id
  7. log print probe status log
  8. create_backup backup ssh key to tar.gz
  9. load_backup 'backup.tar.gz' load backup ssh key from tar.gz
  10. "
  11. SCRIPTS_DIR="/usr/libexec/atlas-probe-scripts"
  12. TMP_BASE_DIR="/tmp/ripe_atlas_probe"
  13. PUB_KEY_FILE="$SCRIPTS_DIR/etc/probe_key.pub"
  14. PRIV_KEY_FILE="$SCRIPTS_DIR/etc/probe_key"
  15. PROBE_ID_FILE="$TMP_BASE_DIR/status/reg_init_reply.txt"
  16. LOG_FILE="/tmp/log/ripe_sw_probe"
  17. STATE_CONFIG="$SCRIPTS_DIR/state/config.txt"
  18. load_backup() {
  19. local backup_arch
  20. local tmp_dir
  21. backup_arch="$1"
  22. tmp_dir="$(mktemp -u -p /var/run/atlas)"
  23. if [ -f "$backup_arch" ]; then
  24. safe_mkdir "$tmp_dir"
  25. tar -xzf "$backup_arch" -C "$tmp_dir/"
  26. if [ -f "$tmp_dir/probe_key.pub" ] && [ -f "$tmp_dir/probe_key" ]; then
  27. mv "$tmp_dir/probe_key.pub" "$PUB_KEY_FILE"
  28. mv "$tmp_dir/probe_key" "$PRIV_KEY_FILE"
  29. rm -rf "$tmp_dir"
  30. print_msg "Info: public and private key loaded from backup"
  31. else
  32. print_msg "Error: Could not extract probe_key or probe_key form backup archive"
  33. rm -rf "$tmp_dir"
  34. exit 1
  35. fi
  36. else
  37. print_msg "Error: Provided backup file $backup_arch does not exists"
  38. exit 1
  39. fi
  40. }
  41. create_backup() {
  42. local back_dir
  43. back_dir="$(pwd)"
  44. if [ -f "$PUB_KEY_FILE" -a -f "$PRIV_KEY_FILE" ]; then
  45. print_msg "Info: Creating backup arch in $back_dir"
  46. tar -czf "$back_dir/atlas-key-backup.tar.gz" -C "$SCRIPTS_DIR/etc" probe_key probe_key.pub
  47. else
  48. print_msg "Error: private or public key does not exists."
  49. exit 1
  50. fi
  51. }
  52. log() {
  53. if [ -f "$LOG_FILE" ];then
  54. tail "$LOG_FILE"
  55. else
  56. print_msg "Error. No log file found. Probe isn't probably running"
  57. exit 1
  58. fi
  59. }
  60. get_key() {
  61. if [ -f "$PUB_KEY_FILE" ]; then
  62. echo "Probe public key (use for registration)"
  63. echo "URL with registration form https://atlas.ripe.net/apply/swprobe/"
  64. echo "=========================================="
  65. cat "$PUB_KEY_FILE"
  66. else
  67. print_msg "Error! Pub. key not found"
  68. exit 1
  69. fi
  70. }
  71. probeid() {
  72. local probe_id
  73. if [ -f "$PROBE_ID_FILE" ]; then
  74. probe_id="$(awk '/PROBE_ID/ {print $2}' "$PROBE_ID_FILE")"
  75. if [ -z "$probe_id" ]; then
  76. print_msg "Probe ID not found SW probe isn't probably registered yet"
  77. exit 1
  78. else
  79. print_msg "Probe ID is $probe_id"
  80. fi
  81. else
  82. print_msg "Probe ID not found. SW probe is not running or probe_key isn't registered yet"
  83. exit 1
  84. fi
  85. }
  86. print_msg() {
  87. echo "$1" >&2
  88. logger -t atlas-sw-probe "$1"
  89. }
  90. stop_service() {
  91. local atlas_pid
  92. local tunnel_pid
  93. local pid_file
  94. print_msg "Stopping atlas sw probe"
  95. print_msg "Kill all atlas processes"
  96. for pid_file in "$SCRIPTS_DIR/run/"*.vol; do
  97. [ -f "$pid_file" ] || continue
  98. # test if proccess is still running
  99. atlas_pid="$(cat "$pid_file")"
  100. if kill -0 "$atlas_pid" 2>/dev/null; then
  101. kill "$atlas_pid"
  102. fi
  103. done
  104. if [ -f "$SCRIPTS_DIR/status/con_keep_pid.vol" ]; then
  105. print_msg "Kill ssh tunnel"
  106. tunnel_pid="$(cat "$SCRIPTS_DIR/status/con_keep_pid.vol")"
  107. if kill -0 "$tunnel_pid" 2>/dev/null; then
  108. kill "$tunnel_pid"
  109. fi
  110. fi
  111. }
  112. safe_mkdir() {
  113. local dir="$1"
  114. if [ -e "$dir" ] && [ ! -d "$dir" -o -L "$dir" ]; then
  115. rm -rf "$dir"
  116. fi
  117. mkdir -p "$dir"
  118. chmod 700 "$dir"
  119. chown root:root "$dir"
  120. }
  121. create_tmp_dirs() {
  122. local dirs
  123. chown -R atlas:atlas "$SCRIPTS_DIR/bin"
  124. chmod 755 "$SCRIPTS_DIR/bin"
  125. dirs='crons data run status'
  126. safe_mkdir "$TMP_BASE_DIR"
  127. for i in $dirs; do
  128. safe_mkdir "$TMP_BASE_DIR/$i"
  129. done
  130. }
  131. start_service() {
  132. local log_stderr
  133. local log_stdout
  134. local rxtxrpt
  135. local test_setting
  136. create_tmp_dirs
  137. config_load atlas
  138. config_get_bool log_stderr "common" log_stderr "0"
  139. config_get_bool log_stdout "common" log_stdout "0"
  140. config_get_bool rxtxrpt "common" rxtxrpt "1"
  141. test_setting=$(grep "^[ ]*RXTXRPT=yes" "$STATE_CONFIG")
  142. # Decide if we should write to permanent storage
  143. if [ "$rxtxrpt" == "1" ] && [ -z "$test_setting" ]; then
  144. echo "RXTXRPT=yes">$STATE_CONFIG
  145. elif [ "$rxtxrpt" == "0" ] && [ ! -z "$test_setting" ]; then
  146. echo "RXTXRPT=no">$STATE_CONFIG
  147. fi
  148. procd_open_instance
  149. procd_set_param command "$SCRIPTS_DIR/bin/ATLAS"
  150. procd_set_param stdout "$log_stdout"
  151. procd_set_param stderr "$log_stderr"
  152. procd_close_instance
  153. }