From 24b540ecdef694a1802e169d228daf358bce6e67 Mon Sep 17 00:00:00 2001 From: Laurence Jones Date: Tue, 18 Oct 2022 16:11:48 +0100 Subject: [PATCH] Cronjob via packages (#1820) * Final version --- config/crowdsec.cron.daily | 4 ++++ debian/crowdsec.cron.daily | 1 + debian/postinst | 1 - debian/postrm | 4 ++-- rpm/SPECS/crowdsec.spec | 10 ++++++---- wizard.sh | 21 --------------------- 6 files changed, 13 insertions(+), 28 deletions(-) create mode 100644 config/crowdsec.cron.daily create mode 120000 debian/crowdsec.cron.daily diff --git a/config/crowdsec.cron.daily b/config/crowdsec.cron.daily new file mode 100644 index 000000000..b4614c7b8 --- /dev/null +++ b/config/crowdsec.cron.daily @@ -0,0 +1,4 @@ +#!/bin/sh +cscli --error hub update && cscli --error hub upgrade +systemctl reload crowdsec +exit 0 \ No newline at end of file diff --git a/debian/crowdsec.cron.daily b/debian/crowdsec.cron.daily new file mode 120000 index 000000000..3fb31fe94 --- /dev/null +++ b/debian/crowdsec.cron.daily @@ -0,0 +1 @@ +../config/crowdsec.cron.daily \ No newline at end of file diff --git a/debian/postinst b/debian/postinst index fca546cae..a862c8875 100644 --- a/debian/postinst +++ b/debian/postinst @@ -82,7 +82,6 @@ if [ "$1" = configure ]; then set -e fi - install_cronjob if [[ -f /var/lib/crowdsec/data/crowdsec.db.backup ]]; then cp /var/lib/crowdsec/data/crowdsec.db.backup /var/lib/crowdsec/data/crowdsec.db diff --git a/debian/postrm b/debian/postrm index 9e89bd730..fc68f31db 100644 --- a/debian/postrm +++ b/debian/postrm @@ -1,7 +1,7 @@ if [ "$1" = "purge" ]; then find /etc/crowdsec -maxdepth 1 -mindepth 1 | grep -v "bouncer" | xargs rm -rf || echo "" rm -rf /var/lib/crowdsec - if [ -d "/etc/cron.daily/" ] && [ -f "/etc/cron.daily/crowdsec-hub" ]; then - rm /etc/cron.daily/crowdsec-hub + if [ -f "/etc/cron.daily/crowdsec" ]; then + rm /etc/cron.daily/crowdsec fi fi diff --git a/rpm/SPECS/crowdsec.spec b/rpm/SPECS/crowdsec.spec index fc6b755f8..8ad3732e8 100644 --- a/rpm/SPECS/crowdsec.spec +++ b/rpm/SPECS/crowdsec.spec @@ -14,8 +14,8 @@ BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) BuildRequires: git BuildRequires: make -BuildRequires: jq BuildRequires: systemd +Requires: crontabs %{?fc33:BuildRequires: systemd-rpm-macros} %{?fc34:BuildRequires: systemd-rpm-macros} %{?fc35:BuildRequires: systemd-rpm-macros} @@ -52,6 +52,7 @@ mkdir -p %{buildroot}%{_sharedstatedir}/%{name}/plugins mkdir -p %{buildroot}%{_sysconfdir}/crowdsec/notifications/ mkdir -p %{buildroot}%{_libdir}/%{name}/plugins/ + install -m 755 -D cmd/crowdsec/crowdsec %{buildroot}%{_bindir}/%{name} install -m 755 -D cmd/crowdsec-cli/cscli %{buildroot}%{_bindir}/cscli install -m 755 -D wizard.sh %{buildroot}/usr/share/crowdsec/wizard.sh @@ -61,6 +62,7 @@ install -m 600 -D config/config.yaml %{buildroot}%{_sysconfdir}/crowdsec install -m 644 -D config/simulation.yaml %{buildroot}%{_sysconfdir}/crowdsec install -m 644 -D config/profiles.yaml %{buildroot}%{_sysconfdir}/crowdsec install -m 644 -D config/console.yaml %{buildroot}%{_sysconfdir}/crowdsec +install -m 750 -D config/%{name}.cron.daily %{buildroot}%{_sysconfdir}/cron.daily/%{name} install -m 644 -D %{SOURCE1} %{buildroot}%{_presetdir} install -m 551 plugins/notifications/slack/notification-slack %{buildroot}%{_libdir}/%{name}/plugins/ @@ -118,6 +120,7 @@ rm -rf %{buildroot} %config(noreplace) %{_sysconfdir}/%{name}/notifications/slack.yaml %config(noreplace) %{_sysconfdir}/%{name}/notifications/splunk.yaml %config(noreplace) %{_sysconfdir}/%{name}/notifications/email.yaml +%config(noreplace) %{_sysconfdir}/cron.daily/%{name} %{_unitdir}/%{name}.service @@ -175,7 +178,6 @@ if [ $1 == 1 ]; then fi cscli hub update - install_cronjob CSCLI_BIN_INSTALLED="/usr/bin/cscli" SILENT=true install_collection #upgrade @@ -228,8 +230,8 @@ if [ $1 == 0 ]; then rm -rf /etc/crowdsec/hub fi -if [ -d "/etc/cron.daily/" ] && [ -f "/etc/cron.daily/crowdsec-hub" ]; then - rm /etc/cron.daily/crowdsec-hub +if [ -f "/etc/cron.daily/crowdsec-hub" ]; then + rm /etc/cron.daily/crowdsec fi #systemctl stop crowdsec || echo "crowdsec was not started" diff --git a/wizard.sh b/wizard.sh index b4ae1e8f6..7a314d86a 100755 --- a/wizard.sh +++ b/wizard.sh @@ -68,7 +68,6 @@ telnet smb ' -CRON_NAME=/etc/cron.daily/crowdsec-hub HTTP_PLUGIN_BINARY="./plugins/notifications/http/notification-http" SLACK_PLUGIN_BINARY="./plugins/notifications/slack/notification-slack" @@ -553,22 +552,6 @@ uninstall_crowdsec() { log_info "crowdsec successfully uninstalled" } -install_cronjob() { - log_info "Installing Hub Cronjob" - if [[ -d /etc/cron.daily/ ]] && [[ ! -f $CRON_NAME ]]; then - echo "/usr/bin/cscli hub update" >> $CRON_NAME - echo "/usr/bin/cscli hub upgrade" >> $CRON_NAME - chmod 755 $CRON_NAME - fi -} - -uninstall_cronjob() { - if [[ -d /etc/cron.daily/ ]] && [[ -f $CRON_NAME ]]; then - log_info "Uninstall Hub Cronjob" - rm $CRON_NAME - fi -} - function show_link { echo "" @@ -635,7 +618,6 @@ main() { ${CSCLI_BIN_INSTALLED} hub update install_collection genacquisition - install_cronjob if ! skip_tmp_acquis; then mv "${TMP_ACQUIS_FILE}" "${ACQUIS_TARGET}" fi @@ -656,7 +638,6 @@ main() { fi check_running_bouncers uninstall_crowdsec - uninstall_cronjob return fi @@ -711,8 +692,6 @@ main() { mkdir -p "${PATTERNS_PATH}" cp "./${PATTERNS_FOLDER}/"* "${PATTERNS_PATH}/" - # install hub cronjob - install_cronjob # api register ${CSCLI_BIN_INSTALLED} machines add --force "$(cat /etc/machine-id)" -a -f "${CROWDSEC_CONFIG_PATH}/${CLIENT_SECRETS}" || log_fatal "unable to add machine to the local API" log_dbg "Crowdsec LAPI registered"