summaryrefslogtreecommitdiff
path: root/rhautoimport
blob: a5361e99f075424a297ef51fbcc674381fceb0ec (plain)
1
2
3
4
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
#
#
#  rhautoimport
#
#  Copyright (C) 2009-2017 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhautoimport.
#
#  rhautoimport is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  any later version.
#
#  rhautoimport is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with rhautoimport. If not, see <http://www.gnu.org/licenses/>.
#

if [ -z "$1" ]; then
  echo "rhautoimport <importer> [ arg1 [ arg2 ] ... ]"
  exit 1
fi

if [ ! -x "/usr/bin/rhautoimport-$1" ]; then
  echo "unknown importer: $1"
  exit 2
fi

LOG_FILE=`mktemp --tmpdir rhautoimport-XXXXXX.log`
RESULT_FILE=`mktemp --tmpdir rhautoimport-XXXXXX.result`
MAIL_TO=`sed 's/#.*//' /etc/rhautoimport/$1.mail 2> /dev/null | xargs`
if [ -z "$MAIL_TO" ]; then
  MAIL_TO="root"
fi

(
  /usr/bin/flock -x -w 10 200

  /usr/bin/rhautoimport-$1 ${@:2} > $LOG_FILE 2>&1 3> $RESULT_FILE
  RETURN_CODE=$?
  TITLE=""
  WAS_LAST=0
  idx=0
  while IFS= read -r line; do
    case $idx in
      0)
        TITLE=$line
        ;;
      1)
        WAS_LAST=$line
        ;;
      *)
        break
        ;;
    esac
    idx=$((idx+1))
  done < $RESULT_FILE
  rm -f $RESULT_FILE

  case $RETURN_CODE in
    0)
      bsd-mailx -a "Content-Type: text/plain; charset=utf-8" -s "$TITLE - Imported Successfully" $MAIL_TO < $LOG_FILE
    ;;
    23)
      bsd-mailx -a "Content-Type: text/plain; charset=utf-8" -s "$TITLE - Imported Successfully (partially failed - check log!)" $MAIL_TO < $LOG_FILE
    ;;
    42)
      # do nothing - the script didn't import anything but no message should be sent
    ;;
    *)
      if [ $WAS_LAST -eq 1 ]; then
        subject="$TITLE - Import Error - last attempt!!!"
      else
        subject="$TITLE - Import Error - will retry"
      fi
      bsd-mailx -a "Content-Type: text/plain; charset=utf-8" -s "$subject" $MAIL_TO < $LOG_FILE
    ;;
  esac
  rm -f $LOG_FILE

) 200>/var/lock/rhautoimport-$1.lock

exit 0