From 30f8b8e6cd56abe6f3ce05c78a392659e3224b54 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Tue, 1 Dec 2020 21:36:50 -0700 Subject: [PATCH] switch to auto-restarter daemon --- agent/config/restarter.service | 7 +++++++ agent/config/restarter.sh | 18 ++++++++++++++++++ agent/config/restarter.timer | 9 +++++++++ agent/src/Lib/Synchronizers.hs | 17 +++++++++++++++++ appmgr/src/version/v0_2_7.rs | 10 ++++++++++ 5 files changed, 61 insertions(+) create mode 100644 agent/config/restarter.service create mode 100644 agent/config/restarter.sh create mode 100644 agent/config/restarter.timer diff --git a/agent/config/restarter.service b/agent/config/restarter.service new file mode 100644 index 000000000..0e599914e --- /dev/null +++ b/agent/config/restarter.service @@ -0,0 +1,7 @@ +[Unit] +Description=restarts dead containers +Requires=docker.service + +[Service] +Type=oneshot +ExecStart=/bin/bash /root/restarter.sh \ No newline at end of file diff --git a/agent/config/restarter.sh b/agent/config/restarter.sh new file mode 100644 index 000000000..6f1408133 --- /dev/null +++ b/agent/config/restarter.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +for CONTAINER in $(docker ps -aq); do + EXIT=`docker inspect -f "{{ .State.ExitCode }}" $CONTAINER` + if [ $EXIT -eq 0 ]; then + continue + fi + if [ $EXIT -eq 143 ]; then + continue + fi + if [ $EXIT -eq 137 ]; then + OOM=`docker inspect -f "{{ .State.OOMKilled }}" $CONTAINER` + if [ "$OOM" == "false" ]; then + continue + fi + fi + docker start $CONTAINER +done \ No newline at end of file diff --git a/agent/config/restarter.timer b/agent/config/restarter.timer new file mode 100644 index 000000000..c9d7ff6fe --- /dev/null +++ b/agent/config/restarter.timer @@ -0,0 +1,9 @@ +[Unit] +Description=restarter + +[Timer] +OnUnitActiveSec=60s +OnBootSec=60s + +[Install] +WantedBy=timers.target \ No newline at end of file diff --git a/agent/src/Lib/Synchronizers.hs b/agent/src/Lib/Synchronizers.hs index d76fd4ce4..d958ff117 100644 --- a/agent/src/Lib/Synchronizers.hs +++ b/agent/src/Lib/Synchronizers.hs @@ -119,6 +119,7 @@ sync_0_2_6 = Synchronizer , syncPrepSslIntermediateCaDir , syncPersistLogs , syncConvertEcdsaCerts + , syncRestarterService ] syncCreateAgentTmp :: SyncOp @@ -536,6 +537,22 @@ replaceDerivativeCerts = do liftIO $ renameDirectory sslDirTmp sslDir liftIO $ systemCtl RestartService "nginx" $> () +syncRestarterService :: SyncOp +syncRestarterService = SyncOp "Install Restarter Service" check migrate False + where + wantedScript = decodeUtf8 $(embedFile "config/restarter.sh") + wantedService = decodeUtf8 $(embedFile "config/restarter.service") + wantedTimer = decodeUtf8 $(embedFile "config/restarter.timer") + check = do + base <- asks $ appFilesystemBase . appSettings + liftIO $ not <$> doesPathExist (toS $ "/etc/systemd/system/timers.target.wants/restarter.timer" `relativeTo` base) + migrate = do + base <- asks $ appFilesystemBase . appSettings + liftIO $ writeFile (toS $ "/usr/local/bin/restarter.sh" `relativeTo` base) wantedScript + liftIO $ writeFile (toS $ "/etc/systemd/system/restarter.service" `relativeTo` base) wantedService + liftIO $ writeFile (toS $ "/etc/systemd/system/restarter.timer" `relativeTo` base) wantedTimer + liftIO . run $ systemctl "enable" "restarter.timer" + failUpdate :: S9Error -> ExceptT Void (ReaderT AgentCtx IO) () failUpdate e = do ref <- asks appIsUpdateFailed diff --git a/appmgr/src/version/v0_2_7.rs b/appmgr/src/version/v0_2_7.rs index 663d6faf1..42d5bbc52 100644 --- a/appmgr/src/version/v0_2_7.rs +++ b/appmgr/src/version/v0_2_7.rs @@ -1,4 +1,5 @@ use super::*; +use crate::util::Invoke; const V0_2_7: emver::Version = emver::Version::new(0, 2, 7, 0); @@ -13,6 +14,15 @@ impl VersionT for Version { &V0_2_7 } async fn up(&self) -> Result<(), Error> { + for (app_id, _) in crate::apps::list_info().await? { + tokio::process::Command::new("docker") + .arg("update") + .arg("--restart") + .arg("no") + .arg(app_id) + .invoke("Docker") + .await?; + } Ok(()) } async fn down(&self) -> Result<(), Error> {