aboutsummaryrefslogtreecommitdiffstats
path: root/mkosibuild-cron
blob: 7678e6e1143b88e29d03fec52a9a70770b08ad28 (plain) (blame)
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
#!/usr/bin/env bash
#
# made by Dennis Eriksen <dnns.no> in 2020.

# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail


LOGDIR=/var/log/mkosibuild/
BACKUPDIR=/var/lib/machines/.backup
BACKUPNUM=5
EXEC=mkosibuild # This provides the exec is in your $PATH
CRONSUFFIX="-cron" # Suffix for backups made through cron

export CRON=true
export CRONSUFFIX

MACHINES=()

# Import config to override above config
if [[ -r /etc/mkosibuild ]]; then
  . /etc/mkosibuild
  crondir="$(echo "$0" | awk -F'/' '{print $3}')"
  if [[ "${crondir}" == "cron.hourly" ]] && [[ -n "${HOURLY_MACHINES+x}" ]]; then
    MACHINES=("${HOURLY_MACHINES[*]}")
  elif [[ "${crondir}" == "cron.daily" ]] && [[ -n "${DAILY_MACHINES+x}" ]]; then
    MACHINES=("${DAILY_MACHINES[*]}")
  elif [[ "${crondir}" == "cron.weekly" ]] && [[ -n "${WEEKLY_MACHINES+x}" ]]; then
    MACHINES=("${WEEKLY_MACHINES[*]}")
  elif [[ "${crondir}" == "cron.monthly" ]] && [[ -n "${MONTHLY_MACHINES+x}" ]]; then
    MACHINES=("${MONTHLY_MACHINES[*]}")
  fi
fi


for machine in ${MACHINES[*]}; do
  echo "${machine}"
done
exit

# Check if logdir exists, and create it if not
[[ -d "${LOGDIR}" ]] || mkdir "${LOGDIR}"
chmod 750 "${LOGDIR}"


LOG="${LOGDIR}/mkosibuild.log"
touch "${LOG}" && chmod 640 "${LOG}"

for machine in ${MACHINES[*]}; do
  BUILDLOG="${LOGDIR}/${machine}.buildlog.$(date +%Y%m%d%H%M%S)"
  touch "${BUILDLOG}" && chmod 640 "${BUILDLOG}"
  #exec >> "${LOG}"
  echo "" >> "${LOG}"
  echo "$(date '+%Y-%m-%d %H:%M:%S') - Building $machine" >> "${LOG}"
  ${EXEC} "$machine" &>> "${BUILDLOG}" && echo "$(date '+%Y-%m-%d %H:%M:%S') - Finished $machine" >> "${LOG}"
  # Delete old backups
  find "${BACKUPDIR}" -name "${machine}.*${CRONSUFFIX}" | tail -n +$((BACKUPNUM + 1)) | xargs rm -fv -- >> "${LOG}"

done